| 1 |
cmake_minimum_required(VERSION 3.14) |
| 2 |
project(ToClean) |
| 3 |
|
| 4 |
set(CMAKE_INTERMEDIATE_DIR_STRATEGY FULL CACHE STRING "" FORCE) |
| 5 |
|
| 6 |
# Utility variables |
| 7 |
set(CSD ${CMAKE_CURRENT_SOURCE_DIR}) |
| 8 |
set(CBD ${CMAKE_CURRENT_BINARY_DIR}) |
| 9 |
set(CLEAN_FILE_CONTENT "File registered for cleaning.\n") |
| 10 |
|
| 11 |
# Lists build-time-generated files that should be cleaned away |
| 12 |
set_property(GLOBAL PROPERTY TOCLEAN_FILES "") |
| 13 |
function(addCleanFile FILENAME) |
| 14 |
set_property(GLOBAL APPEND PROPERTY TOCLEAN_FILES "${FILENAME}") |
| 15 |
endfunction() |
| 16 |
function(writeCleanFile FILENAME) |
| 17 |
file(WRITE "${FILENAME}" ${CLEAN_FILE_CONTENT}) |
| 18 |
endfunction() |
| 19 |
|
| 20 |
set(DUMMY_CONTENT_FILE ${CSD}/toclean.cxx) |
| 21 |
|
| 22 |
# Build a simple project whose compiled objects should be cleaned. |
| 23 |
add_executable(toclean toclean.cxx) |
| 24 |
addCleanFile( |
| 25 |
"${CBD}${CMAKE_FILES_DIRECTORY}/toclean.dir/toclean.cxx${CMAKE_CXX_OUTPUT_EXTENSION}") |
| 26 |
|
| 27 |
# Create a post build custom command that copies a dummy file |
| 28 |
# to a custom location |
| 29 |
function(addPostBuildFile TARGET FILENAME) |
| 30 |
add_custom_command(TARGET ${TARGET} POST_BUILD |
| 31 |
COMMAND ${CMAKE_COMMAND} |
| 32 |
ARGS -E copy ${DUMMY_CONTENT_FILE} ${FILENAME}) |
| 33 |
endfunction() |
| 34 |
|
| 35 |
# Create a custom command whose output should be cleaned. |
| 36 |
set(CustomCommandFile "${CBD}/CustomCommandFile.txt") |
| 37 |
add_custom_command(OUTPUT ${CustomCommandFile} |
| 38 |
DEPENDS ${DUMMY_CONTENT_FILE} |
| 39 |
COMMAND ${CMAKE_COMMAND} |
| 40 |
ARGS -E copy ${DUMMY_CONTENT_FILE} ${CustomCommandFile}) |
| 41 |
add_custom_target(customTarget ALL DEPENDS ${CustomCommandFile}) |
| 42 |
addCleanFile(${CustomCommandFile}) |
| 43 |
|
| 44 |
|
| 45 |
### Tests ADDITIONAL_MAKE_CLEAN_FILES directory property |
| 46 |
if("${CMAKE_GENERATOR}" MATCHES "Makefile") |
| 47 |
# Create a file that must be registered for cleaning. |
| 48 |
set(MakeDirPropFileAbs "${CBD}/MakeDirPropFile.txt") |
| 49 |
writeCleanFile("${MakeDirPropFileAbs}") |
| 50 |
set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES "${MakeDirPropFileAbs}") |
| 51 |
addCleanFile(${MakeDirPropFileAbs}) |
| 52 |
|
| 53 |
# Create a custom command whose output should be cleaned, but whose name |
| 54 |
# is not known until generate-time |
| 55 |
set(MakeDirPropExpFileRel "MakeDirProp_copy${CMAKE_EXECUTABLE_SUFFIX}") |
| 56 |
set(MakeDirPropExpFileAbs "$<TARGET_FILE_DIR:toclean>/${MakeDirPropExpFileRel}") |
| 57 |
addPostBuildFile(toclean "${MakeDirPropExpFileAbs}") |
| 58 |
set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${MakeDirPropExpFileAbs}) |
| 59 |
addCleanFile("${CBD}/${MakeDirPropExpFileRel}") |
| 60 |
endif() |
| 61 |
|
| 62 |
|
| 63 |
### Tests ADDITIONAL_CLEAN_FILES directory property |
| 64 |
|
| 65 |
# Register a file path relative to the build directory |
| 66 |
set(DirPropFileRel "DirPropFileRel.txt") |
| 67 |
writeCleanFile("${CBD}/${DirPropFileRel}") |
| 68 |
set_directory_properties(PROPERTIES ADDITIONAL_CLEAN_FILES ${DirPropFileRel}) |
| 69 |
addCleanFile("${CBD}/${DirPropFileRel}") |
| 70 |
|
| 71 |
# Register an absolute file path |
| 72 |
set(DirPropFileAbs "${CBD}/DirPropFileAbs.txt") |
| 73 |
writeCleanFile("${DirPropFileAbs}") |
| 74 |
set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_CLEAN_FILES ${DirPropFileAbs}) |
| 75 |
addCleanFile("${DirPropFileAbs}") |
| 76 |
|
| 77 |
# Create a custom command whose output should be cleaned, but whose name |
| 78 |
# is not known until generate-time |
| 79 |
set(DirPropExpFileRel "DirProp_copy${CMAKE_EXECUTABLE_SUFFIX}") |
| 80 |
set(DirPropExpFileAbs "$<TARGET_FILE_DIR:toclean>/${DirPropExpFileRel}") |
| 81 |
addPostBuildFile(toclean "${DirPropExpFileAbs}") |
| 82 |
set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_CLEAN_FILES ${DirPropExpFileAbs}) |
| 83 |
addCleanFile("${CBD}/${DirPropExpFileRel}") |
| 84 |
|
| 85 |
|
| 86 |
### Tests ADDITIONAL_CLEAN_FILES target property |
| 87 |
|
| 88 |
function(test_target_property TARGET) |
| 89 |
# Register a file path relative to the build directory |
| 90 |
set(TgtPropFileRel "${TARGET}_TargetPropFileRel.txt") |
| 91 |
writeCleanFile("${CBD}/${TgtPropFileRel}") |
| 92 |
set_target_properties(${TARGET} PROPERTIES ADDITIONAL_CLEAN_FILES ${TgtPropFileRel}) |
| 93 |
addCleanFile("${CBD}/${TgtPropFileRel}") |
| 94 |
|
| 95 |
# Register an absolute file path |
| 96 |
set(TgtPropFileAbs "${CBD}/${TARGET}_TargetPropFileAbs.txt") |
| 97 |
writeCleanFile("${TgtPropFileAbs}") |
| 98 |
set_property(TARGET ${TARGET} APPEND PROPERTY ADDITIONAL_CLEAN_FILES ${TgtPropFileAbs}) |
| 99 |
addCleanFile("${TgtPropFileAbs}") |
| 100 |
|
| 101 |
# Create a custom command whose output should be cleaned, but whose name |
| 102 |
# is not known until generate-time |
| 103 |
set(TgtPropExpFileRel "${TARGET}_TargetPropGenExp.txt") |
| 104 |
set(TgtPropExpFileAbs "$<TARGET_FILE_DIR:toclean>/${TgtPropExpFileRel}") |
| 105 |
addPostBuildFile(${TARGET} "${TgtPropExpFileAbs}") |
| 106 |
set_property(TARGET ${TARGET} APPEND PROPERTY ADDITIONAL_CLEAN_FILES ${TgtPropExpFileAbs}) |
| 107 |
addCleanFile("${CBD}/${TgtPropExpFileRel}") |
| 108 |
endfunction() |
| 109 |
|
| 110 |
# Test target property for various target types |
| 111 |
add_executable(acf_exec toclean.cxx) |
| 112 |
test_target_property(acf_exec) |
| 113 |
add_library(acf_lib toclean.cxx) |
| 114 |
test_target_property(acf_lib) |
| 115 |
add_custom_target(acf_custom ALL DEPENDS ${CustomCommandFile}) |
| 116 |
test_target_property(acf_custom) |
| 117 |
|
| 118 |
# Process subdirectory without targets |
| 119 |
add_subdirectory(EmptySubDir) |
| 120 |
|
| 121 |
|
| 122 |
# Configure a file listing these build-time-generated files. |
| 123 |
get_property(TOCLEAN_FILES GLOBAL PROPERTY TOCLEAN_FILES) |
| 124 |
configure_file(${CSD}/ToCleanFiles.cmake.in ${CBD}/ToCleanFiles.cmake @ONLY) |