|
| 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 ?= ccache gcc |
| 44 | +CFLAGS.target ?= $(CPPFLAGS) $(CFLAGS) |
| 45 | +CXX.target ?= ccache g++ |
| 46 | +CXXFLAGS.target ?= $(CPPFLAGS) $(CXXFLAGS) |
| 47 | +LINK.target ?= $(LINK) |
| 48 | +LDFLAGS.target ?= $(LDFLAGS) |
| 49 | +AR.target ?= $(AR) |
| 50 | +PLI.target ?= pli |
| 51 | + |
| 52 | +# C++ apps need to be linked with g++. |
| 53 | +LINK ?= $(CXX.target) |
| 54 | + |
| 55 | +# TODO(evan): move all cross-compilation logic to gyp-time so we don't need |
| 56 | +# to replicate this environment fallback in make as well. |
| 57 | +CC.host ?= ccache gcc |
| 58 | +CFLAGS.host ?= $(CPPFLAGS_host) $(CFLAGS_host) |
| 59 | +CXX.host ?= ccache g++ |
| 60 | +CXXFLAGS.host ?= $(CPPFLAGS_host) $(CXXFLAGS_host) |
| 61 | +LINK.host ?= $(CXX.host) |
| 62 | +LDFLAGS.host ?= $(LDFLAGS_host) |
| 63 | +AR.host ?= ar |
| 64 | +PLI.host ?= pli |
| 65 | + |
| 66 | +# Define a dir function that can handle spaces. |
| 67 | +# http://www.gnu.org/software/make/manual/make.html#Syntax-of-Functions |
| 68 | +# "leading spaces cannot appear in the text of the first argument as written. |
| 69 | +# These characters can be put into the argument value by variable substitution." |
| 70 | +empty := |
| 71 | +space := $(empty) $(empty) |
| 72 | + |
| 73 | +# http://stackoverflow.com/questions/1189781/using-make-dir-or-notdir-on-a-path-with-spaces |
| 74 | +replace_spaces = $(subst $(space),?,$1) |
| 75 | +unreplace_spaces = $(subst ?,$(space),$1) |
| 76 | +dirx = $(call unreplace_spaces,$(dir $(call replace_spaces,$1))) |
| 77 | + |
| 78 | +# Flags to make gcc output dependency info. Note that you need to be |
| 79 | +# careful here to use the flags that ccache and distcc can understand. |
| 80 | +# We write to a dep file on the side first and then rename at the end |
| 81 | +# so we can't end up with a broken dep file. |
| 82 | +depfile = $(depsdir)/$(call replace_spaces,$@).d |
| 83 | +DEPFLAGS = -MMD -MF $(depfile).raw |
| 84 | + |
| 85 | +# We have to fixup the deps output in a few ways. |
| 86 | +# (1) the file output should mention the proper .o file. |
| 87 | +# ccache or distcc lose the path to the target, so we convert a rule of |
| 88 | +# the form: |
| 89 | +# foobar.o: DEP1 DEP2 |
| 90 | +# into |
| 91 | +# path/to/foobar.o: DEP1 DEP2 |
| 92 | +# (2) we want missing files not to cause us to fail to build. |
| 93 | +# We want to rewrite |
| 94 | +# foobar.o: DEP1 DEP2 \ |
| 95 | +# DEP3 |
| 96 | +# to |
| 97 | +# DEP1: |
| 98 | +# DEP2: |
| 99 | +# DEP3: |
| 100 | +# so if the files are missing, they're just considered phony rules. |
| 101 | +# We have to do some pretty insane escaping to get those backslashes |
| 102 | +# and dollar signs past make, the shell, and sed at the same time. |
| 103 | +# Doesn't work with spaces, but that's fine: .d files have spaces in |
| 104 | +# their names replaced with other characters. |
| 105 | +define fixup_dep |
| 106 | +# The depfile may not exist if the input file didn't have any #includes. |
| 107 | +touch $(depfile).raw |
| 108 | +# Fixup path as in (1). |
| 109 | +sed -e "s|^$(notdir $@)|$@|" $(depfile).raw >> $(depfile) |
| 110 | +# Add extra rules as in (2). |
| 111 | +# We remove slashes and replace spaces with new lines; |
| 112 | +# remove blank lines; |
| 113 | +# delete the first line and append a colon to the remaining lines. |
| 114 | +sed -e 's|\\||' -e 'y| |\n|' $(depfile).raw |\ |
| 115 | + grep -v '^$$' |\ |
| 116 | + sed -e 1d -e 's|$$|:|' \ |
| 117 | + >> $(depfile) |
| 118 | +rm $(depfile).raw |
| 119 | +endef |
| 120 | + |
| 121 | +# Command definitions: |
| 122 | +# - cmd_foo is the actual command to run; |
| 123 | +# - quiet_cmd_foo is the brief-output summary of the command. |
| 124 | + |
| 125 | +quiet_cmd_cc = CC($(TOOLSET)) $@ |
| 126 | +cmd_cc = $(CC.$(TOOLSET)) -o $@ $< $(GYP_CFLAGS) $(DEPFLAGS) $(CFLAGS.$(TOOLSET)) -c |
| 127 | + |
| 128 | +quiet_cmd_cxx = CXX($(TOOLSET)) $@ |
| 129 | +cmd_cxx = $(CXX.$(TOOLSET)) -o $@ $< $(GYP_CXXFLAGS) $(DEPFLAGS) $(CXXFLAGS.$(TOOLSET)) -c |
| 130 | + |
| 131 | +quiet_cmd_touch = TOUCH $@ |
| 132 | +cmd_touch = touch $@ |
| 133 | + |
| 134 | +quiet_cmd_copy = COPY $@ |
| 135 | +# send stderr to /dev/null to ignore messages when linking directories. |
| 136 | +cmd_copy = ln -f "$<" "$@" 2>/dev/null || (rm -rf "$@" && cp -af "$<" "$@") |
| 137 | + |
| 138 | +quiet_cmd_symlink = SYMLINK $@ |
| 139 | +cmd_symlink = ln -sf "$<" "$@" |
| 140 | + |
| 141 | +quiet_cmd_alink = AR($(TOOLSET)) $@ |
| 142 | +cmd_alink = rm -f $@ && $(AR.$(TOOLSET)) crs $@ $(filter %.o,$^) |
| 143 | + |
| 144 | +quiet_cmd_alink_thin = AR($(TOOLSET)) $@ |
| 145 | +cmd_alink_thin = rm -f $@ && $(AR.$(TOOLSET)) crsT $@ $(filter %.o,$^) |
| 146 | + |
| 147 | +# Due to circular dependencies between libraries :(, we wrap the |
| 148 | +# special "figure out circular dependencies" flags around the entire |
| 149 | +# input list during linking. |
| 150 | +quiet_cmd_link = LINK($(TOOLSET)) $@ |
| 151 | +cmd_link = $(LINK.$(TOOLSET)) -o $@ $(GYP_LDFLAGS) $(LDFLAGS.$(TOOLSET)) -Wl,--start-group $(LD_INPUTS) $(LIBS) -Wl,--end-group |
| 152 | + |
| 153 | +# Note: this does not handle spaces in paths |
| 154 | +define xargs |
| 155 | + $(1) $(word 1,$(2)) |
| 156 | +$(if $(word 2,$(2)),$(call xargs,$(1),$(wordlist 2,$(words $(2)),$(2)))) |
| 157 | +endef |
| 158 | + |
| 159 | +define write-to-file |
| 160 | + @: >$(1) |
| 161 | +$(call xargs,@printf "%s\n" >>$(1),$(2)) |
| 162 | +endef |
| 163 | + |
| 164 | +OBJ_FILE_LIST := ar-file-list |
| 165 | + |
| 166 | +define create_archive |
| 167 | + rm -f $(1) $(1).$(OBJ_FILE_LIST); mkdir -p `dirname $(1)` |
| 168 | + $(call write-to-file,$(1).$(OBJ_FILE_LIST),$(filter %.o,$(2))) |
| 169 | + $(AR.$(TOOLSET)) crs $(1) @$(1).$(OBJ_FILE_LIST) |
| 170 | +endef |
| 171 | + |
| 172 | +define create_thin_archive |
| 173 | + rm -f $(1) $(OBJ_FILE_LIST); mkdir -p `dirname $(1)` |
| 174 | + $(call write-to-file,$(1).$(OBJ_FILE_LIST),$(filter %.o,$(2))) |
| 175 | + $(AR.$(TOOLSET)) crsT $(1) @$(1).$(OBJ_FILE_LIST) |
| 176 | +endef |
| 177 | + |
| 178 | +# We support two kinds of shared objects (.so): |
| 179 | +# 1) shared_library, which is just bundling together many dependent libraries |
| 180 | +# into a link line. |
| 181 | +# 2) loadable_module, which is generating a module intended for dlopen(). |
| 182 | +# |
| 183 | +# They differ only slightly: |
| 184 | +# In the former case, we want to package all dependent code into the .so. |
| 185 | +# In the latter case, we want to package just the API exposed by the |
| 186 | +# outermost module. |
| 187 | +# This means shared_library uses --whole-archive, while loadable_module doesn't. |
| 188 | +# (Note that --whole-archive is incompatible with the --start-group used in |
| 189 | +# normal linking.) |
| 190 | + |
| 191 | +# Other shared-object link notes: |
| 192 | +# - Set SONAME to the library filename so our binaries don't reference |
| 193 | +# the local, absolute paths used on the link command-line. |
| 194 | +quiet_cmd_solink = SOLINK($(TOOLSET)) $@ |
| 195 | +cmd_solink = $(LINK.$(TOOLSET)) -o $@ -shared $(GYP_LDFLAGS) $(LDFLAGS.$(TOOLSET)) -Wl,-soname=$(@F) -Wl,--whole-archive $(LD_INPUTS) -Wl,--no-whole-archive $(LIBS) |
| 196 | + |
| 197 | +quiet_cmd_solink_module = SOLINK_MODULE($(TOOLSET)) $@ |
| 198 | +cmd_solink_module = $(LINK.$(TOOLSET)) -o $@ -shared $(GYP_LDFLAGS) $(LDFLAGS.$(TOOLSET)) -Wl,-soname=$(@F) -Wl,--start-group $(filter-out FORCE_DO_CMD, $^) -Wl,--end-group $(LIBS) |
| 199 | + |
| 200 | + |
| 201 | +# Define an escape_quotes function to escape single quotes. |
| 202 | +# This allows us to handle quotes properly as long as we always use |
| 203 | +# use single quotes and escape_quotes. |
| 204 | +escape_quotes = $(subst ','\'',$(1)) |
| 205 | +# This comment is here just to include a ' to unconfuse syntax highlighting. |
| 206 | +# Define an escape_vars function to escape '$' variable syntax. |
| 207 | +# This allows us to read/write command lines with shell variables (e.g. |
| 208 | +# $LD_LIBRARY_PATH), without triggering make substitution. |
| 209 | +escape_vars = $(subst $$,$$$$,$(1)) |
| 210 | +# Helper that expands to a shell command to echo a string exactly as it is in |
| 211 | +# make. This uses printf instead of echo because printf's behaviour with respect |
| 212 | +# to escape sequences is more portable than echo's across different shells |
| 213 | +# (e.g., dash, bash). |
| 214 | +exact_echo = printf '%s\n' '$(call escape_quotes,$(1))' |
| 215 | + |
| 216 | +# Helper to compare the command we're about to run against the command |
| 217 | +# we logged the last time we ran the command. Produces an empty |
| 218 | +# string (false) when the commands match. |
| 219 | +# Tricky point: Make has no string-equality test function. |
| 220 | +# The kernel uses the following, but it seems like it would have false |
| 221 | +# positives, where one string reordered its arguments. |
| 222 | +# arg_check = $(strip $(filter-out $(cmd_$(1)), $(cmd_$@)) \ |
| 223 | +# $(filter-out $(cmd_$@), $(cmd_$(1)))) |
| 224 | +# We instead substitute each for the empty string into the other, and |
| 225 | +# say they're equal if both substitutions produce the empty string. |
| 226 | +# .d files contain ? instead of spaces, take that into account. |
| 227 | +command_changed = $(or $(subst $(cmd_$(1)),,$(cmd_$(call replace_spaces,$@))),\ |
| 228 | + $(subst $(cmd_$(call replace_spaces,$@)),,$(cmd_$(1)))) |
| 229 | + |
| 230 | +# Helper that is non-empty when a prerequisite changes. |
| 231 | +# Normally make does this implicitly, but we force rules to always run |
| 232 | +# so we can check their command lines. |
| 233 | +# $? -- new prerequisites |
| 234 | +# $| -- order-only dependencies |
| 235 | +prereq_changed = $(filter-out FORCE_DO_CMD,$(filter-out $|,$?)) |
| 236 | + |
| 237 | +# Helper that executes all postbuilds until one fails. |
| 238 | +define do_postbuilds |
| 239 | + @E=0;\ |
| 240 | + for p in $(POSTBUILDS); do\ |
| 241 | + eval $$p;\ |
| 242 | + E=$$?;\ |
| 243 | + if [ $$E -ne 0 ]; then\ |
| 244 | + break;\ |
| 245 | + fi;\ |
| 246 | + done;\ |
| 247 | + if [ $$E -ne 0 ]; then\ |
| 248 | + rm -rf "$@";\ |
| 249 | + exit $$E;\ |
| 250 | + fi |
| 251 | +endef |
| 252 | + |
| 253 | +# do_cmd: run a command via the above cmd_foo names, if necessary. |
| 254 | +# Should always run for a given target to handle command-line changes. |
| 255 | +# Second argument, if non-zero, makes it do asm/C/C++ dependency munging. |
| 256 | +# Third argument, if non-zero, makes it do POSTBUILDS processing. |
| 257 | +# Note: We intentionally do NOT call dirx for depfile, since it contains ? for |
| 258 | +# spaces already and dirx strips the ? characters. |
| 259 | +define do_cmd |
| 260 | +$(if $(or $(command_changed),$(prereq_changed)), |
| 261 | + @$(call exact_echo, $($(quiet)cmd_$(1))) |
| 262 | + @mkdir -p "$(call dirx,$@)" "$(dir $(depfile))" |
| 263 | + $(if $(findstring flock,$(word 1,$(cmd_$1))), |
| 264 | + @$(cmd_$(1)) |
| 265 | + @echo " $(quiet_cmd_$(1)): Finished", |
| 266 | + @$(cmd_$(1)) |
| 267 | + ) |
| 268 | + @$(call exact_echo,$(call escape_vars,cmd_$(call replace_spaces,$@) := $(cmd_$(1)))) > $(depfile) |
| 269 | + @$(if $(2),$(fixup_dep)) |
| 270 | + $(if $(and $(3), $(POSTBUILDS)), |
| 271 | + $(call do_postbuilds) |
| 272 | + ) |
| 273 | +) |
| 274 | +endef |
| 275 | + |
| 276 | +# Declare the "all" target first so it is the default, |
| 277 | +# even though we don't have the deps yet. |
| 278 | +.PHONY: all |
| 279 | +all: |
| 280 | + |
| 281 | +# make looks for ways to re-generate included makefiles, but in our case, we |
| 282 | +# don't have a direct way. Explicitly telling make that it has nothing to do |
| 283 | +# for them makes it go faster. |
| 284 | +%.d: ; |
| 285 | + |
| 286 | +# Use FORCE_DO_CMD to force a target to run. Should be coupled with |
| 287 | +# do_cmd. |
| 288 | +.PHONY: FORCE_DO_CMD |
| 289 | +FORCE_DO_CMD: |
| 290 | + |
| 291 | +TOOLSET := target |
| 292 | +# Suffix rules, putting all outputs into $(obj). |
| 293 | +$(obj).$(TOOLSET)/%.o: $(srcdir)/%.c FORCE_DO_CMD |
| 294 | + @$(call do_cmd,cc,1) |
| 295 | +$(obj).$(TOOLSET)/%.o: $(srcdir)/%.cc FORCE_DO_CMD |
| 296 | + @$(call do_cmd,cxx,1) |
| 297 | +$(obj).$(TOOLSET)/%.o: $(srcdir)/%.cpp FORCE_DO_CMD |
| 298 | + @$(call do_cmd,cxx,1) |
| 299 | +$(obj).$(TOOLSET)/%.o: $(srcdir)/%.cxx FORCE_DO_CMD |
| 300 | + @$(call do_cmd,cxx,1) |
| 301 | +$(obj).$(TOOLSET)/%.o: $(srcdir)/%.s FORCE_DO_CMD |
| 302 | + @$(call do_cmd,cc,1) |
| 303 | +$(obj).$(TOOLSET)/%.o: $(srcdir)/%.S FORCE_DO_CMD |
| 304 | + @$(call do_cmd,cc,1) |
| 305 | + |
| 306 | +# Try building from generated source, too. |
| 307 | +$(obj).$(TOOLSET)/%.o: $(obj).$(TOOLSET)/%.c FORCE_DO_CMD |
| 308 | + @$(call do_cmd,cc,1) |
| 309 | +$(obj).$(TOOLSET)/%.o: $(obj).$(TOOLSET)/%.cc FORCE_DO_CMD |
| 310 | + @$(call do_cmd,cxx,1) |
| 311 | +$(obj).$(TOOLSET)/%.o: $(obj).$(TOOLSET)/%.cpp FORCE_DO_CMD |
| 312 | + @$(call do_cmd,cxx,1) |
| 313 | +$(obj).$(TOOLSET)/%.o: $(obj).$(TOOLSET)/%.cxx FORCE_DO_CMD |
| 314 | + @$(call do_cmd,cxx,1) |
| 315 | +$(obj).$(TOOLSET)/%.o: $(obj).$(TOOLSET)/%.s FORCE_DO_CMD |
| 316 | + @$(call do_cmd,cc,1) |
| 317 | +$(obj).$(TOOLSET)/%.o: $(obj).$(TOOLSET)/%.S FORCE_DO_CMD |
| 318 | + @$(call do_cmd,cc,1) |
| 319 | + |
| 320 | +$(obj).$(TOOLSET)/%.o: $(obj)/%.c FORCE_DO_CMD |
| 321 | + @$(call do_cmd,cc,1) |
| 322 | +$(obj).$(TOOLSET)/%.o: $(obj)/%.cc FORCE_DO_CMD |
| 323 | + @$(call do_cmd,cxx,1) |
| 324 | +$(obj).$(TOOLSET)/%.o: $(obj)/%.cpp FORCE_DO_CMD |
| 325 | + @$(call do_cmd,cxx,1) |
| 326 | +$(obj).$(TOOLSET)/%.o: $(obj)/%.cxx FORCE_DO_CMD |
| 327 | + @$(call do_cmd,cxx,1) |
| 328 | +$(obj).$(TOOLSET)/%.o: $(obj)/%.s FORCE_DO_CMD |
| 329 | + @$(call do_cmd,cc,1) |
| 330 | +$(obj).$(TOOLSET)/%.o: $(obj)/%.S FORCE_DO_CMD |
| 331 | + @$(call do_cmd,cc,1) |
| 332 | + |
| 333 | + |
| 334 | +ifeq ($(strip $(foreach prefix,$(NO_LOAD),\ |
| 335 | + $(findstring $(join ^,$(prefix)),\ |
| 336 | + $(join ^,ffi_test_library.target.mk)))),) |
| 337 | + include ffi_test_library.target.mk |
| 338 | +endif |
| 339 | + |
| 340 | +quiet_cmd_regen_makefile = ACTION Regenerating $@ |
| 341 | +cmd_regen_makefile = cd $(srcdir); /home/renegade/dev/node/deps/npm/node_modules/node-gyp/gyp/gyp_main.py -fmake --ignore-environment "-Dlibrary=shared_library" "-Dvisibility=default" "-Dnode_root_dir=/home/renegade/dev/node/out/Release/addons_headers" "-Dnode_gyp_dir=/home/renegade/dev/node/deps/npm/node_modules/node-gyp" "-Dnode_lib_file=/home/renegade/dev/node/out/Release/addons_headers/$(Configuration)/node.lib" "-Dmodule_root_dir=/home/renegade/dev/node/test/ffi/fixture_library" "-Dnode_engine=v8" "--depth=." "-Goutput_dir=." "--generator-output=build" -I/home/renegade/dev/node/test/ffi/fixture_library/build/config.gypi -I/home/renegade/dev/node/deps/npm/node_modules/node-gyp/addon.gypi -I/home/renegade/dev/node/out/Release/addons_headers/include/node/common.gypi "--toplevel-dir=." binding.gyp |
| 342 | +Makefile: $(srcdir)/../../../deps/npm/node_modules/node-gyp/addon.gypi $(srcdir)/../../../out/Release/addons_headers/include/node/common.gypi $(srcdir)/binding.gyp $(srcdir)/build/config.gypi |
| 343 | + $(call do_cmd,regen_makefile) |
| 344 | + |
| 345 | +# "all" is a concatenation of the "all" targets from all the included |
| 346 | +# sub-makefiles. This is just here to clarify. |
| 347 | +all: |
| 348 | + |
| 349 | +# Add in dependency-tracking rules. $(all_deps) is the list of every single |
| 350 | +# target in our tree. Only consider the ones with .d (dependency) info: |
| 351 | +d_files := $(wildcard $(foreach f,$(all_deps),$(depsdir)/$(f).d)) |
| 352 | +ifneq ($(d_files),) |
| 353 | + include $(d_files) |
| 354 | +endif |
0 commit comments