compile and link separately

I need to pass a specific compiler switch to CXX compiler by following statement, in this example use clang and arg: -emit-llvm but during generation, it complains the linker can not use this argument. Apparently cmake is passing this arg to both during object file generation and linking stage.
How do I tell cmake such that, compiling and linking will use different arguments, or more so, tell it to do whole stages separately?

Here is my code:
cmake_minimum_required(VERSION 3.10)
set(ROCMOPENCL /opt/rocm/opencl/)
set(CMAKE_C_COMPILER /opt/rocm-4.1.0/llvm//bin//clang -emit-llvm)
set(CMAKE_CXX_COMPILER /opt/rocm-4.1.0/llvm//bin//clang)
message( “ROCM library path:” ${ROCMOPENCL} )
#add_subdirectory(${ROCMOPENCL}/lib/ build)

foreach(var p25 p31 p188 p41 ex-code-1)
message(STATUS “Generating makefile for ${var}”)
add_executable(${var} ${var}.c)
add_executable(${var}.out ${var}.c)
target_link_libraries(${var} PUBLIC ${ROCMOPENCL}/lib/
target_include_directories(${var} PUBLIC ${ROCMOPENCL}/include)

output of cmake …

– Generating makefile for p25
– The C compiler identification is unknown
– The CXX compiler identification is Clang 12.0.0
– Check for working C compiler: /opt/rocm-4.1.0/llvm//bin//clang
– Check for working C compiler: /opt/rocm-4.1.0/llvm//bin//clang – broken
CMake Error at /usr/local/share/cmake-3.16/Modules/CMakeTestCCompiler.cmake:60 (message):
The C compiler


is not able to compile a simple test program.

It fails with the following output:

Change Dir: /

Run Build Command(s):/usr/bin/make cmTC_4eae3/fast && /usr/bin/make  -f CMakeFiles/cmTC_4eae3.dir/build.make CMakeFiles/cmTC_4eae3.dir/build
make[1]: Entering directory '/'
Building C object CMakeFiles/cmTC_4eae3.dir/testCCompiler.c.o
/opt/rocm-4.1.0/llvm//bin//clang -emit-llvm    -o CMakeFiles/cmTC_4eae3.dir/testCCompiler.c.o   -c /
Linking C executable cmTC_4eae3
/usr/local/bin/cmake -E cmake_link_script CMakeFiles/cmTC_4eae3.dir/link.txt --verbose=1
/opt/rocm-4.1.0/llvm//bin//clang -emit-llvm      CMakeFiles/cmTC_4eae3.dir/testCCompiler.c.o  -o cmTC_4eae3
clang-12: error: -emit-llvm cannot be used when linking
CMakeFiles/cmTC_4eae3.dir/build.make:89: recipe for target 'cmTC_4eae3' failed
make[1]: *** [cmTC_4eae3] Error 1
make[1]: Leaving directory '/'
Makefile:124: recipe for target 'cmTC_4eae3/fast' failed
make: *** [cmTC_4eae3/fast] Error 2

I was at least able to set the compile flag without affecting link flag:
it worked so far:
set(CMAKE_C_COMPILER /opt/rocm-4.1.0/llvm//bin//clang)
set(CMAKE_CXX_COMPILER /opt/rocm-4.1.0/llvm//bin//clang)
set(CMAKE_CXX_FLAGS -emi-llvm)

This is the (or at least one) right way of doing it. CMake prefers to use the compiler to launch the compiler, so adding flags to the compiler will end up being used in both contexts.