summaryrefslogtreecommitdiff
path: root/Tests/RunCMake/Byproducts/CleanByproducts.cmake
blob: 85d958295943519e7a0533e5640b4f8ec13fee50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
cmake_minimum_required(VERSION 3.10)
project(CleanByproducts)

# Configurable parameters
set(TEST_CLEAN_NO_CUSTOM FALSE CACHE BOOL "Value for the CLEAN_NO_CUSTOM PROPERTY")
set(TEST_BUILD_EVENTS TRUE CACHE BOOL "Create byproducts with build events")
set(TEST_CUSTOM_TARGET TRUE CACHE BOOL "Create a byproduct with a custom target")
set(TEST_CUSTOM_COMMAND TRUE CACHE BOOL "Create a byproduct with a custom command")

set_property(DIRECTORY PROPERTY CLEAN_NO_CUSTOM ${TEST_CLEAN_NO_CUSTOM})

macro(add_build_event)
  set(oneValueArgs EVENT)

  cmake_parse_Arguments(ABE "" "${oneValueArgs}" "" ${ARGN})

  # Create two byproducts and only declare one
  add_custom_command(TARGET foo
    ${ABE_EVENT}
    COMMAND ${CMAKE_COMMAND} -E touch foo.${ABE_EVENT}
    COMMAND ${CMAKE_COMMAND} -E touch foo.${ABE_EVENT}.notdeclared
    COMMENT "Creating byproducts with ${ABE_EVENT}"
    BYPRODUCTS foo.${ABE_EVENT}
  )

  # The nondeclared byproduct should always be present
  list(APPEND EXPECTED_PRESENT foo.${ABE_EVENT}.notdeclared)

  # If CLEAN_NO_CUSTOM is set, the declared byproduct should be present
  if(TEST_CLEAN_NO_CUSTOM)
    list(APPEND EXPECTED_PRESENT foo.${ABE_EVENT})
  else()
    list(APPEND EXPECTED_DELETED foo.${ABE_EVENT})
  endif()
endmacro()

add_executable(foo foo.cpp)

# Test build events
if(TEST_BUILD_EVENTS)
  add_build_event(EVENT "PRE_BUILD" ENABLE ${TEST_PRE_BUILD})
  add_build_event(EVENT "PRE_LINK" ENABLE ${TEST_PRE_LINK})
  add_build_event(EVENT "POST_BUILD" ENABLE ${TEST_POST_BUILD})
endif()

# Custom command that generates byproducts
if(TEST_CUSTOM_COMMAND)
  file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/bar.cpp.in "void bar() {}\n")
  add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/bar.cpp
    COMMAND ${CMAKE_COMMAND} -E touch foo.customcommand
    COMMAND ${CMAKE_COMMAND} -E touch foo.customcommand.notdeclared
    COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_BINARY_DIR}/bar.cpp.in ${CMAKE_CURRENT_BINARY_DIR}/bar.cpp
    BYPRODUCTS foo.customcommand
    COMMENT "Creating byproducts with a custom command"
  )

  # The nondeclared byproduct should always be present
  list(APPEND EXPECTED_PRESENT "foo.customcommand.notdeclared")

  # If CLEAN_NO_CUSTOM is set, both the output and byproduct should be present
  if(TEST_CLEAN_NO_CUSTOM)
    list(APPEND EXPECTED_PRESENT "bar.cpp")
    list(APPEND EXPECTED_PRESENT "foo.customcommand")
  else()
    list(APPEND EXPECTED_DELETED "bar.cpp")
    list(APPEND EXPECTED_DELETED "foo.customcommand")
  endif()

  target_sources(foo PUBLIC "${CMAKE_CURRENT_BINARY_DIR}/bar.cpp")
endif()

# Custom target that generates byproducts
if(TEST_CUSTOM_TARGET)
  add_custom_target(foo_file ALL
    DEPENDS foo
    COMMAND ${CMAKE_COMMAND} -E touch foo.customtarget
    COMMAND ${CMAKE_COMMAND} -E touch foo.customtarget.notdeclared
    BYPRODUCTS foo.customtarget
    COMMENT "Creating byproducts with a custom target"
  )

  # The nondeclared byproduct should always be present
  list(APPEND EXPECTED_PRESENT "foo.customtarget.notdeclared")

  # If CLEAN_NO_CUSTOM is set, the declared byproduct should be present
  if(TEST_CLEAN_NO_CUSTOM)
    list(APPEND EXPECTED_PRESENT "foo.customtarget")
  else()
    list(APPEND EXPECTED_DELETED "foo.customtarget")
  endif()
endif()

configure_file(files.cmake.in files.cmake)