Bug 1319156 - part 1 - add features to RustLibrary objects and moz.build definitions; r=chmanchester
authorNathan Froyd <froydnj@mozilla.com>
Fri, 02 Dec 2016 11:39:15 -0500
changeset 325172 8276296cd50509ba109d115d7b1d3b28b761e97c
parent 325171 d9d80b5b44fc7b11a935e853844caa8b51cf16d7
child 325173 b5de6b777970f98b97e9f33db1e7e8b4a7deeeab
push id24
push usermaklebus@msu.edu
push dateTue, 20 Dec 2016 03:11:33 +0000
reviewerschmanchester
bugs1319156
milestone53.0a1
Bug 1319156 - part 1 - add features to RustLibrary objects and moz.build definitions; r=chmanchester Ideally, people will just use the extra argument to RustLibrary to define features for their library.
build/templates.mozbuild
python/mozbuild/mozbuild/frontend/context.py
python/mozbuild/mozbuild/frontend/data.py
python/mozbuild/mozbuild/frontend/emitter.py
--- a/build/templates.mozbuild
+++ b/build/templates.mozbuild
@@ -56,21 +56,25 @@ def CppUnitTests(names, ext='.cpp'):
 
 @template
 def Library(name):
     '''Template for libraries.'''
     LIBRARY_NAME = name
 
 
 @template
-def RustLibrary(name):
+def RustLibrary(name, features=None):
     '''Template for Rust libraries.'''
     Library(name)
 
+    if not features:
+        features = []
+
     IS_RUST_LIBRARY = True
+    RUST_LIBRARY_FEATURES = features
 
 
 @template
 def SharedLibrary(name):
     '''Template for shared libraries.'''
     Library(name)
 
     FORCE_SHARED_LIB = True
--- a/python/mozbuild/mozbuild/frontend/context.py
+++ b/python/mozbuild/mozbuild/frontend/context.py
@@ -947,16 +947,23 @@ VARIABLES = {
 
     'IS_RUST_LIBRARY': (bool, bool,
         """Whether the current library defined by this moz.build is built by Rust.
 
         The library defined by this moz.build should have a build definition in
         a Cargo.toml file that exists in this moz.build's directory.
         """),
 
+    'RUST_LIBRARY_FEATURES': (List, list,
+        """Cargo features to activate for this library.
+
+        This variable should not be used directly; you should be using the
+        RustLibrary template instead.
+        """),
+
     'UNIFIED_SOURCES': (ContextDerivedTypedList(SourcePath, StrictOrderingOnAppendList), list,
         """Source code files that can be compiled together.
 
         This variable contains a list of source code files to compile,
         that can be concatenated all together and built as a single source
         file. This can help make the build faster and reduce the debug info
         size.
         """),
--- a/python/mozbuild/mozbuild/frontend/data.py
+++ b/python/mozbuild/mozbuild/frontend/data.py
@@ -521,34 +521,37 @@ class StaticLibrary(Library):
 
 class RustLibrary(StaticLibrary):
     """Context derived container object for a static library"""
     __slots__ = (
         'cargo_file',
         'crate_type',
         'dependencies',
         'deps_path',
+        'features',
     )
 
-    def __init__(self, context, basename, cargo_file, crate_type, dependencies, **args):
+    def __init__(self, context, basename, cargo_file, crate_type, dependencies,
+                 features, **args):
         StaticLibrary.__init__(self, context, basename, **args)
         self.cargo_file = cargo_file
         self.crate_type = crate_type
         # We need to adjust our naming here because cargo replaces '-' in
         # package names defined in Cargo.toml with underscores in actual
         # filenames. But we need to keep the basename consistent because
         # many other things in the build system depend on that.
         assert self.crate_type == 'staticlib'
         self.lib_name = '%s%s%s' % (context.config.lib_prefix,
                                      basename.replace('-', '_'),
                                      context.config.lib_suffix)
         self.dependencies = dependencies
         build_dir = cargo_target_directory(context)
         self.import_name = mozpath.join(build_dir, self.lib_name)
         self.deps_path = mozpath.join(build_dir, 'deps')
+        self.features = features
 
 
 class SharedLibrary(Library):
     """Context derived container object for a shared library"""
     __slots__ = (
         'soname',
         'variant',
         'symbols_file',
--- a/python/mozbuild/mozbuild/frontend/emitter.py
+++ b/python/mozbuild/mozbuild/frontend/emitter.py
@@ -508,18 +508,25 @@ class TreeMetadataEmitter(LoggingMixin):
             if panic != 'abort':
                 raise SandboxValidationError(
                     ('Cargo.toml for %s does not specify `panic = "abort"`'
                      ' in [profile.%s] section') % (libname, profile_name),
                     context)
 
         dependencies = set(config.get('dependencies', {}).iterkeys())
 
+        features = context.get('RUST_LIBRARY_FEATURES', [])
+        unique_features = set(features)
+        if len(features) != len(unique_features):
+            raise SandboxValidationError(
+                'features for %s should not contain duplicates: %s' % (libname, features),
+                context)
+
         return RustLibrary(context, libname, cargo_file, crate_type,
-                           dependencies, **static_args)
+                           dependencies, features, **static_args)
 
     def _handle_linkables(self, context, passthru, generated_files):
         linkables = []
         host_linkables = []
         def add_program(prog, var):
             if var.startswith('HOST_'):
                 host_linkables.append(prog)
             else: