How to make cmake use -shared -dlink

Hi,

This is my first post here.
I am trying to write a CMakeLists.txt that will build a HIP test code (hipify from cuda):

hipcc --offload-arch=gfx908 -fPIC -fgpu-rdc -std=c++17 -w -c domain/util.cu
hipcc --offload-arch=gfx908 -fPIC -fgpu-rdc -std=c++17 -w -c domain/test.cu -Iinclude
hipcc -fPIC -shared -dlink util.o test.o -o device_link.o -fgpu-rdc --hip-link
/opt/rocm/llvm/bin/clang++ src/main.cpp util.o test.o device_link.o -o exe -L/opt/rocm/hip/lib -lamdhip64 -Iinclude

My cmake version is 3.22.1

I start with:

cmake -S . -B build
– The CXX compiler identification is GNU 7.5.0
– The C compiler identification is GNU 7.5.0
– Detecting CXX compiler ABI info
– Detecting CXX compiler ABI info - done
– Check for working CXX compiler: /usr/bin/c++ - skipped
– Detecting CXX compile features
– Detecting CXX compile features - done
– Detecting C compiler ABI info
– Detecting C compiler ABI info - done
– Check for working C compiler: /usr/bin/cc - skipped
– Detecting C compile features
– Detecting C compile features - done
– Found HIP: /opt/rocm-4.3.1/hip (found version “4.3.21331-94fc2572”)
– Found HIP: 4.3.21331-94fc2572
– Looking for a HIP compiler
– Looking for a HIP compiler - /opt/rocm-4.3.1/llvm/bin/clang++
– The HIP compiler identification is Clang 13.0.0
– Detecting HIP compiler ABI info
– Detecting HIP compiler ABI info - done
– Check for working HIP compiler: /opt/rocm-4.3.1/llvm/bin/clang++ - skipped
– Detecting HIP compile features
– Detecting HIP compile features - done
– Configuring done
– Generating done
– Build files have been written to: build

My CMakeLists.txt have add_library and $<TARGET_OBJECTS:test_obj> but I am unable to make cmake build the object:

cd build; make test_obj VERBOSE=1
make[3]: Nothing to be done for ‘domain/CMakeFiles/test_obj.dir/build’.

What did I miss ?

Thank you for your help.

The solution is to add:

in CMakeLists.txt

   set(CMAKE_HIP_FLAGS "-fPIC -fgpu-rdc -w")

and in domain/CMakeLists.txt

set_source_files_properties(test.cu util.cu PROPERTIES LANGUAGE HIP)
add_library(test_obj OBJECT test.cu)
target_include_directories(test_obj PRIVATE ${PROJECT_SOURCE_DIR}/include)

and in src/CMakeLists.txt

set_source_files_properties(main.cpp PROPERTIES LANGUAGE HIP)

see this commit

If you use the file extension .hip instead of .cu CMake should automatically detect the right language.