Bug 1293253 - part 3 - add configure support for determining a --target value for Rust host code; r=chmanchester
authorNathan Froyd <froydnj@mozilla.com>
Mon, 28 Nov 2016 11:20:39 -0500
changeset 324480 009cffc985ab7e9b5736edfd6c68f9f5203915bb
parent 324479 779d75b98fc5b19a8e45a82ad357f0ecbedcdcd9
child 324481 5586b76ab2a4c7b059485206e36217bc8c0bc4d2
push id24
push usermaklebus@msu.edu
push dateTue, 20 Dec 2016 03:11:33 +0000
reviewerschmanchester
bugs1293253
milestone53.0a1
Bug 1293253 - part 3 - add configure support for determining a --target value for Rust host code; r=chmanchester We'll need this for compiling host binaries. We could just call `rustc` without any --target value whatsoever, but since we use --target for target code, we might as well be consistent and explicit, and use --target for host code as well.
build/moz.configure/rust.configure
--- a/build/moz.configure/rust.configure
+++ b/build/moz.configure/rust.configure
@@ -66,39 +66,46 @@ def rust_compiler(value, rustc, rustc_in
             version {} of the 'rustc' toolchain and make sure it is
             first in your path.
             You can verify this by typing 'rustc --version'.
             '''.format(version, min_version)))
         return True
 
 set_config('MOZ_RUST', rust_compiler)
 
-@depends(rust_compiler, rustc, target)
-@imports('os')
-@imports('subprocess')
-@imports(_from='mozbuild.configure.util', _import='LineIO')
-@imports(_from='mozbuild.shellutil', _import='quote')
-@imports(_from='tempfile', _import='mkstemp')
-def rust_target(rust_compiler, rustc, target):
-    if rust_compiler:
+@template
+def rust_triple_alias(host_or_target):
+    """Template defining the alias used for rustc's --target flag.
+    `host_or_target` is either `host` or `target` (the @depends functions
+    from init.configure).
+    """
+    assert host_or_target in (host, target)
+
+    @depends(rustc, host_or_target, when=rust_compiler)
+    @imports('os')
+    @imports('subprocess')
+    @imports(_from='mozbuild.configure.util', _import='LineIO')
+    @imports(_from='mozbuild.shellutil', _import='quote')
+    @imports(_from='tempfile', _import='mkstemp')
+    def rust_target(rustc, host_or_target):
         # Rust's --target options are similar to, but not exactly the same
         # as, the autoconf-derived targets we use.  An example would be that
         # Rust uses distinct target triples for targetting the GNU C++ ABI
         # and the MSVC C++ ABI on Win32, whereas autoconf has a single
         # triple and relies on the user to ensure that everything is
         # compiled for the appropriate ABI.  We need to perform appropriate
         # munging to get the correct option to rustc.
         #
         # The canonical list of targets supported can be derived from:
         #
         # https://github.com/rust-lang/rust/tree/master/mk/cfg
 
         # Avoid having to write out os+kernel for all the platforms where
         # they don't differ.
-        os_or_kernel = target.kernel if target.kernel == 'Linux' and target.os != 'Android' else target.os
+        os_or_kernel = host_or_target.kernel if host_or_target.kernel == 'Linux' and host_or_target.os != 'Android' else host_or_target.os
         rustc_target = {
             # DragonFly
             ('x86_64', 'DragonFly'): 'x86_64-unknown-dragonfly',
             # FreeBSD
             ('x86', 'FreeBSD'): 'i686-unknown-freebsd',
             ('x86_64', 'FreeBSD'): 'x86_64-unknown-freebsd',
             # NetBSD
             ('x86_64', 'NetBSD'): 'x86_64-unknown-netbsd',
@@ -115,20 +122,20 @@ def rust_target(rust_compiler, rustc, ta
             # Android
             ('x86', 'Android'): 'i686-linux-android',
             ('arm', 'Android'): 'armv7-linux-androideabi',
             # Windows
             # XXX better detection of CXX needed here, to figure out whether
             # we need i686-pc-windows-gnu instead, since mingw32 builds work.
             ('x86', 'WINNT'): 'i686-pc-windows-msvc',
             ('x86_64', 'WINNT'): 'x86_64-pc-windows-msvc',
-        }.get((target.cpu, os_or_kernel), None)
+        }.get((host_or_target.cpu, os_or_kernel), None)
 
         if rustc_target is None:
-            die("Don't know how to translate {} for rustc".format(target.alias))
+            die("Don't know how to translate {} for rustc".format(host_or_target.alias))
 
         # Check to see whether our rustc has a reasonably functional stdlib
         # for our chosen target.
         target_arg = '--target=' + rustc_target
         in_fd, in_path = mkstemp(prefix='conftest', suffix='.rs')
         out_fd, out_path = mkstemp(prefix='conftest', suffix='.rlib')
         os.close(out_fd)
         try:
@@ -143,24 +150,31 @@ def rust_target(rust_compiler, rustc, ta
             cmd = [
                 rustc,
                 '--crate-type', 'staticlib',
                 target_arg,
                 '-o', out_path,
                 in_path,
             ]
             def failed():
-                die('Cannot compile for {} with {}'.format(target.alias, rustc))
+                die('Cannot compile for {} with {}'.format(host_or_target.alias, rustc))
             check_cmd_output(*cmd, onerror=failed)
             if not os.path.exists(out_path) or os.path.getsize(out_path) == 0:
                 failed()
         finally:
             os.remove(in_path)
             os.remove(out_path)
+
         # This target is usable.
         return rustc_target
 
-set_config('RUST_TARGET', rust_target)
+    return rust_target
+
+rust_target_triple = rust_triple_alias(target)
+rust_host_triple = rust_triple_alias(host)
+
+set_config('RUST_TARGET', rust_target_triple)
+set_config('RUST_HOST_TARGET', rust_host_triple)
 
 # Until we remove all the other Rust checks in old-configure.
 add_old_configure_assignment('MOZ_RUST', rust_compiler)
 add_old_configure_assignment('RUSTC', rustc)
-add_old_configure_assignment('RUST_TARGET', rust_target)
+add_old_configure_assignment('RUST_TARGET', rust_target_triple)