8288399: MacOS debug symbol files not always deterministic in reproducible builds

Backport-of: 6633855ca78ff0f5f1213a2a1424436678a30e55
diff --git a/make/autoconf/flags-cflags.m4 b/make/autoconf/flags-cflags.m4
index e6629e9..dbde788 100644
--- a/make/autoconf/flags-cflags.m4
+++ b/make/autoconf/flags-cflags.m4
@@ -116,6 +116,18 @@
     CFLAGS_DEBUG_SYMBOLS="-g"
     ASFLAGS_DEBUG_SYMBOLS="-g"
   elif test "x$TOOLCHAIN_TYPE" = xclang; then
+    if test "x$ALLOW_ABSOLUTE_PATHS_IN_OUTPUT" = "xfalse"; then
+      # Check if compiler supports -fdebug-prefix-map. If so, use that to make
+      # the debug symbol paths resolve to paths relative to the workspace root.
+      workspace_root_trailing_slash="${WORKSPACE_ROOT%/}/"
+      DEBUG_PREFIX_CFLAGS="-fdebug-prefix-map=${workspace_root_trailing_slash}="
+      FLAGS_COMPILER_CHECK_ARGUMENTS(ARGUMENT: [${DEBUG_PREFIX_CFLAGS}],
+        IF_FALSE: [
+            DEBUG_PREFIX_CFLAGS=
+        ]
+      )
+    fi
+
     CFLAGS_DEBUG_SYMBOLS="-g"
     ASFLAGS_DEBUG_SYMBOLS="-g"
   elif test "x$TOOLCHAIN_TYPE" = xxlc; then
diff --git a/make/common/NativeCompilation.gmk b/make/common/NativeCompilation.gmk
index e5c4860..3da95bf 100644
--- a/make/common/NativeCompilation.gmk
+++ b/make/common/NativeCompilation.gmk
@@ -359,9 +359,9 @@
       $1_FLAGS := $(BASIC_ASFLAGS) $$($1_BASE_ASFLAGS)
       $1_COMPILER := $(AS)
 
-      # gcc assembly files must contain an appropriate relative .file
+      # gcc or clang assembly files must contain an appropriate relative .file
       # path for reproducible builds.
-      ifeq ($(TOOLCHAIN_TYPE), gcc)
+      ifneq ($(findstring $(TOOLCHAIN_TYPE), gcc clang), )
         # If no absolute paths allowed, work out relative source file path
         # for assembly .file substitution, otherwise use full file path
         ifeq ($(ALLOW_ABSOLUTE_PATHS_IN_OUTPUT), false)
@@ -403,8 +403,9 @@
     $1_OBJ_DEPS := $$($1_SRC_FILE) $$($$($1_BASE)_COMPILE_VARDEPS_FILE) \
         $$($$($1_BASE)_EXTRA_DEPS) $$($1_VARDEPS_FILE)
     $1_COMPILE_OPTIONS := $$($1_FLAGS) $(CC_OUT_OPTION)$$($1_OBJ) $$($1_SRC_FILE)
-    # For reproducible builds with gcc ensure random symbol generation is seeded deterministically
-    ifeq ($(TOOLCHAIN_TYPE), gcc)
+    # For reproducible builds with gcc and clang ensure random symbol generation is
+    # seeded deterministically
+    ifneq ($(findstring $(TOOLCHAIN_TYPE), gcc clang), )
        ifeq ($$(ENABLE_REPRODUCIBLE_BUILD), true)
          $1_COMPILE_OPTIONS += -frandom-seed="$$($1_FILENAME)"
        endif