Subject: Re: [PATCH] gnu: gcc@7: Use retpoline options when building itself.

From: Alex Vong <[email-masked]>
Date: Sat, 27 Jan 2018 11:19:55 +0800

Hello,

This patch makes gcc use retpoline options when building itself. My last
attempt to build it was successful. But after that I have changed
something, I hope it wouldn't make it fail to build. (It shouldn't,
since the options passed aren't changed.)

Are we going to add these options to other natively compiled programs as
well?

>From f6b9caae6e13936be65550c871208a3425fe4ce4 Mon Sep 17 00:00:00 2001
From: Alex Vong <[email-masked]>
Date: Thu, 25 Jan 2018 23:24:24 +0800
Subject: [PATCH] gnu: gcc@7: Use retpoline options when building itself.

* gnu/packages/gcc.scm (gcc@7)[arguments]: Add retpoline options
to #:make-flags.
---
 gnu/packages/gcc.scm | 40 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)

diff --git a/gnu/packages/gcc.scm b/gnu/packages/gcc.scm
index 48e7b8ee3..6344ce734 100644
--- a/gnu/packages/gcc.scm
+++ b/gnu/packages/gcc.scm
@@ -5,6 +5,7 @@
 ;;; Copyright © 2015 Andreas Enge <[email-masked]>
 ;;; Copyright © 2015, 2016, 2017 Efraim Flashner <[email-masked]>
 ;;; Copyright © 2016 Carlos Sánchez de La Lama <[email-masked]>
+;;; Copyright © 2018 Alex Vong <[email-masked]>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -428,6 +429,45 @@ Go.  It also includes runtime support libraries for these languages.")
                 "0p71bij6bfhzyrs8676a8jmpjsfz392s2rg862sdnsk30jpacb43"))
               (patches (search-patches "gcc-strmov-store-file-names.patch"
                                        "gcc-5.0-libvtv-runpath.patch"))))
+    (arguments
+     (substitute-keyword-arguments `(#:modules ((guix build gnu-build-system)
+                                                (guix build utils)
+                                                (ice-9 regex)
+                                                (srfi srfi-1)
+                                                (srfi srfi-26))
+                                     ,@(package-arguments gcc-6))
+       ;; Use retpoline options when building itself.
+       ((#:make-flags flags)
+        `(let* ((cross-compiling? ,(%current-target-system))
+                (system (if cross-compiling?
+                            ,(%current-target-system)
+                            ,(%current-system)))
+                (retpoline-opts '("-mindirect-branch=thunk"
+                                  "-mfunction-return=thunk"
+                                  "-mindirect-branch-register"))
+                (append-flag
+                 (lambda (flag)
+                   (if (string-match "^((BOOT_)?CFLAGS|C(XX)?FLAGS_FOR_TARGET)="
+                                     flag)
+                       (string-join (cons flag retpoline-opts))
+                       flag)))
+                (add-flag
+                 (lambda (prefix flags)
+                   (if (any (cut string-prefix? prefix <>) flags)
+                       flags
+                       (cons (string-append prefix
+                                            (string-join retpoline-opts))
+                             flags))))
+                (add-gcc-flag (cut add-flag
+                                (if cross-compiling? "CFLAGS=" "BOOT_CFLAGS=")
+                                <>))
+                (add-c-lib-flag (cut add-flag "CFLAGS_FOR_TARGET=" <>))
+                (add-c++-lib-flag (cut add-flag "CXXFLAGS_FOR_TARGET=" <>)))
+           ;; Right now, the retpoline options are x86-specific.
+           (if (any (cut string-prefix? <> system) '("x86_64" "i686"))
+               (add-gcc-flag (add-c-lib-flag (add-c++-lib-flag (map append-flag
+                                                                    ,flags))))
+               ,flags)))))
     (description
      "GCC is the GNU Compiler Collection.  It provides compiler front-ends
 for several languages, including C, C++, Objective-C, Fortran, Ada, and Go.
-- 
2.15.1

Cheers,
Alex