|
| 1 | +# We borrow heavily from the kernel build setup, though we are simpler since |
| 2 | +# we don't have Kconfig tweaking settings on us. |
| 3 | + |
| 4 | +# The implicit make rules have it looking for RCS files, among other things. |
| 5 | +# We instead explicitly write all the rules we care about. |
| 6 | +# It's even quicker (saves ~200ms) to pass -r on the command line. |
| 7 | +MAKEFLAGS=-r |
| 8 | + |
| 9 | +# The source directory tree. |
| 10 | +srcdir := %(srcdir)s |
| 11 | +abs_srcdir := $(abspath $(srcdir)) |
| 12 | + |
| 13 | +# The name of the builddir. |
| 14 | +builddir_name ?= %(builddir)s |
| 15 | + |
| 16 | +# The V=1 flag on command line makes us verbosely print command lines. |
| 17 | +ifdef V |
| 18 | + quiet= |
| 19 | +else |
| 20 | + quiet=quiet_ |
| 21 | +endif |
| 22 | + |
| 23 | +# Specify BUILDTYPE=Release on the command line for a release build. |
| 24 | +BUILDTYPE ?= %(default_configuration)s |
| 25 | + |
| 26 | +# Directory all our build output goes into. |
| 27 | +# Note that this must be two directories beneath src/ for unit tests to pass, |
| 28 | +# as they reach into the src/ directory for data with relative paths. |
| 29 | +builddir ?= $(builddir_name)/$(BUILDTYPE) |
| 30 | +abs_builddir := $(abspath $(builddir)) |
| 31 | +depsdir := $(builddir)/.deps |
| 32 | + |
| 33 | +# Object output directory. |
| 34 | +obj := $(builddir)/obj |
| 35 | +abs_obj := $(abspath $(obj)) |
| 36 | + |
| 37 | +# We build up a list of every single one of the targets so we can slurp in the |
| 38 | +# generated dependency rule Makefiles in one pass. |
| 39 | +all_deps := |
| 40 | + |
| 41 | +%(make_global_settings)s |
| 42 | + |
| 43 | +CC.target ?= %(CC.target)s |
| 44 | +CFLAGS.target ?= $(CPPFLAGS) $(CFLAGS) |
| 45 | +CXX.target ?= %(CXX.target)s |
| 46 | +CXXFLAGS.target ?= $(CPPFLAGS) $(CXXFLAGS) |
| 47 | +LINK.target ?= %(LINK.target)s |
| 48 | +LDFLAGS.target ?= $(LDFLAGS) |
| 49 | +AR.target ?= $(AR) |
| 50 | + |
| 51 | +# C++ apps need to be linked with g++. |
| 52 | +LINK ?= $(CXX.target) |
| 53 | + |
| 54 | +# TODO(evan): move all cross-compilation logic to gyp-time so we don't need |
| 55 | +# to replicate this environment fallback in make as well. |
| 56 | +CC.host ?= %(CC.host)s |
| 57 | +CFLAGS.host ?= $(CPPFLAGS_host) $(CFLAGS_host) |
| 58 | +CXX.host ?= %(CXX.host)s |
| 59 | +CXXFLAGS.host ?= $(CPPFLAGS_host) $(CXXFLAGS_host) |
| 60 | +LINK.host ?= %(LINK.host)s |
| 61 | +LDFLAGS.host ?= $(LDFLAGS_host) |
| 62 | +AR.host ?= %(AR.host)s |
| 63 | + |
| 64 | +# Define a dir function that can handle spaces. |
| 65 | +# http://www.gnu.org/software/make/manual/make.html#Syntax-of-Functions |
| 66 | +# "leading spaces cannot appear in the text of the first argument as written. |
| 67 | +# These characters can be put into the argument value by variable substitution." |
| 68 | +empty := |
| 69 | +space := $(empty) $(empty) |
| 70 | + |
| 71 | +# http://stackoverflow.com/questions/1189781/using-make-dir-or-notdir-on-a-path-with-spaces |
| 72 | +replace_spaces = $(subst $(space),{SPACE_REPLACEMENT},$1) |
| 73 | +unreplace_spaces = $(subst {SPACE_REPLACEMENT},$(space),$1) |
| 74 | +dirx = $(call unreplace_spaces,$(dir $(call replace_spaces,$1))) |
| 75 | + |
| 76 | +# Flags to make gcc output dependency info. Note that you need to be |
| 77 | +# careful here to use the flags that ccache and distcc can understand. |
| 78 | +# We write to a dep file on the side first and then rename at the end |
| 79 | +# so we can't end up with a broken dep file. |
| 80 | +depfile = $(depsdir)/$(call replace_spaces,$@).d |
| 81 | +DEPFLAGS = %(makedep_args)s -MF $(depfile).raw |
| 82 | + |
| 83 | +# We have to fixup the deps output in a few ways. |
| 84 | +# (1) the file output should mention the proper .o file. |
| 85 | +# ccache or distcc lose the path to the target, so we convert a rule of |
| 86 | +# the form: |
| 87 | +# foobar.o: DEP1 DEP2 |
| 88 | +# into |
| 89 | +# path/to/foobar.o: DEP1 DEP2 |
| 90 | +# (2) we want missing files not to cause us to fail to build. |
| 91 | +# We want to rewrite |
| 92 | +# foobar.o: DEP1 DEP2 \ |
| 93 | +# DEP3 |
| 94 | +# to |
| 95 | +# DEP1: |
| 96 | +# DEP2: |
| 97 | +# DEP3: |
| 98 | +# so if the files are missing, they're just considered phony rules. |
| 99 | +# We have to do some pretty insane escaping to get those backslashes |
| 100 | +# and dollar signs past make, the shell, and sed at the same time. |
| 101 | +# Doesn't work with spaces, but that's fine: .d files have spaces in |
| 102 | +# their names replaced with other characters. |
| 103 | +define fixup_dep |
| 104 | +# The depfile may not exist if the input file didn't have any #includes. |
| 105 | +touch $(depfile).raw |
| 106 | +# Fixup path as in (1). |
| 107 | +sed -e "s|^$(notdir $@)|$@|" $(depfile).raw >> $(depfile) |
| 108 | +# Add extra rules as in (2). |
| 109 | +# We remove slashes and replace spaces with new lines; |
| 110 | +# remove blank lines; |
| 111 | +# delete the first line and append a colon to the remaining lines. |
| 112 | +sed -e 's|\\||' -e 'y| |\n|' $(depfile).raw |\ |
| 113 | + grep -v '^$$' |\ |
| 114 | + sed -e 1d -e 's|$$|:|' \ |
| 115 | + >> $(depfile) |
| 116 | +rm $(depfile).raw |
| 117 | +endef |
| 118 | +# Command definitions: |
| 119 | +# - cmd_foo is the actual command to run; |
| 120 | +# - quiet_cmd_foo is the brief-output summary of the command. |
| 121 | + |
| 122 | +quiet_cmd_cc = CC($(TOOLSET)) $@ |
| 123 | +cmd_cc = $(CC.$(TOOLSET)) $(GYP_CFLAGS) $(DEPFLAGS) $(CFLAGS.$(TOOLSET)) -c -o $@ $< |
| 124 | + |
| 125 | +quiet_cmd_cxx = CXX($(TOOLSET)) $@ |
| 126 | +cmd_cxx = $(CXX.$(TOOLSET)) $(GYP_CXXFLAGS) $(DEPFLAGS) $(CXXFLAGS.$(TOOLSET)) -c -o $@ $< |
| 127 | +%(extra_commands)s |
| 128 | +quiet_cmd_touch = TOUCH $@ |
| 129 | +cmd_touch = touch $@ |
| 130 | + |
| 131 | +quiet_cmd_copy = COPY $@ |
| 132 | +# send stderr to /dev/null to ignore messages when linking directories. |
| 133 | +cmd_copy = ln -f "$<" "$@" 2>/dev/null || (rm -rf "$@" && cp %(copy_archive_args)s "$<" "$@") |
| 134 | + |
| 135 | +%(link_commands)s |
| 136 | +# Define an escape_quotes function to escape single quotes. |
| 137 | +# This allows us to handle quotes properly as long as we always use |
| 138 | +# use single quotes and escape_quotes. |
| 139 | +escape_quotes = $(subst ','\'',$(1)) |
| 140 | +# This comment is here just to include a ' to unconfuse syntax highlighting. |
| 141 | +# Define an escape_vars function to escape '$' variable syntax. |
| 142 | +# This allows us to read/write command lines with shell variables (e.g. |
| 143 | +# $LD_LIBRARY_PATH), without triggering make substitution. |
| 144 | +escape_vars = $(subst $$,$$$$,$(1)) |
| 145 | +# Helper that expands to a shell command to echo a string exactly as it is in |
| 146 | +# make. This uses printf instead of echo because printf's behaviour with respect |
| 147 | +# to escape sequences is more portable than echo's across different shells |
| 148 | +# (e.g., dash, bash). |
| 149 | +exact_echo = printf '%%s\n' '$(call escape_quotes,$(1))' |
| 150 | +# Helper to compare the command we're about to run against the command |
| 151 | +# we logged the last time we ran the command. Produces an empty |
| 152 | +# string (false) when the commands match. |
| 153 | +# Tricky point: Make has no string-equality test function. |
| 154 | +# The kernel uses the following, but it seems like it would have false |
| 155 | +# positives, where one string reordered its arguments. |
| 156 | +# arg_check = $(strip $(filter-out $(cmd_$(1)), $(cmd_$@)) \ |
| 157 | +# $(filter-out $(cmd_$@), $(cmd_$(1)))) |
| 158 | +# We instead substitute each for the empty string into the other, and |
| 159 | +# say they're equal if both substitutions produce the empty string. |
| 160 | +# .d files contain {SPACE_REPLACEMENT} instead of spaces, take that into account. |
| 161 | +command_changed = $(or $(subst $(cmd_$(1)),,$(cmd_$(call replace_spaces,$@))), $(subst $(cmd_$(call replace_spaces,$@)),,$(cmd_$(1)))) |
| 162 | + |
| 163 | +# Helper that is non-empty when a prerequisite changes. |
| 164 | +# Normally make does this implicitly, but we force rules to always run |
| 165 | +# so we can check their command lines. |
| 166 | +# $? -- new prerequisites |
| 167 | +# $| -- order-only dependencies |
| 168 | +prereq_changed = $(filter-out FORCE_DO_CMD,$(filter-out $|,$?)) |
| 169 | + |
| 170 | +# Helper that executes all postbuilds until one fails. |
| 171 | +define do_postbuilds |
| 172 | + @E=0;\ |
| 173 | + for p in $(POSTBUILDS); do\ |
| 174 | + eval $$p;\ |
| 175 | + E=$$?;\ |
| 176 | + if [ $$E -ne 0 ]; then\ |
| 177 | + break;\ |
| 178 | + fi;\ |
| 179 | + done;\ |
| 180 | + if [ $$E -ne 0 ]; then\ |
| 181 | + rm -rf "$@";\ |
| 182 | + exit $$E;\ |
| 183 | + fi |
| 184 | +endef |
| 185 | + |
| 186 | +# do_cmd: run a command via the above cmd_foo names, if necessary. |
| 187 | +# Should always run for a given target to handle command-line changes. |
| 188 | +# Second argument, if non-zero, makes it do asm/C/C++ dependency munging. |
| 189 | +# Third argument, if non-zero, makes it do POSTBUILDS processing. |
| 190 | +# Note: We intentionally do NOT call dirx for depfile, since it contains {SPACE_REPLACEMENT} for |
| 191 | +# spaces already and dirx strips the {SPACE_REPLACEMENT} characters. |
| 192 | +define do_cmd |
| 193 | +$(if $(or $(command_changed),$(prereq_changed)), |
| 194 | + @$(call exact_echo, $($(quiet)cmd_$(1))) |
| 195 | + @mkdir -p "$(call dirx,$@)" "$(dir $(depfile))" |
| 196 | + $(if $(findstring flock,$(word %(flock_index)d,$(cmd_$1))), |
| 197 | + @$(cmd_$(1)) |
| 198 | + @echo " $(quiet_cmd_$(1)): Finished", |
| 199 | + @$(cmd_$(1)) |
| 200 | + ) |
| 201 | + @$(call exact_echo,$(call escape_vars,cmd_$(call replace_spaces,$@) := $(cmd_$(1)))) > $(depfile) |
| 202 | + @$(if $(2),$(fixup_dep)) |
| 203 | + $(if $(and $(3), $(POSTBUILDS)), |
| 204 | + $(call do_postbuilds) |
| 205 | + ) |
| 206 | +) |
| 207 | +endef |
| 208 | + |
| 209 | +# Declare the "%(default_target)s" target first so it is the default, |
| 210 | +# even though we don't have the deps yet. |
| 211 | +.PHONY: %(default_target)s |
| 212 | +%(default_target)s: |
| 213 | + |
| 214 | +# make looks for ways to re-generate included makefiles, but in our case, we |
| 215 | +# don't have a direct way. Explicitly telling make that it has nothing to do |
| 216 | +# for them makes it go faster. |
| 217 | +%%.d: ; |
| 218 | + |
| 219 | +# Use FORCE_DO_CMD to force a target to run. Should be coupled with |
| 220 | +# do_cmd. |
| 221 | +.PHONY: FORCE_DO_CMD |
| 222 | +FORCE_DO_CMD: |
| 223 | + |
0 commit comments