[PATCH 1/1] perf python: Don't stop building if python setuptools isn't installed

From: Arnaldo Carvalho de Melo
Date: Sat Dec 17 2022 - 09:37:15 EST


The python3-setuptools package is needed to build the python binding, so
that one can use things like:

# ~acme/git/perf/tools/perf/python/twatch.py
cpu: 6, pid: 4573, tid: 2184618 { type: exit, pid: 4573, ppid: 4172, tid: 2184618, ptid: 4172, time: 12563190090107}
cpu: 24, pid: 4573, tid: 4573 { type: fork, pid: 4573, ppid: 4573, tid: 2190991, ptid: 4573, time: 12563415289331}
cpu: 29, pid: 4573, tid: 2190991 { type: comm, pid: 4573, tid: 2190991, comm: StreamT~ns #401 }
cpu: 29, pid: 4573, tid: 2190991 { type: comm, pid: 4573, tid: 2190991, comm: StreamT~ns #401 }
^CTraceback (most recent call last):
File "/var/home/acme/git/perf/tools/perf/python/twatch.py", line 61, in <module>
main()
File "/var/home/acme/git/perf/tools/perf/python/twatch.py", line 33, in main
evlist.poll(timeout = -1)
KeyboardInterrupt

#

That have 'import perf;'.

But distros don't always have that python3-setuptools (or equivalent)
installed, which was breaking the build. Just check if it is installed
and emit a warning that such binding isn't being built and continue the
build without it:

With it:

$ rpm -q python3-setuptools
python3-setuptools-59.6.0-3.fc36.noarch
$ rm -rf /tmp/build/perf; mkdir -p /tmp/build/perf
$ make O=/tmp/build/perf -C tools/perf install-bin
make: Entering directory '/var/home/acme/git/perf/tools/perf'
<SNIP>
... libpython: [ on ]
<SNIP>
GEN /tmp/build/perf/python/perf.cpython-310-x86_64-linux-gnu.so
<SNIP>
$ ls -la /tmp/build/perf/python/perf.cpython-310-x86_64-linux-gnu.so
-rwxr-xr-x. 1 acme acme 1609112 Dec 17 11:39 /tmp/build/perf/python/perf.cpython-310-x86_64-linux-gnu.so
$

Without it:

$ sudo rpm -e python3-setuptools
$ rm -rf /tmp/build/perf ; mkdir -p /tmp/build/perf
$ make O=/tmp/build/perf -C tools/perf install-bin
make: Entering directory '/var/home/acme/git/perf/tools/perf'
<SNIP>
... libpython: [ on ]
<SNIP>
$ ls -la /tmp/build/perf/python/perf.cpython-310-x86_64-linux-gnu.so
ls: cannot access '/tmp/build/perf/python/perf.cpython-310-x86_64-linux-gnu.so': No such file or directory
$

Reported-by: Linus Torvalds <torvalds@xxxxxxxxxxxxxxxxxxxx>
Cc: Adrian Hunter <adrian.hunter@xxxxxxxxx>
Cc: Ian Rogers <irogers@xxxxxxxxxx>
Cc: Jiri Olsa <jolsa@xxxxxxxxxx>
Cc: Namhyung Kim <namhyung@xxxxxxxxxx>
Signed-off-by: Arnaldo Carvalho de Melo <acme@xxxxxxxxxx>
---
tools/perf/Makefile.config | 9 +++++++--
1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/tools/perf/Makefile.config b/tools/perf/Makefile.config
index 83ed969b95b4a53f..c21bd6010be1384c 100644
--- a/tools/perf/Makefile.config
+++ b/tools/perf/Makefile.config
@@ -890,8 +890,13 @@ else
else
LDFLAGS += $(PYTHON_EMBED_LDFLAGS)
EXTLIBS += $(PYTHON_EMBED_LIBADD)
- PYTHON_EXTENSION_SUFFIX := $(shell $(PYTHON) -c 'from importlib import machinery; print(machinery.EXTENSION_SUFFIXES[0])')
- LANG_BINDINGS += $(obj-perf)python/perf$(PYTHON_EXTENSION_SUFFIX)
+ PYTHON_SETUPTOOLS_INSTALLED := $(shell $(PYTHON) -c 'import setuptools;' 2> /dev/null && echo "yes" || echo "no")
+ ifeq ($(PYTHON_SETUPTOOLS_INSTALLED), yes)
+ PYTHON_EXTENSION_SUFFIX := $(shell $(PYTHON) -c 'from importlib import machinery; print(machinery.EXTENSION_SUFFIXES[0])')
+ LANG_BINDINGS += $(obj-perf)python/perf$(PYTHON_EXTENSION_SUFFIX)
+ else
+ msg := $(warning Missing python setuptools, the python binding won't be built, please install python3-setuptools or equivalent);
+ endif
CFLAGS += -DHAVE_LIBPYTHON_SUPPORT
$(call detected,CONFIG_LIBPYTHON)
endif
--
2.38.1