Reputation: 11
CMake sets directories for objects files implicitly while compiling the source files, but I want to keep all those object files in a particular directory.
For example in my code CMake is creating directories based on the source code directories itself like this:
CMakeFiles/OBJ2.dir/src/lib/kernels/XnnKernel.o
I want to keep them in different locations, but unable to do so.
I tried some commands:
set(OBJ "build/CMakeFiles/OBJ1.dir/src/lib/kernels/igemm8_generic.o")
file(COPY ${OBJ} DESTINATION ${SOURCE_PATH}/src/OBJ_FILES)
As per my understanding, the file(COPY)
command is getting executed before compilation. Please correct me if I am wrong and give me a solution.
Upvotes: 1
Views: 4961
Reputation: 2329
You could use a combination of generator expressions with a custom command. The basic functionality is based on the CMake capability of creating object libraries.
project structure:
.
├── CMakeLists.txt
└── src
└── foo.c
sample CMakeLists.txt
:
cmake_minimum_required(VERSION 3.11)
project(foo C)
set(SOURCES "src/foo.c")
set(LIBNAME "foo")
set(LIBNAME_OBJ "${LIBNAME}_obj")
add_library(${LIBNAME_OBJ} OBJECT ${SOURCES})
target_include_directories(${LIBNAME_OBJ} PUBLIC "include")
target_compile_options(${LIBNAME_OBJ} PUBLIC "-fPIC")
add_library(${LIBNAME} SHARED $<TARGET_OBJECTS:${LIBNAME_OBJ}>)
set(OBJ_DIR ${CMAKE_CURRENT_BINARY_DIR}/obj_dir)
add_custom_command(TARGET ${LIBNAME}
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E make_directory ${OBJ_DIR}
COMMAND ${CMAKE_COMMAND} -E copy_if_different
$<TARGET_OBJECTS:${LIBNAME_OBJ}> ${OBJ_DIR})
The file
command does not support generator expressions with filesystem operations.
Upvotes: 2