CustomCommandsAndTargets.cmake 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. set(CMAKE_INTERMEDIATE_DIR_STRATEGY FULL CACHE STRING "" FORCE)
  2. enable_language(C)
  3. file(REMOVE "${CMAKE_BINARY_DIR}/target_files_custom.cmake")
  4. function(get_write_file_command var filename)
  5. set(${var} ${CMAKE_COMMAND} -DOUTPUT_FILE=${filename} -DGENEX_CONFIG=$<CONFIG> -DINTDIR_CONFIG=${CMAKE_CFG_INTDIR} -P ${CMAKE_SOURCE_DIR}/WriteFile.cmake PARENT_SCOPE)
  6. endfunction()
  7. function(create_targets prefix)
  8. get_write_file_command(cmd ${prefix}Command.txt)
  9. add_custom_command(OUTPUT ${prefix}Command.txt COMMAND ${cmd})
  10. add_custom_target(${prefix}Command DEPENDS ${prefix}Command.txt)
  11. get_write_file_command(cmd ${prefix}Target.txt)
  12. add_custom_target(${prefix}Target COMMAND ${cmd} BYPRODUCTS ${prefix}Target.txt)
  13. get_write_file_command(cmd ${prefix}PostBuild.txt)
  14. add_executable(${prefix}PostBuild ${CMAKE_SOURCE_DIR}/main.c)
  15. add_custom_command(TARGET ${prefix}PostBuild POST_BUILD COMMAND ${cmd} BYPRODUCTS ${prefix}PostBuild.txt)
  16. get_write_file_command(cmd ${prefix}TargetPostBuild.txt)
  17. add_custom_target(${prefix}TargetPostBuild)
  18. add_custom_command(TARGET ${prefix}TargetPostBuild POST_BUILD COMMAND ${cmd} BYPRODUCTS ${prefix}TargetPostBuild.txt)
  19. file(APPEND "${CMAKE_BINARY_DIR}/target_files_custom.cmake"
  20. "set(TARGET_DEPENDS_${prefix}Command [==[${CMAKE_CURRENT_BINARY_DIR}/${prefix}Command.txt]==])
  21. set(TARGET_BYPRODUCTS_${prefix}Target [==[${CMAKE_CURRENT_BINARY_DIR}/${prefix}Target.txt]==])
  22. set(TARGET_BYPRODUCTS_${prefix}PostBuild [==[${CMAKE_CURRENT_BINARY_DIR}/${prefix}PostBuild.txt]==])
  23. set(TARGET_BYPRODUCTS_${prefix}TargetPostBuild [==[${CMAKE_CURRENT_BINARY_DIR}/${prefix}TargetPostBuild.txt]==])
  24. ")
  25. endfunction()
  26. add_subdirectory(CustomCommandsAndTargetsSubdir)
  27. create_targets(Top)
  28. add_executable(RootExe main.c)
  29. add_custom_target(RootCustom COMMAND ${CMAKE_COMMAND} -E touch RootCustom.txt BYPRODUCTS RootCustom.txt)
  30. add_custom_command(OUTPUT main.c COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/main.c ${CMAKE_CURRENT_BINARY_DIR}/main.c DEPENDS RootCustom)
  31. add_executable(LeafExe ${CMAKE_CURRENT_BINARY_DIR}/main.c)
  32. add_custom_target(LeafCustom COMMAND ${CMAKE_COMMAND} -E touch LeafCustom.txt BYPRODUCTS LeafCustom.txt DEPENDS RootCustom RootExe)
  33. add_dependencies(LeafExe RootExe)
  34. file(APPEND "${CMAKE_BINARY_DIR}/target_files_custom.cmake"
  35. "set(TARGET_BYPRODUCTS_LeafCustom [==[${CMAKE_CURRENT_BINARY_DIR}/LeafCustom.txt]==])
  36. set(TARGET_BYPRODUCTS_LeafExe [==[${CMAKE_CURRENT_BINARY_DIR}/main.c]==])
  37. set(TARGET_BYPRODUCTS_RootCustom [==[${CMAKE_CURRENT_BINARY_DIR}/RootCustom.txt]==])
  38. ")
  39. include(${CMAKE_CURRENT_LIST_DIR}/Common.cmake)
  40. generate_output_files(TopPostBuild SubdirPostBuild RootExe LeafExe)
  41. file(APPEND "${CMAKE_BINARY_DIR}/target_files.cmake" "include(\${CMAKE_CURRENT_LIST_DIR}/target_files_custom.cmake)\n")