CMakeLists.txt 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. cmake_minimum_required(VERSION 3.14)
  2. project(ToClean)
  3. # Utility variables
  4. set(TSD ${ToClean_SOURCE_DIR})
  5. set(TBD ${ToClean_BINARY_DIR})
  6. set(CLEAN_FILE_CONTENT "File registered for cleaning.\n")
  7. # Lists build-time-generated files that should be cleaned away
  8. set(TOCLEAN_FILES)
  9. # Build a simple project whose compiled objects should be cleaned.
  10. add_executable(toclean toclean.cxx)
  11. list(APPEND TOCLEAN_FILES
  12. "${TBD}${CMAKE_FILES_DIRECTORY}/toclean.dir/toclean.cxx${CMAKE_CXX_OUTPUT_EXTENSION}")
  13. # Create a custom command whose output should be cleaned.
  14. set(CustomCommandFile "${TBD}/CustomCommandFile.txt")
  15. add_custom_command(OUTPUT ${CustomCommandFile}
  16. DEPENDS ${TSD}/toclean.cxx
  17. COMMAND ${CMAKE_COMMAND}
  18. ARGS -E copy ${TSD}/toclean.cxx ${CustomCommandFile})
  19. add_custom_target(generate ALL DEPENDS ${CustomCommandFile})
  20. list(APPEND TOCLEAN_FILES ${CustomCommandFile})
  21. ### Tests ADDITIONAL_MAKE_CLEAN_FILES directory property
  22. if("${CMAKE_GENERATOR}" MATCHES "Makefile")
  23. # Create a file that must be registered for cleaning.
  24. set(MakeDirPropFile "${TBD}/MakeDirPropFile.txt")
  25. file(WRITE "${MakeDirPropFile}" ${CLEAN_FILE_CONTENT})
  26. set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES "${MakeDirPropFile}")
  27. list(APPEND TOCLEAN_FILES "${MakeDirPropFile}")
  28. # Create a custom command whose output should be cleaned, but whose name
  29. # is not known until generate-time
  30. set(MakeDirPropExpFileRel "MakeDirProp_copy${CMAKE_EXECUTABLE_SUFFIX}")
  31. set(MakeDirPropExpFile "$<TARGET_FILE_DIR:toclean>/${MakeDirPropExpFileRel}")
  32. add_custom_command(TARGET toclean POST_BUILD
  33. COMMAND ${CMAKE_COMMAND}
  34. ARGS -E copy $<TARGET_FILE:toclean> ${MakeDirPropExpFile})
  35. set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${MakeDirPropExpFile})
  36. list(APPEND TOCLEAN_FILES "${TBD}/${MakeDirPropExpFileRel}")
  37. endif()
  38. ### Tests ADDITIONAL_CLEAN_FILES directory property
  39. # Register a file path relative to the build directory
  40. set(DirPropFileRel "DirPropFileRel.txt")
  41. file(WRITE "${TBD}/${DirPropFileRel}" ${CLEAN_FILE_CONTENT})
  42. set_directory_properties(PROPERTIES ADDITIONAL_CLEAN_FILES ${DirPropFileRel})
  43. list(APPEND TOCLEAN_FILES "${TBD}/${DirPropFileRel}")
  44. # Register an absolute file path
  45. set(DirPropFileAbs "${TBD}/DirPropFileAbs.txt")
  46. file(WRITE "${DirPropFileAbs}" ${CLEAN_FILE_CONTENT})
  47. set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_CLEAN_FILES ${DirPropFileAbs})
  48. list(APPEND TOCLEAN_FILES "${DirPropFileAbs}")
  49. # Create a custom command whose output should be cleaned, but whose name
  50. # is not known until generate-time
  51. set(DirPropExpFileRel "DirProp_copy${CMAKE_EXECUTABLE_SUFFIX}")
  52. set(DirPropExpFile "$<TARGET_FILE_DIR:toclean>/${DirPropExpFileRel}")
  53. add_custom_command(TARGET toclean POST_BUILD
  54. COMMAND ${CMAKE_COMMAND}
  55. ARGS -E copy $<TARGET_FILE:toclean> ${DirPropExpFile})
  56. set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_CLEAN_FILES ${DirPropExpFile})
  57. list(APPEND TOCLEAN_FILES "${TBD}/${DirPropExpFileRel}")
  58. ### Tests ADDITIONAL_CLEAN_FILES target property
  59. # Register a file path relative to the build directory
  60. set(TgtPropFileRel "TargetPropFileRel.txt")
  61. file(WRITE "${TBD}/${TgtPropFileRel}" ${CLEAN_FILE_CONTENT})
  62. set_target_properties(toclean PROPERTIES ADDITIONAL_CLEAN_FILES ${TgtPropFileRel})
  63. list(APPEND TOCLEAN_FILES "${TBD}/${TgtPropFileRel}")
  64. # Register an absolute file path
  65. set(TgtPropFileAbs "${TBD}/TargetPropFileAbs.txt")
  66. file(WRITE "${TgtPropFileAbs}" ${CLEAN_FILE_CONTENT})
  67. set_property(TARGET toclean APPEND PROPERTY ADDITIONAL_CLEAN_FILES ${TgtPropFileAbs})
  68. list(APPEND TOCLEAN_FILES "${TgtPropFileAbs}")
  69. # Create a custom command whose output should be cleaned, but whose name
  70. # is not known until generate-time
  71. set(TgtPropExpFileRel "TgtProp_copy${CMAKE_EXECUTABLE_SUFFIX}")
  72. set(TgtPropExpFile "$<TARGET_FILE_DIR:toclean>/${TgtPropExpFileRel}")
  73. add_custom_command(TARGET toclean POST_BUILD
  74. COMMAND ${CMAKE_COMMAND}
  75. ARGS -E copy $<TARGET_FILE:toclean> ${TgtPropExpFile})
  76. set_property(TARGET toclean APPEND PROPERTY ADDITIONAL_CLEAN_FILES ${TgtPropExpFile})
  77. list(APPEND TOCLEAN_FILES "${TBD}/${TgtPropExpFileRel}")
  78. # Configure a file listing these build-time-generated files.
  79. configure_file(${TSD}/ToCleanFiles.cmake.in ${TBD}/ToCleanFiles.cmake @ONLY)