Hi,
I’ve got an unusual scenario where I’m trying to build and load a shared library as a plugin to clang, and I’m struggling to figure out how to express this with CMake.
The project has two parts:
- the sources that are used to build a shared library,
some_library.so
. It must be a shared library. - the sources that use
some_library.so
– not by linking against it, but loading it into clang via the flag-fpass-plugin=/path/to/some_library.so
Part 1 works without issue: add_library(some_library SHARED src.cpp)
.
The problem shows up with part 2. When I write
target_compile_options(part_2 "-fpass-plugin=$<TARGET_FILE:some_library>")
as far as CMake is concerned, part_2
doesn’t really depend on some_library
, since that dependence happens implicitly through the custom flag. This means part_2
gets compiled before some_library.so
is actually built, so the compilation fails:
clang: can't load "/path/to/some_library.so", file doesn't exist
I’ve tried to explicitly specify this dependence by
add_dependencies(part_2 some_library)
but that also doesn’t ensure that part_2
is built after some_library
(presumably because some_library
is a shared library, so CMake thinks part_2
and some_library
can still be built in parallel).
I’ve also tried making an intermediate file (that is not a shared library) to depend on
add_custom_command(
TARGET some_library
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E echo "built!" > ${CMAKE_BINARY_DIR}/some_library.built
BYPRODUCTS ${CMAKE_BINARY_DIR}/some_library.built
)
add_dependencies(part_2 ${CMAKE_BINARY_DIR}/some_library.built)
but this also doesn’t work. part_2
is still being compiled in parallel with some_library
.
Is there some way to ensure that part_2
will be compiled only once some_library.so
has finished building?