pw_build: Use -ffile-prefix-map by default

By default, Clang and GCC use file paths directly as provided for debug
symbols and macros like __FILE__. This means that binaries produced on
different machines can be different and may include undesireable info
like usernames.

Clang and GCC provide the -ffile-prefix-map option to strip non-portable
prefixes from filenames. Add a config that applies -ffile-prefix-map to
Pigweed builds by default.

Change-Id: Icd0fc11b70130b1b0e3ec193b1d245c18a2112b8
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/52340
Reviewed-by: Rob Mohr <mohrr@google.com>
Commit-Queue: Wyatt Hepler <hepler@google.com>
diff --git a/pw_build/BUILD.gn b/pw_build/BUILD.gn
index 02105cf..39eb992 100644
--- a/pw_build/BUILD.gn
+++ b/pw_build/BUILD.gn
@@ -122,6 +122,17 @@
   ]
 }
 
+# Removes system-dependent prefixes from macros like __FILE__ and debug symbols.
+config("relative_paths") {
+  cflags = [
+    # Remove absolute paths to the repo root.
+    "-ffile-prefix-map=" + rebase_path("//") + "=",
+
+    # Remove relative paths from the build directory to the source tree.
+    "-ffile-prefix-map=" + rebase_path("//", root_build_dir) + "=",
+  ]
+}
+
 # This group is linked into all pw_executable, pw_static_library, and
 # pw_shared_library targets. This makes it possible to ensure symbols are
 # defined without a dependency on them.
diff --git a/pw_build/defaults.gni b/pw_build/defaults.gni
index 6d341e7..80a28e2 100644
--- a/pw_build/defaults.gni
+++ b/pw_build/defaults.gni
@@ -39,6 +39,7 @@
     "$dir_pw_build:reduced_size",
     "$dir_pw_build:strict_warnings",
     "$dir_pw_build:cpp17",
+    "$dir_pw_build:relative_paths",
   ]
   public_deps += [ "$dir_pw_polyfill:overrides" ]
 }
diff --git a/pw_build/docs.rst b/pw_build/docs.rst
index 9263d06..f26fd82 100644
--- a/pw_build/docs.rst
+++ b/pw_build/docs.rst
@@ -49,10 +49,11 @@
 
 Pigweed defines wrappers around the four basic GN binary types ``source_set``,
 ``executable``, ``static_library``, and ``shared_library``. These wrappers apply
-default arguments to each target as specified in the ``default_configs`` and
-``default_public_deps`` build args. Additionally, they allow defaults to be
-removed on a per-target basis using ``remove_configs`` and
-``remove_public_deps`` variables, respectively.
+default arguments to each target, as defined in ``pw_build/default.gni``.
+Arguments may be added or removed globally using the ``default_configs``,
+``default_public_deps``, and ``remove_default_configs`` build args.
+Additionally, arguments may be removed on a per-target basis with the
+``remove_configs`` and ``remove_public_deps`` variables.
 
 The ``pw_executable`` template provides additional functionality around building
 complete binaries. As Pigweed is a collection of libraries, it does not know how