Skip to contentSkip to conversation

tools build: Fix rust feature detection

PATCH In review Arnaldo Carvalho de Melo 2 participants 7 messages Feb 9 – Feb 10 perf events

lore
Share & tools
lore

Arnaldo Carvalho de Melo

author maintainer
#0 lore raw

PATCH 1/1 tools build: Fix rust feature detection

Features in FEATURE_TESTS_BASIC will be set as being available if test-all.c builds, so since the rust test isn't included in test-all.c, we can't have 'rust' in there, remove it from FEATURE_TESTS_BASIC and use feature-check so that it tries to build test-rust.bin, doing the actual feature detection.

On a system lacking a rust compiler:

Makefile.config:1158: Rust is not found. Test workloads with rust are disabled.

Auto-detecting system features:

  ...                                   libdw: [ on  ]
  ...                                   glibc: [ on  ]
  ...                                  libelf: [ on  ]
  ...                                 libnuma: [ on  ]
  ...                  numa_num_possible_cpus: [ on  ]
  ...                               libpython: [ on  ]
  ...                             libcapstone: [ on  ]
  ...                               llvm-perf: [ on  ]
  ...                                    zlib: [ on  ]
  ...                                    lzma: [ on  ]
  ...                                     bpf: [ on  ]
  ...                                  libaio: [ on  ]
  ...                                 libzstd: [ on  ]
  ...                              libopenssl: [ on  ]

