Last active
August 29, 2015 14:12
-
-
Save urkle/5eaebd631dc8277370d2 to your computer and use it in GitHub Desktop.
Copy File cmake function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cmake_minimum_required(VERSION 2.8.12) | |
function(CopyFile target copy_file dest_path) | |
if(NOT IS_ABSOLUTE ${copy_file}) | |
set(copy_file ${CMAKE_CURRENT_SOURCE_DIR}/${copy_file}) | |
endif() | |
set(config ) | |
set(_mode ) | |
foreach(entry ${ARGN}) | |
if(entry STREQUAL "ONLY") | |
set(_mode ${entry}) | |
else() | |
if(_mode STREQUAL "ONLY") | |
list(APPEND config ${entry}) | |
endif() | |
endif() | |
endforeach() | |
if(config) | |
get_filename_component(_filename ${copy_file} NAME) | |
string(REGEX REPLACE "[^a-zA-Z0-9_]+" "_" clean_file ${_filename}) | |
set(_SCRIPT_FILE "${CMAKE_CURRENT_BINARY_DIR}/Copy_${clean_file}.cmake") | |
file(WRITE ${_SCRIPT_FILE} | |
"# Generated script file\n" | |
"if(DO_COPY)\n" | |
" file(COPY ${copy_file} DESTINATION \${DEST})\n" | |
"endif()\n" | |
) | |
set(_do_copy "") | |
foreach(_conf ${config}) | |
set(_do_copy "$<CONFIG:${_conf}>,${_do_copy}") | |
endforeach() | |
if(NOT _do_copy) | |
message(FATAL_ERROR "No configurations passed to ONLY?") | |
endif() | |
set(_do_copy "$<OR:${_do_copy}0>") | |
add_custom_command(TARGET ${target} | |
POST_BUILD | |
COMMAND ${CMAKE_COMMAND} -DDEST="$<TARGET_FILE_DIR:${target}>/${dest_path}" -DDO_COPY=${_do_copy} -P "${_SCRIPT_FILE}" | |
) | |
else() | |
add_custom_command( TARGET ${target} POST_BUILD | |
COMMAND ${CMAKE_COMMAND} -E copy_if_different | |
"${copy_file}" | |
"\"$<TARGET_FILE_DIR:${target}>/${dest_path}\"" | |
) | |
endif() | |
endfunction() | |
add_executable(MyTarget | |
main.c | |
) | |
CopyFile(MyTarget "test.txt" "") | |
CopyFile(MyTarget "test_debug.txt" "" ONLY Debug RelWithDebInfo) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int main() | |
{ | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
always copied |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Only copied in debug build type |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment