diff options
author | Pedro Navarro <pnavarro@netflix.com> | 2018-09-06 12:16:52 -0700 |
---|---|---|
committer | Brad King <brad.king@kitware.com> | 2018-09-28 11:30:32 -0400 |
commit | 80e2f8ee0c4f94e679a2e4cfa9123cc28bce2e4f (patch) | |
tree | c3056069f5509aae12d73b24f3ffd790d8dd0253 /Tests | |
parent | c7f1ed03d764a6165350236969376498feb2b2ff (diff) | |
download | cmake-80e2f8ee0c4f94e679a2e4cfa9123cc28bce2e4f.tar.gz |
Ninja,Makefile: Add tests for handling of byproducts by clean operations
Diffstat (limited to 'Tests')
-rw-r--r-- | Tests/RunCMake/Byproducts/CMakeLists.txt | 3 | ||||
-rw-r--r-- | Tests/RunCMake/Byproducts/CleanByproducts.cmake | 93 | ||||
-rw-r--r-- | Tests/RunCMake/Byproducts/RunCMakeTest.cmake | 58 | ||||
-rw-r--r-- | Tests/RunCMake/Byproducts/files.cmake.in | 2 | ||||
-rw-r--r-- | Tests/RunCMake/Byproducts/foo.cpp | 14 | ||||
-rw-r--r-- | Tests/RunCMake/CMakeLists.txt | 3 |
6 files changed, 173 insertions, 0 deletions
diff --git a/Tests/RunCMake/Byproducts/CMakeLists.txt b/Tests/RunCMake/Byproducts/CMakeLists.txt new file mode 100644 index 0000000000..bf2ef1506e --- /dev/null +++ b/Tests/RunCMake/Byproducts/CMakeLists.txt @@ -0,0 +1,3 @@ +cmake_minimum_required(VERSION 3.10) +project(${RunCMake_TEST} NONE) +include(${RunCMake_TEST}.cmake) diff --git a/Tests/RunCMake/Byproducts/CleanByproducts.cmake b/Tests/RunCMake/Byproducts/CleanByproducts.cmake new file mode 100644 index 0000000000..85d9582959 --- /dev/null +++ b/Tests/RunCMake/Byproducts/CleanByproducts.cmake @@ -0,0 +1,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) diff --git a/Tests/RunCMake/Byproducts/RunCMakeTest.cmake b/Tests/RunCMake/Byproducts/RunCMakeTest.cmake new file mode 100644 index 0000000000..a7584ee4bb --- /dev/null +++ b/Tests/RunCMake/Byproducts/RunCMakeTest.cmake @@ -0,0 +1,58 @@ +include(RunCMake) + +function(run_CleanByproducts case) + set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/CleanByproducts-${case}-build) + set(RunCMake_TEST_OPTIONS "${ARGN}") + + run_cmake(CleanByproducts) + set(RunCMake_TEST_NO_CLEAN 1) + + run_cmake_command(CleanByProducts-build ${CMAKE_COMMAND} --build .) + include("${RunCMake_TEST_BINARY_DIR}/files.cmake") + + message("Checking that all expected files are present") + check_files(EXPECTED_PRESENT "${RunCMake_TEST_BINARY_DIR}" TRUE) + check_files(EXPECTED_DELETED "${RunCMake_TEST_BINARY_DIR}" TRUE) + + run_cmake_command(CleanByProducts-clean ${CMAKE_COMMAND} --build . --target clean) + + message("Checking that only the expected files are present after cleaning") + check_files(EXPECTED_PRESENT "${RunCMake_TEST_BINARY_DIR}" TRUE) + check_files(EXPECTED_DELETED "${RunCMake_TEST_BINARY_DIR}" FALSE) +endfunction() + +function(check_files list path has_to_exist) + foreach(file IN LISTS ${list}) + message("Checking ${file}") + set(file_exists FALSE) + if(EXISTS "${path}/${file}") + set(file_exists TRUE) + endif() + + if(file_exists AND NOT has_to_exist) + message(FATAL_ERROR "${file} should have been deleted") + elseif(NOT file_exists AND has_to_exist) + message(FATAL_ERROR "${file} does not exist") + elseif(file_exists AND has_to_exist) + message("${file} found as expected") + elseif(NOT file_exists AND NOT has_to_exist) + message("${file} deleted as expected") + endif() + + endforeach() +endfunction() + + +# Iterate through all possible test values +set(counter 0) +foreach(test_clean_no_custom TRUE FALSE) + foreach(test_build_events TRUE FALSE) + foreach(test_custom_command TRUE FALSE) + foreach(test_custom_target TRUE FALSE) + math(EXPR counter "${counter} + 1") + message("Test ${counter} - CLEAN_NO_CUSTOM: ${test_clean_no_custom}, Build events: ${test_build_events}, Custom command: ${test_custom_command}, Custom target: ${test_custom_target}") + run_CleanByproducts("buildevents${counter}" -DCLEAN_NO_CUSTOM=${test_clean_no_custom} -DTEST_BUILD_EVENTS=${test_build_events} -DTEST_CUSTOM_COMMAND=${test_custom_command} -DTEST_CUSTOM_TARGET=${test_custom_target}) + endforeach() + endforeach() + endforeach() +endforeach() diff --git a/Tests/RunCMake/Byproducts/files.cmake.in b/Tests/RunCMake/Byproducts/files.cmake.in new file mode 100644 index 0000000000..a7d4831fc9 --- /dev/null +++ b/Tests/RunCMake/Byproducts/files.cmake.in @@ -0,0 +1,2 @@ +set(EXPECTED_PRESENT "@EXPECTED_PRESENT@") +set(EXPECTED_DELETED "@EXPECTED_DELETED@") diff --git a/Tests/RunCMake/Byproducts/foo.cpp b/Tests/RunCMake/Byproducts/foo.cpp new file mode 100644 index 0000000000..d47cb91a69 --- /dev/null +++ b/Tests/RunCMake/Byproducts/foo.cpp @@ -0,0 +1,14 @@ +int bar(int y) +{ + return y * 6; +} + +int foo(int x) +{ + return x * bar(x); +} + +int main() +{ + return foo(4); +} diff --git a/Tests/RunCMake/CMakeLists.txt b/Tests/RunCMake/CMakeLists.txt index 080d0d0971..90681b9a9d 100644 --- a/Tests/RunCMake/CMakeLists.txt +++ b/Tests/RunCMake/CMakeLists.txt @@ -144,6 +144,9 @@ endif() add_RunCMake_test(AndroidTestUtilities) add_RunCMake_test(BuildDepends) if(UNIX AND "${CMAKE_GENERATOR}" MATCHES "Unix Makefiles|Ninja") + add_RunCMake_test(Byproducts) +endif() +if(UNIX AND "${CMAKE_GENERATOR}" MATCHES "Unix Makefiles|Ninja") add_RunCMake_test(CompilerChange) endif() add_RunCMake_test(CompilerNotFound) |