Skip to content

Commit

Permalink
Bug 1373878 - part 2 - build system support for Rust tests; r=rillian
Browse files Browse the repository at this point in the history
  • Loading branch information
froydnj committed Oct 2, 2017
1 parent 2d3d2c4 commit f0fcd23
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 1 deletion.
7 changes: 7 additions & 0 deletions build/templates.mozbuild
Original file line number Diff line number Diff line change
Expand Up @@ -157,5 +157,12 @@ def NoVisibilityFlags():
COMPILE_FLAGS['VISIBILITY'] = []


@template
def RustTest(name, features=None):
RUST_TEST = name

if features:
RUST_TEST_FEATURES = features

include('gecko_templates.mozbuild')
include('test_templates.mozbuild')
12 changes: 12 additions & 0 deletions config/rules.mk
Original file line number Diff line number Diff line change
Expand Up @@ -1008,6 +1008,18 @@ force-cargo-library-check:
@true
endif # RUST_LIBRARY_FILE

ifdef RUST_TEST

ifdef RUST_TEST_FEATURES
rust_features_flag := --features "$(RUST_TEST_FEATURES)"
endif

force-cargo-test-run:
$(call RUN_CARGO,test $(cargo_target_flag) -p $(RUST_TEST) $(rust_features_flag),$(target_cargo_env_vars))

check:: force-cargo-test-run
endif

ifdef HOST_RUST_LIBRARY_FILE

ifdef HOST_RUST_LIBRARY_FEATURES
Expand Down
12 changes: 11 additions & 1 deletion python/mozbuild/mozbuild/backend/recursivemake.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
RustLibrary,
HostRustLibrary,
RustProgram,
RustTest,
SharedLibrary,
SimpleProgram,
Sources,
Expand Down Expand Up @@ -571,6 +572,9 @@ def consume_object(self, obj):
build_target = self._build_target_for_obj(obj)
self._compile_graph[build_target]

elif isinstance(obj, RustTest):
self._process_rust_test(obj, backend_file)

elif isinstance(obj, Program):
self._process_program(obj, backend_file)
self._process_linked_libraries(obj, backend_file)
Expand Down Expand Up @@ -1123,6 +1127,12 @@ def _process_host_rust_program(self, obj, backend_file):
'HOST_RUST_PROGRAMS',
'HOST_RUST_CARGO_PROGRAMS')

def _process_rust_test(self, obj, backend_file):
self._no_skip['check'].add(backend_file.relobjdir)
backend_file.write_once('CARGO_FILE := $(srcdir)/Cargo.toml\n')
backend_file.write_once('RUST_TEST := %s\n' % obj.name)
backend_file.write_once('RUST_TEST_FEATURES := %s\n' % ' '.join(obj.features))

def _process_simple_program(self, obj, backend_file):
if obj.is_unit_test:
backend_file.write('CPP_UNIT_TESTS += %s\n' % obj.program)
Expand Down Expand Up @@ -1261,7 +1271,7 @@ def _process_static_library(self, libdef, backend_file):

def _process_rust_library(self, libdef, backend_file):
backend_file.write_once('%s := %s\n' % (libdef.LIB_FILE_VAR, libdef.import_name))
backend_file.write('CARGO_FILE := $(srcdir)/Cargo.toml\n')
backend_file.write_once('CARGO_FILE := $(srcdir)/Cargo.toml\n')
# Need to normalize the path so Cargo sees the same paths from all
# possible invocations of Cargo with this CARGO_TARGET_DIR. Otherwise,
# Cargo's dependency calculations don't work as we expect and we wind
Expand Down
14 changes: 14 additions & 0 deletions python/mozbuild/mozbuild/frontend/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -1097,6 +1097,20 @@ def aggregate(files):
HostRustLibrary template instead.
"""),

'RUST_TEST': (unicode, unicode,
"""Name of a Rust test to build and run via `cargo test`.
This variable should not be used directly; you should be using the
RustTest template instead.
"""),

'RUST_TEST_FEATURES': (List, list,
"""Cargo features to activate for RUST_TEST.
This variable should not be used directly; you should be using the
RustTest template instead.
"""),

'UNIFIED_SOURCES': (ContextDerivedTypedList(SourcePath, StrictOrderingOnAppendList), list,
"""Source code files that can be compiled together.
Expand Down
12 changes: 12 additions & 0 deletions python/mozbuild/mozbuild/frontend/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,18 @@ class HostRustProgram(BaseRustProgram):
TARGET_SUBST_VAR = 'RUST_HOST_TARGET'


class RustTest(ContextDerived):
__slots__ = (
'name',
'features',
)

def __init__(self, context, name, features):
ContextDerived.__init__(self, context)
self.name = name
self.features = features


class BaseLibrary(Linkable):
"""Generic context derived container object for libraries."""
__slots__ = (
Expand Down
10 changes: 10 additions & 0 deletions python/mozbuild/mozbuild/frontend/emitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
RustLibrary,
HostRustLibrary,
RustProgram,
RustTest,
SharedLibrary,
SimpleProgram,
Sources,
Expand Down Expand Up @@ -1124,6 +1125,15 @@ def emit_from_context(self, context):
Manifest('components',
mozpath.basename(c)))

if self.config.substs.get('MOZ_RUST_TESTS', None):
rust_test = context.get('RUST_TEST', None)
if rust_test:
# TODO: more sophisticated checking of the declared name vs.
# contents of the Cargo.toml file.
features = context.get('RUST_TEST_FEATURES', [])

yield RustTest(context, rust_test, features)

for obj in self._process_test_manifests(context):
yield obj

Expand Down

0 comments on commit f0fcd23

Please sign in to comment.