diff mbox series

[v2] kbuild: make all file references relative to source root

Message ID 20250315-kbuild-prefix-map-v2-1-00e1983b2a23@weissschuh.net (mailing list archive)
State New
Headers show
Series [v2] kbuild: make all file references relative to source root | expand

Commit Message

Thomas Weißschuh March 15, 2025, 1:20 p.m. UTC
-fmacro-prefix-map only affects __FILE__ and __BASE_FILE__.
Other references, for example in debug information, are not affected.
This makes handling of file references in the compiler outputs harder to
use and creates problems for reproducible builds.

Switch to -ffile-prefix map which affects all references.

Also drop the documentation section advising manual specification of
-fdebug-prefix-map for reproducible builds, as it is not necessary
anymore.

Suggested-by: Ben Hutchings <ben@decadent.org.uk>
Link: https://lore.kernel.org/lkml/c49cc967294f9a3a4a34f69b6a8727a6d3959ed8.camel@decadent.org.uk/
Acked-by: Borislav Petkov (AMD) <bp@alien8.de> # arch/x86/
Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
---
Changes in v2:
- Pick up Ack from Borislav
- Merge all changes into single patch
- Also drop link to KCFLAGS from docs
- Link to v1: https://lore.kernel.org/r/20250313-kbuild-prefix-map-v1-0-38cea8448c5f@weissschuh.net
---
 Documentation/kbuild/reproducible-builds.rst | 17 -----------------
 Makefile                                     |  2 +-
 arch/x86/boot/Makefile                       |  2 +-
 arch/x86/boot/compressed/Makefile            |  2 +-
 4 files changed, 3 insertions(+), 20 deletions(-)


---
base-commit: a57512d6cd88eba04cdc1fb83832c00d248bd0d1
change-id: 20250312-kbuild-prefix-map-5ae76c209e7a

Best regards,

Comments

Masahiro Yamada March 16, 2025, 2:06 a.m. UTC | #1
On Sat, Mar 15, 2025 at 10:20 PM Thomas Weißschuh <linux@weissschuh.net> wrote:
>
> -fmacro-prefix-map only affects __FILE__ and __BASE_FILE__.
> Other references, for example in debug information, are not affected.
> This makes handling of file references in the compiler outputs harder to
> use and creates problems for reproducible builds.
>
> Switch to -ffile-prefix map which affects all references.
>
> Also drop the documentation section advising manual specification of
> -fdebug-prefix-map for reproducible builds, as it is not necessary
> anymore.
>
> Suggested-by: Ben Hutchings <ben@decadent.org.uk>
> Link: https://lore.kernel.org/lkml/c49cc967294f9a3a4a34f69b6a8727a6d3959ed8.camel@decadent.org.uk/
> Acked-by: Borislav Petkov (AMD) <bp@alien8.de> # arch/x86/
> Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
> ---
> Changes in v2:
> - Pick up Ack from Borislav
> - Merge all changes into single patch
> - Also drop link to KCFLAGS from docs
> - Link to v1: https://lore.kernel.org/r/20250313-kbuild-prefix-map-v1-0-38cea8448c5f@weissschuh.net
> ---

Applied to linux-kbuild. Thanks.


--
Best Regards
Masahiro Yamada
Ben Hutchings March 16, 2025, 7:08 p.m. UTC | #2
On Sat, 2025-03-15 at 14:20 +0100, Thomas Weißschuh wrote:
> -fmacro-prefix-map only affects __FILE__ and __BASE_FILE__.
> Other references, for example in debug information, are not affected.
> This makes handling of file references in the compiler outputs harder to
> use and creates problems for reproducible builds.
> 
> Switch to -ffile-prefix map which affects all references.

This appears to cover all the C sources, but not quite all the assembly
sources:

[...]

> --- a/arch/x86/boot/Makefile
> +++ b/arch/x86/boot/Makefile
> @@ -54,7 +54,7 @@ targets += cpustr.h
>  
>  KBUILD_CFLAGS	:= $(REALMODE_CFLAGS) -D_SETUP
>  KBUILD_AFLAGS	:= $(KBUILD_CFLAGS) -D__ASSEMBLY__
> -KBUILD_CFLAGS	+= $(call cc-option,-fmacro-prefix-map=$(srctree)/=)
> +KBUILD_CFLAGS	+= $(call cc-option,-ffile-prefix-map=$(srctree)/=)
[...]

I think this addition to KBUILD_CFLAGS needs to be done before the
assignment to KBUILD_AFLAGS.

Also, in some older versions of gcc the -ffile-prefix-map option didn't
affect assembly sources - gas only understands --debug-prefix-map and
gcc did not do the necessary conversion.  But this works properly since
at least gcc 12, so I wouldn't worry too much about it.

Ben.
Nathan Chancellor March 17, 2025, 5:17 p.m. UTC | #3
On Sun, Mar 16, 2025 at 08:08:37PM +0100, Ben Hutchings wrote:
> On Sat, 2025-03-15 at 14:20 +0100, Thomas Weißschuh wrote:
> > -fmacro-prefix-map only affects __FILE__ and __BASE_FILE__.
> > Other references, for example in debug information, are not affected.
> > This makes handling of file references in the compiler outputs harder to
> > use and creates problems for reproducible builds.
> > 
> > Switch to -ffile-prefix map which affects all references.
> 
> This appears to cover all the C sources, but not quite all the assembly
> sources:
> 
> [...]
> 
> > --- a/arch/x86/boot/Makefile
> > +++ b/arch/x86/boot/Makefile
> > @@ -54,7 +54,7 @@ targets += cpustr.h
> >  
> >  KBUILD_CFLAGS	:= $(REALMODE_CFLAGS) -D_SETUP
> >  KBUILD_AFLAGS	:= $(KBUILD_CFLAGS) -D__ASSEMBLY__
> > -KBUILD_CFLAGS	+= $(call cc-option,-fmacro-prefix-map=$(srctree)/=)
> > +KBUILD_CFLAGS	+= $(call cc-option,-ffile-prefix-map=$(srctree)/=)
> [...]
> 
> I think this addition to KBUILD_CFLAGS needs to be done before the
> assignment to KBUILD_AFLAGS.

