-
-
Save socantre/7ee63133a0a3a08f3990 to your computer and use it in GitHub Desktop.
set(LIBFOO_TAR_HEADERS | |
"${CMAKE_CURRENT_BINARY_DIR}/include/foo/foo.h" | |
"${CMAKE_CURRENT_BINARY_DIR}/include/foo/foo_utils.h" | |
) | |
add_custom_command(OUTPUT ${LIBFOO_TAR_HEADERS} | |
COMMAND ${CMAKE_COMMAND} -E tar xzf "${CMAKE_CURRENT_SOURCE_DIR}/libfoo/foo.tar" | |
COMMAND ${CMAKE_COMMAND} -E touch ${LIBFOO_TAR_HEADERS} | |
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/include/foo" | |
DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/libfoo/foo.tar" | |
COMMENT "Unpacking foo.tar" | |
VERBATIM | |
) | |
add_custom_target(libfoo_untar DEPENDS ${LIBFOO_TAR_HEADERS}) | |
add_library(foo INTERFACE) | |
target_include_directories(foo INTERFACE "${CMAKE_CURRENT_BINARY_DIR}/include/foo") | |
target_link_libraries(foo INTERFACE ${FOO_LIBRARIES}) |
So I ended up doing this: A macro called add_something that creates a target [ combining
add_custom_command
+add_custom_target
]
Every time I do NOT find something like this macro in plain CMake it makes me feel like I still don't understand CMake.
But no, it's apparently CMake that does not understand the most basic feature people need.
Best page found so far:
https://samthursfield.wordpress.com/2015/11/21/cmake-dependencies-between-targets-and-files-and-custom-commands/
Thank you for posting this. I am perplexed about one thing though. add_library
and add_executable
know that source files are present in ${CMAKE_CURRENT_SOURCE_DIR}
so we don't need to explicitly mention it. But, add_custom_command
needs the full path to a file in the current source directory. So,
add_custom_command(OUTPUT cross_compile_file.o
${CXX_CROSS_COMPILER} -c cross_compiled_file.cpp
MAIN_DEPENDENCY cross_compiled_file.cpp
VERBATIM)
results in no input files: cannot find cross_compiled_file.cpp
error from the compiler. The behavior of cmake seems inconsistent here.
I guess the reasoning is that the custom_command could be anything so cmake isn't going to assume anything.
Helps a lot. Thanks :)