@@ -204,6 +204,68 @@ DEPENDENCY_TARGET_SED_PATTERN := \
204204 -e 's/$$$$/ :/' \
205205 #
206206
207+ ################################################################################
208+ # When absolute paths are not allowed in the output, and the compiler does not
209+ # support any options to avoid it, we need to rewrite compile commands to use
210+ # relative paths. By doing this, the __FILE__ macro will resolve to relative
211+ # paths. The relevant input paths on the command line are the -I flags and the
212+ # path to the source file itself.
213+ #
214+ # The macro MakeCommandRelative is used to rewrite the command line like this:
215+ # 'CD $(WORKSPACE_ROOT) && <cmd>'
216+ # and changes all paths in cmd to be relative to the workspace root. This only
217+ # works properly if the build dir is inside the workspace root. If it's not,
218+ # relative paths are still calculated, but depending on the distance between the
219+ # dirs, paths in the build dir may end up as essentially absolute anyway.
220+ #
221+ # The fix-deps-file macro is used to adjust the contents of the generated make
222+ # dependency files to contain paths compatible with make.
223+ #
224+ ifeq ($(ALLOW_ABSOLUTE_PATHS_IN_OUTPUT)-$(FILE_MACRO_CFLAGS), false-)
225+ # Need to handle -I flags as both '-Ifoo' and '-I foo'.
226+ MakeCommandRelative = \
227+ $(CD) $(WORKSPACE_ROOT) && \
228+ $(foreach o, $1, \
229+ $(if $(filter $(WORKSPACE_ROOT)/% $(OUTPUTDIR)/%, $o), \
230+ $(call RelativePath, $o, $(WORKSPACE_ROOT)) \
231+ , \
232+ $(if $(filter -I$(WORKSPACE_ROOT)/%, $o), \
233+ -I$(call RelativePath, $(patsubst -I%, %, $o), $(WORKSPACE_ROOT)) \
234+ , \
235+ $o \
236+ ) \
237+ ) \
238+ )
239+
240+ # When compiling with relative paths, the deps file comes out with relative
241+ # paths.
242+ ifeq ($(TOOLCHAIN_TYPE), solstudio)
243+ define fix-deps-file
244+ $(SED) -e 's|\./|$(WORKSPACE_ROOT)/|g' $1.tmp > $1
245+ endef
246+ else
247+ define fix-deps-file
248+ $(SED) -e 's|^\([ ]*\)|\1$(WORKSPACE_ROOT)|' $1.tmp > $1
249+ endef
250+ endif
251+ else
252+ # By default the MakeCommandRelative macro does nothing.
253+ MakeCommandRelative = $1
254+
255+ # Even with absolute paths on the command line, the Solaris studio compiler
256+ # doesn't output the full path to the object file in the generated deps files.
257+ # For other toolchains, no adjustment is needed.
258+ ifeq ($(TOOLCHAIN_TYPE), solstudio)
259+ define fix-deps-file
260+ $(SED) 's|^$$(@F):|$$@:|' $1.tmp > $1
261+ endef
262+ else
263+ define fix-deps-file
264+ $(MV) $1.tmp $1
265+ endef
266+ endif
267+ endif
268+
207269################################################################################
208270# Create the recipe needed to compile a single native source file.
209271#
@@ -214,7 +276,6 @@ DEPENDENCY_TARGET_SED_PATTERN := \
214276# Remaining parameters are named arguments:
215277# FILE - The full path of the source file to compiler
216278# BASE - The name of the rule for the entire binary to build ($1)
217- # DISABLE_THIS_FILE_DEFINE - Set to true to disable the THIS_FILE define.
218279#
219280SetupCompileNativeFile = $(NamedParamsMacroTemplate)
220281define SetupCompileNativeFileBody
@@ -236,12 +297,6 @@ define SetupCompileNativeFileBody
236297 # This is the definite source file to use for $1_FILENAME.
237298 $1_SRC_FILE := $$($1_FILE)
238299
239- ifneq ($$($1_DEFINE_THIS_FILE), false)
240- ifneq ($$($$($1_BASE)_DEFINE_THIS_FILE), false)
241- $1_THIS_FILE = -DTHIS_FILE='"$$($1_FILENAME)"'
242- endif
243- endif
244-
245300 ifeq ($$($1_OPTIMIZATION), )
246301 $1_OPT_CFLAGS := $$($$($1_BASE)_OPT_CFLAGS)
247302 $1_OPT_CXXFLAGS := $$($$($1_BASE)_OPT_CXXFLAGS)
@@ -284,13 +339,13 @@ define SetupCompileNativeFileBody
284339 ifneq ($$(filter %.c, $$($1_FILENAME)), )
285340 # Compile as a C file
286341 $1_FLAGS := $(CFLAGS_CCACHE) $$($1_USE_PCH_FLAGS) $$($1_BASE_CFLAGS) \
287- $$($1_OPT_CFLAGS) $$($1_CFLAGS) $$($1_THIS_FILE) -c
342+ $$($1_OPT_CFLAGS) $$($1_CFLAGS) -c
288343 $1_COMPILER := $$($$($1_BASE)_CC)
289344 $1_DEP_FLAG := $(C_FLAG_DEPS)
290345 else ifneq ($$(filter %.m, $$($1_FILENAME)), )
291346 # Compile as an Objective-C file
292347 $1_FLAGS := -x objective-c $(CFLAGS_CCACHE) $$($1_USE_PCH_FLAGS) \
293- $$($1_BASE_CFLAGS) $$($1_OPT_CFLAGS) $$($1_CFLAGS) $$($1_THIS_FILE) -c
348+ $$($1_BASE_CFLAGS) $$($1_OPT_CFLAGS) $$($1_CFLAGS) -c
294349 $1_COMPILER := $$($$($1_BASE)_CC)
295350 $1_DEP_FLAG := $(C_FLAG_DEPS)
296351 else ifneq ($$(filter %.s %.S, $$($1_FILENAME)), )
@@ -301,7 +356,7 @@ define SetupCompileNativeFileBody
301356 else ifneq ($$(filter %.cpp %.cc %.mm, $$($1_FILENAME)), )
302357 # Compile as a C++ or Objective-C++ file
303358 $1_FLAGS := $(CFLAGS_CCACHE) $$($1_USE_PCH_FLAGS) $$($1_BASE_CXXFLAGS) \
304- $$($1_OPT_CXXFLAGS) $$($1_CXXFLAGS) $$($1_THIS_FILE) -c
359+ $$($1_OPT_CXXFLAGS) $$($1_CXXFLAGS) -c
305360 $1_COMPILER := $$($$($1_BASE)_CXX)
306361 $1_DEP_FLAG := $(CXX_FLAG_DEPS)
307362 else
@@ -341,21 +396,17 @@ define SetupCompileNativeFileBody
341396 $$(call LogInfo, Compiling $$($1_FILENAME) (for $$($$($1_BASE)_BASENAME)))
342397 $$(call MakeDir, $$(@D))
343398 ifneq ($(TOOLCHAIN_TYPE), microsoft)
344- ifeq ($(TOOLCHAIN_TYPE)$$(filter %.s, $$($1_FILENAME)), solstudio)
345- # The Solaris studio compiler doesn't output the full path to the
346- # object file in the generated deps files. Fixing it with sed. If
347- # compiling assembly, don't try this.
348- $$(call ExecuteWithLog, $$@, \
349- $$($1_COMPILER) $$($1_DEP_FLAG) $$($1_DEPS_FILE).tmp $$($1_COMPILE_OPTIONS))
350- $(SED) 's|^$$(@F):|$$@:|' $$($1_DEPS_FILE).tmp > $$($1_DEPS_FILE)
351- else
352- $$(call ExecuteWithLog, $$@, \
353- $$($1_COMPILER) $$($1_DEP_FLAG) $$($1_DEPS_FILE) $$($1_COMPILE_OPTIONS))
354- endif
355- # Create a dependency target file from the dependency file.
356- # Solution suggested by http://make.mad-scientist.net/papers/advanced-auto-dependency-generation/
399+ $$(call ExecuteWithLog, $$@, $$(call MakeCommandRelative, \
400+ $$($1_COMPILER) $$($1_DEP_FLAG) \
401+ $$(addsuffix .tmp, $$($1_DEPS_FILE)) \
402+ $$($1_COMPILE_OPTIONS)))
357403 ifneq ($$($1_DEPS_FILE), )
358- $(SED) $(DEPENDENCY_TARGET_SED_PATTERN) $$($1_DEPS_FILE) > $$($1_DEPS_TARGETS_FILE)
404+ $$(call fix-deps-file, $$($1_DEPS_FILE))
405+ # Create a dependency target file from the dependency file.
406+ # Solution suggested by:
407+ # http://make.mad-scientist.net/papers/advanced-auto-dependency-generation/
408+ $(SED) $(DEPENDENCY_TARGET_SED_PATTERN) $$($1_DEPS_FILE) \
409+ > $$($1_DEPS_TARGETS_FILE)
359410 endif
360411 else
361412 # The Visual Studio compiler lacks a feature for generating make
@@ -365,8 +416,8 @@ define SetupCompileNativeFileBody
365416 # Keep as much as possible on one execution line for best performance
366417 # on Windows. No need to save exit code from compilation since
367418 # pipefail is always active on Windows.
368- $$(call ExecuteWithLog, $$@, \
369- $$($1_COMPILER) -showIncludes $$($1_COMPILE_OPTIONS)) \
419+ $$(call ExecuteWithLog, $$@, $$(call MakeCommandRelative, \
420+ $$($1_COMPILER) -showIncludes $$($1_COMPILE_OPTIONS))) \
370421 | $(TR) -d '\r' | $(GREP) -v -e "^Note: including file:" \
371422 -e "^$$($1_FILENAME)$$$$" || test "$$$$?" = "1" ; \
372423 $(ECHO) $$@: \\ > $$($1_DEPS_FILE) ; \
@@ -430,7 +481,6 @@ endef
430481# STRIPFLAGS Optionally change the flags given to the strip command
431482# PRECOMPILED_HEADER Header file to use as precompiled header
432483# PRECOMPILED_HEADER_EXCLUDE List of source files that should not use PCH
433- # DEFINE_THIS_FILE Set to false to not set the THIS_FILE preprocessor macro
434484#
435485# After being called, some variables are exported from this macro, all prefixed
436486# with parameter 1 followed by a '_':
@@ -734,7 +784,6 @@ define SetupNativeCompilationBody
734784 FILE := $$($1_GENERATED_PCH_SRC), \
735785 BASE := $1, \
736786 EXTRA_CXXFLAGS := -Fp$$($1_PCH_FILE) -Yc$$(notdir $$($1_PRECOMPILED_HEADER)), \
737- DEFINE_THIS_FILE := false, \
738787 ))
739788
740789 $1_USE_PCH_FLAGS := \
@@ -769,7 +818,8 @@ define SetupNativeCompilationBody
769818 $$($1_PCH_FILE): $$($1_PRECOMPILED_HEADER) $$($1_COMPILE_VARDEPS_FILE)
770819 $$(call LogInfo, Generating precompiled header)
771820 $$(call MakeDir, $$(@D))
772- $$(call ExecuteWithLog, $$@, $$($1_PCH_COMMAND) $$< -o $$@)
821+ $$(call ExecuteWithLog, $$@, $$(call MakeCommandRelative, \
822+ $$($1_PCH_COMMAND) $$< -o $$@))
773823 $(SED) $(DEPENDENCY_TARGET_SED_PATTERN) $$($1_PCH_DEPS_FILE) \
774824 > $$($1_PCH_DEPS_TARGETS_FILE)
775825
@@ -825,9 +875,9 @@ define SetupNativeCompilationBody
825875 $$($1_RES): $$($1_VERSIONINFO_RESOURCE) $$($1_RES_VARDEPS_FILE)
826876 $$(call LogInfo, Compiling resource $$(notdir $$($1_VERSIONINFO_RESOURCE)) (for $$($1_BASENAME)))
827877 $$(call MakeDir, $$(@D) $$($1_OBJECT_DIR))
828- $$(call ExecuteWithLog, $$@, \
878+ $$(call ExecuteWithLog, $$@, $$(call MakeCommandRelative, \
829879 $$($1_RC) $$($1_RC_FLAGS) $$($1_SYSROOT_CFLAGS) $(CC_OUT_OPTION)$$@ \
830- $$($1_VERSIONINFO_RESOURCE) 2>&1 )
880+ $$($1_VERSIONINFO_RESOURCE) 2>&1 ))
831881 # Windows RC compiler does not support -showIncludes, so we mis-use CL
832882 # for this. Filter out RC specific arguments that are unknown to CL.
833883 # For some unknown reason, in this case CL actually outputs the show
0 commit comments