... rust: [ OFF ]

  $ cat /tmp/build/perf-tools-next/feature/test-rust.make.output
  /bin/sh: line 1: rustc: command not found
  $ file /tmp/build/perf-tools-next/feature/test-rust.bin
  /tmp/build/perf-tools-next/feature/test-rust.bin: cannot open `/tmp/build/perf-tools-next/feature/test-rust.bin' (No such file or directory)
  $
  $ perf -vv | grep RUST
                  rust: [ OFF ]  # HAVE_RUST_SUPPORT
  $

And after installing it:

... rust: [ on ]

  $ cat /tmp/build/perf-tools-next/feature/test-rust.make.output
  $ file /tmp/build/perf-tools-next/feature/test-rust.bin
/tmp/build/perf-tools-next/feature/test-rust.bin: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=9c416edf673ee3705b97bae893a99a6fcf1ee258, for GNU/Linux 3.2.0, with debug_info, not stripped
  $
  $ perf -vv | grep RUST
                  rust: [ on  ]  # HAVE_RUST_SUPPORT
  $
Fixes 6a32fa5ccd33da5d ("tools build: Add a feature test for rust compiler")
Cc Dmitrii Dolgov <[email protected]>
---
 tools/build/Makefile.feature | 3 +--
 tools/perf/Makefile.config   | 1 +
 2 files changed, 2 insertions(+), 2 deletions(-)
tools/build/Makefile.feature +1 -2
index 64d21152fc81aad6..0b7a7c38cb88a7d0 100644
@@ -100,8 +100,7 @@ FEATURE_TESTS_BASIC :=                  \
         disassembler-four-args		\
         disassembler-init-styled	\
         file-handle			\
-        libopenssl			\
-        rust
+        libopenssl
 
 # FEATURE_TESTS_BASIC + FEATURE_TESTS_EXTRA is the complete list
 # of all feature tests
tools/perf/Makefile.config +1 -0
index 94aecfe38b95f716..a8dc72cfe48eec52 100644
@@ -1154,6 +1154,7 @@ ifneq ($(NO_LIBTRACEEVENT),1)
 endif
 
 ifndef NO_RUST
+  $(call feature_check,rust)
   ifneq ($(feature-rust), 1)
     $(warning Rust is not found. Test workloads with rust are disabled.)
     NO_RUST := 1
Signature · 1 line
2.53.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 6 lines · Arnaldo Carvalho de Melo

Features in FEATURE_TESTS_BASIC will be set as being available if test-all.c builds, so since the rust test isn't included in test-all.c, we can't have 'rust' in there, remove it from FEATURE_TESTS_BASIC and use feature-check so that it tries to build test-rust.bin, doing the actual feature detection.

My bad, thanks for fixing it.

Quoted 10 lines

And after installing it:

... rust: [ on ]

  $ cat /tmp/build/perf-tools-next/feature/test-rust.make.output
  $ file /tmp/build/perf-tools-next/feature/test-rust.bin
/tmp/build/perf-tools-next/feature/test-rust.bin: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=9c416edf673ee3705b97bae893a99a6fcf1ee258, for GNU/Linux 3.2.0, with debug_info, not stripped
  $
  $ perf -vv | grep RUST
                  rust: [ on  ]  # HAVE_RUST_SUPPORT

I've noticed few more problem, maybe worth fixing it here as well:

* test-rust.bin is missing from the list of FILES, and thus is not removed by the clean target. This could lead to a false feature detection, since the binary stays there.

* Target to build test-rust.bin does not emit a dependencies file.

I've tested the following diff after first having rust installed, then removing it and building again.

tools/build/feature/Makefile +3 -2
index 9ae69d85716..28d4ff0841a 100644
@@ -73,7 +73,8 @@ FILES=                                          \
          test-clang-bpf-co-re.bin              \
          test-file-handle.bin                  \
          test-libpfm4.bin                      \
-         test-libopenssl.bin
+         test-libopenssl.bin                   \
+         test-rust.bin
 
 FILES := $(addprefix $(OUTPUT),$(FILES))
 
@@ -112,7 +113,7 @@ __BUILD = $(CC) $(CFLAGS) -MD -Wall -Werror -o $@ $(patsubst %.bin,%.c,$(@F)) $(
 __BUILDXX = $(CXX) $(CXXFLAGS) -MD -Wall -Werror -o $@ $(patsubst %.bin,%.cpp,$(@F)) $(LDFLAGS)
   BUILDXX = $(__BUILDXX) > $(@:.bin=.make.output) 2>&1
 
-__BUILDRS = $(RUSTC) $(RUSTC_FLAGS) -o $@ $(patsubst %.bin,%.rs,$(@F))
+__BUILDRS = $(RUSTC) $(RUSTC_FLAGS) --emit=dep-info=$(patsubst %.bin,%.d,$(@F)),link -o $@ $(patsubst %.bin,%.rs,$(@F))
   BUILDRS = $(__BUILDRS) > $(@:.bin=.make.output) 2>&1
 
author maintainer
#2 lore raw
Quoted 22 lines · Dmitry Dolgov
Quoted 6 lines · Arnaldo Carvalho de Melo

Features in FEATURE_TESTS_BASIC will be set as being available if test-all.c builds, so since the rust test isn't included in test-all.c, we can't have 'rust' in there, remove it from FEATURE_TESTS_BASIC and use feature-check so that it tries to build test-rust.bin, doing the actual feature detection.

My bad, thanks for fixing it.

Quoted 10 lines

And after installing it:

... rust: [ on ]

  $ cat /tmp/build/perf-tools-next/feature/test-rust.make.output
  $ file /tmp/build/perf-tools-next/feature/test-rust.bin
/tmp/build/perf-tools-next/feature/test-rust.bin: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=9c416edf673ee3705b97bae893a99a6fcf1ee258, for GNU/Linux 3.2.0, with debug_info, not stripped
  $
  $ perf -vv | grep RUST
                  rust: [ on  ]  # HAVE_RUST_SUPPORT

I've noticed few more problem, maybe worth fixing it here as well:

Quoted 8 lines

* test-rust.bin is missing from the list of FILES, and thus is not removed by the clean target. This could lead to a false feature detection, since the binary stays there.

* Target to build test-rust.bin does not emit a dependencies file.

I've tested the following diff after first having rust installed, then removing it and building again.

I'll add it with your Signed-off-by, etc, please submit as a complete patch next time :-)

- Arnaldo

Quoted 23 lines
tools/build/feature/Makefile +3 -2
index 9ae69d85716..28d4ff0841a 100644
@@ -73,7 +73,8 @@ FILES=                                          \
          test-clang-bpf-co-re.bin              \
          test-file-handle.bin                  \
          test-libpfm4.bin                      \
-         test-libopenssl.bin
+         test-libopenssl.bin                   \
+         test-rust.bin
 
 FILES := $(addprefix $(OUTPUT),$(FILES))
 
@@ -112,7 +113,7 @@ __BUILD = $(CC) $(CFLAGS) -MD -Wall -Werror -o $@ $(patsubst %.bin,%.c,$(@F)) $(
 __BUILDXX = $(CXX) $(CXXFLAGS) -MD -Wall -Werror -o $@ $(patsubst %.bin,%.cpp,$(@F)) $(LDFLAGS)
   BUILDXX = $(__BUILDXX) > $(@:.bin=.make.output) 2>&1
 
-__BUILDRS = $(RUSTC) $(RUSTC_FLAGS) -o $@ $(patsubst %.bin,%.rs,$(@F))
+__BUILDRS = $(RUSTC) $(RUSTC_FLAGS) --emit=dep-info=$(patsubst %.bin,%.d,$(@F)),link -o $@ $(patsubst %.bin,%.rs,$(@F))
   BUILDRS = $(__BUILDRS) > $(@:.bin=.make.output) 2>&1
 
author maintainer
#3 lore raw
In reply to Dmitry Dolgov
Quoted 22 lines · Dmitry Dolgov
Quoted 6 lines · Arnaldo Carvalho de Melo

Features in FEATURE_TESTS_BASIC will be set as being available if test-all.c builds, so since the rust test isn't included in test-all.c, we can't have 'rust' in there, remove it from FEATURE_TESTS_BASIC and use feature-check so that it tries to build test-rust.bin, doing the actual feature detection.

My bad, thanks for fixing it.

Quoted 10 lines

And after installing it:

... rust: [ on ]

  $ cat /tmp/build/perf-tools-next/feature/test-rust.make.output
  $ file /tmp/build/perf-tools-next/feature/test-rust.bin
/tmp/build/perf-tools-next/feature/test-rust.bin: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=9c416edf673ee3705b97bae893a99a6fcf1ee258, for GNU/Linux 3.2.0, with debug_info, not stripped
  $
  $ perf -vv | grep RUST
                  rust: [ on  ]  # HAVE_RUST_SUPPORT

I've noticed few more problem, maybe worth fixing it here as well:

Quoted 8 lines

* test-rust.bin is missing from the list of FILES, and thus is not removed by the clean target. This could lead to a false feature detection, since the binary stays there.

* Target to build test-rust.bin does not emit a dependencies file.

I've tested the following diff after first having rust installed, then removing it and building again.

I tried here building it with rust installed, it detected, ok, then I removed and tried to rebuild, it still thinks rust is there:

⬢ [acme@toolbx perf-tools-next]$ rpm -qa | grep rust p11-kit-trust-0.25.8-1.fc43.x86_64
rust-srpm-macros-28.4-1.fc43.noarch
⬢ [acme@toolbx perf-tools-next]$ m
make: Entering directory '/home/acme/git/linux/tools/perf'
BUILD: Doing 'make -j32' parallel build
Warning: Kernel ABI header differences:
diff -u tools/arch/arm64/include/asm/cputype.h arch/arm64/include/asm/cputype.h diff -u tools/perf/arch/s390/entry/syscalls/syscall.tbl arch/s390/kernel/syscalls/syscall.tbl

Auto-detecting system features:

...                                   libdw: [ on  ]
...                                   glibc: [ on  ]
...                                  libelf: [ on  ]
...                                 libnuma: [ on  ]
...                  numa_num_possible_cpus: [ on  ]
...                               libpython: [ on  ]
...                             libcapstone: [ on  ]
...                               llvm-perf: [ on  ]
...                                    zlib: [ on  ]
...                                    lzma: [ on  ]
...                                     bpf: [ on  ]
...                                  libaio: [ on  ]
...                                 libzstd: [ on  ]
...                              libopenssl: [ on  ]
...                                    rust: [ on  ]

INSTALL libsubcmd_headers
INSTALL libapi_headers
INSTALL libperf_headers
INSTALL libsymbol_headers
INSTALL libbpf_headers
INSTALL binaries
INSTALL tests
INSTALL libperf-jvmti.so
INSTALL libexec
INSTALL perf-archive
INSTALL perf-iostat
INSTALL python-scripts
install: omitting directory 'scripts/python/Perf-Trace-Util/lib/Perf/Trace/__pycache__'
INSTALL dlfilters
INSTALL perf_completion-script
INSTALL perf-tip
make: Leaving directory '/home/acme/git/linux/tools/perf'
⬢ [acme@toolbx perf-tools-next]$

⬢ [acme@toolbx perf-tools-next]$ perf -vv | grep rust
rust: [ on ] # HAVE_RUST_SUPPORT
⬢ [acme@toolbx perf-tools-next]$

I.e. it doesn't rebuild perf and thus doesn't notice that it doesn't have what is needed, if I touch some other file and thus it triggers a rebuild, it still manages to build it...

⬢ [acme@toolbx perf-tools-next]$ perf -vv | grep rust
rust: [ on ] # HAVE_RUST_SUPPORT
⬢ [acme@toolbx perf-tools-next]$

Trying:

⬢ [acme@toolbx perf-tools-next]$ readelf -wi /tmp/build/perf-tools-next/tests/workloads/code_with_type.a | grep -m1 producer

    <c>   DW_AT_producer    : (indirect string, offset: 0): clang LLVM (rustc version 1.93.0 (254b59607 2026-01-19) (Fedora 1.93.0-1.fc43))

⬢ [acme@toolbx perf-tools-next]$ rm -f /tmp/build/perf-tools-next/tests/workloads/code_with_type.a
⬢ [acme@toolbx perf-tools-next]$
rustc /tmp/build/perf-tools-next/tests/workloads/code_with_type.a
/bin/sh: line 1: rustc: command not found
make[5]: *** [/home/acme/git/linux/tools/build/Makefile.build:120: /tmp/build/perf-tools-next/tests/workloads/code_with_type.a] Error 127 make[4]: *** [/home/acme/git/linux/tools/build/Makefile.build:156: workloads] Error 2
make[3]: *** [/home/acme/git/linux/tools/build/Makefile.build:156: tests] Error 2
make[2]: *** [Makefile.perf:782: /tmp/build/perf-tools-next/perf-test-in.o] Error 2
make[2]: *** Waiting for unfinished jobs....
make[1]: *** [Makefile.perf:286: sub-make] Error 2
make: *** [Makefile:119: install-bin] Error 2
make: Leaving directory '/home/acme/git/linux/tools/perf'
⬢ [acme@toolbx perf-tools-next]$

So there is still some dep missing...

- Arnaldo

Quoted 23 lines
tools/build/feature/Makefile +3 -2
index 9ae69d85716..28d4ff0841a 100644
@@ -73,7 +73,8 @@ FILES=                                          \
          test-clang-bpf-co-re.bin              \
          test-file-handle.bin                  \
          test-libpfm4.bin                      \
-         test-libopenssl.bin
+         test-libopenssl.bin                   \
+         test-rust.bin
 
 FILES := $(addprefix $(OUTPUT),$(FILES))
 
@@ -112,7 +113,7 @@ __BUILD = $(CC) $(CFLAGS) -MD -Wall -Werror -o $@ $(patsubst %.bin,%.c,$(@F)) $(
 __BUILDXX = $(CXX) $(CXXFLAGS) -MD -Wall -Werror -o $@ $(patsubst %.bin,%.cpp,$(@F)) $(LDFLAGS)
   BUILDXX = $(__BUILDXX) > $(@:.bin=.make.output) 2>&1
 
-__BUILDRS = $(RUSTC) $(RUSTC_FLAGS) -o $@ $(patsubst %.bin,%.rs,$(@F))
+__BUILDRS = $(RUSTC) $(RUSTC_FLAGS) --emit=dep-info=$(patsubst %.bin,%.d,$(@F)),link -o $@ $(patsubst %.bin,%.rs,$(@F))
   BUILDRS = $(__BUILDRS) > $(@:.bin=.make.output) 2>&1
 
#4 lore raw
Quoted 9 lines · Arnaldo Carvalho de Melo

I tried here building it with rust installed, it detected, ok, then I removed and tried to rebuild, it still thinks rust is there:

[...]

I.e. it doesn't rebuild perf and thus doesn't notice that it doesn't have what is needed, if I touch some other file and thus it triggers a rebuild, it still manages to build it...

Yeah, looks like the problem is due to rust not adding any actual depedencies into the dep-info file, when compiling the feature test file:

/perf/feature/test-rust.d: test-rust.rs

/perf/feature/test-rust.bin: test-rust.rs

test-rust.rs:

To fix that I've tried to add an extra dependency on the rust binary on top of that after the compilation is finished. While hacky, it worked just fine, and perf catched it when rust was removed. Haven't faced any issues after few rounds of installing/removing rust either. If this approach sound fine, I can post the patch tomorrow.

author maintainer
#5 lore raw
Quoted 17 lines · Dmitry Dolgov
Quoted 9 lines · Arnaldo Carvalho de Melo

I tried here building it with rust installed, it detected, ok, then I removed and tried to rebuild, it still thinks rust is there:

[...]

I.e. it doesn't rebuild perf and thus doesn't notice that it doesn't have what is needed, if I touch some other file and thus it triggers a rebuild, it still manages to build it...

Yeah, looks like the problem is due to rust not adding any actual depedencies into the dep-info file, when compiling the feature test file:

/perf/feature/test-rust.d: test-rust.rs

/perf/feature/test-rust.bin: test-rust.rs

Quoted 7 lines

test-rust.rs:

To fix that I've tried to add an extra dependency on the rust binary on top of that after the compilation is finished. While hacky, it worked just fine, and perf catched it when rust was removed. Haven't faced any issues after few rounds of installing/removing rust either. If this approach sound fine, I can post the patch tomorrow.

Looks hacky, can't we just try to run:

⬢ [acme@toolbx linux]$ HAVE_RUST=$(rustc --version > /dev/null ; echo $?) ⬢ [acme@toolbx linux]$ echo $HAVE_RUST
0
⬢ [acme@toolbx linux]$ HAVE_RUST=$(arustc --version > /dev/null ; echo $?)
bash: arustc: command not found
⬢ [acme@toolbx linux]$ echo $HAVE_RUST
127
⬢ [acme@toolbx linux]$

How would your dep on the rust binary work? where would you expect it to be? It may be installed on some different path, etc.

Perhaps there is precedent with some other component...

- Arnaldo

#6 lore raw
Quoted 10 lines · Arnaldo Carvalho de Melo
Quoted 6 lines

To fix that I've tried to add an extra dependency on the rust binary on top of that after the compilation is finished. While hacky, it worked just fine, and perf catched it when rust was removed. Haven't faced any issues after few rounds of installing/removing rust either. If this approach sound fine, I can post the patch tomorrow.

Looks hacky, can't we just try to run:

Quoted 8 lines

⬢ [acme@toolbx linux]$ HAVE_RUST=$(rustc --version > /dev/null ; echo $?) ⬢ [acme@toolbx linux]$ echo $HAVE_RUST
0
⬢ [acme@toolbx linux]$ HAVE_RUST=$(arustc --version > /dev/null ; echo $?)
bash: arustc: command not found
⬢ [acme@toolbx linux]$ echo $HAVE_RUST
127
⬢ [acme@toolbx linux]$

Do you mean replacing the whole feature check with this, instead of compiling a dummy code?

Quoted 2 lines

How would your dep on the rust binary work? where would you expect it to be? It may be installed on some different path, etc.

It has to be in the PATH, so the actual binary could be picked up with "which" (it seems to be used in one other place there as well).

Quoted 1 line

Perhaps there is precedent with some other component...

I haven't found anything similar among the other features, but I can check out other components as well.