|
| 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 := .. |
| 11 | +abs_srcdir := $(abspath $(srcdir)) |
| 12 | + |
| 13 | +# The name of the builddir. |
| 14 | +builddir_name ?= . |
| 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 ?= Release |
| 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 | + |
| 42 | + |
| 43 | +CC.target ?= $(CC) |
| 44 | +CFLAGS.target ?= $(CPPFLAGS) $(CFLAGS) |
| 45 | +CXX.target ?= $(CXX) |
| 46 | +CXXFLAGS.target ?= $(CPPFLAGS) $(CXXFLAGS) |
| 47 | +LINK.target ?= $(LINK) |
| 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 ?= gcc |
| 57 | +CFLAGS.host ?= $(CPPFLAGS_host) $(CFLAGS_host) |
| 58 | +CXX.host ?= g++ |
| 59 | +CXXFLAGS.host ?= $(CPPFLAGS_host) $(CXXFLAGS_host) |
| 60 | +LINK.host ?= $(CXX.host) |
| 61 | +LDFLAGS.host ?= |
| 62 | +AR.host ?= ar |
| 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),?,$1) |
| 73 | +unreplace_spaces = $(subst ?,$(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 = -MMD -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 | + |
| 119 | +# Command definitions: |
| 120 | +# - cmd_foo is the actual command to run; |
| 121 | +# - quiet_cmd_foo is the brief-output summary of the command. |
| 122 | + |
| 123 | +quiet_cmd_cc = CC($(TOOLSET)) $@ |
| 124 | +cmd_cc = $(CC.$(TOOLSET)) $(GYP_CFLAGS) $(DEPFLAGS) $(CFLAGS.$(TOOLSET)) -c -o $@ $< |
| 125 | + |
| 126 | +quiet_cmd_cxx = CXX($(TOOLSET)) $@ |
| 127 | +cmd_cxx = $(CXX.$(TOOLSET)) $(GYP_CXXFLAGS) $(DEPFLAGS) $(CXXFLAGS.$(TOOLSET)) -c -o $@ $< |
| 128 | + |
| 129 | +quiet_cmd_objc = CXX($(TOOLSET)) $@ |
| 130 | +cmd_objc = $(CC.$(TOOLSET)) $(GYP_OBJCFLAGS) $(DEPFLAGS) -c -o $@ $< |
| 131 | + |
| 132 | +quiet_cmd_objcxx = CXX($(TOOLSET)) $@ |
| 133 | +cmd_objcxx = $(CXX.$(TOOLSET)) $(GYP_OBJCXXFLAGS) $(DEPFLAGS) -c -o $@ $< |
| 134 | + |
| 135 | +# Commands for precompiled header files. |
| 136 | +quiet_cmd_pch_c = CXX($(TOOLSET)) $@ |
| 137 | +cmd_pch_c = $(CC.$(TOOLSET)) $(GYP_PCH_CFLAGS) $(DEPFLAGS) $(CXXFLAGS.$(TOOLSET)) -c -o $@ $< |
| 138 | +quiet_cmd_pch_cc = CXX($(TOOLSET)) $@ |
| 139 | +cmd_pch_cc = $(CC.$(TOOLSET)) $(GYP_PCH_CXXFLAGS) $(DEPFLAGS) $(CXXFLAGS.$(TOOLSET)) -c -o $@ $< |
| 140 | +quiet_cmd_pch_m = CXX($(TOOLSET)) $@ |
| 141 | +cmd_pch_m = $(CC.$(TOOLSET)) $(GYP_PCH_OBJCFLAGS) $(DEPFLAGS) -c -o $@ $< |
| 142 | +quiet_cmd_pch_mm = CXX($(TOOLSET)) $@ |
| 143 | +cmd_pch_mm = $(CC.$(TOOLSET)) $(GYP_PCH_OBJCXXFLAGS) $(DEPFLAGS) -c -o $@ $< |
| 144 | + |
| 145 | +# gyp-mac-tool is written next to the root Makefile by gyp. |
| 146 | +# Use $(4) for the command, since $(2) and $(3) are used as flag by do_cmd |
| 147 | +# already. |
| 148 | +quiet_cmd_mac_tool = MACTOOL $(4) $< |
| 149 | +cmd_mac_tool = ./gyp-mac-tool $(4) $< "$@" |
| 150 | + |
| 151 | +quiet_cmd_mac_package_framework = PACKAGE FRAMEWORK $@ |
| 152 | +cmd_mac_package_framework = ./gyp-mac-tool package-framework "$@" $(4) |
| 153 | + |
| 154 | +quiet_cmd_infoplist = INFOPLIST $@ |
| 155 | +cmd_infoplist = $(CC.$(TOOLSET)) -E -P -Wno-trigraphs -x c $(INFOPLIST_DEFINES) "$<" -o "$@" |
| 156 | + |
| 157 | +quiet_cmd_touch = TOUCH $@ |
| 158 | +cmd_touch = touch $@ |
| 159 | + |
| 160 | +quiet_cmd_copy = COPY $@ |
| 161 | +# send stderr to /dev/null to ignore messages when linking directories. |
| 162 | +cmd_copy = rm -rf "$@" && cp -af "$<" "$@" |
| 163 | + |
| 164 | +quiet_cmd_alink = LIBTOOL-STATIC $@ |
| 165 | +cmd_alink = rm -f $@ && ./gyp-mac-tool filter-libtool libtool $(GYP_LIBTOOLFLAGS) -static -o $@ $(filter %.o,$^) |
| 166 | + |
| 167 | +quiet_cmd_link = LINK($(TOOLSET)) $@ |
| 168 | +cmd_link = $(LINK.$(TOOLSET)) $(GYP_LDFLAGS) $(LDFLAGS.$(TOOLSET)) -o "$@" $(LD_INPUTS) $(LIBS) |
| 169 | + |
| 170 | +quiet_cmd_solink = SOLINK($(TOOLSET)) $@ |
| 171 | +cmd_solink = $(LINK.$(TOOLSET)) -shared $(GYP_LDFLAGS) $(LDFLAGS.$(TOOLSET)) -o "$@" $(LD_INPUTS) $(LIBS) |
| 172 | + |
| 173 | +quiet_cmd_solink_module = SOLINK_MODULE($(TOOLSET)) $@ |
| 174 | +cmd_solink_module = $(LINK.$(TOOLSET)) -bundle $(GYP_LDFLAGS) $(LDFLAGS.$(TOOLSET)) -o $@ $(filter-out FORCE_DO_CMD, $^) $(LIBS) |
| 175 | + |
| 176 | + |
| 177 | +# Define an escape_quotes function to escape single quotes. |
| 178 | +# This allows us to handle quotes properly as long as we always use |
| 179 | +# use single quotes and escape_quotes. |
| 180 | +escape_quotes = $(subst ','\'',$(1)) |
| 181 | +# This comment is here just to include a ' to unconfuse syntax highlighting. |
| 182 | +# Define an escape_vars function to escape '$' variable syntax. |
| 183 | +# This allows us to read/write command lines with shell variables (e.g. |
| 184 | +# $LD_LIBRARY_PATH), without triggering make substitution. |
| 185 | +escape_vars = $(subst $$,$$$$,$(1)) |
| 186 | +# Helper that expands to a shell command to echo a string exactly as it is in |
| 187 | +# make. This uses printf instead of echo because printf's behaviour with respect |
| 188 | +# to escape sequences is more portable than echo's across different shells |
| 189 | +# (e.g., dash, bash). |
| 190 | +exact_echo = printf '%s\n' '$(call escape_quotes,$(1))' |
| 191 | + |
| 192 | +# Helper to compare the command we're about to run against the command |
| 193 | +# we logged the last time we ran the command. Produces an empty |
| 194 | +# string (false) when the commands match. |
| 195 | +# Tricky point: Make has no string-equality test function. |
| 196 | +# The kernel uses the following, but it seems like it would have false |
| 197 | +# positives, where one string reordered its arguments. |
| 198 | +# arg_check = $(strip $(filter-out $(cmd_$(1)), $(cmd_$@)) \ |
| 199 | +# $(filter-out $(cmd_$@), $(cmd_$(1)))) |
| 200 | +# We instead substitute each for the empty string into the other, and |
| 201 | +# say they're equal if both substitutions produce the empty string. |
| 202 | +# .d files contain ? instead of spaces, take that into account. |
| 203 | +command_changed = $(or $(subst $(cmd_$(1)),,$(cmd_$(call replace_spaces,$@))),\ |
| 204 | + $(subst $(cmd_$(call replace_spaces,$@)),,$(cmd_$(1)))) |
| 205 | + |
| 206 | +# Helper that is non-empty when a prerequisite changes. |
| 207 | +# Normally make does this implicitly, but we force rules to always run |
| 208 | +# so we can check their command lines. |
| 209 | +# $? -- new prerequisites |
| 210 | +# $| -- order-only dependencies |
| 211 | +prereq_changed = $(filter-out FORCE_DO_CMD,$(filter-out $|,$?)) |
| 212 | + |
| 213 | +# Helper that executes all postbuilds until one fails. |
| 214 | +define do_postbuilds |
| 215 | + @E=0;\ |
| 216 | + for p in $(POSTBUILDS); do\ |
| 217 | + eval $$p;\ |
| 218 | + E=$$?;\ |
| 219 | + if [ $$E -ne 0 ]; then\ |
| 220 | + break;\ |
| 221 | + fi;\ |
| 222 | + done;\ |
| 223 | + if [ $$E -ne 0 ]; then\ |
| 224 | + rm -rf "$@";\ |
| 225 | + exit $$E;\ |
| 226 | + fi |
| 227 | +endef |
| 228 | + |
| 229 | +# do_cmd: run a command via the above cmd_foo names, if necessary. |
| 230 | +# Should always run for a given target to handle command-line changes. |
| 231 | +# Second argument, if non-zero, makes it do asm/C/C++ dependency munging. |
| 232 | +# Third argument, if non-zero, makes it do POSTBUILDS processing. |
| 233 | +# Note: We intentionally do NOT call dirx for depfile, since it contains ? for |
| 234 | +# spaces already and dirx strips the ? characters. |
| 235 | +define do_cmd |
| 236 | +$(if $(or $(command_changed),$(prereq_changed)), |
| 237 | + @$(call exact_echo, $($(quiet)cmd_$(1))) |
| 238 | + @mkdir -p "$(call dirx,$@)" "$(dir $(depfile))" |
| 239 | + $(if $(findstring flock,$(word 2,$(cmd_$1))), |
| 240 | + @$(cmd_$(1)) |
| 241 | + @echo " $(quiet_cmd_$(1)): Finished", |
| 242 | + @$(cmd_$(1)) |
| 243 | + ) |
| 244 | + @$(call exact_echo,$(call escape_vars,cmd_$(call replace_spaces,$@) := $(cmd_$(1)))) > $(depfile) |
| 245 | + @$(if $(2),$(fixup_dep)) |
| 246 | + $(if $(and $(3), $(POSTBUILDS)), |
| 247 | + $(call do_postbuilds) |
| 248 | + ) |
| 249 | +) |
| 250 | +endef |
| 251 | + |
| 252 | +# Declare the "all" target first so it is the default, |
| 253 | +# even though we don't have the deps yet. |
| 254 | +.PHONY: all |
| 255 | +all: |
| 256 | + |
| 257 | +# make looks for ways to re-generate included makefiles, but in our case, we |
| 258 | +# don't have a direct way. Explicitly telling make that it has nothing to do |
| 259 | +# for them makes it go faster. |
| 260 | +%.d: ; |
| 261 | + |
| 262 | +# Use FORCE_DO_CMD to force a target to run. Should be coupled with |
| 263 | +# do_cmd. |
| 264 | +.PHONY: FORCE_DO_CMD |
| 265 | +FORCE_DO_CMD: |
| 266 | + |
| 267 | +TOOLSET := target |
| 268 | +# Suffix rules, putting all outputs into $(obj). |
| 269 | +$(obj).$(TOOLSET)/%.o: $(srcdir)/%.c FORCE_DO_CMD |
| 270 | + @$(call do_cmd,cc,1) |
| 271 | +$(obj).$(TOOLSET)/%.o: $(srcdir)/%.cc FORCE_DO_CMD |
| 272 | + @$(call do_cmd,cxx,1) |
| 273 | +$(obj).$(TOOLSET)/%.o: $(srcdir)/%.cpp FORCE_DO_CMD |
| 274 | + @$(call do_cmd,cxx,1) |
| 275 | +$(obj).$(TOOLSET)/%.o: $(srcdir)/%.cxx FORCE_DO_CMD |
| 276 | + @$(call do_cmd,cxx,1) |
| 277 | +$(obj).$(TOOLSET)/%.o: $(srcdir)/%.m FORCE_DO_CMD |
| 278 | + @$(call do_cmd,objc,1) |
| 279 | +$(obj).$(TOOLSET)/%.o: $(srcdir)/%.mm FORCE_DO_CMD |
| 280 | + @$(call do_cmd,objcxx,1) |
| 281 | +$(obj).$(TOOLSET)/%.o: $(srcdir)/%.S FORCE_DO_CMD |
| 282 | + @$(call do_cmd,cc,1) |
| 283 | +$(obj).$(TOOLSET)/%.o: $(srcdir)/%.s FORCE_DO_CMD |
| 284 | + @$(call do_cmd,cc,1) |
| 285 | + |
| 286 | +# Try building from generated source, too. |
| 287 | +$(obj).$(TOOLSET)/%.o: $(obj).$(TOOLSET)/%.c FORCE_DO_CMD |
| 288 | + @$(call do_cmd,cc,1) |
| 289 | +$(obj).$(TOOLSET)/%.o: $(obj).$(TOOLSET)/%.cc FORCE_DO_CMD |
| 290 | + @$(call do_cmd,cxx,1) |
| 291 | +$(obj).$(TOOLSET)/%.o: $(obj).$(TOOLSET)/%.cpp FORCE_DO_CMD |
| 292 | + @$(call do_cmd,cxx,1) |
| 293 | +$(obj).$(TOOLSET)/%.o: $(obj).$(TOOLSET)/%.cxx FORCE_DO_CMD |
| 294 | + @$(call do_cmd,cxx,1) |
| 295 | +$(obj).$(TOOLSET)/%.o: $(obj).$(TOOLSET)/%.m FORCE_DO_CMD |
| 296 | + @$(call do_cmd,objc,1) |
| 297 | +$(obj).$(TOOLSET)/%.o: $(obj).$(TOOLSET)/%.mm FORCE_DO_CMD |
| 298 | + @$(call do_cmd,objcxx,1) |
| 299 | +$(obj).$(TOOLSET)/%.o: $(obj).$(TOOLSET)/%.S FORCE_DO_CMD |
| 300 | + @$(call do_cmd,cc,1) |
| 301 | +$(obj).$(TOOLSET)/%.o: $(obj).$(TOOLSET)/%.s FORCE_DO_CMD |
| 302 | + @$(call do_cmd,cc,1) |
| 303 | + |
| 304 | +$(obj).$(TOOLSET)/%.o: $(obj)/%.c FORCE_DO_CMD |
| 305 | + @$(call do_cmd,cc,1) |
| 306 | +$(obj).$(TOOLSET)/%.o: $(obj)/%.cc FORCE_DO_CMD |
| 307 | + @$(call do_cmd,cxx,1) |
| 308 | +$(obj).$(TOOLSET)/%.o: $(obj)/%.cpp FORCE_DO_CMD |
| 309 | + @$(call do_cmd,cxx,1) |
| 310 | +$(obj).$(TOOLSET)/%.o: $(obj)/%.cxx FORCE_DO_CMD |
| 311 | + @$(call do_cmd,cxx,1) |
| 312 | +$(obj).$(TOOLSET)/%.o: $(obj)/%.m FORCE_DO_CMD |
| 313 | + @$(call do_cmd,objc,1) |
| 314 | +$(obj).$(TOOLSET)/%.o: $(obj)/%.mm FORCE_DO_CMD |
| 315 | + @$(call do_cmd,objcxx,1) |
| 316 | +$(obj).$(TOOLSET)/%.o: $(obj)/%.S FORCE_DO_CMD |
| 317 | + @$(call do_cmd,cc,1) |
| 318 | +$(obj).$(TOOLSET)/%.o: $(obj)/%.s FORCE_DO_CMD |
| 319 | + @$(call do_cmd,cc,1) |
| 320 | + |
| 321 | + |
| 322 | +ifeq ($(strip $(foreach prefix,$(NO_LOAD),\ |
| 323 | + $(findstring $(join ^,$(prefix)),\ |
| 324 | + $(join ^,binding.target.mk)))),) |
| 325 | + include binding.target.mk |
| 326 | +endif |
| 327 | + |
| 328 | +quiet_cmd_regen_makefile = ACTION Regenerating $@ |
| 329 | +cmd_regen_makefile = cd $(srcdir); /Users/trott/io.js/deps/npm/node_modules/node-gyp/gyp/gyp_main.py -fmake --ignore-environment "--toplevel-dir=." -I/Users/trott/io.js/test/addons-napi/1_hello_world/build/config.gypi -I/Users/trott/io.js/deps/npm/node_modules/node-gyp/addon.gypi -I/Users/trott/io.js/common.gypi "--depth=." "-Goutput_dir=." "--generator-output=build" "-Dlibrary=shared_library" "-Dvisibility=default" "-Dnode_root_dir=/Users/trott/io.js" "-Dnode_gyp_dir=/Users/trott/io.js/deps/npm/node_modules/node-gyp" "-Dnode_lib_file=/Users/trott/io.js/$(Configuration)/node.lib" "-Dmodule_root_dir=/Users/trott/io.js/test/addons-napi/1_hello_world" "-Dnode_engine=v8" binding.gyp |
| 330 | +Makefile: $(srcdir)/../../../deps/npm/node_modules/node-gyp/addon.gypi $(srcdir)/build/config.gypi $(srcdir)/binding.gyp $(srcdir)/../../../common.gypi |
| 331 | + $(call do_cmd,regen_makefile) |
| 332 | + |
| 333 | +# "all" is a concatenation of the "all" targets from all the included |
| 334 | +# sub-makefiles. This is just here to clarify. |
| 335 | +all: |
| 336 | + |
| 337 | +# Add in dependency-tracking rules. $(all_deps) is the list of every single |
| 338 | +# target in our tree. Only consider the ones with .d (dependency) info: |
| 339 | +d_files := $(wildcard $(foreach f,$(all_deps),$(depsdir)/$(f).d)) |
| 340 | +ifneq ($(d_files),) |
| 341 | + include $(d_files) |
| 342 | +endif |
0 commit comments