Bug 1231764 - part 6 - build rust code via cargo; r=chmanchester
authorNathan Froyd <froydnj@gmail.com>
Sat, 06 Aug 2016 00:49:26 -0400
changeset 308451 0ceb4834ee8f544ba908031e279963c39be3b47b
parent 308450 edfecf5c779f2993769ddeaa8410d2ca2ef5b9c2
child 308452 0ac120171cecdd3b347e84156fab9d2f49a204d9
push id30537
push userphilringnalda@gmail.com
push dateSun, 07 Aug 2016 06:34:58 +0000
treeherdermozilla-central@70948f120f5f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerschmanchester
bugs1231764
milestone51.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1231764 - part 6 - build rust code via cargo; r=chmanchester This patch is really two separate changes. The first change is that rust crates are large, standalone entities that may contain multitudes of source files. It therefore doesn't make sense to keep them in SOURCES, as we have been doing. Moving to use cargo will require a higher-level approach, which suggests that we need a different, higher-level representation for Rust sources in the build system. The representation here is to have the build system refer to things defined in Cargo.toml files as the entities dealt with in the build system, and let Cargo deal with the details of actually building things. This approach means that adding a new crate to an existing library just requires editing Rust and Cargo.toml files, rather than dealing with moz.build, which seems more natural to Rust programmers. By having the source files for libraries (and binaries in subsequent iterations of this support) checked in to the tree, we can also take advantage of Cargo.lock files. The second is that we switch the core build system over to building via cargo, rather than invoking rustc directly. We also clean up a number of leftover things from the Old Way of doing things. A number of tests are added to confirm that we'll only permit crates to be built that have dependencies in-tree.
build/moz.configure/rust.configure
build/templates.mozbuild
config/rules.mk
dom/media/gtest/moz.build
media/libstagefright/binding/mp4parse-cargo.patch
media/libstagefright/binding/mp4parse/Cargo.toml
media/libstagefright/binding/update-rust.sh
media/libstagefright/moz.build
python/mozbuild/mozbuild/backend/common.py
python/mozbuild/mozbuild/backend/recursivemake.py
python/mozbuild/mozbuild/frontend/context.py
python/mozbuild/mozbuild/frontend/data.py
python/mozbuild/mozbuild/frontend/emitter.py
python/mozbuild/mozbuild/test/frontend/data/crate-build-dependency-absolute-path/Cargo.toml
python/mozbuild/mozbuild/test/frontend/data/crate-build-dependency-absolute-path/moz.build
python/mozbuild/mozbuild/test/frontend/data/crate-build-dependency-nonexistent-path/Cargo.toml
python/mozbuild/mozbuild/test/frontend/data/crate-build-dependency-nonexistent-path/moz.build
python/mozbuild/mozbuild/test/frontend/data/crate-build-dependency-recursive/Cargo.toml
python/mozbuild/mozbuild/test/frontend/data/crate-build-dependency-recursive/moz.build
python/mozbuild/mozbuild/test/frontend/data/crate-build-dependency-recursive/ok/Cargo.toml
python/mozbuild/mozbuild/test/frontend/data/crate-build-dependency-version-only/Cargo.toml
python/mozbuild/mozbuild/test/frontend/data/crate-build-dependency-version-only/moz.build
python/mozbuild/mozbuild/test/frontend/data/crate-build-dependency-with-dict/Cargo.toml
python/mozbuild/mozbuild/test/frontend/data/crate-build-dependency-with-dict/moz.build
python/mozbuild/mozbuild/test/frontend/data/crate-dependency-absolute-path/Cargo.toml
python/mozbuild/mozbuild/test/frontend/data/crate-dependency-absolute-path/moz.build
python/mozbuild/mozbuild/test/frontend/data/crate-dependency-nonexistent-path/Cargo.toml
python/mozbuild/mozbuild/test/frontend/data/crate-dependency-nonexistent-path/moz.build
python/mozbuild/mozbuild/test/frontend/data/crate-dependency-path-resolution/Cargo.toml
python/mozbuild/mozbuild/test/frontend/data/crate-dependency-path-resolution/moz.build
python/mozbuild/mozbuild/test/frontend/data/crate-dependency-path-resolution/shallow/Cargo.toml
python/mozbuild/mozbuild/test/frontend/data/crate-dependency-path-resolution/the/depths/Cargo.toml
python/mozbuild/mozbuild/test/frontend/data/crate-dependency-recursive/Cargo.toml
python/mozbuild/mozbuild/test/frontend/data/crate-dependency-recursive/moz.build
python/mozbuild/mozbuild/test/frontend/data/crate-dependency-recursive/ok/Cargo.toml
python/mozbuild/mozbuild/test/frontend/data/crate-dependency-version-only/Cargo.toml
python/mozbuild/mozbuild/test/frontend/data/crate-dependency-version-only/moz.build
python/mozbuild/mozbuild/test/frontend/data/crate-dependency-with-dict/Cargo.toml
python/mozbuild/mozbuild/test/frontend/data/crate-dependency-with-dict/moz.build
python/mozbuild/mozbuild/test/frontend/data/duplicated-crate-names/Cargo.toml
python/mozbuild/mozbuild/test/frontend/data/duplicated-crate-names/crate-one/Cargo.toml
python/mozbuild/mozbuild/test/frontend/data/duplicated-crate-names/crate-one/duplicate/Cargo.toml
python/mozbuild/mozbuild/test/frontend/data/duplicated-crate-names/crate-two/Cargo.toml
python/mozbuild/mozbuild/test/frontend/data/duplicated-crate-names/crate-two/duplicate/Cargo.toml
python/mozbuild/mozbuild/test/frontend/data/duplicated-crate-names/moz.build
python/mozbuild/mozbuild/test/frontend/data/rust-library-dash-folding/Cargo.toml
python/mozbuild/mozbuild/test/frontend/data/rust-library-dash-folding/moz.build
python/mozbuild/mozbuild/test/frontend/data/rust-library-invalid-crate-type/Cargo.toml
python/mozbuild/mozbuild/test/frontend/data/rust-library-invalid-crate-type/moz.build
python/mozbuild/mozbuild/test/frontend/data/rust-library-name-mismatch/Cargo.toml
python/mozbuild/mozbuild/test/frontend/data/rust-library-name-mismatch/moz.build
python/mozbuild/mozbuild/test/frontend/data/rust-library-no-cargo-toml/moz.build
python/mozbuild/mozbuild/test/frontend/data/rust-library-no-lib-section/Cargo.toml
python/mozbuild/mozbuild/test/frontend/data/rust-library-no-lib-section/moz.build
python/mozbuild/mozbuild/test/frontend/test_emitter.py
toolkit/library/gtest/moz.build
toolkit/library/gtest/rust/Cargo.lock
toolkit/library/gtest/rust/Cargo.toml
toolkit/library/gtest/rust/lib.rs
toolkit/library/gtest/rust/moz.build
toolkit/library/moz.build
toolkit/library/rust/Cargo.lock
toolkit/library/rust/Cargo.toml
toolkit/library/rust/lib.rs
toolkit/library/rust/moz.build
toolkit/toolkit.mozbuild
--- a/build/moz.configure/rust.configure
+++ b/build/moz.configure/rust.configure
@@ -24,16 +24,30 @@ cargo = check_prog('CARGO', cargo_binary
 def rustc_info(rustc):
         out = check_cmd_output(rustc, '--version', '--verbose').splitlines()
         info = dict((s.strip() for s in line.split(':', 1)) for line in out[1:])
         return namespace(
             version=Version(info.get('release', '0')),
             commit=info.get('commit-hash', 'unknown'),
         )
 
+@depends_if(cargo)
+@checking('cargo support for --frozen')
+@imports('subprocess')
+def cargo_supports_frozen(cargo):
+    try:
+        lines = subprocess.check_output(
+            [cargo, 'help', 'build']
+        ).splitlines()
+        return any('    --frozen' in l for l in lines)
+    except subprocess.CalledProcessError as e:
+        die('Failed to call cargo: %s', e.message)
+
+set_config('MOZ_CARGO_SUPPORTS_FROZEN', cargo_supports_frozen)
+
 @depends('--enable-rust', rustc, rustc_info)
 @imports(_from='textwrap', _import='dedent')
 def rust_compiler(value, rustc, rustc_info):
     if value:
         if not rustc:
             die(dedent('''\
             Rust compiler not found.
             To compile rust language sources, you must have 'rustc' in your path.
@@ -100,20 +114,17 @@ def rust_target(rust_compiler, rustc, ta
             # 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)
 
         if rustc_target is None:
-            if cross_compiling:
-                die("Don't know how to translate {} for rustc".format(target.alias))
-            # Fall back to implicit (native) target when not cross-compiling
-            return None
+            die("Don't know how to translate {} for rustc".format(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:
@@ -136,16 +147,16 @@ def rust_target(rust_compiler, rustc, ta
                 die('Cannot compile for {} with {}'.format(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 target_arg
+        return rustc_target
 
 set_config('RUST_TARGET', rust_target)
 
 # 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)
--- a/build/templates.mozbuild
+++ b/build/templates.mozbuild
@@ -59,16 +59,24 @@ def CppUnitTests(names, ext='.cpp'):
 
 @template
 def Library(name):
     '''Template for libraries.'''
     LIBRARY_NAME = name
 
 
 @template
+def RustLibrary(name):
+    '''Template for Rust libraries.'''
+    Library(name)
+
+    IS_RUST_LIBRARY = True
+
+
+@template
 def SharedLibrary(name):
     '''Template for shared libraries.'''
     Library(name)
 
     FORCE_SHARED_LIB = True
 
     Binary()
 
--- a/config/rules.mk
+++ b/config/rules.mk
@@ -235,20 +235,19 @@ endif
 COBJS = $(notdir $(CSRCS:.c=.$(OBJ_SUFFIX)))
 SOBJS = $(notdir $(SSRCS:.S=.$(OBJ_SUFFIX)))
 # CPPSRCS can have different extensions (eg: .cpp, .cc)
 CPPOBJS = $(notdir $(addsuffix .$(OBJ_SUFFIX),$(basename $(CPPSRCS))))
 CMOBJS = $(notdir $(CMSRCS:.m=.$(OBJ_SUFFIX)))
 CMMOBJS = $(notdir $(CMMSRCS:.mm=.$(OBJ_SUFFIX)))
 # ASFILES can have different extensions (.s, .asm)
 ASOBJS = $(notdir $(addsuffix .$(OBJ_SUFFIX),$(basename $(ASFILES))))
-RSOBJS = $(addprefix lib,$(notdir $(RSSRCS:.rs=.rlib)))
 RS_STATICLIB_CRATE_OBJ = $(addprefix lib,$(notdir $(RS_STATICLIB_CRATE_SRC:.rs=.$(LIB_SUFFIX))))
 ifndef OBJS
-_OBJS = $(COBJS) $(SOBJS) $(CPPOBJS) $(CMOBJS) $(CMMOBJS) $(ASOBJS) $(RSOBJS)
+_OBJS = $(COBJS) $(SOBJS) $(CPPOBJS) $(CMOBJS) $(CMMOBJS) $(ASOBJS)
 OBJS = $(strip $(_OBJS))
 endif
 
 HOST_COBJS = $(addprefix host_,$(notdir $(HOST_CSRCS:.c=.$(OBJ_SUFFIX))))
 # HOST_CPPOBJS can have different extensions (eg: .cpp, .cc)
 HOST_CPPOBJS = $(addprefix host_,$(notdir $(addsuffix .$(OBJ_SUFFIX),$(basename $(HOST_CPPSRCS)))))
 HOST_CMOBJS = $(addprefix host_,$(notdir $(HOST_CMSRCS:.m=.$(OBJ_SUFFIX))))
 HOST_CMMOBJS = $(addprefix host_,$(notdir $(HOST_CMMSRCS:.mm=.$(OBJ_SUFFIX))))
@@ -561,17 +560,17 @@ GLOBAL_DEPS += Makefile $(addprefix $(DE
 ##############################################
 ifdef COMPILE_ENVIRONMENT
 OBJ_TARGETS = $(OBJS) $(PROGOBJS) $(HOST_OBJS) $(HOST_PROGOBJS)
 
 compile:: host target
 
 host:: $(HOST_LIBRARY) $(HOST_PROGRAM) $(HOST_SIMPLE_PROGRAMS)
 
-target:: $(LIBRARY) $(SHARED_LIBRARY) $(PROGRAM) $(SIMPLE_PROGRAMS)
+target:: $(LIBRARY) $(SHARED_LIBRARY) $(PROGRAM) $(SIMPLE_PROGRAMS) $(RUST_LIBRARY_FILE)
 
 include $(MOZILLA_DIR)/config/makefiles/target_binaries.mk
 endif
 
 ##############################################
 ifneq (1,$(NO_PROFILE_GUIDED_OPTIMIZE))
 ifdef MOZ_PROFILE_USE
 ifeq ($(OS_ARCH)_$(GNU_CC), WINNT_)
@@ -910,26 +909,43 @@ ifdef ASFILES
 # The AS_DASH_C_FLAG is needed cause not all assemblers (Solaris) accept
 # a '-c' flag.
 $(ASOBJS):
 	$(REPORT_BUILD_VERBOSE)
 	$(AS) $(ASOUTOPTION)$@ $(ASFLAGS) $($(notdir $<)_FLAGS) $(AS_DASH_C_FLAG) $(_VPATH_SRCS)
 endif
 
 ifdef MOZ_RUST
-# Assume any system libraries rustc links against are already
-# in the target's LIBS.
-$(RSOBJS):
+ifdef CARGO_FILE
+
+ifdef MOZ_DEBUG
+cargo_build_flags =
+else
+cargo_build_flags = --release
+endif
+ifdef MOZ_CARGO_SUPPORTS_FROZEN
+cargo_build_flags += --frozen
+endif
+
+cargo_build_flags += --manifest-path $(CARGO_FILE)
+cargo_build_flags += --target=$(RUST_TARGET)
+cargo_build_flags += --verbose
+
+# Assume any system libraries rustc links against are already in the target's LIBS.
+#
+# We need to run cargo unconditionally, because cargo is the only thing that
+# has full visibility into how changes in Rust sources might affect the final
+# build.
+force-cargo-build:
 	$(REPORT_BUILD)
-	$(RUSTC) $(RUST_TARGET) $(RUSTFLAGS) --crate-type rlib --emit dep-info=$(MDDEPDIR)/$(call mk_libname,$<).pp,link=$(call mk_libname,$<) $(_VPATH_SRCS)
+	env CARGO_TARGET_DIR=. RUSTC=$(RUSTC) $(CARGO) build $(cargo_build_flags) --
 
-$(RS_STATICLIB_CRATE_OBJ):
-	$(REPORT_BUILD)
-	$(RUSTC) $(RUST_TARGET) $(RUSTFLAGS) --crate-type staticlib $(RLIB_EXTERN_CRATE_OPTIONS) --emit dep-info=$(MDDEPDIR)/$(call mk_global_crate_libname,$(RS_STATICLIB_CRATE_SRC)).pp,link=$@ $(RS_STATICLIB_CRATE_SRC)
-endif
+$(RUST_LIBRARY_FILE): force-cargo-build
+endif # CARGO_FILE
+endif # MOZ_RUST
 
 $(SOBJS):
 	$(REPORT_BUILD)
 	$(AS) -o $@ $(ASFLAGS) $($(notdir $<)_FLAGS) $(LOCAL_INCLUDES) -c $<
 
 $(CPPOBJS):
 	$(REPORT_BUILD_VERBOSE)
 	$(call BUILDSTATUS,OBJECT_FILE $@)
--- a/dom/media/gtest/moz.build
+++ b/dom/media/gtest/moz.build
@@ -32,17 +32,16 @@ if CONFIG['MOZ_EME']:
 
 if CONFIG['MOZ_WEBM_ENCODER']:
     UNIFIED_SOURCES += [
         'TestVideoTrackEncoder.cpp',
         'TestWebMWriter.cpp',
     ]
 
 if CONFIG['MOZ_RUST']:
-    SOURCES += ['hello.rs',]
     UNIFIED_SOURCES += ['TestRust.cpp',]
 
 
 TEST_HARNESS_FILES.gtest += [
     '../test/gizmo-frag.mp4',
     '../test/gizmo.mp4',
     '../test/vp9cake.webm',
     'dash_dashinit.mp4',
new file mode 100644
--- /dev/null
+++ b/media/libstagefright/binding/mp4parse-cargo.patch
@@ -0,0 +1,26 @@
+--- a/media/libstagefright/binding/mp4parse/Cargo.toml
++++ b/media/libstagefright/binding/mp4parse/Cargo.toml
+@@ -17,23 +17,6 @@ exclude = [
+   "*.mp4",
+ ]
+ 
+-build = "build.rs"
+-
+-[dependencies]
+-byteorder = "0.5.0"
+-afl = { version = "0.1.1", optional = true }
+-afl-plugin = { version = "0.1.1", optional = true }
+-abort_on_panic = { version = "1.0.0", optional = true }
+-
+-[dev-dependencies]
+-test-assembler = "0.1.2"
+-
+-[build-dependencies]
+-rusty-cheddar = "0.3.2"
+-
+-[features]
+-fuzz = ["afl", "afl-plugin", "abort_on_panic"]
+-
+ # Somewhat heavy-handed, but we want at least -Z force-overflow-checks=on.
+ [profile.release]
+ debug-assertions = true
--- a/media/libstagefright/binding/mp4parse/Cargo.toml
+++ b/media/libstagefright/binding/mp4parse/Cargo.toml
@@ -12,28 +12,11 @@ license = "MPL-2.0"
 repository = "https://github.com/mozilla/mp4parse-rust"
 
 # Cargo includes random files from the working directory
 # by default! Avoid bloating the package with test files.
 exclude = [
   "*.mp4",
 ]
 
-build = "build.rs"
-
-[dependencies]
-byteorder = "0.5.0"
-afl = { version = "0.1.1", optional = true }
-afl-plugin = { version = "0.1.1", optional = true }
-abort_on_panic = { version = "1.0.0", optional = true }
-
-[dev-dependencies]
-test-assembler = "0.1.2"
-
-[build-dependencies]
-rusty-cheddar = "0.3.2"
-
-[features]
-fuzz = ["afl", "afl-plugin", "abort_on_panic"]
-
 # Somewhat heavy-handed, but we want at least -Z force-overflow-checks=on.
 [profile.release]
 debug-assertions = true
--- a/media/libstagefright/binding/update-rust.sh
+++ b/media/libstagefright/binding/update-rust.sh
@@ -33,13 +33,14 @@ popd
 rm -rf mp4parse/src/byteorder
 mkdir mp4parse/src/byteorder
 cp _upstream/byteorder/src/lib.rs mp4parse/src/byteorder/mod.rs
 cp _upstream/byteorder/src/new.rs mp4parse/src/byteorder/new.rs
 
 echo "Applying patches..."
 patch -p4 < byteorder-mod.patch
 patch -p4 < mp4parse-mod.patch
+patch -p4 < mp4parse-cargo.patch
 
 echo "Cleaning up..."
 rm -rf _upstream
 
 echo "Updated to ${VER}."
--- a/media/libstagefright/moz.build
+++ b/media/libstagefright/moz.build
@@ -75,19 +75,16 @@ SOURCES += [
     'frameworks/av/media/libstagefright/MetaData.cpp',
     'system/core/libutils/RefBase.cpp',
     'system/core/libutils/String16.cpp',
     'system/core/libutils/String8.cpp',
     'system/core/libutils/VectorImpl.cpp',
 ]
 
 if CONFIG['MOZ_RUST']:
-    SOURCES += [
-        'binding/mp4parse/src/lib.rs',
-    ]
     EXPORTS += [
         'binding/include/mp4parse.h',
     ]
 
 UNIFIED_SOURCES += [
     'binding/Adts.cpp',
     'binding/AnnexB.cpp',
     'binding/Box.cpp',
--- a/python/mozbuild/mozbuild/backend/common.py
+++ b/python/mozbuild/mozbuild/backend/common.py
@@ -25,17 +25,16 @@ from mozbuild.frontend.data import (
     ExampleWebIDLInterface,
     IPDLFile,
     FinalTargetPreprocessedFiles,
     FinalTargetFiles,
     GeneratedEventWebIDLFile,
     GeneratedWebIDLFile,
     PreprocessedTestWebIDLFile,
     PreprocessedWebIDLFile,
-    RustCrate,
     SharedLibrary,
     TestManifest,
     TestWebIDLFile,
     UnifiedSources,
     XPIDLFile,
     WebIDLFile,
 )
 from mozbuild.jar import (
--- a/python/mozbuild/mozbuild/backend/recursivemake.py
+++ b/python/mozbuild/mozbuild/backend/recursivemake.py
@@ -54,17 +54,17 @@ from ..frontend.data import (
     JARManifest,
     JavaJarData,
     Library,
     LocalInclude,
     ObjdirFiles,
     ObjdirPreprocessedFiles,
     PerSourceFlag,
     Program,
-    RustCrate,
+    RustLibrary,
     SharedLibrary,
     SimpleProgram,
     Sources,
     StaticLibrary,
     TestManifest,
     VariablePassthru,
     XPIDLFile,
 )
@@ -572,19 +572,21 @@ class RecursiveMakeBackend(CommonBackend
             # automated.
             if isinstance(obj.wrapped, JavaJarData):
                 self._process_java_jar_data(obj.wrapped, backend_file)
             elif isinstance(obj.wrapped, AndroidEclipseProjectData):
                 self._process_android_eclipse_project_data(obj.wrapped, backend_file)
             else:
                 return False
 
-        elif isinstance(obj, RustCrate):
-            # Nothing to do because |Sources| has done the work for us.
-            pass
+        elif isinstance(obj, RustLibrary):
+            self.backend_input_files.add(obj.cargo_file)
+            self._process_rust_library(obj, backend_file)
+            # No need to call _process_linked_libraries, because Rust
+            # libraries are self-contained objects at this point.
 
         elif isinstance(obj, SharedLibrary):
             self._process_shared_library(obj, backend_file)
             self._process_linked_libraries(obj, backend_file)
 
         elif isinstance(obj, StaticLibrary):
             self._process_static_library(obj, backend_file)
             self._process_linked_libraries(obj, backend_file)
@@ -1157,47 +1159,40 @@ class RecursiveMakeBackend(CommonBackend
             backend_file.write('IS_COMPONENT := 1\n')
         if libdef.soname:
             backend_file.write('DSO_SONAME := %s\n' % libdef.soname)
         if libdef.is_sdk:
             backend_file.write('SDK_LIBRARY := %s\n' % libdef.import_name)
         if libdef.symbols_file:
             backend_file.write('SYMBOLS_FILE := %s\n' % libdef.symbols_file)
 
-        rust_rlibs = [o for o in libdef.linked_libraries if isinstance(o, RustCrate)]
-        if rust_rlibs:
-            # write out Rust file with extern crate declarations.
-            extern_crate_file = mozpath.join(libdef.objdir, 'rul.rs')
-            with self._write_file(extern_crate_file) as f:
-                f.write('// AUTOMATICALLY GENERATED.  DO NOT EDIT.\n\n')
-                for rlib in rust_rlibs:
-                    f.write('extern crate %s;\n' % rlib.crate_name)
-
-            backend_file.write('RS_STATICLIB_CRATE_SRC := %s\n' % extern_crate_file)
-
     def _process_static_library(self, libdef, backend_file):
         backend_file.write_once('LIBRARY_NAME := %s\n' % libdef.basename)
         backend_file.write('FORCE_STATIC_LIB := 1\n')
         backend_file.write('REAL_LIBRARY := %s\n' % libdef.lib_name)
         if libdef.is_sdk:
             backend_file.write('SDK_LIBRARY := %s\n' % libdef.import_name)
         if libdef.no_expand_lib:
             backend_file.write('NO_EXPAND_LIBS := 1\n')
 
+    def _process_rust_library(self, libdef, backend_file):
+        backend_file.write_once('RUST_LIBRARY_FILE := %s\n' % libdef.import_name)
+        backend_file.write('CARGO_FILE := $(srcdir)/Cargo.toml')
+
     def _process_host_library(self, libdef, backend_file):
         backend_file.write('HOST_LIBRARY_NAME = %s\n' % libdef.basename)
 
     def _build_target_for_obj(self, obj):
         return '%s/%s' % (mozpath.relpath(obj.objdir,
             self.environment.topobjdir), obj.KIND)
 
     def _process_linked_libraries(self, obj, backend_file):
         def write_shared_and_system_libs(lib):
             for l in lib.linked_libraries:
-                if isinstance(l, StaticLibrary):
+                if isinstance(l, (StaticLibrary, RustLibrary)):
                     write_shared_and_system_libs(l)
                 else:
                     backend_file.write_once('SHARED_LIBS += %s/%s\n'
                         % (pretty_relpath(l), l.import_name))
             for l in lib.linked_system_libs:
                 backend_file.write_once('OS_LIBS += %s\n' % l)
 
         def pretty_relpath(lib):
@@ -1209,24 +1204,25 @@ class RecursiveMakeBackend(CommonBackend
         self._compile_graph[build_target]
 
         for lib in obj.linked_libraries:
             if not isinstance(lib, ExternalLibrary):
                 self._compile_graph[build_target].add(
                     self._build_target_for_obj(lib))
             relpath = pretty_relpath(lib)
             if isinstance(obj, Library):
-                if isinstance(lib, StaticLibrary):
+                if isinstance(lib, RustLibrary):
+                    # We don't need to do anything here; we will handle
+                    # linkage for any RustLibrary elsewhere.
+                    continue
+                elif isinstance(lib, StaticLibrary):
                     backend_file.write_once('STATIC_LIBS += %s/%s\n'
                                         % (relpath, lib.import_name))
                     if isinstance(obj, SharedLibrary):
                         write_shared_and_system_libs(lib)
-                elif isinstance(lib, RustCrate):
-                    backend_file.write_once('RLIB_EXTERN_CRATE_OPTIONS += --extern %s=%s/%s\n'
-                                            % (lib.crate_name, relpath, lib.rlib_filename))
                 elif isinstance(obj, SharedLibrary):
                     assert lib.variant != lib.COMPONENT
                     backend_file.write_once('SHARED_LIBS += %s/%s\n'
                                         % (relpath, lib.import_name))
             elif isinstance(obj, (Program, SimpleProgram)):
                 if isinstance(lib, StaticLibrary):
                     backend_file.write_once('STATIC_LIBS += %s/%s\n'
                                         % (relpath, lib.import_name))
@@ -1235,23 +1231,38 @@ class RecursiveMakeBackend(CommonBackend
                     assert lib.variant != lib.COMPONENT
                     backend_file.write_once('SHARED_LIBS += %s/%s\n'
                                         % (relpath, lib.import_name))
             elif isinstance(obj, (HostLibrary, HostProgram, HostSimpleProgram)):
                 assert isinstance(lib, HostLibrary)
                 backend_file.write_once('HOST_LIBS += %s/%s\n'
                                    % (relpath, lib.import_name))
 
-        # We have to link the Rust super-crate after all intermediate static
-        # libraries have been listed to ensure that the Rust objects are
+        # We have to link any Rust libraries after all intermediate static
+        # libraries have been listed to ensure that the Rust libraries are
         # searched after the C/C++ objects that might reference Rust symbols.
-        # Building the Rust super-crate will take care of Rust->Rust linkage.
-        if isinstance(obj, SharedLibrary) and any(isinstance(o, RustCrate)
+        # Building Rust crates normally takes care of Rust->Rust linkage, but
+        # we have to be careful: a savvy user might have specified that there
+        # is a staticlib (that contains the Rust runtime) and several other
+        # rlibs (which are plain archive files) to be linked into a given
+        # library.  We need to ensure that the staticlib comes after the
+        # rlibs to ensure symbols are found correctly.
+        if isinstance(obj, SharedLibrary) and any(isinstance(o, RustLibrary)
                                                   for o in obj.linked_libraries):
-            backend_file.write('STATIC_LIBS += $(RS_STATICLIB_CRATE_OBJ)\n')
+            libs = [l for l in obj.linked_libraries if isinstance(l, RustLibrary)]
+            def name_cmp(l1, l2):
+                if l1.crate_type == 'staticlib':
+                    return 1
+                if l2.crate_type == 'staticlib':
+                    return -1
+                return cmp(l1.basename, l2.basename)
+            libs.sort(cmp=name_cmp)
+            for l in libs:
+                relpath = pretty_relpath(l)
+                backend_file.write('STATIC_LIBS += %s/%s\n' % (relpath, l.import_name))
 
         for lib in obj.linked_system_libs:
             if obj.KIND == 'target':
                 backend_file.write_once('OS_LIBS += %s\n' % lib)
             else:
                 backend_file.write_once('HOST_EXTRA_LIBS += %s\n' % lib)
 
         # Process library-based defines
@@ -1434,17 +1445,16 @@ class RecursiveMakeBackend(CommonBackend
             self.backend_input_files.add(obj.input_path)
 
         self._makefile_out_count += 1
 
     def _handle_linked_rust_crates(self, obj, extern_crate_file):
         backend_file = self._get_backend_file_for(obj)
 
         backend_file.write('RS_STATICLIB_CRATE_SRC := %s\n' % extern_crate_file)
-        backend_file.write('STATIC_LIBS += librul.$(LIB_SUFFIX)\n')
 
     def _handle_ipdl_sources(self, ipdl_dir, sorted_ipdl_sources,
                              unified_ipdl_cppsrcs_mapping):
         # Write out a master list of all IPDL source files.
         mk = Makefile()
 
         mk.add_statement('ALL_IPDLSRCS := %s' % ' '.join(sorted_ipdl_sources))
 
--- a/python/mozbuild/mozbuild/frontend/context.py
+++ b/python/mozbuild/mozbuild/frontend/context.py
@@ -940,16 +940,23 @@ VARIABLES = {
         Accepts assembler, C, C++, Objective C/C++.
         """),
 
     'FILES_PER_UNIFIED_FILE': (int, int,
         """The number of source files to compile into each unified source file.
 
         """),
 
+    '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.
+        """),
+
     '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
@@ -440,41 +440,64 @@ class Library(BaseLibrary):
     )
 
     def __init__(self, context, basename, real_name=None, is_sdk=False):
         BaseLibrary.__init__(self, context, real_name or basename)
         self.basename = basename
         self.is_sdk = is_sdk
 
 
-class RustCrate(Library):
-    """Context derived container object for a Rust rlib"""
-
-    def __init__(self, context, basename, crate_name, srcdir, rlib_filename, link_into):
-        Library.__init__(self, context, basename)
-        self.crate_name = crate_name
-        self.srcdir = srcdir
-        self.rlib_filename = rlib_filename
-        self.link_into = link_into
-
-
 class StaticLibrary(Library):
     """Context derived container object for a static library"""
     __slots__ = (
         'link_into',
         'no_expand_lib',
     )
 
     def __init__(self, context, basename, real_name=None, is_sdk=False,
         link_into=None, no_expand_lib=False):
         Library.__init__(self, context, basename, real_name, is_sdk)
         self.link_into = link_into
         self.no_expand_lib = no_expand_lib
 
 
+class RustLibrary(StaticLibrary):
+    """Context derived container object for a static library"""
+    __slots__ = (
+        'cargo_file',
+        'crate_type',
+    )
+
+    def __init__(self, context, basename, cargo_file, crate_type, **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
+        )
+        # cargo creates several directories and places its build artifacts
+        # in those directories.  The directory structure depends not only
+        # on the target, but also what sort of build we are doing.
+        rust_build_kind = 'release'
+        if context.config.substs.get('MOZ_DEBUG'):
+            rust_build_kind = 'debug'
+        self.import_name = '%s/%s/%s' % (
+            context.config.substs['RUST_TARGET'],
+            rust_build_kind,
+            self.lib_name,
+        )
+
+
 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
@@ -15,16 +15,17 @@ from collections import defaultdict, Ord
 from mach.mixin.logging import LoggingMixin
 from mozbuild.util import (
     memoize,
     OrderedDefaultDict,
 )
 
 import mozpack.path as mozpath
 import mozinfo
+import pytoml
 
 from .data import (
     AndroidAssetsDirs,
     AndroidExtraPackages,
     AndroidExtraResDirs,
     AndroidResDirs,
     BaseSources,
     BrandingFiles,
@@ -55,17 +56,17 @@ from .data import (
     Linkable,
     LocalInclude,
     ObjdirFiles,
     ObjdirPreprocessedFiles,
     PerSourceFlag,
     PreprocessedTestWebIDLFile,
     PreprocessedWebIDLFile,
     Program,
-    RustCrate,
+    RustLibrary,
     SdkFiles,
     SharedLibrary,
     SimpleProgram,
     Sources,
     StaticLibrary,
     TestHarnessFiles,
     TestWebIDLFile,
     TestManifest,
@@ -123,16 +124,18 @@ class TreeMetadataEmitter(LoggingMixin):
             if isinstance(k, unicode):
                 k = k.encode('ascii')
             self.info[k] = v
 
         self._libs = OrderedDefaultDict(list)
         self._binaries = OrderedDict()
         self._linkage = []
         self._static_linking_shared = set()
+        self._crate_verified_local = set()
+        self._crate_directories = dict()
 
         # Keep track of external paths (third party build systems), starting
         # from what we run a subconfigure in. We'll eliminate some directories
         # as we traverse them with moz.build (e.g. js/src).
         subconfigures = os.path.join(self.config.topobjdir, 'subconfigures')
         paths = []
         if os.path.exists(subconfigures):
             paths = open(subconfigures).read().splitlines()
@@ -190,17 +193,17 @@ class TreeMetadataEmitter(LoggingMixin):
             objs = list(self._emit_libs_derived(contexts))
             self._emitter_time += time.time() - start
 
             for o in emit_objs(objs): yield o
 
     def _emit_libs_derived(self, contexts):
         # First do FINAL_LIBRARY linkage.
         for lib in (l for libs in self._libs.values() for l in libs):
-            if not isinstance(lib, (StaticLibrary, RustCrate)) or not lib.link_into:
+            if not isinstance(lib, (StaticLibrary, RustLibrary)) or not lib.link_into:
                 continue
             if lib.link_into not in self._libs:
                 raise SandboxValidationError(
                     'FINAL_LIBRARY ("%s") does not match any LIBRARY_NAME'
                     % lib.link_into, contexts[lib.objdir])
             candidates = self._libs[lib.link_into]
 
             # When there are multiple candidates, but all are in the same
@@ -368,16 +371,134 @@ class TreeMetadataEmitter(LoggingMixin):
         # context more or less truthful about where the external library is.
         context = Context(config=self.config)
         context.add_source(mozpath.join(self.config.topsrcdir, dir, 'dummy'))
         if force_static:
             return ExternalStaticLibrary(context, name)
         else:
             return ExternalSharedLibrary(context, name)
 
+    def _parse_cargo_file(self, toml_file):
+        """Parse toml_file and return a Python object representation of it."""
+        with open(toml_file, 'r') as f:
+            return pytoml.load(f)
+
+    def _verify_deps(self, context, crate_dir, crate_name, dependencies, description='Dependency'):
+        """Verify that a crate's dependencies all specify local paths."""
+        for dep_crate_name, values in dependencies.iteritems():
+            # A simple version number.
+            if isinstance(values, (str, unicode)):
+                raise SandboxValidationError(
+                    '%s %s of crate %s does not list a path' % (description, dep_crate_name, crate_name),
+                    context)
+
+            dep_path = values.get('path', None)
+            if not dep_path:
+                raise SandboxValidationError(
+                    '%s %s of crate %s does not list a path' % (description, dep_crate_name, crate_name),
+                    context)
+
+            # Try to catch the case where somebody listed a
+            # local path for development.
+            if os.path.isabs(dep_path):
+                raise SandboxValidationError(
+                    '%s %s of crate %s has a non-relative path' % (description, dep_crate_name, crate_name),
+                    context)
+
+            if not os.path.exists(mozpath.join(context.config.topsrcdir, crate_dir, dep_path)):
+                raise SandboxValidationError(
+                    '%s %s of crate %s refers to a non-existent path' % (description, dep_crate_name, crate_name),
+                    context)
+
+    def _verify_local_paths(self, context, crate_dir, config):
+        """Verify that a Cargo.toml config specifies local paths for all its dependencies."""
+        crate_name = config['package']['name']
+
+        # This is not exactly how cargo works (cargo would permit
+        # identically-named packages with different versions in the same
+        # library), but we want to try and avoid using multiple
+        # different versions of a single package for code size reasons.
+        if crate_name not in self._crate_directories:
+            self._crate_directories[crate_name] = crate_dir
+        else:
+            previous_dir = self._crate_directories[crate_name]
+            if crate_dir != previous_dir:
+                raise SandboxValidationError(
+                    'Crate %s found at multiple paths: %s, %s' % (crate_name, crate_dir, previous_dir),
+                    context)
+
+        # This check should ideally be positioned when we're recursing through
+        # dependencies, but then we would never get the chance to run the
+        # duplicated names check above.
+        if crate_name in self._crate_verified_local:
+            return
+
+        crate_deps = config.get('dependencies', {})
+        if crate_deps:
+            self._verify_deps(context, crate_dir, crate_name, crate_deps)
+
+        has_custom_build = 'build' in config['package']
+        build_deps = config.get('build-dependencies', {})
+        if has_custom_build and build_deps:
+            self._verify_deps(context, crate_dir, crate_name, build_deps,
+                              description='Build dependency')
+
+        # We have now verified that all the declared dependencies of
+        # this crate have local paths.  Now verify recursively.
+        self._crate_verified_local.add(crate_name)
+
+        if crate_deps:
+            for dep_crate_name, values in crate_deps.iteritems():
+                rel_dep_dir = mozpath.normpath(mozpath.join(crate_dir, values['path']))
+                dep_toml = mozpath.join(context.config.topsrcdir, rel_dep_dir, 'Cargo.toml')
+                self._verify_local_paths(context, rel_dep_dir, self._parse_cargo_file(dep_toml))
+
+        if has_custom_build and build_deps:
+            for dep_crate_name, values in build_deps.iteritems():
+                rel_dep_dir = mozpath.normpath(mozpath.join(crate_dir, values['path']))
+                dep_toml = mozpath.join(context.config.topsrcdir, rel_dep_dir, 'Cargo.toml')
+                self._verify_local_paths(context, rel_dep_dir, self._parse_cargo_file(dep_toml))
+
+    def _rust_library(self, context, libname, static_args):
+        # We need to note any Rust library for linking purposes.
+        cargo_file = mozpath.join(context.srcdir, 'Cargo.toml')
+        if not os.path.exists(cargo_file):
+            raise SandboxValidationError(
+                'No Cargo.toml file found in %s' % cargo_file, context)
+
+        config = self._parse_cargo_file(cargo_file)
+        crate_name = config['package']['name']
+
+        if crate_name != libname:
+            raise SandboxValidationError(
+                'library %s does not match Cargo.toml-defined package %s' % (libname, crate_name),
+                context)
+
+        lib_section = config.get('lib', None)
+        if not lib_section:
+            raise SandboxValidationError(
+                'Cargo.toml for %s has no [lib] section' % libname,
+                context)
+
+        crate_type = lib_section.get('crate-type', None)
+        if not crate_type:
+            raise SandboxValidationError(
+                'Can\'t determine a crate-type for %s from Cargo.toml' % libname,
+                context)
+
+        crate_type = crate_type[0]
+        if crate_type != 'staticlib':
+            raise SandboxValidationError(
+                'crate-type %s is not permitted for %s' % (crate_type, libname),
+                context)
+
+        self._verify_local_paths(context, context.relsrcdir, config)
+
+        return RustLibrary(context, libname, cargo_file, crate_type, **static_args)
+
     def _handle_linkables(self, context, passthru, generated_files):
         has_linkables = False
 
         for kind, cls in [('PROGRAM', Program), ('HOST_PROGRAM', HostProgram)]:
             program = context.get(kind)
             if program:
                 if program in self._binaries:
                     raise SandboxValidationError(
@@ -575,34 +696,49 @@ class TreeMetadataEmitter(LoggingMixin):
                         'action', 'generate_symbols_file.py')
                     defines = ()
                     if lib.defines:
                         defines = lib.defines.get_defines()
                     yield GeneratedFile(context, script,
                         'generate_symbols_file', lib.symbols_file,
                         [symbols_file], defines)
             if static_lib:
-                lib = StaticLibrary(context, libname, **static_args)
+                is_rust_library = context.get('IS_RUST_LIBRARY')
+                if is_rust_library:
+                    lib = self._rust_library(context, libname, static_args)
+                else:
+                    lib = StaticLibrary(context, libname, **static_args)
                 self._libs[libname].append(lib)
                 self._linkage.append((context, lib, 'USE_LIBS'))
+
+                # Multiple staticlibs for a library means multiple copies
+                # of the Rust runtime, which will result in linking errors
+                # later on.
+                if is_rust_library:
+                    staticlibs = [l for l in self._libs[libname]
+                                  if isinstance(l, RustLibrary) and l.crate_type == 'staticlib']
+                    if len(staticlibs) > 1:
+                        raise SandboxValidationError(
+                            'Cannot have multiple Rust staticlibs in %s: %s' % (libname, ', '.join(l.basename for l in staticlibs)),
+                            context)
+
                 has_linkables = True
 
             if lib_defines:
                 if not libname:
                     raise SandboxValidationError('LIBRARY_DEFINES needs a '
                         'LIBRARY_NAME to take effect', context)
                 lib.lib_defines.update(lib_defines)
 
         # Only emit sources if we have linkables defined in the same context.
         # Note the linkables are not emitted in this function, but much later,
         # after aggregation (because of e.g. USE_LIBS processing).
         if not has_linkables:
             return
 
-        rust_sources = []
         sources = defaultdict(list)
         gen_sources = defaultdict(list)
         all_flags = {}
         for symbol in ('SOURCES', 'HOST_SOURCES', 'UNIFIED_SOURCES'):
             srcs = sources[symbol]
             gen_srcs = gen_sources[symbol]
             context_srcs = context.get(symbol, [])
             for f in context_srcs:
@@ -644,17 +780,16 @@ class TreeMetadataEmitter(LoggingMixin):
         # in the definition; we'll add it in programmatically after defining
         # things.
         suffix_map = {
             '.s': set(['.asm']),
             '.c': set(),
             '.m': set(),
             '.mm': set(),
             '.cpp': set(['.cc', '.cxx']),
-            '.rs': set(),
             '.S': set(),
         }
 
         # The inverse of the above, mapping suffixes to their canonical suffix.
         canonicalized_suffix_map = {}
         for suffix, alternatives in suffix_map.iteritems():
             alternatives.add(suffix)
             for a in alternatives:
@@ -689,52 +824,18 @@ class TreeMetadataEmitter(LoggingMixin):
                 sorted_files = sorted(srcs, key=canonical_suffix_for_file)
                 for canonical_suffix, files in itertools.groupby(
                         sorted_files, canonical_suffix_for_file):
                     arglist = [context, list(files), canonical_suffix]
                     if (variable.startswith('UNIFIED_') and
                             'FILES_PER_UNIFIED_FILE' in context):
                         arglist.append(context['FILES_PER_UNIFIED_FILE'])
                     obj = cls(*arglist)
-
-                    # Rust is special.  Each Rust file that we compile
-                    # is a separate crate and gets compiled into its own
-                    # rlib file (Rust's equivalent of a .a file).  When
-                    # we go to link Rust sources into a library or
-                    # executable, we have a separate, single crate that
-                    # gets compiled as a staticlib (like a rlib, but
-                    # includes the actual Rust runtime).
-                    if canonical_suffix == '.rs':
-                        if not final_lib:
-                            raise SandboxValidationError(
-                                'Rust sources must be destined for a FINAL_LIBRARY')
-                        # If we're building a shared library, we're going to
-                        # auto-generate a module that includes all of the rlib
-                        # files.  This means we can't permit Rust files as
-                        # immediate inputs of the shared library.
-                        if libname and shared_lib:
-                            raise SandboxValidationError(
-                                'No Rust sources permitted as an immediate input of %s: %s' % (shlib, files))
-                        rust_sources.append(obj)
                     yield obj
 
-        # Rust sources get translated into rlibs, which are essentially static
-        # libraries.  We need to note all of them for linking, too.
-        if libname and static_lib:
-            for s in rust_sources:
-                for f in s.files:
-                    (base, _) = mozpath.splitext(mozpath.basename(f))
-                    crate_name = context.relsrcdir.replace('/', '_') + '_' + base
-                    rlib_filename = 'lib' + base + '.rlib'
-                    lib = RustCrate(context, libname, crate_name,
-                                          mozpath.join(context.srcdir, mozpath.dirname(f)),
-                                          rlib_filename, final_lib)
-                    self._libs[libname].append(lib)
-                    self._linkage.append((context, lib, 'USE_LIBS'))
-
         for f, flags in all_flags.iteritems():
             if flags.flags:
                 ext = mozpath.splitext(f)[1]
                 yield PerSourceFlag(context, f, flags.flags)
 
 
     def emit_from_context(self, context):
         """Convert a Context to tree metadata objects.
new file mode 100644
--- /dev/null
+++ b/python/mozbuild/mozbuild/test/frontend/data/crate-build-dependency-absolute-path/Cargo.toml
@@ -0,0 +1,14 @@
+[package]
+name = "random-crate"
+version = "0.1.0"
+authors = [
+  "Nobody <nobody@mozilla.org>",
+]
+
+build = "build.rs"
+
+[lib]
+crate-type = ["staticlib"]
+
+[build-dependencies]
+bogus = { version = "0.1.0", path = "/path/to/somewhere" }
new file mode 100644
--- /dev/null
+++ b/python/mozbuild/mozbuild/test/frontend/data/crate-build-dependency-absolute-path/moz.build
@@ -0,0 +1,18 @@
+# Any copyright is dedicated to the Public Domain.
+# http://creativecommons.org/publicdomain/zero/1.0/
+
+@template
+def Library(name):
+    '''Template for libraries.'''
+    LIBRARY_NAME = name
+
+
+@template
+def RustLibrary(name):
+    '''Template for Rust libraries.'''
+    Library(name)
+
+    IS_RUST_LIBRARY = True
+
+
+RustLibrary('random-crate')
new file mode 100644
--- /dev/null
+++ b/python/mozbuild/mozbuild/test/frontend/data/crate-build-dependency-nonexistent-path/Cargo.toml
@@ -0,0 +1,14 @@
+[package]
+name = "random-crate"
+version = "0.1.0"
+authors = [
+  "Nobody <nobody@mozilla.org>",
+]
+
+build = "build.rs"
+
+[lib]
+crate-type = ["staticlib"]
+
+[build-dependencies]
+bogus = { version = "0.1.0", path = "../bogus" }
new file mode 100644
--- /dev/null
+++ b/python/mozbuild/mozbuild/test/frontend/data/crate-build-dependency-nonexistent-path/moz.build
@@ -0,0 +1,18 @@
+# Any copyright is dedicated to the Public Domain.
+# http://creativecommons.org/publicdomain/zero/1.0/
+
+@template
+def Library(name):
+    '''Template for libraries.'''
+    LIBRARY_NAME = name
+
+
+@template
+def RustLibrary(name):
+    '''Template for Rust libraries.'''
+    Library(name)
+
+    IS_RUST_LIBRARY = True
+
+
+RustLibrary('random-crate')
new file mode 100644
--- /dev/null
+++ b/python/mozbuild/mozbuild/test/frontend/data/crate-build-dependency-recursive/Cargo.toml
@@ -0,0 +1,12 @@
+[package]
+name = "random-crate"
+version = "0.1.0"
+authors = [
+  "Nobody <nobody@mozilla.org>",
+]
+
+[lib]
+crate-type = ["staticlib"]
+
+[dependencies]
+ok = { version = "0.1.0", path = "ok" }
new file mode 100644
--- /dev/null
+++ b/python/mozbuild/mozbuild/test/frontend/data/crate-build-dependency-recursive/moz.build
@@ -0,0 +1,18 @@
+# Any copyright is dedicated to the Public Domain.
+# http://creativecommons.org/publicdomain/zero/1.0/
+
+@template
+def Library(name):
+    '''Template for libraries.'''
+    LIBRARY_NAME = name
+
+
+@template
+def RustLibrary(name):
+    '''Template for Rust libraries.'''
+    Library(name)
+
+    IS_RUST_LIBRARY = True
+
+
+RustLibrary('random-crate')
new file mode 100644
--- /dev/null
+++ b/python/mozbuild/mozbuild/test/frontend/data/crate-build-dependency-recursive/ok/Cargo.toml
@@ -0,0 +1,11 @@
+[package]
+name = "ok"
+version = "0.1.0"
+authors = [
+  "Nobody <nobody@mozilla.org>",
+]
+
+build = "build.rs"
+
+[build-dependencies]
+bogus = { version = "0.1.0" }
new file mode 100644
--- /dev/null
+++ b/python/mozbuild/mozbuild/test/frontend/data/crate-build-dependency-version-only/Cargo.toml
@@ -0,0 +1,14 @@
+[package]
+name = "random-crate"
+version = "0.1.0"
+authors = [
+  "Nobody <nobody@mozilla.org>",
+]
+
+build = "build.rs"
+
+[lib]
+crate-type = ["staticlib"]
+
+[build-dependencies]
+bogus = "0.1.0"
new file mode 100644
--- /dev/null
+++ b/python/mozbuild/mozbuild/test/frontend/data/crate-build-dependency-version-only/moz.build
@@ -0,0 +1,18 @@
+# Any copyright is dedicated to the Public Domain.
+# http://creativecommons.org/publicdomain/zero/1.0/
+
+@template
+def Library(name):
+    '''Template for libraries.'''
+    LIBRARY_NAME = name
+
+
+@template
+def RustLibrary(name):
+    '''Template for Rust libraries.'''
+    Library(name)
+
+    IS_RUST_LIBRARY = True
+
+
+RustLibrary('random-crate')
new file mode 100644
--- /dev/null
+++ b/python/mozbuild/mozbuild/test/frontend/data/crate-build-dependency-with-dict/Cargo.toml
@@ -0,0 +1,14 @@
+[package]
+name = "random-crate"
+version = "0.1.0"
+authors = [
+  "Nobody <nobody@mozilla.org>",
+]
+
+build = "build.rs"
+
+[lib]
+crate-type = ["staticlib"]
+
+[build-dependencies]
+bogus = { version = "0.1.0" }
new file mode 100644
--- /dev/null
+++ b/python/mozbuild/mozbuild/test/frontend/data/crate-build-dependency-with-dict/moz.build
@@ -0,0 +1,18 @@
+# Any copyright is dedicated to the Public Domain.
+# http://creativecommons.org/publicdomain/zero/1.0/
+
+@template
+def Library(name):
+    '''Template for libraries.'''
+    LIBRARY_NAME = name
+
+
+@template
+def RustLibrary(name):
+    '''Template for Rust libraries.'''
+    Library(name)
+
+    IS_RUST_LIBRARY = True
+
+
+RustLibrary('random-crate')
new file mode 100644
--- /dev/null
+++ b/python/mozbuild/mozbuild/test/frontend/data/crate-dependency-absolute-path/Cargo.toml
@@ -0,0 +1,12 @@
+[package]
+name = "random-crate"
+version = "0.1.0"
+authors = [
+  "Nobody <nobody@mozilla.org>",
+]
+
+[lib]
+crate-type = ["staticlib"]
+
+[dependencies]
+bogus = { version = "0.1.0", path = "/path/to/somewhere" }
new file mode 100644
--- /dev/null
+++ b/python/mozbuild/mozbuild/test/frontend/data/crate-dependency-absolute-path/moz.build
@@ -0,0 +1,18 @@
+# Any copyright is dedicated to the Public Domain.
+# http://creativecommons.org/publicdomain/zero/1.0/
+
+@template
+def Library(name):
+    '''Template for libraries.'''
+    LIBRARY_NAME = name
+
+
+@template
+def RustLibrary(name):
+    '''Template for Rust libraries.'''
+    Library(name)
+
+    IS_RUST_LIBRARY = True
+
+
+RustLibrary('random-crate')
new file mode 100644
--- /dev/null
+++ b/python/mozbuild/mozbuild/test/frontend/data/crate-dependency-nonexistent-path/Cargo.toml
@@ -0,0 +1,12 @@
+[package]
+name = "random-crate"
+version = "0.1.0"
+authors = [
+  "Nobody <nobody@mozilla.org>",
+]
+
+[lib]
+crate-type = ["staticlib"]
+
+[dependencies]
+bogus = { version = "0.1.0", path = "../bogus" }
new file mode 100644
--- /dev/null
+++ b/python/mozbuild/mozbuild/test/frontend/data/crate-dependency-nonexistent-path/moz.build
@@ -0,0 +1,18 @@
+# Any copyright is dedicated to the Public Domain.
+# http://creativecommons.org/publicdomain/zero/1.0/
+
+@template
+def Library(name):
+    '''Template for libraries.'''
+    LIBRARY_NAME = name
+
+
+@template
+def RustLibrary(name):
+    '''Template for Rust libraries.'''
+    Library(name)
+
+    IS_RUST_LIBRARY = True
+
+
+RustLibrary('random-crate')
new file mode 100644
--- /dev/null
+++ b/python/mozbuild/mozbuild/test/frontend/data/crate-dependency-path-resolution/Cargo.toml
@@ -0,0 +1,12 @@
+[package]
+name = "random-crate"
+version = "0.1.0"
+authors = [
+  "Nobody <nobody@mozilla.org>",
+]
+
+[lib]
+crate-type = ["staticlib"]
+
+[dependencies]
+deep-crate = { version = "0.1.0", path = "the/depths" }
new file mode 100644
--- /dev/null
+++ b/python/mozbuild/mozbuild/test/frontend/data/crate-dependency-path-resolution/moz.build
@@ -0,0 +1,18 @@
+# Any copyright is dedicated to the Public Domain.
+# http://creativecommons.org/publicdomain/zero/1.0/
+
+@template
+def Library(name):
+    '''Template for libraries.'''
+    LIBRARY_NAME = name
+
+
+@template
+def RustLibrary(name):
+    '''Template for Rust libraries.'''
+    Library(name)
+
+    IS_RUST_LIBRARY = True
+
+
+RustLibrary('random-crate')
new file mode 100644
--- /dev/null
+++ b/python/mozbuild/mozbuild/test/frontend/data/crate-dependency-path-resolution/shallow/Cargo.toml
@@ -0,0 +1,6 @@
+[package]
+name = "shallow-crate"
+version = "0.1.0"
+authors = [
+  "Nobody <nobody@mozilla.org>",
+]
new file mode 100644
--- /dev/null
+++ b/python/mozbuild/mozbuild/test/frontend/data/crate-dependency-path-resolution/the/depths/Cargo.toml
@@ -0,0 +1,9 @@
+[package]
+name = "deep-crate"
+version = "0.1.0"
+authors = [
+  "Nobody <nobody@mozilla.org>",
+]
+
+[dependencies]
+shallow-crate = { path = "../../shallow" }
new file mode 100644
--- /dev/null
+++ b/python/mozbuild/mozbuild/test/frontend/data/crate-dependency-recursive/Cargo.toml
@@ -0,0 +1,12 @@
+[package]
+name = "random-crate"
+version = "0.1.0"
+authors = [
+  "Nobody <nobody@mozilla.org>",
+]
+
+[lib]
+crate-type = ["staticlib"]
+
+[dependencies]
+ok = { version = "0.1.0", path = "ok" }
new file mode 100644
--- /dev/null
+++ b/python/mozbuild/mozbuild/test/frontend/data/crate-dependency-recursive/moz.build
@@ -0,0 +1,18 @@
+# Any copyright is dedicated to the Public Domain.
+# http://creativecommons.org/publicdomain/zero/1.0/
+
+@template
+def Library(name):
+    '''Template for libraries.'''
+    LIBRARY_NAME = name
+
+
+@template
+def RustLibrary(name):
+    '''Template for Rust libraries.'''
+    Library(name)
+
+    IS_RUST_LIBRARY = True
+
+
+RustLibrary('random-crate')
new file mode 100644
--- /dev/null
+++ b/python/mozbuild/mozbuild/test/frontend/data/crate-dependency-recursive/ok/Cargo.toml
@@ -0,0 +1,9 @@
+[package]
+name = "ok"
+version = "0.1.0"
+authors = [
+  "Nobody <nobody@mozilla.org>",
+]
+
+[dependencies]
+bogus = { version = "0.1.0" }
new file mode 100644
--- /dev/null
+++ b/python/mozbuild/mozbuild/test/frontend/data/crate-dependency-version-only/Cargo.toml
@@ -0,0 +1,12 @@
+[package]
+name = "random-crate"
+version = "0.1.0"
+authors = [
+  "Nobody <nobody@mozilla.org>",
+]
+
+[lib]
+crate-type = ["staticlib"]
+
+[dependencies]
+bogus = "0.1.0"
new file mode 100644
--- /dev/null
+++ b/python/mozbuild/mozbuild/test/frontend/data/crate-dependency-version-only/moz.build
@@ -0,0 +1,18 @@
+# Any copyright is dedicated to the Public Domain.
+# http://creativecommons.org/publicdomain/zero/1.0/
+
+@template
+def Library(name):
+    '''Template for libraries.'''
+    LIBRARY_NAME = name
+
+
+@template
+def RustLibrary(name):
+    '''Template for Rust libraries.'''
+    Library(name)
+
+    IS_RUST_LIBRARY = True
+
+
+RustLibrary('random-crate')
new file mode 100644
--- /dev/null
+++ b/python/mozbuild/mozbuild/test/frontend/data/crate-dependency-with-dict/Cargo.toml
@@ -0,0 +1,12 @@
+[package]
+name = "random-crate"
+version = "0.1.0"
+authors = [
+  "Nobody <nobody@mozilla.org>",
+]
+
+[lib]
+crate-type = ["staticlib"]
+
+[dependencies]
+bogus = { version = "0.1.0" }
new file mode 100644
--- /dev/null
+++ b/python/mozbuild/mozbuild/test/frontend/data/crate-dependency-with-dict/moz.build
@@ -0,0 +1,18 @@
+# Any copyright is dedicated to the Public Domain.
+# http://creativecommons.org/publicdomain/zero/1.0/
+
+@template
+def Library(name):
+    '''Template for libraries.'''
+    LIBRARY_NAME = name
+
+
+@template
+def RustLibrary(name):
+    '''Template for Rust libraries.'''
+    Library(name)
+
+    IS_RUST_LIBRARY = True
+
+
+RustLibrary('random-crate')
new file mode 100644
--- /dev/null
+++ b/python/mozbuild/mozbuild/test/frontend/data/duplicated-crate-names/Cargo.toml
@@ -0,0 +1,13 @@
+[package]
+name = "random-crate"
+version = "0.1.0"
+authors = [
+  "Nobody <nobody@mozilla.org>",
+]
+
+[lib]
+crate-type = ["staticlib"]
+
+[dependencies]
+crate-one = { path = "crate-one" }
+crate-two = { path = "crate-two" }
new file mode 100644
--- /dev/null
+++ b/python/mozbuild/mozbuild/test/frontend/data/duplicated-crate-names/crate-one/Cargo.toml
@@ -0,0 +1,9 @@
+[package]
+name = "crate-one"
+version = "0.1.0"
+authors = [
+  "Nobody <nobody@mozilla.org>",
+]
+
+[dependencies]
+duplicate = { path = "duplicate" }
new file mode 100644
--- /dev/null
+++ b/python/mozbuild/mozbuild/test/frontend/data/duplicated-crate-names/crate-one/duplicate/Cargo.toml
@@ -0,0 +1,6 @@
+[package]
+name = "duplicate"
+version = "0.1.0"
+authors = [
+  "Nobody <nobody@mozilla.org>",
+]
new file mode 100644
--- /dev/null
+++ b/python/mozbuild/mozbuild/test/frontend/data/duplicated-crate-names/crate-two/Cargo.toml
@@ -0,0 +1,9 @@
+[package]
+name = "crate-two"
+version = "0.1.0"
+authors = [
+  "Nobody <nobody@mozilla.org>",
+]
+
+[dependencies]
+duplicate = { path = "duplicate" }
new file mode 100644
--- /dev/null
+++ b/python/mozbuild/mozbuild/test/frontend/data/duplicated-crate-names/crate-two/duplicate/Cargo.toml
@@ -0,0 +1,6 @@
+[package]
+name = "duplicate"
+version = "0.1.0"
+authors = [
+  "Nobody <nobody@mozilla.org>",
+]
new file mode 100644
--- /dev/null
+++ b/python/mozbuild/mozbuild/test/frontend/data/duplicated-crate-names/moz.build
@@ -0,0 +1,18 @@
+# Any copyright is dedicated to the Public Domain.
+# http://creativecommons.org/publicdomain/zero/1.0/
+
+@template
+def Library(name):
+    '''Template for libraries.'''
+    LIBRARY_NAME = name
+
+
+@template
+def RustLibrary(name):
+    '''Template for Rust libraries.'''
+    Library(name)
+
+    IS_RUST_LIBRARY = True
+
+
+RustLibrary('random-crate')
new file mode 100644
--- /dev/null
+++ b/python/mozbuild/mozbuild/test/frontend/data/rust-library-dash-folding/Cargo.toml
@@ -0,0 +1,9 @@
+[package]
+name = "random-crate"
+version = "0.1.0"
+authors = [
+  "Nobody <nobody@mozilla.org>",
+]
+
+[lib]
+crate-type = ["staticlib"]
new file mode 100644
--- /dev/null
+++ b/python/mozbuild/mozbuild/test/frontend/data/rust-library-dash-folding/moz.build
@@ -0,0 +1,18 @@
+# Any copyright is dedicated to the Public Domain.
+# http://creativecommons.org/publicdomain/zero/1.0/
+
+@template
+def Library(name):
+    '''Template for libraries.'''
+    LIBRARY_NAME = name
+
+
+@template
+def RustLibrary(name):
+    '''Template for Rust libraries.'''
+    Library(name)
+
+    IS_RUST_LIBRARY = True
+
+
+RustLibrary('random-crate')
new file mode 100644
--- /dev/null
+++ b/python/mozbuild/mozbuild/test/frontend/data/rust-library-invalid-crate-type/Cargo.toml
@@ -0,0 +1,9 @@
+[package]
+name = "random-crate"
+version = "0.1.0"
+authors = [
+  "Nobody <nobody@mozilla.org>",
+]
+
+[lib]
+crate-type = ["dylib"]
new file mode 100644
--- /dev/null
+++ b/python/mozbuild/mozbuild/test/frontend/data/rust-library-invalid-crate-type/moz.build
@@ -0,0 +1,18 @@
+# Any copyright is dedicated to the Public Domain.
+# http://creativecommons.org/publicdomain/zero/1.0/
+
+@template
+def Library(name):
+    '''Template for libraries.'''
+    LIBRARY_NAME = name
+
+
+@template
+def RustLibrary(name):
+    '''Template for Rust libraries.'''
+    Library(name)
+
+    IS_RUST_LIBRARY = True
+
+
+RustLibrary('random-crate')
new file mode 100644
--- /dev/null
+++ b/python/mozbuild/mozbuild/test/frontend/data/rust-library-name-mismatch/Cargo.toml
@@ -0,0 +1,6 @@
+[package]
+name = "deterministic-crate"
+version = "0.1.0"
+authors = [
+  "Nobody <nobody@mozilla.org>",
+]
new file mode 100644
--- /dev/null
+++ b/python/mozbuild/mozbuild/test/frontend/data/rust-library-name-mismatch/moz.build
@@ -0,0 +1,18 @@
+# Any copyright is dedicated to the Public Domain.
+# http://creativecommons.org/publicdomain/zero/1.0/
+
+@template
+def Library(name):
+    '''Template for libraries.'''
+    LIBRARY_NAME = name
+
+
+@template
+def RustLibrary(name):
+    '''Template for Rust libraries.'''
+    Library(name)
+
+    IS_RUST_LIBRARY = True
+
+
+RustLibrary('random-crate')
new file mode 100644
--- /dev/null
+++ b/python/mozbuild/mozbuild/test/frontend/data/rust-library-no-cargo-toml/moz.build
@@ -0,0 +1,18 @@
+# Any copyright is dedicated to the Public Domain.
+# http://creativecommons.org/publicdomain/zero/1.0/
+
+@template
+def Library(name):
+    '''Template for libraries.'''
+    LIBRARY_NAME = name
+
+
+@template
+def RustLibrary(name):
+    '''Template for Rust libraries.'''
+    Library(name)
+
+    IS_RUST_LIBRARY = True
+
+
+RustLibrary('random-crate')
new file mode 100644
--- /dev/null
+++ b/python/mozbuild/mozbuild/test/frontend/data/rust-library-no-lib-section/Cargo.toml
@@ -0,0 +1,6 @@
+[package]
+name = "random-crate"
+version = "0.1.0"
+authors = [
+  "Nobody <nobody@mozilla.org>",
+]
new file mode 100644
--- /dev/null
+++ b/python/mozbuild/mozbuild/test/frontend/data/rust-library-no-lib-section/moz.build
@@ -0,0 +1,18 @@
+# Any copyright is dedicated to the Public Domain.
+# http://creativecommons.org/publicdomain/zero/1.0/
+
+@template
+def Library(name):
+    '''Template for libraries.'''
+    LIBRARY_NAME = name
+
+
+@template
+def RustLibrary(name):
+    '''Template for Rust libraries.'''
+    Library(name)
+
+    IS_RUST_LIBRARY = True
+
+
+RustLibrary('random-crate')
--- a/python/mozbuild/mozbuild/test/frontend/test_emitter.py
+++ b/python/mozbuild/mozbuild/test/frontend/test_emitter.py
@@ -25,16 +25,17 @@ from mozbuild.frontend.data import (
     GeneratedFile,
     GeneratedSources,
     HostDefines,
     HostSources,
     IPDLFile,
     JARManifest,
     LocalInclude,
     Program,
+    RustLibrary,
     SdkFiles,
     SharedLibrary,
     SimpleProgram,
     Sources,
     StaticLibrary,
     TestHarnessFiles,
     TestManifest,
     UnifiedSources,
@@ -987,16 +988,141 @@ class TestEmitterBasic(unittest.TestCase
 
     def test_final_target_pp_files_non_srcdir(self):
         '''Test that non-srcdir paths in FINAL_TARGET_PP_FILES throws errors.'''
         reader = self.reader('final-target-pp-files-non-srcdir')
         with self.assertRaisesRegexp(SandboxValidationError,
              'Only source directory paths allowed in FINAL_TARGET_PP_FILES:'):
             self.read_topsrcdir(reader)
 
+    def test_crate_dependency_version_only(self):
+        '''Test that including a crate with a version-only dependency fails.'''
+        reader = self.reader('crate-dependency-version-only')
+        with self.assertRaisesRegexp(SandboxValidationError,
+             'Dependency.*of crate.*does not list a path'):
+            self.read_topsrcdir(reader)
+
+    def test_crate_dependency_with_dict(self):
+        '''Test that including a crate with a dict-using version-only dependency fails.'''
+        reader = self.reader('crate-dependency-with-dict')
+        with self.assertRaisesRegexp(SandboxValidationError,
+             'Dependency.*of crate.*does not list a path'):
+            self.read_topsrcdir(reader)
+
+    def test_crate_dependency_absolute_path(self):
+        '''Test that including a crate with an absolute-path dependency fails.'''
+        reader = self.reader('crate-dependency-absolute-path')
+        with self.assertRaisesRegexp(SandboxValidationError,
+             'Dependency.*of crate.*has a non-relative path'):
+            self.read_topsrcdir(reader)
+
+    def test_crate_dependency_nonexistent_path(self):
+        '''Test that including a crate with a non-existent dependency fails.'''
+        reader = self.reader('crate-dependency-nonexistent-path')
+        with self.assertRaisesRegexp(SandboxValidationError,
+             'Dependency.*of crate.*refers to a non-existent path'):
+            self.read_topsrcdir(reader)
+
+    def test_crate_build_dependency_version_only(self):
+        '''Test that including a crate with a version-only dependency fails.'''
+        reader = self.reader('crate-build-dependency-version-only')
+        with self.assertRaisesRegexp(SandboxValidationError,
+             'Build dependency.*of crate.*does not list a path'):
+            self.read_topsrcdir(reader)
+
+    def test_crate_build_dependency_with_dict(self):
+        '''Test that including a crate with a dict-using version-only dependency fails.'''
+        reader = self.reader('crate-build-dependency-with-dict')
+        with self.assertRaisesRegexp(SandboxValidationError,
+             'Build dependency.*of crate.*does not list a path'):
+            self.read_topsrcdir(reader)
+
+    def test_crate_build_dependency_absolute_path(self):
+        '''Test that including a crate with an absolute-path dependency fails.'''
+        reader = self.reader('crate-build-dependency-absolute-path')
+        with self.assertRaisesRegexp(SandboxValidationError,
+             'Build dependency.*of crate.*has a non-relative path'):
+            self.read_topsrcdir(reader)
+
+    def test_crate_build_dependency_nonexistent_path(self):
+        '''Test that including a crate with a non-existent dependency fails.'''
+        reader = self.reader('crate-build-dependency-nonexistent-path')
+        with self.assertRaisesRegexp(SandboxValidationError,
+             'Build dependency.*of crate.*refers to a non-existent path'):
+            self.read_topsrcdir(reader)
+
+    def test_crate_dependency_recursive(self):
+        reader = self.reader('crate-dependency-recursive')
+        with self.assertRaisesRegexp(SandboxValidationError,
+             'Dependency.*of crate.*does not list a path'):
+            self.read_topsrcdir(reader)
+
+    def test_crate_build_dependency_recursive(self):
+        reader = self.reader('crate-build-dependency-recursive')
+        with self.assertRaisesRegexp(SandboxValidationError,
+             'Build dependency.*of crate.*does not list a path'):
+            self.read_topsrcdir(reader)
+
+    def test_rust_library_no_cargo_toml(self):
+        '''Test that defining a RustLibrary without a Cargo.toml fails.'''
+        reader = self.reader('rust-library-no-cargo-toml')
+        with self.assertRaisesRegexp(SandboxValidationError,
+             'No Cargo.toml file found'):
+            self.read_topsrcdir(reader)
+
+    def test_rust_library_name_mismatch(self):
+        '''Test that defining a RustLibrary that doesn't match Cargo.toml fails.'''
+        reader = self.reader('rust-library-name-mismatch')
+        with self.assertRaisesRegexp(SandboxValidationError,
+             'library.*does not match Cargo.toml-defined package'):
+            self.read_topsrcdir(reader)
+
+    def test_rust_library_no_lib_section(self):
+        '''Test that a RustLibrary Cargo.toml with no [lib] section fails.'''
+        reader = self.reader('rust-library-no-lib-section')
+        with self.assertRaisesRegexp(SandboxValidationError,
+             'Cargo.toml for.* has no \\[lib\\] section'):
+            self.read_topsrcdir(reader)
+
+    def test_rust_library_invalid_crate_type(self):
+        '''Test that a RustLibrary Cargo.toml has a permitted crate-type.'''
+        reader = self.reader('rust-library-invalid-crate-type')
+        with self.assertRaisesRegexp(SandboxValidationError,
+             'crate-type.* is not permitted'):
+            self.read_topsrcdir(reader)
+
+    def test_rust_library_dash_folding(self):
+        '''Test that on-disk names of RustLibrary objects convert dashes to underscores.'''
+        reader = self.reader('rust-library-dash-folding',
+                             extra_substs=dict(RUST_TARGET='i686-pc-windows-msvc'))
+        objs = self.read_topsrcdir(reader)
+
+        self.assertEqual(len(objs), 1)
+        lib = objs[0]
+        self.assertIsInstance(lib, RustLibrary)
+        self.assertRegexpMatches(lib.lib_name, "random_crate")
+        self.assertRegexpMatches(lib.import_name, "random_crate")
+        self.assertRegexpMatches(lib.basename, "random-crate")
+
+    def test_crate_dependency_path_resolution(self):
+        '''Test recursive dependencies resolve with the correct paths.'''
+        reader = self.reader('crate-dependency-path-resolution',
+                             extra_substs=dict(RUST_TARGET='i686-pc-windows-msvc'))
+        objs = self.read_topsrcdir(reader)
+
+        self.assertEqual(len(objs), 1)
+        self.assertIsInstance(objs[0], RustLibrary)
+
+    def test_duplicate_crate_names(self):
+        '''Test that crates are not duplicated in the tree.'''
+        reader = self.reader('duplicated-crate-names')
+        with self.assertRaisesRegexp(SandboxValidationError,
+             'Crate.*found at multiple paths'):
+            self.read_topsrcdir(reader)
+
     def test_android_res_dirs(self):
         """Test that ANDROID_RES_DIRS works properly."""
         reader = self.reader('android-res-dirs')
         objs = self.read_topsrcdir(reader)
 
         self.assertEqual(len(objs), 1)
         self.assertIsInstance(objs[0], AndroidResDirs)
 
--- a/toolkit/library/gtest/moz.build
+++ b/toolkit/library/gtest/moz.build
@@ -6,8 +6,13 @@
 
 Libxul('xul-gtest')
 
 FINAL_TARGET = 'dist/bin/gtest'
 
 USE_LIBS += [
     'static:xul',
 ]
+
+if CONFIG['MOZ_RUST']:
+    USE_LIBS += [
+        'gkrust-gtest',
+    ]
new file mode 100644
--- /dev/null
+++ b/toolkit/library/gtest/rust/Cargo.lock
@@ -0,0 +1,11 @@
+[root]
+name = "gkrust-gtest"
+version = "0.1.0"
+dependencies = [
+ "mp4parse-gtest 0.1.0",
+]
+
+[[package]]
+name = "mp4parse-gtest"
+version = "0.1.0"
+
new file mode 100644
--- /dev/null
+++ b/toolkit/library/gtest/rust/Cargo.toml
@@ -0,0 +1,36 @@
+[package]
+name = "gkrust-gtest"
+version = "0.1.0"
+authors = ["nobody@mozilla.org"]
+license = "MPL-2.0"
+description = "Testing code for libgkrust"
+
+[dependencies]
+mp4parse-gtest = { path = "../../../../dom/media/gtest" }
+
+[lib]
+path = "lib.rs"
+crate-type = ["staticlib"]
+test = false
+doctest = false
+bench = false
+doc = false
+plugin = false
+harness = false
+
+# Explicitly specify what our profiles use.
+[profile.dev]
+opt-level = 1
+debug = true
+rpath = false
+lto = false
+debug-assertions = true
+codegen-units = 1
+
+[profile.release]
+opt-level = 2
+debug = true
+rpath = false
+lto = false
+debug-assertions = false
+codegen-units = 1
new file mode 100644
--- /dev/null
+++ b/toolkit/library/gtest/rust/lib.rs
@@ -0,0 +1,5 @@
+// This Source Code Form is subject to the terms of the Mozilla Public
+// License, v. 2.0. If a copy of the MPL was not distributed with this
+// file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+extern crate mp4parse_gtest;
new file mode 100644
--- /dev/null
+++ b/toolkit/library/gtest/rust/moz.build
@@ -0,0 +1,7 @@
+# -*- Mode: python; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 40 -*-
+# vim: set filetype=python:
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+RustLibrary('gkrust-gtest')
--- a/toolkit/library/moz.build
+++ b/toolkit/library/moz.build
@@ -379,10 +379,16 @@ if CONFIG['COMPILE_ENVIRONMENT']:
         )
     GENERATED_FILES += ['dependentlibs.list']
     GENERATED_FILES['dependentlibs.list'].script = 'dependentlibs.py:gen_list'
     GENERATED_FILES['dependentlibs.list'].inputs = [
         '!%s' % full_libname,
     ]
     FINAL_TARGET_FILES += ['!dependentlibs.list']
 
-# This needs to be last
+# This library needs to be last to make XPCOM module registration work.
 USE_LIBS += ['StaticXULComponentsEnd']
+
+# The above library needs to be last for C++ purposes.  This library,
+# however, is entirely composed of Rust code, and needs to come after
+# all the C++ code so any possible C++ -> Rust calls can be resolved.
+if CONFIG['MOZ_RUST']:
+    USE_LIBS += ['gkrust']
new file mode 100644
--- /dev/null
+++ b/toolkit/library/rust/Cargo.lock
@@ -0,0 +1,11 @@
+[root]
+name = "gkrust"
+version = "0.1.0"
+dependencies = [
+ "mp4parse 0.4.0",
+]
+
+[[package]]
+name = "mp4parse"
+version = "0.4.0"
+
new file mode 100644
--- /dev/null
+++ b/toolkit/library/rust/Cargo.toml
@@ -0,0 +1,36 @@
+[package]
+name = "gkrust"
+version = "0.1.0"
+authors = ["nobody@mozilla.org"]
+license = "MPL-2.0"
+description = "Rust code for libxul"
+
+[dependencies]
+mp4parse = { path = "../../../media/libstagefright/binding/mp4parse" }
+
+[lib]
+path = "lib.rs"
+crate-type = ["staticlib"]
+test = false
+doctest = false
+bench = false
+doc = false
+plugin = false
+harness = false
+
+# Explicitly specify what our profiles use.
+[profile.dev]
+opt-level = 1
+debug = true
+rpath = false
+lto = false
+debug-assertions = true
+codegen-units = 1
+
+[profile.release]
+opt-level = 2
+debug = true
+rpath = false
+lto = false
+debug-assertions = false
+codegen-units = 1
new file mode 100644
--- /dev/null
+++ b/toolkit/library/rust/lib.rs
@@ -0,0 +1,5 @@
+// This Source Code Form is subject to the terms of the Mozilla Public
+// License, v. 2.0. If a copy of the MPL was not distributed with this
+// file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+extern crate mp4parse;
new file mode 100644
--- /dev/null
+++ b/toolkit/library/rust/moz.build
@@ -0,0 +1,7 @@
+# -*- Mode: python; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 40 -*-
+# vim: set filetype=python:
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+RustLibrary('gkrust')
--- a/toolkit/toolkit.mozbuild
+++ b/toolkit/toolkit.mozbuild
@@ -1,8 +1,9 @@
+# -*- Mode: python; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 40 -*-
 # vim: set filetype=python:
 # This Source Code Form is subject to the terms of the Mozilla Public
 # License, v. 2.0. If a copy of the MPL was not distributed with this
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 if CONFIG['MOZ_SANDBOX']:
     DIRS += ['/security/sandbox']
 
@@ -137,16 +138,22 @@ DIRS += [
     '/startupcache',
     '/js/ductwork/debugger',
     '/other-licenses/snappy',
 ]
 
 if CONFIG['MOZ_GIO_COMPONENT']:
     DIRS += ['/extensions/gio']
 
+if CONFIG['MOZ_RUST']:
+    DIRS += [
+        '/toolkit/library/gtest/rust',
+        '/toolkit/library/rust',
+    ]
+
 DIRS += [
     '/toolkit/library/StaticXULComponentsEnd',
     '/toolkit/library',
 ]
 
 if 'gtk' in CONFIG['MOZ_WIDGET_TOOLKIT']:
     DIRS += ['/toolkit/system/gnome']