This probably belongs in KBUILD_CPPFLAGS then, similar to clang's
'--target' flag and other options that we always want invoked?

Cheers,
Nathan
diff mbox series

Patch

diff --git a/Documentation/kbuild/reproducible-builds.rst b/Documentation/kbuild/reproducible-builds.rst
index f2dcc39044e66ddd165646e0b51ccb0209aca7dd..a7762486c93fcd3eba08b836bed622a41e829e41 100644
--- a/Documentation/kbuild/reproducible-builds.rst
+++ b/Documentation/kbuild/reproducible-builds.rst
@@ -46,21 +46,6 @@  The kernel embeds the building user and host names in
 `KBUILD_BUILD_USER and KBUILD_BUILD_HOST`_ variables.  If you are
 building from a git commit, you could use its committer address.
 
-Absolute filenames
-------------------
-
-When the kernel is built out-of-tree, debug information may include
-absolute filenames for the source files.  This must be overridden by
-including the ``-fdebug-prefix-map`` option in the `KCFLAGS`_ variable.
-
-Depending on the compiler used, the ``__FILE__`` macro may also expand
-to an absolute filename in an out-of-tree build.  Kbuild automatically
-uses the ``-fmacro-prefix-map`` option to prevent this, if it is
-supported.
-
-The Reproducible Builds web site has more information about these
-`prefix-map options`_.
-
 Generated files in source packages
 ----------------------------------
 
@@ -131,7 +116,5 @@  See ``scripts/setlocalversion`` for details.
 
 .. _KBUILD_BUILD_TIMESTAMP: kbuild.html#kbuild-build-timestamp
 .. _KBUILD_BUILD_USER and KBUILD_BUILD_HOST: kbuild.html#kbuild-build-user-kbuild-build-host
-.. _KCFLAGS: kbuild.html#kcflags
-.. _prefix-map options: https://reproducible-builds.org/docs/build-path/
 .. _Reproducible Builds project: https://reproducible-builds.org/
 .. _SOURCE_DATE_EPOCH: https://reproducible-builds.org/docs/source-date-epoch/
diff --git a/Makefile b/Makefile
index 5c333682dc9142b1aacfe454a5c77f5923554b7d..4f920187cee658ae4d1b807fca365f6994274828 100644
--- a/Makefile
+++ b/Makefile
@@ -1067,7 +1067,7 @@  endif
 
 # change __FILE__ to the relative path to the source directory
 ifdef building_out_of_srctree
-KBUILD_CPPFLAGS += $(call cc-option,-fmacro-prefix-map=$(srcroot)/=)
+KBUILD_CPPFLAGS += $(call cc-option,-ffile-prefix-map=$(srcroot)/=)
 KBUILD_RUSTFLAGS += --remap-path-prefix=$(srcroot)/=
 endif
 
diff --git a/arch/x86/boot/Makefile b/arch/x86/boot/Makefile
index 9cc0ff6e9067d574488a35573eff4d0f8449e398..f500f82864aae80deb74faa3df9a8b6333d6c4ca 100644
--- a/arch/x86/boot/Makefile
+++ b/arch/x86/boot/Makefile
@@ -54,7 +54,7 @@  targets += cpustr.h
 
 KBUILD_CFLAGS	:= $(REALMODE_CFLAGS) -D_SETUP
 KBUILD_AFLAGS	:= $(KBUILD_CFLAGS) -D__ASSEMBLY__
-KBUILD_CFLAGS	+= $(call cc-option,-fmacro-prefix-map=$(srctree)/=)
+KBUILD_CFLAGS	+= $(call cc-option,-ffile-prefix-map=$(srctree)/=)
 KBUILD_CFLAGS	+= -fno-asynchronous-unwind-tables
 KBUILD_CFLAGS	+= $(CONFIG_CC_IMPLICIT_FALLTHROUGH)
 
diff --git a/arch/x86/boot/compressed/Makefile b/arch/x86/boot/compressed/Makefile
index 5edee7a9786c67e13c295473751b82387bcbd67e..ad324978b2e5b1b6f8be82647769c99db8257ac7 100644
--- a/arch/x86/boot/compressed/Makefile
+++ b/arch/x86/boot/compressed/Makefile
@@ -38,7 +38,7 @@  KBUILD_CFLAGS += -fno-stack-protector
 KBUILD_CFLAGS += $(call cc-disable-warning, address-of-packed-member)
 KBUILD_CFLAGS += $(call cc-disable-warning, gnu)
 KBUILD_CFLAGS += -Wno-pointer-sign
-KBUILD_CFLAGS += $(call cc-option,-fmacro-prefix-map=$(srctree)/=)
+KBUILD_CFLAGS += $(call cc-option,-ffile-prefix-map=$(srctree)/=)
 KBUILD_CFLAGS += -fno-asynchronous-unwind-tables
 KBUILD_CFLAGS += -D__DISABLE_EXPORTS
 # Disable relocation relaxation in case the link is not PIE.