Skip to content

Commit

Permalink
Bug 1808184 - Avoid rebuilding RUST_LIBRARY_FILE based on dependencie…
Browse files Browse the repository at this point in the history
…s r=sylvestre

This generalizes Bug 1806618 to library files, avoiding a few extra calls
to rustc.

Differential Revision: https://phabricator.services.mozilla.com/D165808
  • Loading branch information
serge-sans-paille committed Mar 15, 2023
1 parent c2959ec commit c54c32e
Showing 1 changed file with 30 additions and 22 deletions.
52 changes: 30 additions & 22 deletions config/makefiles/rust.mk
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@ ifeq (WASI,$(OS_ARCH))
force-cargo-library-build: CARGO_RUSTCFLAGS += -C target-feature=-crt-static
endif


# 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
Expand All @@ -437,7 +438,34 @@ force-cargo-library-build:
$(REPORT_BUILD)
$(call CARGO_BUILD) --lib $(cargo_target_flag) $(rust_features_flag) -- $(cargo_rustc_flags)

$(RUST_LIBRARY_FILE): force-cargo-library-build
# RUST_OBJ_DEPENDENCIES(RUST_OBJ,REBUILD_RULE)
# Generates a rule suitable to rebuild RUST_OBJ with REBUILD_RULE only if its
# dependencies are obsolete.
#
# It relies on the fact that upon build, cargo generates a dependency file named
# `$(RUST_OBJ).d'. Unfortunately the lhs of the rule has an absolute path,
# so we extract it under the name $(RUST_OBJ)_deps below.
#
# If the dependencies are empty, the file was not created so we force a rebuild.
# Otherwise we add it to the dependency list.
#
# $(patsubst %.a,%,$(1)).d is used to extract the dependency name, the pattern
# used by rust is:
# - program_name -> program_name.d
# - library_name.a -> library_name.d
#
# The actual rule is a bit tricky. The `+' prefix allow for recursive parallel
# make, and it's skipped (`:') if we already triggered a rebuild as part of the
# dependency chain.
#
define RUST_OBJ_DEPENDENCIES
$(1)_deps := $(wordlist 2, 10000000, $(file < $(patsubst %.a,%,$(1)).d))
$(1): $(CARGO_FILE) $(call resfile,module) $(if $$($(1)_deps),$$($(1)_deps),$(2))
$(if $$($(1)_deps),+$(MAKE) $(2),:)
endef

$(eval $(call RUST_OBJ_DEPENDENCIES,$(RUST_LIBRARY_FILE),force-cargo-library-build))

# When we are building in --enable-release mode; we add an additional check to confirm
# that we are not importing any networking-related functions in rust code. This reduces
# the chance of proxy bypasses originating from rust code.
Expand Down Expand Up @@ -518,27 +546,7 @@ force-cargo-program-build: $(call resfile,module)
$(REPORT_BUILD)
$(call CARGO_BUILD) $(addprefix --bin ,$(RUST_CARGO_PROGRAMS)) $(cargo_target_flag) -- $(addprefix -C link-arg=$(CURDIR)/,$(call resfile,module)) $(CARGO_RUSTCFLAGS)

# RUST_PROGRAM_DEPENDENCIES(RUST_PROGRAM)
# Generates a rule suitable to rebuild RUST_PROGRAM only if its dependencies are
# obsolete.
# It relies on the fact that upon build, cargo generates a dependency file named
# `$(RUST_PROGRAM).d'. Unfortunately the lhs of the rule has an absolute path,
# so we extract it under the name $(RUST_PROGRAM)_deps below.
#
# If the dependencies are empty, the file was not created so we force a rebuild.
# Otherwise we add it to the dependency list.
#
# The actual rule is a bit tricky. The `+' prefix allow for recursive parallel
# make, and it's skipped (`:') if we already triggered a rebuild as part of the
# dependency chain.
#
define RUST_PROGRAM_DEPENDENCIES
$(1)_deps := $(wordlist 2, 10000000, $(file < $(1).d))
$(1): $(CARGO_FILE) $(call resfile,module) $(if $$($(1)_deps),$$($(1)_deps),force-cargo-program-build)
$(if $$($(1)_deps),+$(MAKE) force-cargo-program-build,:)
endef

$(foreach RUST_PROGRAM,$(RUST_PROGRAMS), $(eval $(call RUST_PROGRAM_DEPENDENCIES,$(RUST_PROGRAM))))
$(foreach RUST_PROGRAM,$(RUST_PROGRAMS), $(eval $(call RUST_PROGRAM_DEPENDENCIES,$(RUST_PROGRAM),force-cargo-program-build)))

ifndef CARGO_NO_AUTO_ARG
force-cargo-program-%:
Expand Down

0 comments on commit c54c32e

Please sign in to comment.