Tests: Add cases verifying flag ordering rules

This commit is contained in:
Brad King 2021-06-03 10:40:56 -04:00
parent ccc83ce162
commit 8f68bcad8f
8 changed files with 95 additions and 3 deletions

View File

@ -489,6 +489,11 @@ if(BUILD_TESTING)
if(CMAKE_Fortran_COMPILER)
set(CompileOptions_BUILD_OPTIONS -DTEST_FORTRAN=1)
endif()
if(_isMultiConfig)
set(CompileOptions_CTEST_OPTIONS --build-config $<CONFIGURATION>)
else()
set(CompileOptions_BUILD_OPTIONS -DCMAKE_BUILD_TYPE=$<CONFIGURATION>)
endif()
ADD_TEST_MACRO(CompileOptions CompileOptions)
ADD_TEST_MACRO(CompatibleInterface CompatibleInterface)
ADD_TEST_MACRO(AliasTarget AliasTarget)

View File

@ -1,5 +1,11 @@
cmake_minimum_required(VERSION 2.8.12)
cmake_minimum_required(VERSION 3.0)
if(POLICY CMP0092)
cmake_policy(SET CMP0092 NEW)
endif()
get_property(_isMultiConfig GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
if(NOT _isMultiConfig AND NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Debug CACHE STRING "Choose the type of build" FORCE)
endif()
project(CompileOptions)
add_library(testlib other.cpp)
@ -49,6 +55,24 @@ if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang|Borland|Embarcadero" AND NOT "${CMAK
)
endif()
if(CMAKE_CXX_COMPILER_ID MATCHES "^(GNU|AppleClang|MSVC)$")
target_compile_definitions(CompileOptions PRIVATE "DO_FLAG_TESTS")
if(CMAKE_CXX_COMPILER_ID MATCHES "^(GNU|AppleClang)$")
string(APPEND CMAKE_CXX_FLAGS " -w")
endif()
string(APPEND CMAKE_CXX_FLAGS " -DFLAG_A=1 -DFLAG_B=1")
string(APPEND CMAKE_CXX_FLAGS_DEBUG " -DFLAG_A=2 -DFLAG_C=1")
string(APPEND CMAKE_CXX_FLAGS_RELEASE " -DFLAG_A=2 -DFLAG_C=1")
string(APPEND CMAKE_CXX_FLAGS_RELWITHDEBINFO " -DFLAG_A=2 -DFLAG_C=1")
string(APPEND CMAKE_CXX_FLAGS_MINSIZEREL " -DFLAG_A=2 -DFLAG_C=1")
set_property(TARGET CompileOptions APPEND PROPERTY COMPILE_OPTIONS "-DFLAG_B=2" "-DFLAG_C=2" "-DFLAG_D=1")
set_property(TARGET testlib APPEND PROPERTY INTERFACE_COMPILE_OPTIONS "-DFLAG_D=2")
if(NOT CMAKE_GENERATOR MATCHES "^Visual Studio 9")
set_property(TARGET testlib APPEND PROPERTY INTERFACE_COMPILE_OPTIONS "-DFLAG_E=1")
set_property(SOURCE main.cpp PROPERTY COMPILE_OPTIONS "-DFLAG_E=2")
endif()
endif()
target_link_libraries(CompileOptions testlib)
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")

View File

@ -37,6 +37,24 @@
# endif
#endif
#ifdef DO_FLAG_TESTS
# if FLAG_A != 2
# error "FLAG_A is not 2"
# endif
# if FLAG_B != 2
# error "FLAG_B is not 2"
# endif
# if FLAG_C != 2
# error "FLAG_C is not 2"
# endif
# if FLAG_D != 2
# error "FLAG_D is not 2"
# endif
# if defined(FLAG_E) && FLAG_E != 2
# error "FLAG_E is not 2"
# endif
#endif
#include <string.h>
int main()

View File

@ -595,7 +595,10 @@ set_property(TEST RunCMake.target_link_options APPEND
add_RunCMake_test(target_compile_definitions)
add_RunCMake_test(target_compile_features)
add_RunCMake_test(target_compile_options -DCMAKE_C_COMPILER_ID=${CMAKE_C_COMPILER_ID})
add_RunCMake_test(target_compile_options
-DCMAKE_C_COMPILER_ID=${CMAKE_C_COMPILER_ID}
-DCMAKE_C_SIMULATE_ID=${CMAKE_C_SIMULATE_ID}
)
add_RunCMake_test(target_include_directories)
add_RunCMake_test(target_sources)
add_RunCMake_test(CheckCompilerFlag -DCMake_TEST_CUDA=${CMake_TEST_CUDA}

View File

@ -0,0 +1,3 @@
-w +-O +-O0 [^
]*-O1 +-O2 +-O3 [^
]*CMakeFiles[\/]order\.dir[\/](Custom[\/])?order\.c\.o

View File

@ -0,0 +1,19 @@
get_property (isMultiConfig GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
if(isMultiConfig)
set(CMAKE_CONFIGURATION_TYPES "Custom" CACHE STRING "" FORCE)
else()
set(CMAKE_BUILD_TYPE "Custom" CACHE STRING "" FORCE)
endif()
enable_language(C)
string(APPEND CMAKE_C_FLAGS " -w -O")
set(CMAKE_C_FLAGS_CUSTOM "-O0")
add_executable(order order.c)
set_property(TARGET order APPEND PROPERTY COMPILE_OPTIONS -O1)
add_library(iface INTERFACE)
set_property(TARGET iface APPEND PROPERTY INTERFACE_COMPILE_OPTIONS -O2)
target_link_libraries(order PRIVATE iface)
set_property(SOURCE order.c PROPERTY COMPILE_OPTIONS -O3)

View File

@ -19,3 +19,16 @@ if (CMAKE_C_COMPILER_ID MATCHES "GNU|Clang")
run_cmake_target(CMP0101-BEFORE_keyword OLD CMP0101_OLD)
run_cmake_target(CMP0101-BEFORE_keyword NEW CMP0101_NEW)
endif()
function(run_Order)
run_cmake_with_options(Order)
set(RunCMake_TEST_NO_CLEAN 1)
set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/Order-build)
set(RunCMake_TEST_OUTPUT_MERGE 1)
run_cmake_command(Order-build ${CMAKE_COMMAND} --build . --verbose --config Custom)
endfunction()
if(RunCMake_GENERATOR MATCHES "Ninja|Make" AND
CMAKE_C_COMPILER_ID MATCHES "^(GNU|Clang|AppleClang)$" AND
NOT CMAKE_C_SIMULATE_ID STREQUAL "MSVC")
run_Order()
endif()

View File

@ -0,0 +1,7 @@
#ifndef __OPTIMIZE__
# error "Optimizations not enabled!"
#endif
int main(void)
{
return 0;
}