error: building the downstream libh3 C library using the Solana SDK via cmake

Viewed 196

Solana has this helloworld program that you can build (and deploy on a validator): https://github.com/solana-labs/example-helloworld

I am trying to add this simple dependency to the helloworld example

h3ron = { git = "https://github.com/nmandery/h3ron.git"}

running npm run build:program-rust would trigger this error:

 Compiling h3ron-h3-sys v0.12.0 (https://github.com/nmandery/h3ron.git#ab3a27f3)
error: failed to run custom build command for `h3ron-h3-sys v0.12.0 (https://github.com/nmandery/h3ron.git#ab3a27f3)`

Caused by:
  process didn't exit successfully: `/home/pc/Documents/funspace/crypto-world/lab/solanaworkspace/example-helloworld/src/program-rust/target/release/build/h3ron-h3-sys-1ab6cd26d7e5abe9/build-script-build` (exit status: 101)
  --- stdout
  cargo:rerun-if-changed=libh3
  CMAKE_TOOLCHAIN_FILE_bpfel-unknown-unknown = None
  CMAKE_TOOLCHAIN_FILE_bpfel_unknown_unknown = None
  TARGET_CMAKE_TOOLCHAIN_FILE = None
  CMAKE_TOOLCHAIN_FILE = None
  CMAKE_GENERATOR_bpfel-unknown-unknown = None
  CMAKE_GENERATOR_bpfel_unknown_unknown = None
  TARGET_CMAKE_GENERATOR = None
  CMAKE_GENERATOR = None
  CMAKE_PREFIX_PATH_bpfel-unknown-unknown = None
  CMAKE_PREFIX_PATH_bpfel_unknown_unknown = None
  TARGET_CMAKE_PREFIX_PATH = None
  CMAKE_PREFIX_PATH = None
  CMAKE_bpfel-unknown-unknown = None
  CMAKE_bpfel_unknown_unknown = None
  TARGET_CMAKE = None
  CMAKE = None
  running: "cmake" "/home/pc/.cargo/git/checkouts/h3ron-31217b227e8cd3a6/ab3a27f/h3ron-h3-sys/libh3" "-DBUILD_BENCHMARKS=OFF" "-DBUILD_FILTERS=OFF" "-DBUILD_GENERATORS=OFF" "-DBUILD_TESTING=OFF" "-DENABLE_COVERAGE=OFF" "-DENABLE_DOCS=OFF" "-DENABLE_FORMAT=OFF" "-DENABLE_LINTING=OFF" "-DCMAKE_INSTALL_PREFIX=/home/pc/Documents/funspace/crypto-world/lab/solanaworkspace/example-helloworld/src/program-rust/target/bpfel-unknown-unknown/release/build/h3ron-h3-sys-df478ba73015eab9/out" "-DCMAKE_C_FLAGS= -ffunction-sections -fdata-sections -fPIC --target=bpfel-unknown-unknown" "-DCMAKE_C_COMPILER=/home/pc/.local/share/solana/install/releases/1.9.2/solana-release/bin/sdk/bpf/dependencies/bpf-tools/llvm/bin/clang" "-DCMAKE_CXX_FLAGS= -ffunction-sections -fdata-sections -fPIC" "-DCMAKE_CXX_COMPILER=/usr/bin/c++" "-DCMAKE_ASM_FLAGS= -ffunction-sections -fdata-sections -fPIC --target=bpfel-unknown-unknown" "-DCMAKE_ASM_COMPILER=/home/pc/.local/share/solana/install/releases/1.9.2/solana-release/bin/sdk/bpf/dependencies/bpf-tools/llvm/bin/clang" "-DCMAKE_BUILD_TYPE=Release"
  -- The C compiler identification is Clang 13.0.0
  -- Detecting C compiler ABI info
  -- Detecting C compiler ABI info - failed
  -- Check for working C compiler: /home/pc/.local/share/solana/install/releases/1.9.2/solana-release/bin/sdk/bpf/dependencies/bpf-tools/llvm/bin/clang
  -- Check for working C compiler: /home/pc/.local/share/solana/install/releases/1.9.2/solana-release/bin/sdk/bpf/dependencies/bpf-tools/llvm/bin/clang - broken
  -- Configuring incomplete, errors occurred!
  See also "/home/pc/Documents/funspace/crypto-world/lab/solanaworkspace/example-helloworld/src/program-rust/target/bpfel-unknown-unknown/release/build/h3ron-h3-sys-df478ba73015eab9/out/build/CMakeFiles/CMakeOutput.log".
  See also "/home/pc/Documents/funspace/crypto-world/lab/solanaworkspace/example-helloworld/src/program-rust/target/bpfel-unknown-unknown/release/build/h3ron-h3-sys-df478ba73015eab9/out/build/CMakeFiles/CMakeError.log".

  --- stderr
  CMake Error at /usr/local/share/cmake-3.22/Modules/CMakeTestCCompiler.cmake:69 (message):
    The C compiler

      "/home/pc/.local/share/solana/install/releases/1.9.2/solana-release/bin/sdk/bpf/dependencies/bpf-tools/llvm/bin/clang"

    is not able to compile a simple test program.

    It fails with the following output:

      Change Dir: /home/pc/Documents/funspace/crypto-world/lab/solanaworkspace/example-helloworld/src/program-rust/target/bpfel-unknown-unknown/release/build/h3ron-h3-sys-df478ba73015eab9/out/build/CMakeFiles/CMakeTmp

      Run Build Command(s):/usr/bin/make -f Makefile cmTC_006a3/fast && /usr/bin/make  -f CMakeFiles/cmTC_006a3.dir/build.make CMakeFiles/cmTC_006a3.dir/build
      make[1]: Entering directory '/home/pc/Documents/funspace/crypto-world/lab/solanaworkspace/example-helloworld/src/program-rust/target/bpfel-unknown-unknown/release/build/h3ron-h3-sys-df478ba73015eab9/out/build/CMakeFiles/CMakeTmp'
      Building C object CMakeFiles/cmTC_006a3.dir/testCCompiler.c.o
      /home/pc/.local/share/solana/install/releases/1.9.2/solana-release/bin/sdk/bpf/dependencies/bpf-tools/llvm/bin/clang   -ffunction-sections -fdata-sections -fPIC --target=bpfel-unknown-unknown  -std=gnu99 -MD -MT CMakeFiles/cmTC_006a3.dir/testCCompiler.c.o -MF CMakeFiles/cmTC_006a3.dir/testCCompiler.c.o.d -o CMakeFiles/cmTC_006a3.dir/testCCompiler.c.o -c /home/pc/Documents/funspace/crypto-world/lab/solanaworkspace/example-helloworld/src/program-rust/target/bpfel-unknown-unknown/release/build/h3ron-h3-sys-df478ba73015eab9/out/build/CMakeFiles/CMakeTmp/testCCompiler.c
      Linking C executable cmTC_006a3
      /usr/local/bin/cmake -E cmake_link_script CMakeFiles/cmTC_006a3.dir/link.txt --verbose=1
      /home/pc/.local/share/solana/install/releases/1.9.2/solana-release/bin/sdk/bpf/dependencies/bpf-tools/llvm/bin/clang  -ffunction-sections -fdata-sections -fPIC --target=bpfel-unknown-unknown  -rdynamic CMakeFiles/cmTC_006a3.dir/testCCompiler.c.o -o cmTC_006a3
      /usr/bin/ld: unknown architecture of input file `CMakeFiles/cmTC_006a3.dir/testCCompiler.c.o' is incompatible with i386:x86-64 output
      collect2: error: ld returned 1 exit status
      clang-13: error: linker (via gcc) command failed with exit code 1 (use -v to see invocation)
      make[1]: *** [CMakeFiles/cmTC_006a3.dir/build.make:100: cmTC_006a3] Error 1
      make[1]: Leaving directory '/home/pc/Documents/funspace/crypto-world/lab/solanaworkspace/example-helloworld/src/program-rust/target/bpfel-unknown-unknown/release/build/h3ron-h3-sys-df478ba73015eab9/out/build/CMakeFiles/CMakeTmp'
      make: *** [Makefile:127: cmTC_006a3/fast] Error 2





    CMake will not be able to correctly generate this project.
  Call Stack (most recent call first):
    CMakeLists.txt:59 (project)


  thread 'main' panicked at '
  command did not execute successfully, got: exit status: 1

  build script failed, must exit now', /home/pc/.cargo/registry/src/github.com-1ecc6299db9ec823/cmake-0.1.46/src/lib.rs:974:5
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
warning: build failed, waiting for other jobs to finish...
error: build failed

Now the problem is building the downstream libh3 C library using the Solana SDK via cmake. It appears cmake correctly detects the C compiler of the SDK, but is not able to find a suitable linker. So it falls back to your systems default linker, but that one does not support the bpfel architecture. I suppose there should be a suitable linker with in the SDK in the same directory as the compiler found by cmake.

How can I fix this please?

1 Answers

The issue has to do with the linker passed down to CMake during the dependent build. We want CMake to use the entire sysroot and linker provided by bpf-tools in $HOME/.local/share/solana/install/active_release/bin/sdk/bpf/dependencies/bpf-tools/llvm/bin/lld. I was able to get the correct linker at my clone: https://github.com/joncinque/h3ron

Unfortunately, there's still an issue because the build uses the wrong header files, opting for the /usr/include ones instead of the ones provided by the bpf tools at $HOME/.local/share/solana/install/active_release/bin/sdk/bpf/dependencies/bpf-tools/

Someone who knows more about CMake would probably be able to help you point the compiler at the correct headers instead of the default ones.

For reference, the error is now:

/usr/include/stdint.h:26:10: fatal error: 'bits/libc-header-start.h' file not found

OLD ANSWER, NOT USEFUL:

This may be resolved in one of the newer releases, once cargo build-bpf defaults to always using rust-lld instead of looking for the system linker.

Check the commit at https://github.com/solana-labs/rust/commit/87ba5c61a5fe7f6fc5b74298127592dd0bc38389

The change to use bpf-tools v1.21 should be available starting with 1.9.3, can you update your Solana release to the current latest with solana-install init 1.9.4 and try again?

Related