Re: [PATCH] perf python: Track linked libraries as extension dependencies
From: Ian Rogers
Date: Thu Oct 01 2026 - 12:34:08 EST
On Thu, Oct 1, 2026 at 8:48 AM James Clark <james.clark@xxxxxxxxxx> wrote:
>
> Since the Python extension switched to linking perf libraries, setup.py
> only lists util/python.c as a source. The libraries are supplied through
> LDFLAGS, which setuptools does not consider when deciding whether the
> extension needs to be rebuilt.
>
> When a library changes, make invokes setup.py, but setuptools can skip
> the build and the recipe copies the stale cached extension back into
> python/. This leaves perf and its Python module running different
> versions of the same code.
>
> Pass the linked library paths to setup.py and declare them as Extension
> dependencies so that library changes trigger a rebuild. Include
> EXTRA_PERFLIBS in the shared library list so that both make and
> setuptools track those inputs as well, preserving the existing linker
> order.
>
> Fixes: 9dabf4003423 ("perf python: Switch module to linking libraries from building source")
> Assisted-by: Codex:GPT-6-Astra
> Signed-off-by: James Clark <james.clark@xxxxxxxxxx>
Reviewed-by: Ian Rogers <irogers@xxxxxxxxxx>
Thanks!
Ian
> ---
> tools/perf/Makefile.perf | 5 +++--
> tools/perf/util/setup.py | 1 +
> 2 files changed, 4 insertions(+), 2 deletions(-)
>
> diff --git a/tools/perf/Makefile.perf b/tools/perf/Makefile.perf
> index 7ceefc1da774..dbf7cceaa005 100644
> --- a/tools/perf/Makefile.perf
> +++ b/tools/perf/Makefile.perf
> @@ -526,8 +526,8 @@ endif
> EXTLIBS := $(call filter-out,$(EXCLUDE_EXTLIBS),$(EXTLIBS))
> LIBS = -Wl,--whole-archive $(PERFLIBS) $(EXTRA_PERFLIBS) -Wl,--no-whole-archive -Wl,--start-group $(EXTLIBS) -Wl,--end-group
>
> -PERFLIBS_PY := $(call filter-out,$(LIBPERF_BENCH) $(LIBPERF_TEST),$(PERFLIBS))
> -LIBS_PY = -Wl,--whole-archive $(PERFLIBS_PY) $(EXTRA_PERFLIBS) -Wl,--no-whole-archive -Wl,--start-group $(EXTLIBS) -Wl,--end-group
> +PERFLIBS_PY := $(call filter-out,$(LIBPERF_BENCH) $(LIBPERF_TEST),$(PERFLIBS)) $(EXTRA_PERFLIBS)
> +LIBS_PY = -Wl,--whole-archive $(PERFLIBS_PY) -Wl,--no-whole-archive -Wl,--start-group $(EXTLIBS) -Wl,--end-group
>
> export INSTALL SHELL_PATH
>
> @@ -559,6 +559,7 @@ $(shell [ -d '$(OUTPUT)python' ] || mkdir -p '$(OUTPUT)python')
> $(OUTPUT)python/perf$(PYTHON_EXTENSION_SUFFIX): util/python.c util/setup.py python/perf.pyi $(PERFLIBS_PY)
> $(QUIET_GEN)LDSHARED="$(CC) -pthread -shared" \
> CFLAGS='$(CFLAGS)' LDFLAGS='$(LDFLAGS) $(LIBS_PY)' \
> + PYTHON_EXT_DEPS='$(PERFLIBS_PY)' \
> $(PYTHON_WORD) util/setup.py \
> $(python_setup_quiet) build_ext; \
> cp $(PYTHON_EXTBUILD_LIB)perf*.so $(OUTPUT)python/; \
> diff --git a/tools/perf/util/setup.py b/tools/perf/util/setup.py
> index 8380980ade4c..3b6a59f66944 100644
> --- a/tools/perf/util/setup.py
> +++ b/tools/perf/util/setup.py
> @@ -173,6 +173,7 @@ def main() -> None:
> perf = Extension(
> 'perf',
> sources=[os.path.join(src_perf, 'util/python.c')],
> + depends=shlex.split(os.getenv('PYTHON_EXT_DEPS', '')),
> include_dirs=['util/include'],
> extra_compile_args=cflags,
> )
>
> ---
> base-commit: 705da5b15ab89ba97b11eedbe507c2fd83d31cb9
> change-id: 20261001-james-perf-stale-python-build-d6b17da2f231
>
> Best regards,
> --
> James Clark <james.clark@xxxxxxxxxx>
>