Ninja,Makefile: Add tests for handling of byproducts by clean operations

This commit is contained in:
Pedro Navarro 2018-09-06 12:16:52 -07:00 committed by Brad King
parent c7f1ed03d7
commit 80e2f8ee0c
6 changed files with 173 additions and 0 deletions

View File

@ -0,0 +1,3 @@
cmake_minimum_required(VERSION 3.10)
project(${RunCMake_TEST} NONE)
include(${RunCMake_TEST}.cmake)

View File

@ -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)

View File

@ -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()

View File

@ -0,0 +1,2 @@
set(EXPECTED_PRESENT "@EXPECTED_PRESENT@")
set(EXPECTED_DELETED "@EXPECTED_DELETED@")

View File

@ -0,0 +1,14 @@
int bar(int y)
{
return y * 6;
}
int foo(int x)
{
return x * bar(x);
}
int main()
{
return foo(4);
}

View File

@ -143,6 +143,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()