hdf5 icon indicating copy to clipboard operation
hdf5 copied to clipboard

cross-compile code, using already compiled target hdf5 lib in the sysroot

Open user706 opened this issue 2 years ago • 5 comments

Hi,

(yes this is no bug... but anyway...)

this is not about cross-compiling hdf5, but about using a cross-compiler to compile (raspberry pi) armhf code and link against a sysroot (so the sysroot contains the armhf version of hdf5 already; no need to compile that).

I'm using cmake with

#...
set(CMAKE_SYSROOT /sysroot)
#...
set(HDF5_FIND_DEBUG TRUE)
set(HDF5_ROOT ${CMAKE_SYSROOT})
find_package(HDF5 COMPONENTS CXX REQUIRED)

It fails with:

-- HDF5 CXX compiler wrapper is unable to compile a minimal HDF5 program.
CMake Error at /usr/share/cmake-3.22/Modules/FindPackageHandleStandardArgs.cmake:230 (message):
  Could NOT find HDF5 (missing: HDF5_LIBRARIES HDF5_INCLUDE_DIRS CXX) (found
  version "")
Call Stack (most recent call first):
  /usr/share/cmake-3.22/Modules/FindPackageHandleStandardArgs.cmake:594 (_FPHSA_FAILURE_MESSAGE)
  /usr/share/cmake-3.22/Modules/FindHDF5.cmake:1010 (find_package_handle_standard_args)
  CMakeLists.txt:56 (find_package)

So the thing is... this gets called:

/sysroot/bin/h5c++  /code/backend/build_arm_ubuntu/CMakeFiles/hdf5/cmake_hdf5_test.cxx

in this line https://gitlab.kitware.com/cmake/cmake/-/blob/0bfd4f1ed68180d3912386fb53d559b2a9e84b1b/Modules/FindHDF5.cmake#L364

resulting in -- HDF5 CXX compiler wrapper is unable to compile a minimal HDF5 program..

I can inspect it by adding -show flag:

/sysroot/bin/h5c++ -show  /code/backend/build_arm_ubuntu/CMakeFiles/hdf5/cmake_hdf5_test.cxx

which gives

arm-linux-gnueabihf-g++ -I/usr/include/hdf5/serial -D_FILE_OFFSET_BITS=64 -c /code/backend/build_arm_ubuntu/CMakeFiles/hdf5/cmake_hdf5_test.cxx
arm-linux-gnueabihf-g++ -I/usr/include/hdf5/serial -D_FILE_OFFSET_BITS=64 cmake_hdf5_test.o -L/usr/lib/arm-linux-gnueabihf/hdf5/serial /usr/lib/arm-linux-gnueabihf/hdf5/serial/libhdf5_hl_cpp.a /usr/lib/arm-linux-gnueabihf/hdf5/serial/libhdf5_cpp.a /usr/lib/arm-linux-gnueabihf/hdf5/serial/libhdf5_hl.a /usr/lib/arm-linux-gnueabihf/hdf5/serial/libhdf5.a -lpthread -lsz -lz -ldl -lm -Wl,-rpath -Wl,/usr/lib/arm-linux-gnueabihf/hdf5/serial

Juck! It's using the hosts include path (-I/usr/include/hdf5/serial) instead of the sysroot's include path (-I/sysroot/usr/include/hdf5/serial). And the same with the libs.

Is this known? How do you guys cross-compile and properly link to a sysroot's hdf5?

user706 avatar Oct 27 '22 19:10 user706

I think I probably need to do something like this... (but hate it, as it modifies my sysroot away from the way it would look on the actual raspberry pi):

On raspberry pi (or chroot): "destroy" the image, just so that it works for cross-compiling:

sudo apt install hdf5-tools
cd /usr/bin
for tool in h5cc h5c++ h5fc; do h5redeploy -prefix=/sysroot/usr -exec-prefix=/sysroot/usr/lib/arm-linux-gnueabihf -tool=$tool; done

I'll try it now (and let you know), but would love to know how you guys cross-compile.

user706 avatar Oct 27 '22 19:10 user706

ok, now kindof working, but super-ugly:

first run this (to be run on raspberry pi or in chroot -- the result is only for the sysroot! It will no longer work natively on the raspberry pi):

sudo apt install hdf5-tools
cd /usr/bin
for tool in h5cc h5c++ h5fc; do 
    echo yes | h5redeploy -prefix=/sysroot/usr -exec-prefix=/sysroot/usr/lib/arm-linux-gnueabihf  -libdir=/sysroot/usr/lib/arm-linux-gnueabihf -includedir=/sysroot/usr/include
done

then when crosscompiling, use this enormously ugly hack...

export HDF5_CXX="arm-linux-gnueabihf-g++ --sysroot /sysroot"  # yes, ugly as hell hack! misusing this to insert stuff into the script!
export HDF5_CLINKER="$HDF5_CXX" 
#           |
#      HDF5_CXXLINKER not used in script (hmmm...)
export HDF5_CXXLINKER="$HDF5_CXX"   # if it is fixed (maybe it is already, in the latest version)

PS: Regarding the ugly as hell hack (see comment in code above)... thank you for not putting quotation marks ("") around $CC and $CLINKER in these lines: lineA, lineB. Please keep it that way.

user706 avatar Oct 27 '22 21:10 user706

One similar but better method, that leaves the raspberry pi image in a good state, is to copy /sysroot/usr/bin/{h5cc,h5c++,h5fc} to e.g. /tools/ and then run /sysroot/usr/bin/h5redeploy in that folder and in the end set the exported environment variables (as above) and then also ensure that /tools has highest priority in your path:

export PATH="/tools:$PATH"

user706 avatar Oct 28 '22 09:10 user706

Another choice is to not use the h5cc(and such) and use CMake to properly find the CMake hdf5config.cmake files and use the imported targets using CMake build commands (add_executable, target_include_directories, target_link_libraries, etc)

byrnHDF avatar Mar 15 '23 21:03 byrnHDF

Another choice is to not use the h5cc(and such) and use CMake to properly find the CMake hdf5config.cmake files and use the imported targets using CMake build commands (add_executable, target_include_directories, target_link_libraries, etc)

Rereading this, was hdf5 built with autotools? That would explain why you are using h5redeploy script and FindHDF5 module is executed. It would be interesting to know if a CMake build of HDF5 would have the same issues.

byrnHDF avatar Mar 15 '24 13:03 byrnHDF