From 1703b00c7fc34f473e84f4ba29bdc73476637005 Mon Sep 17 00:00:00 2001 From: Brad King Date: Mon, 25 Mar 2013 09:43:22 -0400 Subject: Test evaluation of per-config COMPILE_DEFINITIONS (#14037) Teach the CompileDefinitions test to cover evaluation of config-specific generator expressions. --- Tests/CompileDefinitions/CMakeLists.txt | 15 +++++-- .../add_definitions_command/CMakeLists.txt | 1 + .../CMakeLists.txt | 3 ++ Tests/CompileDefinitions/compiletest.cpp | 24 +++++++++++ Tests/CompileDefinitions/runtest.c | 47 ++++++++++++++++++++++ .../CompileDefinitions/target_prop/CMakeLists.txt | 5 +++ 6 files changed, 92 insertions(+), 3 deletions(-) create mode 100644 Tests/CompileDefinitions/runtest.c diff --git a/Tests/CompileDefinitions/CMakeLists.txt b/Tests/CompileDefinitions/CMakeLists.txt index e7d91bf..d3e9a3e 100644 --- a/Tests/CompileDefinitions/CMakeLists.txt +++ b/Tests/CompileDefinitions/CMakeLists.txt @@ -7,10 +7,19 @@ if ("${CMAKE_GENERATOR}" STREQUAL "Visual Studio 6") add_definitions(-DNO_SPACES_IN_DEFINE_VALUES) endif() +# Use compile flags to tell executables which config is built +# without depending on the compile definitions functionality. +foreach(c DEBUG RELEASE RELWITHDEBINFO MINSIZEREL) + set(CMAKE_C_FLAGS_${c} "${CMAKE_C_FLAGS_${c}} -DTEST_CONFIG_${c}") + set(CMAKE_CXX_FLAGS_${c} "${CMAKE_CXX_FLAGS_${c}} -DTEST_CONFIG_${c}") +endforeach() + +set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS + "BUILD_CONFIG_NAME=\"$\"" + ) + add_subdirectory(add_definitions_command) add_subdirectory(target_prop) add_subdirectory(add_definitions_command_with_target_prop) -file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/dummyexecutable.cpp" "int main(int, char **) { return 0; }\n") - -add_executable(CompileDefinitions "${CMAKE_CURRENT_BINARY_DIR}/dummyexecutable.cpp") +add_executable(CompileDefinitions runtest.c) diff --git a/Tests/CompileDefinitions/add_definitions_command/CMakeLists.txt b/Tests/CompileDefinitions/add_definitions_command/CMakeLists.txt index a6372af..d3886a1 100644 --- a/Tests/CompileDefinitions/add_definitions_command/CMakeLists.txt +++ b/Tests/CompileDefinitions/add_definitions_command/CMakeLists.txt @@ -3,5 +3,6 @@ project(add_definitions_command) add_definitions(-DCMAKE_IS_FUN -DCMAKE_IS=Fun -DCMAKE_IS_="Fun" -DCMAKE_IS_REALLY="Very Fun") add_definitions(-DCMAKE_IS_="Fun" -DCMAKE_IS_REALLY="Very Fun" -DCMAKE_IS_FUN -DCMAKE_IS=Fun) +add_definitions(-DBUILD_IS_DEBUG=$ -DBUILD_IS_NOT_DEBUG=$>) add_executable(add_definitions_command_executable ../compiletest.cpp) diff --git a/Tests/CompileDefinitions/add_definitions_command_with_target_prop/CMakeLists.txt b/Tests/CompileDefinitions/add_definitions_command_with_target_prop/CMakeLists.txt index e415390..5587f7f 100644 --- a/Tests/CompileDefinitions/add_definitions_command_with_target_prop/CMakeLists.txt +++ b/Tests/CompileDefinitions/add_definitions_command_with_target_prop/CMakeLists.txt @@ -12,3 +12,6 @@ set_property(TARGET add_definitions_command_with_target_prop_executable APPEND P add_definitions(-DCMAKE_IS_FUN) set_property(TARGET add_definitions_command_with_target_prop_executable APPEND PROPERTY COMPILE_DEFINITIONS CMAKE_IS=Fun CMAKE_IS_="Fun") + +add_definitions(-DBUILD_IS_DEBUG=$) +set_property(TARGET add_definitions_command_with_target_prop_executable APPEND PROPERTY COMPILE_DEFINITIONS BUILD_IS_NOT_DEBUG=$>) diff --git a/Tests/CompileDefinitions/compiletest.cpp b/Tests/CompileDefinitions/compiletest.cpp index f18e59e..14b8eab 100644 --- a/Tests/CompileDefinitions/compiletest.cpp +++ b/Tests/CompileDefinitions/compiletest.cpp @@ -45,6 +45,30 @@ enum { // TEST_GENERATOR_EXPRESSIONS #endif +#ifndef BUILD_IS_DEBUG +# error "BUILD_IS_DEBUG not defined!" +#endif +#ifndef BUILD_IS_NOT_DEBUG +# error "BUILD_IS_NOT_DEBUG not defined!" +#endif + +// Check per-config definitions. +#ifdef TEST_CONFIG_DEBUG +# if !BUILD_IS_DEBUG +# error "BUILD_IS_DEBUG false with TEST_CONFIG_DEBUG!" +# endif +# if BUILD_IS_NOT_DEBUG +# error "BUILD_IS_NOT_DEBUG true with TEST_CONFIG_DEBUG!" +# endif +#else +# if BUILD_IS_DEBUG +# error "BUILD_IS_DEBUG true without TEST_CONFIG_DEBUG!" +# endif +# if !BUILD_IS_NOT_DEBUG +# error "BUILD_IS_NOT_DEBUG false without TEST_CONFIG_DEBUG!" +# endif +#endif + int main(int argc, char **argv) { return 0; diff --git a/Tests/CompileDefinitions/runtest.c b/Tests/CompileDefinitions/runtest.c new file mode 100644 index 0000000..02d2cad --- /dev/null +++ b/Tests/CompileDefinitions/runtest.c @@ -0,0 +1,47 @@ +#include +#include +#include + +#ifndef BUILD_CONFIG_NAME +# error "BUILD_CONFIG_NAME not defined!" +#endif + +int main() +{ + char build_config_name[] = BUILD_CONFIG_NAME; + char* c; + for(c = build_config_name; *c; ++c) + { + *c = (char)tolower((int)*c); + } + fprintf(stderr, "build_config_name=\"%s\"\n", build_config_name); +#ifdef TEST_CONFIG_DEBUG + if(strcmp(build_config_name, "debug") != 0) + { + fprintf(stderr, "build_config_name is not \"debug\"\n"); + return 1; + } +#endif +#ifdef TEST_CONFIG_RELEASE + if(strcmp(build_config_name, "release") != 0) + { + fprintf(stderr, "build_config_name is not \"release\"\n"); + return 1; + } +#endif +#ifdef TEST_CONFIG_MINSIZEREL + if(strcmp(build_config_name, "minsizerel") != 0) + { + fprintf(stderr, "build_config_name is not \"minsizerel\"\n"); + return 1; + } +#endif +#ifdef TEST_CONFIG_RELWITHDEBINFO + if(strcmp(build_config_name, "relwithdebinfo") != 0) + { + fprintf(stderr, "build_config_name is not \"relwithdebinfo\"\n"); + return 1; + } +#endif + return 0; +} diff --git a/Tests/CompileDefinitions/target_prop/CMakeLists.txt b/Tests/CompileDefinitions/target_prop/CMakeLists.txt index abdf257..1ef2d6d 100644 --- a/Tests/CompileDefinitions/target_prop/CMakeLists.txt +++ b/Tests/CompileDefinitions/target_prop/CMakeLists.txt @@ -14,3 +14,8 @@ set_property(TARGET target_prop_executable APPEND PROPERTY COMPILE_DEFINITIONS "$<0:GE_NOT_DEFINED>" "$<1:ARGUMENT;LIST>" ) + +set_property(TARGET target_prop_executable APPEND PROPERTY COMPILE_DEFINITIONS + BUILD_IS_DEBUG=$ + BUILD_IS_NOT_DEBUG=$> + ) -- cgit v0.12