| 1 |
# Distributed under the OSI-approved BSD 3-Clause License. See accompanying |
| 2 |
# file LICENSE.rst or https://cmake.org/licensing for details. |
| 3 |
|
| 4 |
cmake_minimum_required(VERSION 3.12 FATAL_ERROR) |
| 5 |
|
| 6 |
project(test C) |
| 7 |
|
| 8 |
if(CMAKE_C_COMPILER_ID STREQUAL "GHS") |
| 9 |
add_link_options("-non_shared") |
| 10 |
endif() |
| 11 |
|
| 12 |
add_library(lib1 lib1.c) |
| 13 |
|
| 14 |
set(TEST_MISSING_TARGET_SRC 0) |
| 15 |
set(TEST_MISSING_TARGET_DEP 0) |
| 16 |
set(TEST_MISSING_DEP 0) |
| 17 |
set(TEST_DEP_CYCLE 0) |
| 18 |
|
| 19 |
add_executable(exe1 exe1.c) |
| 20 |
target_link_libraries(exe1 lib1) |
| 21 |
|
| 22 |
add_custom_target(target_cmd ALL |
| 23 |
COMMAND ${CMAKE_COMMAND} -E echo "target_cmd" > target_cmd |
| 24 |
COMMAND ${CMAKE_COMMAND} -E echo "target_cmd_extra" > target_cmd_extra.txt |
| 25 |
BYPRODUCTS target_cmd target_cmd_extra.txt |
| 26 |
COMMENT "CT: Processing target_cmd") |
| 27 |
|
| 28 |
add_custom_command(TARGET target_cmd PRE_BUILD |
| 29 |
COMMAND ${CMAKE_COMMAND} -E echo "target_cmd_prebuild" > target_cmd_prebuild.txt |
| 30 |
BYPRODUCTS target_cmd_prebuild.txt |
| 31 |
COMMENT "CT: Processing target_cmd_prebuild") |
| 32 |
#event does not run for custom targets |
| 33 |
add_custom_command(TARGET target_cmd PRE_LINK |
| 34 |
COMMAND ${CMAKE_COMMAND} -E echo "executing target_cmd_prelink commands" |
| 35 |
COMMAND ${CMAKE_COMMAND} -E echo "target_cmd_prelink" > target_cmd_prelink.txt |
| 36 |
BYPRODUCTS target_cmd_prelink.txt |
| 37 |
COMMENT "CT: Processing target_cmd_prelink") |
| 38 |
add_custom_command(TARGET target_cmd POST_BUILD |
| 39 |
COMMAND ${CMAKE_COMMAND} -E echo "executing target_cmd_postbuild commands" |
| 40 |
COMMAND ${CMAKE_COMMAND} -E echo "target_cmd_postbuild" > target_cmd_postbuild.txt |
| 41 |
BYPRODUCTS target_cmd_postbuild.txt |
| 42 |
COMMENT "CT: Processing target_cmd_postbuild") |
| 43 |
|
| 44 |
add_custom_target(target_empty ALL |
| 45 |
COMMENT "CT: Processing target_empty") |
| 46 |
|
| 47 |
add_custom_command(TARGET target_empty PRE_BUILD |
| 48 |
COMMAND ${CMAKE_COMMAND} -E echo "target_empty_prebuild" > target_empty_prebuild.txt |
| 49 |
BYPRODUCTS target_empty_prebuild.txt |
| 50 |
COMMENT "CT: Processing target_empty_prebuild") |
| 51 |
add_custom_command(TARGET target_empty POST_BUILD |
| 52 |
COMMAND ${CMAKE_COMMAND} -E echo "target_empty_postbuild" > target_empty_postbuild.txt |
| 53 |
BYPRODUCTS target_empty_postbuild.txt |
| 54 |
COMMENT "CT: Processing target_empty_postbuild") |
| 55 |
|
| 56 |
add_dependencies(target_cmd target_empty) |
| 57 |
|
| 58 |
add_custom_command( |
| 59 |
OUTPUT out_of_order_dep.txt |
| 60 |
COMMAND ${CMAKE_COMMAND} -E echo "out_of_order_dep" > out_of_order_dep.txt |
| 61 |
COMMENT "CT: generate text file out_of_order_dep" |
| 62 |
DEPENDS dependsA.txt |
| 63 |
) |
| 64 |
|
| 65 |
if(TEST_MISSING_TARGET_SRC) |
| 66 |
set(SRC_FILE does_not_exist) |
| 67 |
endif() |
| 68 |
if(TEST_MISSING_TARGET_DEP) |
| 69 |
set(DEP_FILE does_not_exist) |
| 70 |
endif() |
| 71 |
|
| 72 |
add_custom_target(target_update_files |
| 73 |
DEPENDS genc_do_not_list.txt ${DEP_FILE} |
| 74 |
SOURCES gena.txt genb.txt another_file.c ${SRC_FILE} |
| 75 |
BYPRODUCTS junkit.txt |
| 76 |
COMMAND ${CMAKE_COMMAND} -E copy another_file.c junkit.txt |
| 77 |
COMMENT "CT: Processing target_update_files") |
| 78 |
|
| 79 |
add_custom_command( |
| 80 |
OUTPUT force_rebuild gena.txt genb.txt genc_do_not_list.txt |
| 81 |
COMMAND ${CMAKE_COMMAND} -E copy dependsA.txt gena.txt |
| 82 |
COMMAND ${CMAKE_COMMAND} -E echo "genb" > genb.txt |
| 83 |
COMMAND ${CMAKE_COMMAND} -E echo "genc" > genc_do_not_list.txt |
| 84 |
DEPENDS out_of_order_dep.txt dependsA.txt |
| 85 |
COMMENT "CT: generate text files A, B, and C" |
| 86 |
) |
| 87 |
|
| 88 |
if(TEST_MISSING_DEP) |
| 89 |
set(MISSING_DEP MISSING_DEP) |
| 90 |
endif() |
| 91 |
if(TEST_DEP_CYCLE) |
| 92 |
set(DEP_CYCLE out_of_order_dep.txt) |
| 93 |
endif() |
| 94 |
|
| 95 |
add_custom_command( |
| 96 |
OUTPUT dependsA.txt |
| 97 |
COMMAND ${CMAKE_COMMAND} -E echo "dependsA" > dependsA.txt |
| 98 |
DEPENDS ${MISSING_DEP} ${DEP_CYCLE} another_file.c |
| 99 |
COMMENT "CT: generate text file dependsA" |
| 100 |
) |
| 101 |
|
| 102 |
add_custom_command( |
| 103 |
OUTPUT another_file.c |
| 104 |
COMMAND ${CMAKE_COMMAND} -E echo "//auto-gen file" > another_file.c |
| 105 |
COMMENT "CT: generate C file another_file" |
| 106 |
) |
| 107 |
|
| 108 |
add_dependencies(target_update_files target_empty) |