Deterministic builds with clang and lld describes several degrees of build determinism. Here is the description of local determinism:
Like incremental basic determinism, but builds are also independent of the name of the build directory. Builds of the same source code on the same machine produce exactly the same output every time, independent of the location of the source checkout directory or the build directory.
This is often a stretch goal for a build system. For example, many autotools builds use absolute paths. CMake's Ninja generator uses absolute paths (https://gitlab.kitware.com/cmake/cmake/-/issues/13894).
Options discouraged by the post -fdebug-prefix-map
,
-ffile-prefix-map
, and -fmacro-prefix-map
are
useful for such build systems.
-fdebug-compilation-dir=
This option overrides the compilation directory in DWARF.
-fdebug-compilation-dir=.
is commonly used to set the
compilation directory to the special directory .
. When
input files are specified as relative paths,
-fdebug-compilation-dir=.
suffices.
1 | % clang -c -g -fdebug-compilation-dir=. ../src/a.c |
The environment variable PWD
, if exists and is an
absolute path, is used by GCC and Clang as a fallback when
-fdebug-compilation-dir=
is unspecified. Therefore, we can
invoke PWD=/proc/self/cwd clang ...
.
-fdebug-prefix-map=
See Debug info path remapping for the original GCC patch. See https://gcc.gnu.org/onlinedocs/gcc/Debugging-Options.html for the documentation.
In Clang, the implementation requires changes to Clang CodeGen (mainly implemented by Support Debug Info path remapping), assembler (https://reviews.llvm.org/D48988 https://reviews.llvm.org/D48989).
Generated debug information for assembly source
When an assembler source file has no debug
information/.file
directive, GNU assembler synthesizes a
DW_TAG_compile_unit
entry. This entry is generated when an
assembler source file has at least one .loc
directive
without a .debug_info
section. The entry is mainly used to
provide address ranges for the file.
1 | % cat a.s |
LLVM integrated assembler has a similar behavior. See https://github.com/llvm/llvm-project/issues/37398 for the original feature request.
https://github.com/llvm/llvm-project/issues/56609
reported that -fdebug-prefix-map
did not work for generated
DWARF v5 debug information. There were two issues.
DW_AT_comp_dir
was not mapped. Implemented by https://reviews.llvm.org/D131749.DW_TAG_compile_unit
'sDW_AT_name
(also sued byDW_TAG_label
'sDW_AT_decl_file
) was not mapped. Implemented by https://reviews.llvm.org/D131848.