Fix out-of-tree build of some tools
Subsystems (3)
cover Fix out-of-tree build of some tools
perf and rtla currently don't fully support out-of-tree builds, as they may still create files in their source directory. This series fixes all the instances of this problem that I have found.
Ben.
Ben Hutchings (3):
rtla: Fix output files in source tree
perf tools: Put Python egg info in output directory perf tools: Put Python bytecode in output directory
tools/perf/Makefile.perf | 9 ++++++++- tools/tracing/rtla/Makefile | 31 ++++++++++++++++++----------- tools/tracing/rtla/tests/timerlat.t | 4 ++-- 3 files changed, 29 insertions(+), 15 deletions(-)
PATCH 1/3 rtla: Fix output files in source tree
Some output files (src/timerlat.bpf.o, src/timerlat.skel.h, example/timerlat_bpf_action.o, tests/bpf/bpf_action_map.o) are currently generated in the source tree, preventing a fully out-of-tree build. To fix this:
- Add $(OUTPUT) to their filenames in the relevant Makefile rules, and create subdirectories as needed
- Add $(OUTPUT)src to the include path
- Add ${OUTPUT} to the BPF object filename in tests/timerlat.t
--- tools/tracing/rtla/Makefile | 31 ++++++++++++++++++----------- tools/tracing/rtla/tests/timerlat.t | 4 ++-- 2 files changed, 21 insertions(+), 14 deletions(-)
tools/tracing/rtla/Makefile +19 -12
@@ -66,30 +66,37 @@ ifeq ($(config),1) include Makefile.config endif +INCLUDES = -I$(OUTPUT)src + CFLAGS += $(INCLUDES) $(LIB_INCLUDES) export CFLAGS OUTPUT srctree ifeq ($(BUILD_BPF_SKEL),1) -src/timerlat.bpf.o: src/timerlat.bpf.c +$(OUTPUT)src/timerlat.bpf.o: src/timerlat.bpf.c + mkdir -p $(@D) $(QUIET_CLANG)$(CLANG) -g -O2 -target bpf -c $(filter %.c,$^) -o $@ -src/timerlat.skel.h: src/timerlat.bpf.o +$(OUTPUT)src/timerlat.skel.h: $(OUTPUT)src/timerlat.bpf.o + mkdir -p $(@D) $(QUIET_GENSKEL)$(SYSTEM_BPFTOOL) gen skeleton $< > $@ -example/timerlat_bpf_action.o: example/timerlat_bpf_action.c +$(OUTPUT)example/timerlat_bpf_action.o: example/timerlat_bpf_action.c + mkdir -p $(@D) $(QUIET_CLANG)$(CLANG) -g -O2 -target bpf -c $(filter %.c,$^) -o $@ -tests/bpf/bpf_action_map.o: tests/bpf/bpf_action_map.c +$(OUTPUT)tests/bpf/bpf_action_map.o: tests/bpf/bpf_action_map.c + mkdir -p $(@D) $(QUIET_CLANG)$(CLANG) -g -O2 -target bpf -c $(filter %.c,$^) -o $@ else -src/timerlat.skel.h: - $(Q)echo '/* BPF skeleton is disabled */' > src/timerlat.skel.h +$(OUTPUT)src/timerlat.skel.h: + mkdir -p $(@D) + $(Q)echo '/* BPF skeleton is disabled */' > $@ -example/timerlat_bpf_action.o: example/timerlat_bpf_action.c +$(OUTPUT)example/timerlat_bpf_action.o: example/timerlat_bpf_action.c $(Q)echo "BPF skeleton support is disabled, skipping example/timerlat_bpf_action.o" -tests/bpf/bpf_action_map.o: tests/bpf/bpf_action_map.c +$(OUTPUT)tests/bpf/bpf_action_map.o: tests/bpf/bpf_action_map.c $(Q)echo "BPF skeleton support is disabled, skipping tests/bpf/bpf_action_map.o" endif
@@ -103,7 +110,7 @@ static: $(RTLA_IN) rtla.%: fixdep FORCE make -f $(srctree)/tools/build/Makefile.build dir=. $@ -$(RTLA_IN): fixdep FORCE src/timerlat.skel.h +$(RTLA_IN): fixdep FORCE $(OUTPUT)src/timerlat.skel.h make $(build)=rtla clean: doc_clean fixdep-clean
@@ -111,10 +118,10 @@ clean: doc_clean fixdep-clean $(Q)find . -name '*.o' -delete -o -name '\.*.cmd' -delete -o -name '\.*.d' -delete $(Q)rm -f rtla rtla-static fixdep FEATURE-DUMP rtla-* $(Q)rm -rf feature - $(Q)rm -f src/timerlat.bpf.o src/timerlat.skel.h example/timerlat_bpf_action.o + $(Q)rm -f $(OUTPUT)src/timerlat.bpf.o $(OUTPUT)src/timerlat.skel.h $(OUTPUT)example/timerlat_bpf_action.o $(Q)rm -f $(UNIT_TESTS) -check: $(RTLA) tests/bpf/bpf_action_map.o +check: $(RTLA) $(OUTPUT)tests/bpf/bpf_action_map.o RTLA=$(RTLA) BPFTOOL=$(SYSTEM_BPFTOOL) prove -o -f -v tests/ -examples: example/timerlat_bpf_action.o +examples: $(OUTPUT)example/timerlat_bpf_action.o .PHONY: FORCE clean check
tools/tracing/rtla/tests/timerlat.t +2 -2
@@ -74,12 +74,12 @@ then # Test BPF action program properly in BPF mode [ -z "$BPFTOOL" ] && BPFTOOL=bpftool check "hist with BPF action program (BPF mode)" \ - "timerlat hist -T 2 --bpf-action tests/bpf/bpf_action_map.o --on-threshold shell,command='$BPFTOOL map dump name rtla_test_map'" \ + "timerlat hist -T 2 --bpf-action ${OUTPUT}tests/bpf/bpf_action_map.o --on-threshold shell,command='$BPFTOOL map dump name rtla_test_map'" \ 2 '"value": 42' else # Test BPF action program failure in non-BPF mode check "hist with BPF action program (non-BPF mode)" \ - "timerlat hist -T 2 --bpf-action tests/bpf/bpf_action_map.o" \ + "timerlat hist -T 2 --bpf-action ${OUTPUT}tests/bpf/bpf_action_map.o" \ 1 "BPF actions are not supported in tracefs-only mode" fi done
PATCH 2/3 perf tools: Put Python egg info in output directory
In reply to Ben HutchingsInstalling the Python extension currently creates an egg-info directory in the source tree, preventing a fully out-of-tree build.
Add the necessary runes to the setup.py comamnd line to relocate the egg-info directory in an out-of-tree build.
--- tools/perf/Makefile.perf | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
tools/perf/Makefile.perf +3 -1
@@ -1152,7 +1152,9 @@ install-bin: install-tools install-tests install: install-bin try-install-man install-python_ext: - $(PYTHON_WORD) util/setup.py --quiet install --root='/$(DESTDIR_SQ)' + $(PYTHON_WORD) util/setup.py --quiet \ + $(if $(OUTPUT),egg_info --egg-base $(OUTPUT)) \ + install --root='/$(DESTDIR_SQ)' # 'make install-doc' should call 'make -C Documentation install' $(INSTALL_DOC_TARGETS):
PATCH 3/3 perf tools: Put Python bytecode in output directory
In reply to Ben HutchingsThe PMU events are processed into C sources by Python scripts, which normally results in writing bytecode for each module into the source tree. This prevents a fully out-of-tree build.
To fix this, set $PYTHONPYCACHEPREFIX to relocate the bytecode cache directory in an out-of-tree build.
--- tools/perf/Makefile.perf | 5 +++++ 1 file changed, 5 insertions(+)
tools/perf/Makefile.perf +5 -0
@@ -400,6 +400,11 @@ PYTHON_EXTBUILD_LIB := $(PYTHON_EXTBUILD)lib/ PYTHON_EXTBUILD_TMP := $(PYTHON_EXTBUILD)tmp/ export PYTHON_EXTBUILD_LIB PYTHON_EXTBUILD_TMP +# Put Python bytecode in output directory +ifdef OUTPUT +export PYTHONPYCACHEPREFIX := $(OUTPUT)/__pycache__ +endif + python-clean := $(call QUIET_CLEAN, python) $(RM) -r $(PYTHON_EXTBUILD) $(OUTPUT)python/perf*.so # Use the detected configuration
Steven Rostedt
maintainerRe: [PATCH 1/3] rtla: Fix output files in source tree
In reply to Ben HutchingsQuoted 10 lines · On Thu, 21 May 2026 16:35:25 +0200 Ben Hutchings <[email protected]> wrote:
Some output files (
src/timerlat.bpf.o,src/timerlat.skel.h,example/timerlat_bpf_action.o,tests/bpf/bpf_action_map.o) are currently generated in the source tree, preventing a fully out-of-tree build. To fix this:- Add $(OUTPUT) to their filenames in the relevant
Makefilerules, and create subdirectories as needed
- Add $(OUTPUT)src to the include path
- Add ${OUTPUT} to the BPF object filename in tests/timerlat.t
Quoted 4 lines
Fixes e34293ddcebd ("rtla/timerlat: Add BPF skeleton to collect samples")Fixes 0304a3b7ec9a ("rtla/timerlat: Add example for BPF action program")Fixes 5525aebd4e0c ("rtla/tests: Test BPF action program")Signed-off-by Ben Hutchings <[email protected]>
Hi Ben,
Can you send this as a separate patch. The rtla code is handled via a different tree than the perf code. So these patches will not be going together.
Thanks,
-- Steve
[email protected]
AI reviewRe: [PATCH 1/3] rtla: Fix output files in source tree
In reply to Ben HutchingsThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] `make clean` fails to remove the generated `$(OUTPUT)tests/bpf/bpf_action_map.o` file during…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] make clean fails to remove the generated $(OUTPUT)tests/bpf/bpf_action_map.o file during out-of-tree builds. --
commit ce6ce36bb0778cda3451364e21ac622e234e5df7
Author: Ben Hutchings <[email protected]>
rtla: Fix output files in source tree
This commit updates the rtla Makefile to place several generated BPF object files and the skeleton header into the output directory to support fully out-of-tree builds. It adds the $(OUTPUT) prefix to these files in the build rules, tests, and the clean target.
Quoted 4 lines
tools/tracing/rtla/Makefile +0
-0
[ ... ]
tools/tracing/rtla/Makefile clean: doc_clean fixdep-clean · line 121
Quoted 6 lines
tools/tracing/rtla/Makefile +1
-1@@ -111,10 +118,10 @@ clean: doc_clean fixdep-clean $(Q)find . -name '*.o' -delete -o -name '\.*.cmd' -delete -o -name '\.*.d' -delete $(Q)rm -f rtla rtla-static fixdep FEATURE-DUMP rtla-* $(Q)rm -rf feature - $(Q)rm -f src/timerlat.bpf.o src/timerlat.skel.h example/timerlat_bpf_action.o + $(Q)rm -f $(OUTPUT)src/timerlat.bpf.o $(OUTPUT)src/timerlat.skel.h $(OUTPUT)example/timerlat_bpf_action.o
[Severity: Low]
Does this leave $(OUTPUT)tests/bpf/bpf_action_map.o behind after a clean?
Previously, tests/bpf/bpf_action_map.o was cleaned up by the find command on the first line of this target. Now that it is placed in the $(OUTPUT) directory, the find command might not reach it during an out-of-tree build, and it does not appear to be included in this explicit rm command either.
Quoted 1 line
tools/tracing/rtla/Makefile +0
-0$(Q)rm -f $(UNIT_TESTS)
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
[email protected]
AI reviewRe: [PATCH 3/3] perf tools: Put Python bytecode in output directory
In reply to Ben HutchingsThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] The use of `PYTHONPYCACHEPREFIX` fails to resolve out-of-tree build pollution for Python 3.6 and…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] The use of PYTHONPYCACHEPREFIX fails to resolve out-of-tree build pollution for Python 3.6 and 3.7.
- [Low] The newly configured relocated Python bytecode cache directory ($(OUTPUT)/__pycache__) is not removed during make clean or make python-clean. --
commit 7f6b9146d44a693c3114eaae7826841344f49ebd
Author: Ben Hutchings <[email protected]>
perf tools: Put Python bytecode in output directory
This commit sets PYTHONPYCACHEPREFIX during perf tool builds when an output directory is specified. This aims to prevent Python from writing bytecode into the source tree during out-of-tree builds.
tools/perf/Makefile.perf PYTHON_EXTBUILD_LIB := $(PYTHON_EXTBUILD)lib/ · line 404
Quoted 10 lines
tools/perf/Makefile.perf +3
-0@@ -399,6 +399,11 @@ PYTHON_EXTBUILD_LIB := $(PYTHON_EXTBUILD)lib/ PYTHON_EXTBUILD_TMP := $(PYTHON_EXTBUILD)tmp/ export PYTHON_EXTBUILD_LIB PYTHON_EXTBUILD_TMP +# Put Python bytecode in output directory +ifdef OUTPUT +export PYTHONPYCACHEPREFIX := $(OUTPUT)/__pycache__
[Severity: Medium]
Since the perf build system explicitly supports Python 3.6 and 3.7, does setting PYTHONPYCACHEPREFIX resolve the out-of-tree build pollution for those versions?
This environment variable was introduced in Python 3.8 and is silently ignored in earlier versions. Would exporting PYTHONDONTWRITEBYTECODE=1 be a more universally supported alternative?
tools/perf/Makefile.perf PYTHON_EXTBUILD_LIB := $(PYTHON_EXTBUILD)lib/ · line 408
Quoted 3 lines
tools/perf/Makefile.perf +2
-0+endif + python-clean := $(call QUIET_CLEAN, python) $(RM) -r $(PYTHON_EXTBUILD) $(OUTPUT)python/perf*.so
[Severity: Low]
Does the newly configured relocated Python bytecode cache directory need to be explicitly removed in the python-clean target?
The existing clean rules do not match the deep absolute paths created by PYTHONPYCACHEPREFIX, which leaves the entire __pycache__ directory hierarchy orphaned in the build output after a make clean.
Quoted 1 line
tools/perf/Makefile.perf +0
-0# Use the detected configuration
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Quoted 6 lines · Ben Hutchings
perf and rtla currently don't fully support out-of-tree builds, as they may still create files in their source directory. This series fixes all the instances of this problem that I have found.
Ben.
Thanks for the cleanup! I ran Sashiko locally and it noted that we should also fix the clean steps to remove these files (I'll copy the full text below).
``` commit cf212be834ce6b0df5cccdbc4ad977a917ff8e48 Author: Ben Hutchings <[email protected]> perf tools: Put Python egg info in output directory Installing the Python extension currently creates an egg-info directory in the source tree, preventing a fully out-of-tree build. Add the necessary runes to the setup.py comamnd line to relocate the egg-info directory in an out-of-tree build. > diff --git a/tools/perf/Makefile.perf b/tools/perf/Makefile.perf > index fc92d6ceac5b..b9a9bdddb89a 100644 > --- a/tools/perf/Makefile.perf > +++ b/tools/perf/Makefile.perf > @@ -905,7 +910,9 @@ install-bin: install-tools install-tests > install: install-bin try-install-man > > install-python_ext: > - $(PYTHON_WORD) util/setup.py --quiet install --root='/$(DESTDIR_SQ)' > + $(PYTHON_WORD) util/setup.py --quiet \ > + $(if $(OUTPUT),egg_info --egg-base $(OUTPUT)) \ > + install --root='/$(DESTDIR_SQ)' This relocates the egg-info directory to the output directory, which helps keep the source tree clean. However, does the `python-clean` target need to be updated to clean up this directory? Currently, `python-clean` is defined as: python-clean := $(call QUIET_CLEAN, python) $(RM) -r $(PYTHON_EXTBUILD) $(OUTPUT)python/perf*.so Should we also clean `$(OUTPUT)/perf.egg-info` (or `perf.egg-info` for in-tree builds) to ensure `make clean` removes all generated python metadata? For example: python-clean := $(call QUIET_CLEAN, python) \ $(RM) -r $(PYTHON_EXTBUILD) $(OUTPUT)python/perf*.so \ $(OUTPUT)perf.egg-info perf.egg-info commit dd754ea75966ec7ade41500afeb32d944f20a8f9 Author: Ben Hutchings <[email protected]> perf tools: Put Python bytecode in output directory The PMU events are processed into C sources by Python scripts, which normally results in writing bytecode for each module into the source tree. This prevents a fully out-of-tree build. To fix this, set $PYTHONPYCACHEPREFIX to relocate the bytecode cache directory in an out-of-tree build. > diff --git a/tools/perf/Makefile.perf b/tools/perf/Makefile.perf > index fc92d6ceac5b..b9a9bdddb89a 100644 > --- a/tools/perf/Makefile.perf > +++ b/tools/perf/Makefile.perf > @@ -400,6 +400,11 @@ PYTHON_EXTBUILD_LIB := $(PYTHON_EXTBUILD)lib/ > PYTHON_EXTBUILD_TMP := $(PYTHON_EXTBUILD)tmp/ > export PYTHON_EXTBUILD_LIB PYTHON_EXTBUILD_TMP > > +# Put Python bytecode in output directory > +ifdef OUTPUT > +export PYTHONPYCACHEPREFIX := $(OUTPUT)/__pycache__ > +endif > + > python-clean := $(call QUIET_CLEAN, python) $(RM) -r $(PYTHON_EXTBUILD) $(OUTPUT)python/perf*.so Similar to the egg-info relocation, does `python-clean` need to clean up the relocated `__pycache__` directory under `$(OUTPUT)`? If `PYTHONPYCACHEPREFIX` is exported to `$(OUTPUT)/__pycache__`, running `make clean` currently leaves this directory behind. Should we add it to the `RM` list in `python-clean`? For example: python-clean := $(call QUIET_CLEAN, python) \ $(RM) -r $(PYTHON_EXTBUILD) $(OUTPUT)python/perf*.so \ $(if $(OUTPUT),$(OUTPUT)/__pycache__) ```
Thanks,
Ian
Quoted 10 lines
Ben Hutchings (3):
rtla: Fix output files in source tree
perf tools: Put Python egg info in output directory perf tools: Put Python bytecode in output directorytools/perf/Makefile.perf | 9 ++++++++- tools/tracing/rtla/Makefile | 31 ++++++++++++++++++----------- tools/tracing/rtla/tests/timerlat.t | 4 ++-- 3 files changed, 29 insertions(+), 15 deletions(-)
Ian Rogers
reviewerQuoted 7 lines · Ian Rogers
Quoted 5 lines · Ben Hutchings
perf and rtla currently don't fully support out-of-tree builds, as they may still create files in their source directory. This series fixes all the instances of this problem that I have found.
Quoted 6 lines
Quoted 1 line
Ben.
Thanks for the cleanup! I ran Sashiko locally and it noted that we should also fix the clean steps to remove these files (I'll copy the full text below).
Reviewed-by Ian Rogers <[email protected]>
Hi Ben,
Did you make any progress on this? Checking upstream I don't see the patches as landed and they look useful.
Thanks,
Ian
Quoted 101 lines
``` commit cf212be834ce6b0df5cccdbc4ad977a917ff8e48 Author: Ben Hutchings <[email protected]> perf tools: Put Python egg info in output directory Installing the Python extension currently creates an egg-info directory in the source tree, preventing a fully out-of-tree build. Add the necessary runes to the setup.py comamnd line to relocate the egg-info directory in an out-of-tree build. > diff --git a/tools/perf/Makefile.perf b/tools/perf/Makefile.perf > index fc92d6ceac5b..b9a9bdddb89a 100644 > --- a/tools/perf/Makefile.perf > +++ b/tools/perf/Makefile.perf > @@ -905,7 +910,9 @@ install-bin: install-tools install-tests > install: install-bin try-install-man > > install-python_ext: > - $(PYTHON_WORD) util/setup.py --quiet install --root='/$(DESTDIR_SQ)' > + $(PYTHON_WORD) util/setup.py --quiet \ > + $(if $(OUTPUT),egg_info --egg-base $(OUTPUT)) \ > + install --root='/$(DESTDIR_SQ)' This relocates the egg-info directory to the output directory, which helps keep the source tree clean. However, does the `python-clean` target need to be updated to clean up this directory? Currently, `python-clean` is defined as: python-clean := $(call QUIET_CLEAN, python) $(RM) -r $(PYTHON_EXTBUILD) $(OUTPUT)python/perf*.so Should we also clean `$(OUTPUT)/perf.egg-info` (or `perf.egg-info` for in-tree builds) to ensure `make clean` removes all generated python metadata? For example: python-clean := $(call QUIET_CLEAN, python) \ $(RM) -r $(PYTHON_EXTBUILD) $(OUTPUT)python/perf*.so \ $(OUTPUT)perf.egg-info perf.egg-info commit dd754ea75966ec7ade41500afeb32d944f20a8f9 Author: Ben Hutchings <[email protected]> perf tools: Put Python bytecode in output directory The PMU events are processed into C sources by Python scripts, which normally results in writing bytecode for each module into the source tree. This prevents a fully out-of-tree build. To fix this, set $PYTHONPYCACHEPREFIX to relocate the bytecode cache directory in an out-of-tree build. > diff --git a/tools/perf/Makefile.perf b/tools/perf/Makefile.perf > index fc92d6ceac5b..b9a9bdddb89a 100644 > --- a/tools/perf/Makefile.perf > +++ b/tools/perf/Makefile.perf > @@ -400,6 +400,11 @@ PYTHON_EXTBUILD_LIB := $(PYTHON_EXTBUILD)lib/ > PYTHON_EXTBUILD_TMP := $(PYTHON_EXTBUILD)tmp/ > export PYTHON_EXTBUILD_LIB PYTHON_EXTBUILD_TMP > > +# Put Python bytecode in output directory > +ifdef OUTPUT > +export PYTHONPYCACHEPREFIX := $(OUTPUT)/__pycache__ > +endif > + > python-clean := $(call QUIET_CLEAN, python) $(RM) -r $(PYTHON_EXTBUILD) $(OUTPUT)python/perf*.so Similar to the egg-info relocation, does `python-clean` need to clean up the relocated `__pycache__` directory under `$(OUTPUT)`? If `PYTHONPYCACHEPREFIX` is exported to `$(OUTPUT)/__pycache__`, running `make clean` currently leaves this directory behind. Should we add it to the `RM` list in `python-clean`? For example: python-clean := $(call QUIET_CLEAN, python) \ $(RM) -r $(PYTHON_EXTBUILD) $(OUTPUT)python/perf*.so \ $(if $(OUTPUT),$(OUTPUT)/__pycache__) ```Thanks,
IanQuoted 10 lines
Ben Hutchings (3):
rtla: Fix output files in source tree
perf tools: Put Python egg info in output directory perf tools: Put Python bytecode in output directorytools/perf/Makefile.perf | 9 ++++++++- tools/tracing/rtla/Makefile | 31 ++++++++++++++++++----------- tools/tracing/rtla/tests/timerlat.t | 4 ++-- 3 files changed, 29 insertions(+), 15 deletions(-)
Ian Rogers
reviewerPATCH v2 1/2 perf tools: Put Python egg info in output directory
In reply to Ben HutchingsFrom: Ben Hutchings <[email protected]>
Installing the Python extension currently creates an egg-info directory in the source tree, preventing a fully out-of-tree build.
Add the necessary runes to the setup.py comamnd line to relocate the egg-info directory in an out-of-tree build.
---
v2: Rebase
--- tools/perf/Makefile.perf | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
tools/perf/Makefile.perf +3 -1
@@ -908,7 +908,9 @@ install-build-deps: $(Q)$(SHELL) $(srctree)/tools/perf/scripts/install-build-deps.sh $(INSTALL_BUILD_DEPS_ARGS) install-python_ext: - $(PYTHON_WORD) util/setup.py $(python_setup_quiet) install --root='/$(DESTDIR_SQ)' + $(PYTHON_WORD) util/setup.py $(python_setup_quiet) \ + $(if $(OUTPUT),egg_info --egg-base $(OUTPUT)) \ + install --root='/$(DESTDIR_SQ)' # 'make install-doc' should call 'make -C Documentation install' $(INSTALL_DOC_TARGETS):
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
Ian Rogers
reviewerPATCH v2 2/2 perf tools: Put Python bytecode in output directory
From: Ben Hutchings <[email protected]>
The PMU events are processed into C sources by Python scripts, which normally results in writing bytecode for each module into the source tree. This prevents a fully out-of-tree build.
To fix this, set $PYTHONPYCACHEPREFIX to relocate the bytecode cache directory in an out-of-tree build. Note, this environment variable is only supported by python 3.8+, but the file Documentation/Changes states the minimum supported python for builds is 3.9.x. In an in-tree build in tools/perf setting the variable causes a single __pycache__ directory in the source tree.
Expand python-clean to also clean the __pycache__ and perf.pyi.
---
v2: Rebase. Add clean step and commit message comment wrt older python
versions (Sashiko). Make setting the environment variable true for in-tree builds, that simplifies the clean.
--- tools/perf/Makefile.perf | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
tools/perf/Makefile.perf +8 -2
@@ -397,7 +397,8 @@ PYTHON_EXTBUILD_LIB := $(PYTHON_EXTBUILD)lib/ PYTHON_EXTBUILD_TMP := $(PYTHON_EXTBUILD)tmp/ export PYTHON_EXTBUILD_LIB PYTHON_EXTBUILD_TMP -python-clean := $(call QUIET_CLEAN, python) $(RM) -r $(PYTHON_EXTBUILD) $(OUTPUT)python/perf*.so +# Put Python bytecode in output directory +export PYTHONPYCACHEPREFIX := $(OUTPUT)/__pycache__ ifneq ($(quiet),) python_setup_quiet=--quiet
@@ -919,7 +920,12 @@ $(INSTALL_DOC_TARGETS): ### Cleaning rules python-clean: - $(python-clean) + $(call QUIET_CLEAN, python) $(RM) -r $(PYTHON_EXTBUILD) $(OUTPUT)__pycache__ \ + $(OUTPUT)python/perf*.so + @if [ "$(abspath python/perf.pyi)" != "$(abspath $(OUTPUT)python/perf.pyi)" ]; \ + then \ + $(RM) $(OUTPUT)python/perf.pyi; \ + fi $(LIBAPI_OUTPUT) $(LIBBPF_OUTPUT) $(LIBPERF_OUTPUT) $(LIBSUBCMD_OUTPUT) $(LIBSYMBOL_OUTPUT): $(Q)$(MKDIR) -p $@
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v2 1/2] perf tools: Put Python egg info in output directory
In reply to Ian RogersSashiko has reviewed this patch and found no issues. It looks great!
Quoted 9 lines
From: Ben Hutchings <[email protected]>
Installing the Python extension currently creates an egg-info directory in the source tree, preventing a fully out-of-tree build.
Add the necessary runes to the
setup.pycomamnd line to relocate the egg-info directory in an out-of-tree build.Signed-off-by Ben Hutchings <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
[email protected]
AI reviewRe: [PATCH v2 2/2] perf tools: Put Python bytecode in output directory
In reply to Ian RogersThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] perf: root filesystem pollution or build failure due to stray slash in PYTHONPYCACHEPREFIX --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] perf: root filesystem pollution or build failure due to stray slash in PYTHONPYCACHEPREFIX --
commit 3de8bc01d44a9b11e10c327ec26bbff8605926df
Author: Ben Hutchings <[email protected]>
perf tools: Put Python bytecode in output directory
This commit sets the PYTHONPYCACHEPREFIX environment variable to relocate the Python bytecode cache directory to the output directory during out-of-tree builds. It also updates the python-clean rule to remove the new cache directory and perf.pyi.
tools/perf/Makefile.perf PYTHON_EXTBUILD_LIB := $(PYTHON_EXTBUILD)lib/ · line 401
Quoted 11 lines
tools/perf/Makefile.perf +2
-1@@ -397,7 +397,8 @@ PYTHON_EXTBUILD_LIB := $(PYTHON_EXTBUILD)lib/ PYTHON_EXTBUILD_TMP := $(PYTHON_EXTBUILD)tmp/ export PYTHON_EXTBUILD_LIB PYTHON_EXTBUILD_TMP -python-clean := $(call QUIET_CLEAN, python) $(RM) -r $(PYTHON_EXTBUILD) $(OUTPUT)python/perf*.so +# Put Python bytecode in output directory +export PYTHONPYCACHEPREFIX := $(OUTPUT)/__pycache__
[Severity: Medium]
For in-tree builds where $(OUTPUT) is empty, does the unconditional inclusion of the slash cause this to evaluate to the absolute path /__pycache__?
When Python 3.8+ runs during the build, it appears it would attempt to write bytecode files to the root of the filesystem. Could this cause a regression by breaking the build with permission errors for unprivileged users, or silently pollute the root filesystem if executed as root?
[ ... ]
tools/perf/Makefile.perf $(INSTALL_DOC_TARGETS): · line 923
Quoted 6 lines
tools/perf/Makefile.perf +1
-1@@ -919,7 +920,12 @@ $(INSTALL_DOC_TARGETS): ### Cleaning rules python-clean: - $(python-clean) + $(call QUIET_CLEAN, python) $(RM) -r $(PYTHON_EXTBUILD) $(OUTPUT)__pycache__ \
[Severity: Medium]
Since $(OUTPUT)__pycache__ would evaluate to __pycache__ in the current directory during in-tree builds, would this fail to clean the incorrectly placed bytecode directory at the root of the filesystem?
Quoted 5 lines
tools/perf/Makefile.perf +5
-0+ $(OUTPUT)python/perf*.so + @if [ "$(abspath python/perf.pyi)" != "$(abspath $(OUTPUT)python/perf.pyi)" ]; \ + then \ + $(RM) $(OUTPUT)python/perf.pyi; \ + fi
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Ian Rogers
reviewerRe: [PATCH v2 2/2] perf tools: Put Python bytecode in output directory
Quoted 31 lines · On Mon, Sep 21, 2026 at 11:05 AM <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] perf: root filesystem pollution or build failure due to stray slash in PYTHONPYCACHEPREFIX --commit
3de8bc01d44a9b11e10c327ec26bbff8605926df
Author: Ben Hutchings <[email protected]>perf tools: Put Python bytecode in output directory
This commit sets the PYTHONPYCACHEPREFIX environment variable to relocate the Python bytecode cache directory to the output directory during out-of-tree builds. It also updates the python-clean rule to remove the new cache directory and perf.pyi.
tools/perf/Makefile.perf PYTHON_EXTBUILD_LIB := $(PYTHON_EXTBUILD)lib/ · line 401
Quoted 11 lines
tools/perf/Makefile.perf +2
-1@@ -397,7 +397,8 @@ PYTHON_EXTBUILD_LIB := $(PYTHON_EXTBUILD)lib/ PYTHON_EXTBUILD_TMP := $(PYTHON_EXTBUILD)tmp/ export PYTHON_EXTBUILD_LIB PYTHON_EXTBUILD_TMP -python-clean := $(call QUIET_CLEAN, python) $(RM) -r $(PYTHON_EXTBUILD) $(OUTPUT)python/perf*.so +# Put Python bytecode in output directory +export PYTHONPYCACHEPREFIX := $(OUTPUT)/__pycache__[Severity: Medium]
For in-tree builds where $(OUTPUT) is empty, does the unconditional inclusion of the slash cause this to evaluate to the absolute path /__pycache__?
Quoted 4 lines
When Python 3.8+ runs during the build, it appears it would attempt to write bytecode files to the root of the filesystem. Could this cause a regression by breaking the build with permission errors for unprivileged users, or silently pollute the root filesystem if executed as root?
This is already the behavior. The difference now is that instead of multiple __pycache__ directories for the directory the python script is within, there is a single top-level __pycache__ directory that is handled by the clean step.
Quoted 5 lines
[ ... ]
Quoted 3 lines
tools/perf/Makefile.perf +0
-0@@ -919,7 +920,12 @@ $(INSTALL_DOC_TARGETS): ### Cleaning rules
Quoted 8 lines
tools/perf/Makefile.perf $(INSTALL_DOC_TARGETS): · line 923
Quoted 3 lines
tools/perf/Makefile.perf +1
-1python-clean: - $(python-clean) + $(call QUIET_CLEAN, python) $(RM) -r $(PYTHON_EXTBUILD) $(OUTPUT)__pycache__ \[Severity: Medium]
Since $(OUTPUT)__pycache__ would evaluate to__pycache__in the current directory during in-tree builds, would this fail to clean the incorrectly placed bytecode directory at the root of the filesystem?
Same issue; yes, the build would fail but the build would already be failing if this were a problem.
Thanks,
Ian
Quoted 8 lines
tools/perf/Makefile.perf $(INSTALL_DOC_TARGETS): · line 928
Quoted 5 lines
tools/perf/Makefile.perf +5
-0+ $(OUTPUT)python/perf*.so + @if [ "$(abspath python/perf.pyi)" != "$(abspath $(OUTPUT)python/perf.pyi)" ]; \ + then \ + $(RM) $(OUTPUT)python/perf.pyi; \ + fi--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Ian Rogers
reviewerRe: [PATCH v2 1/2] perf tools: Put Python egg info in output directory
In reply to Ian RogersQuoted 6 lines · Ian Rogers
From: Ben Hutchings <[email protected]>
Installing the Python extension currently creates an egg-info directory in the source tree, preventing a fully out-of-tree build.
Quoted 7 lines
Add the necessary runes to the
setup.pycomamnd line to relocate the egg-info directory in an out-of-tree build.Signed-off-by Ben Hutchings <[email protected]>Reviewed-by Ian Rogers <[email protected]>---
v2: Rebase
Ben's build clean up that I've then rebased and addressed an issue with building in tools/perf seems like a nice to land thing. I'm not sure what more I can do to push this forward, and I replied on the list about the Sashiko feedback, which was a little wide of the mark. Let me know what you think; perhaps the tags are not done properly.
Thanks,
Ian
Quoted 22 lines
--- tools/perf/Makefile.perf | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)tools/perf/Makefile.perf +3
-2@@ -908,7 +908,9 @@ install-build-deps: $(Q)$(SHELL) $(srctree)/tools/perf/scripts/install-build-deps.sh $(INSTALL_BUILD_DEPS_ARGS) install-python_ext: - $(PYTHON_WORD) util/setup.py $(python_setup_quiet) install --root='/$(DESTDIR_SQ)' + $(PYTHON_WORD) util/setup.py $(python_setup_quiet) \ + $(if $(OUTPUT),egg_info --egg-base $(OUTPUT)) \ + install --root='/$(DESTDIR_SQ)' # 'make install-doc' should call 'make -C Documentation install' $(INSTALL_DOC_TARGETS): --2.55.0.1082.g2b9226bbc0-goog
Re: [PATCH v2 1/2] perf tools: Put Python egg info in output directory
Quoted 15 lines · Ian Rogers
Quoted 6 lines · Ian Rogers
From: Ben Hutchings <[email protected]>
Installing the Python extension currently creates an egg-info directory in the source tree, preventing a fully out-of-tree build.
Quoted 7 lines
Add the necessary runes to the
setup.pycomamnd line to relocate the egg-info directory in an out-of-tree build.Signed-off-by Ben Hutchings <[email protected]>Reviewed-by Ian Rogers <[email protected]>---
v2: Rebase
Quoted 5 lines
Ben's build clean up that I've then rebased and addressed an issue with building in
tools/perfseems like a nice to land thing. I'm not sure what more I can do to push this forward, and I replied on the list about the Sashiko feedback, which was a little wide of the mark. Let me know what you think; perhaps the tags are not done properly.
I guess they are, you carried it forward and then pushed upstream, so merits your Signed-off-by, I think.
Thanks, applied to perf-tools-next, for v7.4.
- Arnaldo