Bug 1467039 - Default to gold on local builds. r?build draft
authorMike Hommey <mh+mozilla@glandium.org>
Thu, 07 Jun 2018 08:46:31 +0900
changeset 807235 42b96511be8bb1386d55485d10308609e14428ad
parent 806934 a2e4bbd59dc74db9f12815ce2cec3aff21fd5cca
child 807238 83d9a05ea3801b828c0e77e7cd7cda2add6ed886
push id113052
push userbmo:mh+mozilla@glandium.org
push dateWed, 13 Jun 2018 22:45:53 +0000
reviewersbuild
bugs1467039, 1455767
milestone62.0a1
Bug 1467039 - Default to gold on local builds. r?build We used to do that before bug 1455767. Taking a step back, what we want (and what this change implements) is to: - allow to specify a linker to use explicitly - check that using the corresponding linker flags makes us use that linker - if no linker is specified, and developer options are enabled, we want to try to use gold if it's available, otherwise, we want to detect what kind of linker the default linker is - if --disable-gold is explicitly given, we don't want to automatically use it.
build/moz.configure/toolchain.configure
--- a/build/moz.configure/toolchain.configure
+++ b/build/moz.configure/toolchain.configure
@@ -1552,84 +1552,78 @@ def is_linker_option_enabled(target):
 option('--enable-gold',
        env='MOZ_FORCE_GOLD',
        help='Enable GNU Gold Linker when it is not already the default',
        when=is_linker_option_enabled)
 
 imply_option('--enable-linker', 'gold', when='--enable-gold')
 
 js_option('--enable-linker', nargs=1,
-          help='Select the linker {bfd, gold, lld, lld-*, other}',
+          help='Select the linker {bfd, gold, lld, lld-*}',
           when=is_linker_option_enabled)
 
 
-@depends('--enable-linker', c_compiler, developer_options,
+@depends('--enable-linker', c_compiler, developer_options, '--enable-gold',
          extra_toolchain_flags, when=is_linker_option_enabled)
 @checking('for linker', lambda x: x.KIND)
 @imports('os')
 @imports('shutil')
-def select_linker(linker, c_compiler, developer_options, toolchain_flags):
+def select_linker(linker, c_compiler, developer_options, enable_gold,
+                  toolchain_flags):
 
-    linker = linker[0] if linker else 'other'
+    linker = linker[0] if linker else None
 
-    if linker not in ('bfd', 'gold', 'lld', 'other') and not linker.startswith("lld-"):
+    if linker not in ('bfd', 'gold', 'lld', None) and not linker.startswith("lld-"):
         # Check that we are trying to use a supported linker
         die('Unsupported linker ' + linker)
 
     # Check the kind of linker
     version_check = ['-Wl,--version']
     cmd_base = c_compiler.wrapper + [c_compiler.compiler] + c_compiler.flags
-    # Generate the compiler flag
-    linker_flag = ["-fuse-ld=" + linker] if linker != "other" else []
-    cmd = cmd_base + linker_flag + version_check
-    if toolchain_flags:
-        cmd += toolchain_flags
+
+    def try_linker(linker):
+        # Generate the compiler flag
+        linker_flag = ["-fuse-ld=" + linker] if linker else []
+        cmd = cmd_base + linker_flag + version_check
+        if toolchain_flags:
+            cmd += toolchain_flags
+
+        cmd_output = check_cmd_output(*cmd).decode('utf-8')
+        if 'GNU ld' in cmd_output:
+            # We are using the normal linker
+            kind = 'bfd'
 
-    if (linker == 'gold' or developer_options) and linker != 'bfd':
-        if 'GNU gold' in check_cmd_output(*cmd).decode('utf-8'):
-            # We have detected gold, will build with -fuse-ld=gold
-            return namespace(
-                KIND='gold',
-                LINKER_FLAG=linker_flag,
-            )
+        elif 'GNU gold' in cmd_output:
+            kind = 'gold'
+
+        elif 'LLD' in cmd_output:
+            kind = 'lld'
 
-        # gold is only required if --enable-gold is used.
-        if linker == 'gold':
-            die('Could not find gold')
-        # Else fallthrough.
+        else:
+            kind = 'unknown'
 
-    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'
+            KIND=kind,
+            LINKER_FLAG=linker_flag,
         )
 
-    # Special case for Android. In the ndk, it is gold
-    if 'GNU gold' in cmd_output:
-        return namespace(
-            KIND='gold'
-        )
+    result = try_linker(linker)
+
+    if (linker is None and enable_gold.origin == 'default' and
+            developer_options and result.KIND == 'bfd'):
+        gold = try_linker('gold')
 
-    if 'LLD' in cmd_output:
-        return namespace(
-            KIND='lld',
-            LINKER_FLAG=linker_flag,
-        )
-    elif linker.startswith('lld'):
-        # We forced the lld linker but could not find the string
-        # when checking, fail the build
+        if gold.KIND == 'gold':
+            result = gold
+
+    # If an explicit linker was given, error out if what we found is different.
+    if linker and not linker.startswith(result.KIND):
         die("Could not use {} as linker".format(linker))
 
-    # For other platforms without gold or the GNU linker
-    return namespace(
-        KIND='other'
-    )
+    return result
 
 
 set_config('LD_IS_BFD', depends(select_linker.KIND)
            (lambda x: x == 'bfd' or None))
 set_config('LINKER_LDFLAGS', select_linker.LINKER_FLAG)
 
 
 js_option('--enable-clang-plugin', env='ENABLE_CLANG_PLUGIN',