Bug 1161339 - Add rust support to moz.build. r=ted
authorRalph Giles <giles@mozilla.com>
Fri, 08 May 2015 15:18:00 -0700
changeset 274454 ea593c621ffe1ce2c4cc16bf91a33bc8ab9027fa
parent 274453 b811c7d4f39bd5b9d88e72a6a428a20cbdb52712
child 274455 083d678bb7fd7a4a057eb74ee86d1824188b7700
push id863
push userraliiev@mozilla.com
push dateMon, 03 Aug 2015 13:22:43 +0000
treeherdermozilla-release@f6321b14228d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
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 1161339 - Add rust support to moz.build. r=ted Add .rs as a recognized file extension in SOURCES. Propagate that through to the Makefile backend and add a dependency generated and an explicit rule to call $(RUSTC) to compile them. rustc builds static libraries, not obj files. At least, if one asks it to output an obj file, I'm not clear how to get all the compiler-specific runtime libraries the code will expect to link to. Therefore we generate a static library for each rust source file (which must be a complete crate for the time being) and link that. Because of the extension it ends up on the LIBS line in the the corresponding .desc file. Note that the static library does still depend on some system libraries, e.g. -ldl -lpthread -lm on linux. Gecko already links to all of those, so we don't keep track of it here. Should we need to add explicit linkage for other targets, rustc does print a list to stderr which can be parsed.
--- a/config/rules.mk
+++ b/config/rules.mk
@@ -239,18 +239,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)))
+RSOBJS = $(addprefix lib,$(notdir $(RSSRCS:.rs=.$(LIB_SUFFIX))))
 ifndef OBJS
 OBJS = $(strip $(_OBJS))
 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))))
@@ -871,16 +872,21 @@ endif # Sun Studio on Solaris
 # This dependency must be first for the $< flag to work correctly, and the
 # rules that have commands for these targets must not list any other
 # prerequisites, or they will override the $< variable.
 define src_objdep
 $(basename $2$(notdir $1)).$(OBJ_SUFFIX): $1 $$(call mkdir_deps,$$(MDDEPDIR))
 $(foreach f,$(CSRCS) $(SSRCS) $(CPPSRCS) $(CMSRCS) $(CMMSRCS) $(ASFILES),$(eval $(call src_objdep,$(f))))
 $(foreach f,$(HOST_CSRCS) $(HOST_CPPSRCS) $(HOST_CMSRCS) $(HOST_CMMSRCS),$(eval $(call src_objdep,$(f),host_)))
+# The rust compiler only outputs library objects, and so we need different
+# mangling to generate dependency rules for it.
+mk_libname = $(basename lib$(notdir $1)).$(LIB_SUFFIX)
+src_libdep = $(call mk_libname,$1): $1 $$(call mkdir_deps,$$(MDDEPDIR))
+$(foreach f,$(RSSRCS),$(eval $(call src_libdep,$(f))))
 # Rules for building native targets must come first because of the host_ prefix
@@ -919,16 +925,24 @@ endef
 ifdef ASFILES
 # The AS_DASH_C_FLAG is needed cause not all assemblers (Solaris) accept
 # a '-c' flag.
 	$(AS) $(ASOUTOPTION)$@ $(ASFLAGS) $($(notdir $<)_FLAGS) $(AS_DASH_C_FLAG) $(_VPATH_SRCS)
+ifdef MOZ_RUST
+# Assume any system libraries rustc links against are already
+# in the target's LIBS.
+	$(RUSTC) --crate-type staticlib -o $(call mk_libname,$<) $(_VPATH_SRCS)
 	$(AS) -o $@ $(ASFLAGS) $($(notdir $<)_FLAGS) $(LOCAL_INCLUDES) $(TARGET_LOCAL_INCLUDES) -c $<
--- a/python/mozbuild/mozbuild/backend/recursivemake.py
+++ b/python/mozbuild/mozbuild/backend/recursivemake.py
@@ -442,16 +442,17 @@ class RecursiveMakeBackend(CommonBackend
         elif isinstance(obj, (Sources, GeneratedSources)):
             suffix_map = {
                 '.s': 'ASFILES',
                 '.c': 'CSRCS',
                 '.m': 'CMSRCS',
                 '.mm': 'CMMSRCS',
                 '.cpp': 'CPPSRCS',
+                '.rs': 'RSSRCS',
                 '.S': 'SSRCS',
             var = suffix_map[obj.canonical_suffix]
             for f in sorted(obj.files):
                 backend_file.write('%s += %s\n' % (var, f))
         elif isinstance(obj, HostSources):
             suffix_map = {
                 '.c': 'HOST_CSRCS',
--- a/python/mozbuild/mozbuild/frontend/emitter.py
+++ b/python/mozbuild/mozbuild/frontend/emitter.py
@@ -722,16 +722,17 @@ 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():
             for a in alternatives: