Cmake custom command to copy and rename

I have an existing cmake file that generates a .so library. I want to change it so that it then makes a copy of .so name something else. This is roughly what I have:

CMAKE_MINIMUM_REQUIRED(VERSION 2.8.7) PROJECT(test1) SET(TEST_SOURCES f1.c) ADD_LIBRARY(test SHARED ${TEST_SOURCES}) ADD_CUSTOM_COMMAND( OUTPUT custom1 COMMAND cp libtest.so custom1 DEPENDS libtest.so) 

I understand that there are better ways than hardcoding the name of the library, I just do it while I try to figure out how to make it work at all. What am I missing that will lead to the execution of my copy / rename command? Note. This is not an installation time. Thanks

+8
source share
3 answers
 add_custom_target(CopyAndRename COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_BINARY_DIR}/test.so ${CMAKE_BINARY_DIR}/SomeThingElse.so ) 

add_custom_target introduces a new target named CopyAndRename. You can run it with:

 make CopyAndRename 
+6
source

I would stick with the original idea of ​​using add_custom_command . However, I would recommend using the add_custom_command(TARGET ...) form rather than add_custom_command(OUTPUT ...) .

The OUTPUT version is designed to use the output file as a source file for another CMake target in the same CMakeLists.txt file. The user command is called when this goal is built (right at the beginning), since the goal requires the file to exist at this point.

In your case, not one goal depends on your file - this is the product of the build process.

If you use add_custom_command(TARGET ...) , this ensures that the command is executed every time the target is rebuilt. Using add_custom_target requires you to keep this in mind if the original target is updated ( test in this case). It also unnecessarily adds to your list of goals - perhaps this is not a problem for a tiny project, but it can be for large projects.

This is how I installed the command:

 SET(COPY_TO_PATH custom1) ADD_CUSTOM_COMMAND(TARGET test POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:test> ${COPY_TO_PATH} COMMENT "Copying 'test' library to '${COPY_TO_PATH}'") 

This adds the command as an event after the test build. As you correctly identified, the names of hardcoding libraries are small; therefore CMake provides "generator expressions" to handle this. The generator expression $<TARGET_FILE:test> resolves the full path of the library created by test , regardless of platform or compiler. Note that generator expressions can only be used in the COMMAND argument; you could not use them in the COMMENT part, for example.

The actual command invokes CMake itself ( ${CMAKE_COMMAND} ) with the -E option on the command line . This provides a cross-platform way to get a copy, since cp not a Windows command. To see all the -E options, run cmake -E help .

+16
source

You may consider using configure_file

 configure_file("path-to-your-so-file.so" "new-path-to-your-so-file.so" COPYONLY) 
0
source

Source: https://habr.com/ru/post/953048/


All Articles