Bug 1351109 - Move the gold linker support to the python configure r?glandium draft
authorSylvestre Ledru <sledru@mozilla.com>
Wed, 05 Jul 2017 11:27:56 +0200
changeset 607072 226eedfbebf89f532f19be68152fa9320e80d55c
parent 599721 f571c2bc3c51351dab64d597f09222d93dcc4492
child 636928 a5e647883475abe834ea147fab855fb585559130
push id67878
push userbmo:sledru@mozilla.com
push dateTue, 11 Jul 2017 20:14:53 +0000
reviewersglandium
bugs1351109
milestone56.0a1
Bug 1351109 - Move the gold linker support to the python configure r?glandium MozReview-Commit-ID: 1FC0W7EqdCI
build/autoconf/compiler-opts.m4
build/moz.configure/old.configure
build/moz.configure/toolchain.configure
--- a/build/autoconf/compiler-opts.m4
+++ b/build/autoconf/compiler-opts.m4
@@ -114,58 +114,16 @@ AC_DEFUN([MOZ_COMPILER_OPTS],
 if test "$CLANG_CXX"; then
     ## We disable return-type-c-linkage because jsval is defined as a C++ type but is
     ## returned by C functions. This is possible because we use knowledge about the ABI
     ## to typedef it to a C type with the same layout when the headers are included
     ## from C.
     _WARNINGS_CXXFLAGS="${_WARNINGS_CXXFLAGS} -Wno-unknown-warning-option -Wno-return-type-c-linkage"
 fi
 
-if test -n "$DEVELOPER_OPTIONS"; then
-    MOZ_FORCE_GOLD=1
-fi
-
-MOZ_ARG_ENABLE_BOOL(gold,
-[  --enable-gold           Enable GNU Gold Linker when it is not already the default],
-    MOZ_FORCE_GOLD=1,
-    MOZ_FORCE_GOLD=
-    )
-
-if test "$GNU_CC" -a -n "$MOZ_FORCE_GOLD"; then
-    dnl if the default linker is BFD ld, check if gold is available and try to use it
-    dnl for local builds only.
-    if $CC -Wl,--version 2>&1 | grep -q "GNU ld"; then
-        GOLD=$($CC -print-prog-name=ld.gold)
-        case "$GOLD" in
-        /*)
-            ;;
-        *)
-            GOLD=$(which $GOLD)
-            ;;
-        esac
-        if test -n "$GOLD"; then
-            mkdir -p $_objdir/build/unix/gold
-            rm -f $_objdir/build/unix/gold/ld
-            ln -s "$GOLD" $_objdir/build/unix/gold/ld
-            if $CC -B $_objdir/build/unix/gold -Wl,--version 2>&1 | grep -q "GNU gold"; then
-                LDFLAGS="$LDFLAGS -B $_objdir/build/unix/gold"
-            else
-                rm -rf $_objdir/build/unix/gold
-            fi
-        fi
-    fi
-fi
-if test "$GNU_CC"; then
-    if $CC $LDFLAGS -Wl,--version 2>&1 | grep -q "GNU ld"; then
-        LD_IS_BFD=1
-    fi
-fi
-
-AC_SUBST([LD_IS_BFD])
-
 if test "$GNU_CC"; then
     if test -z "$DEVELOPER_OPTIONS"; then
         CFLAGS="$CFLAGS -ffunction-sections -fdata-sections"
         CXXFLAGS="$CXXFLAGS -ffunction-sections -fdata-sections"
     fi
     CFLAGS="$CFLAGS -fno-math-errno"
     CXXFLAGS="$CXXFLAGS -fno-exceptions -fno-math-errno"
 fi
--- a/build/moz.configure/old.configure
+++ b/build/moz.configure/old.configure
@@ -176,17 +176,16 @@ def old_configure_options(*options):
     '--enable-dump-painting',
     '--enable-elf-hack',
     '--enable-extensions',
     '--enable-faststripe',
     '--enable-feeds',
     '--enable-gamepad',
     '--enable-gconf',
     '--enable-gczeal',
-    '--enable-gold',
     '--enable-hardware-aec-ns',
     '--enable-icf',
     '--enable-install-strip',
     '--enable-ion',
     '--enable-ios-target',
     '--enable-jitspew',
     '--enable-libjpeg-turbo',
     '--enable-libproxy',
--- a/build/moz.configure/toolchain.configure
+++ b/build/moz.configure/toolchain.configure
@@ -1056,8 +1056,84 @@ js_option('--enable-release',
                'options. This may slow down builds.')
 
 @depends('--enable-release')
 def developer_options(value):
     if not value:
         return True
 
 add_old_configure_assignment('DEVELOPER_OPTIONS', developer_options)
+
+# Linker detection
+# ==============================================================
+
+@depends(target)
+def build_not_win_mac(target):
+    if target.kernel not in ('Darwin', 'WINNT'):
+        return True
+
+
+option('--enable-gold',
+       env='MOZ_FORCE_GOLD',
+       help='Enable GNU Gold Linker when it is not already the default',
+       when=build_not_win_mac)
+
+
+@depends('--enable-gold', c_compiler, developer_options, check_build_environment, when=build_not_win_mac)
+@checking('for ld', lambda x: x.KIND)
+@imports('os')
+@imports('shutil')
+def enable_gold(enable_gold_option, c_compiler, developer_options, build_env):
+    linker = None
+    # Used to check the kind of linker
+    version_check = ['-Wl,--version']
+    cmd_base = c_compiler.wrapper + [c_compiler.compiler] + c_compiler.flags
+    if enable_gold_option or developer_options:
+        # Try to force the usage of gold
+        targetDir = os.path.join(build_env.topobjdir, 'build', 'unix', 'gold')
+
+        gold_detection_arg = '-print-prog-name=ld.gold'
+        gold = check_cmd_output(c_compiler.compiler, gold_detection_arg).strip()
+        if not gold:
+            die('Could not find gold')
+
+        goldFullPath = find_program(gold)
+        if goldFullPath is None:
+            die('Could not find gold')
+
+        if os.path.exists(targetDir):
+            shutil.rmtree(targetDir)
+        os.makedirs(targetDir)
+        os.symlink(goldFullPath, os.path.join(targetDir, 'ld'))
+
+        linker = ['-B', targetDir]
+        cmd = cmd_base + linker + version_check
+        if 'GNU gold' in check_cmd_output(*cmd).decode('utf-8'):
+            # We have detected gold, will build with the -B workaround
+            return namespace(
+                KIND='gold',
+                LINKER_FLAG=linker,
+            )
+        else:
+            # The -B trick didn't work, removing the directory
+            shutil.rmtree(targetDir)
+
+    cmd = cmd_base + version_check
+    cmd_output = check_cmd_output(*cmd).decode('utf-8')
+    # using decode because ld can be localized and python will
+    # have problems with french accent for example
+    if 'GNU ld' in cmd_output:
+        # We are using the normal linker
+        return namespace(
+            KIND='bfd'
+        )
+
+    # Special case for Android. In the ndk, it is gold
+    if 'GNU gold' in cmd_output:
+        return namespace(
+            KIND='gold'
+        )
+
+    die("Could not find any linker")
+
+
+set_config('LD_IS_BFD', depends(enable_gold.KIND)(lambda x: x == 'bfd' or None))
+set_config('LINKER_LDFLAGS', enable_gold.LINKER_FLAG)