From a858466aacb046a953367aeb2ee6c456812fc2dd Mon Sep 17 00:00:00 2001 From: Brad King Date: Mon, 12 Sep 2022 17:36:42 -0400 Subject: MSVC: Add test for debug information format Verify that the format's flags are used to compile, both in the main project and in `try_compile` test projects. --- Tests/CMakeLists.txt | 9 +++ Tests/MSVCDebugInformationFormat/CMakeLists.txt | 81 ++++++++++++++++++++++ Tests/MSVCDebugInformationFormat/override-C.cmake | 7 ++ .../MSVCDebugInformationFormat/override-CUDA.cmake | 6 ++ .../MSVCDebugInformationFormat/override-CXX.cmake | 7 ++ .../override-Fortran.cmake | 4 ++ Tests/MSVCDebugInformationFormat/verify.F90 | 1 + Tests/MSVCDebugInformationFormat/verify.c | 1 + Tests/MSVCDebugInformationFormat/verify.cu | 1 + Tests/MSVCDebugInformationFormat/verify.cxx | 1 + Tests/MSVCDebugInformationFormat/verify.h | 29 ++++++++ 11 files changed, 147 insertions(+) create mode 100644 Tests/MSVCDebugInformationFormat/CMakeLists.txt create mode 100644 Tests/MSVCDebugInformationFormat/override-C.cmake create mode 100644 Tests/MSVCDebugInformationFormat/override-CUDA.cmake create mode 100644 Tests/MSVCDebugInformationFormat/override-CXX.cmake create mode 100644 Tests/MSVCDebugInformationFormat/override-Fortran.cmake create mode 100644 Tests/MSVCDebugInformationFormat/verify.F90 create mode 100644 Tests/MSVCDebugInformationFormat/verify.c create mode 100644 Tests/MSVCDebugInformationFormat/verify.cu create mode 100644 Tests/MSVCDebugInformationFormat/verify.cxx create mode 100644 Tests/MSVCDebugInformationFormat/verify.h diff --git a/Tests/CMakeLists.txt b/Tests/CMakeLists.txt index b6fec4d..abe742e 100644 --- a/Tests/CMakeLists.txt +++ b/Tests/CMakeLists.txt @@ -2080,6 +2080,15 @@ if(BUILD_TESTING) if(NOT CMAKE_C_COMPILER_ID STREQUAL "Clang" AND NOT CMAKE_C_COMPILER_ID STREQUAL "IntelLLVM") ADD_TEST_MACRO(PrecompiledHeader foo) endif() + + set(MSVCDebugInformationFormat_BUILD_OPTIONS -DCMake_TEST_CUDA=${CMake_TEST_CUDA}) + if(CMAKE_Fortran_COMPILER) + list(APPEND MSVCDebugInformationFormat_BUILD_OPTIONS -DCMake_TEST_Fortran=1) + endif() + ADD_TEST_MACRO(MSVCDebugInformationFormat) + set_property(TEST MSVCDebugInformationFormat APPEND + PROPERTY LABELS "CUDA") + set(MSVCRuntimeLibrary_BUILD_OPTIONS -DCMake_TEST_CUDA=${CMake_TEST_CUDA}) ADD_TEST_MACRO(MSVCRuntimeLibrary) set_property(TEST MSVCRuntimeLibrary APPEND diff --git a/Tests/MSVCDebugInformationFormat/CMakeLists.txt b/Tests/MSVCDebugInformationFormat/CMakeLists.txt new file mode 100644 index 0000000..b09bc6c --- /dev/null +++ b/Tests/MSVCDebugInformationFormat/CMakeLists.txt @@ -0,0 +1,81 @@ +cmake_minimum_required(VERSION 3.24) +cmake_policy(SET CMP0141 NEW) + +# The debug information format flags do not change preprocessor definitions, +# so override our table of flags to artificially add a definition we can check. +set(CMAKE_USER_MAKE_RULES_OVERRIDE_C ${CMAKE_CURRENT_SOURCE_DIR}/override-C.cmake) +set(CMAKE_USER_MAKE_RULES_OVERRIDE_CXX ${CMAKE_CURRENT_SOURCE_DIR}/override-CXX.cmake) +set(CMAKE_USER_MAKE_RULES_OVERRIDE_CUDA ${CMAKE_CURRENT_SOURCE_DIR}/override-CUDA.cmake) +set(CMAKE_USER_MAKE_RULES_OVERRIDE_Fortran ${CMAKE_CURRENT_SOURCE_DIR}/override-Fortran.cmake) + +project(MSVCDebugInformationFormat) +if(CMake_TEST_CUDA) + enable_language(CUDA) +endif() +if(CMake_TEST_Fortran) + enable_language(Fortran) +endif() + +include_directories(${CMAKE_CURRENT_SOURCE_DIR}) + +if("${CMAKE_C_COMPILER_ID};${CMAKE_C_SIMULATE_ID};${CMAKE_C_COMPILER_FRONTEND_VARIANT}" STREQUAL "Clang;MSVC;GNU") + set(verify_default VERIFY_Z7) + set(NO_COMPILER_PDB 1) +elseif("${CMAKE_C_COMPILER_ID};${CMAKE_C_SIMULATE_ID};${CMAKE_C_COMPILER_FRONTEND_VARIANT}" STREQUAL "Clang;MSVC;MSVC") + set(verify_default VERIFY_Zi) + set(NO_EDIT_AND_CONTINUE 1) +else() + set(verify_default VERIFY_Zi) +endif() + +set(verify_def_Embedded -DVERIFY_Z7) +set(verify_def_ProgramDatabase -DVERIFY_Zi) +set(verify_def_EditAndContinue -DVERIFY_ZI) + +function(verify_combination format lang src) + # Test that try_compile builds with this debug format. + set(CMAKE_MSVC_DEBUG_INFORMATION_FORMAT "${format}") + set(CMAKE_TRY_COMPILE_CONFIGURATION "Debug") + set(CMAKE_TRY_COMPILE_TARGET_TYPE "STATIC_LIBRARY") + try_compile(${format}_COMPILES + ${CMAKE_CURRENT_BINARY_DIR}/try_compile/${format} + ${CMAKE_CURRENT_SOURCE_DIR}/${src} + COMPILE_DEFINITIONS ${verify_def_${format}} + CMAKE_FLAGS -DINCLUDE_DIRECTORIES=${CMAKE_CURRENT_SOURCE_DIR} + OUTPUT_VARIABLE ${format}_OUTPUT + ) + if(${format}_COMPILES) + message(STATUS "try_compile ${lang} with ${format} worked") + else() + string(REPLACE "\n" "\n " ${format}_OUTPUT " ${${format}_OUTPUT}") + message(SEND_ERROR "try_compile ${lang} with ${format} failed:\n${${format}_OUTPUT}") + endif() + + # Test that targets build with this debug format. + set(CMAKE_MSVC_DEBUG_INFORMATION_FORMAT "$<$>:${format}>$<$>:BadContent>") + add_library(${format}-${lang} ${src}) + set_property(TARGET ${format}-${lang} PROPERTY BOOL_TRUE TRUE) + target_compile_definitions(${format}-${lang} PRIVATE ${verify_def_${format}}) +endfunction() + +function(verify lang src) + add_library(default-${lang} ${src}) + target_compile_definitions(default-${lang} PRIVATE "$<$:${verify_default}>") + + verify_combination(Embedded ${lang} ${src}) + if(NOT NO_COMPILER_PDB) + verify_combination(ProgramDatabase ${lang} ${src}) + if(NOT NO_EDIT_AND_CONTINUE AND NOT lang MATCHES "^(Fortran)$") + verify_combination(EditAndContinue ${lang} ${src}) + endif() + endif() +endfunction() + +verify(C verify.c) +verify(CXX verify.cxx) +if(CMake_TEST_CUDA) + verify(CUDA verify.cu) +endif() +if(CMake_TEST_Fortran) + verify(Fortran verify.F90) +endif() diff --git a/Tests/MSVCDebugInformationFormat/override-C.cmake b/Tests/MSVCDebugInformationFormat/override-C.cmake new file mode 100644 index 0000000..e8f5ae4 --- /dev/null +++ b/Tests/MSVCDebugInformationFormat/override-C.cmake @@ -0,0 +1,7 @@ +set(var "CMAKE_C_COMPILE_OPTIONS_MSVC_DEBUG_INFORMATION_FORMAT_Embedded") +string(REPLACE "-Z7" "-Z7;-DTEST_Z7" "${var}" "${${var}}") +string(REPLACE "-gcodeview" "-gcodeview;-DTEST_Z7" "${var}" "${${var}}") +set(var "CMAKE_C_COMPILE_OPTIONS_MSVC_DEBUG_INFORMATION_FORMAT_ProgramDatabase") +string(REPLACE "-Zi" "-Zi;-DTEST_Zi" "${var}" "${${var}}") +set(var "CMAKE_C_COMPILE_OPTIONS_MSVC_DEBUG_INFORMATION_FORMAT_EditAndContinue") +string(REPLACE "-ZI" "-ZI;-DTEST_ZI" "${var}" "${${var}}") diff --git a/Tests/MSVCDebugInformationFormat/override-CUDA.cmake b/Tests/MSVCDebugInformationFormat/override-CUDA.cmake new file mode 100644 index 0000000..f870775 --- /dev/null +++ b/Tests/MSVCDebugInformationFormat/override-CUDA.cmake @@ -0,0 +1,6 @@ +set(var "CMAKE_CUDA_COMPILE_OPTIONS_MSVC_DEBUG_INFORMATION_FORMAT_Embedded") +string(REPLACE "-Z7" "-Z7;-DTEST_Z7" "${var}" "${${var}}") +set(var "CMAKE_CUDA_COMPILE_OPTIONS_MSVC_DEBUG_INFORMATION_FORMAT_ProgramDatabase") +string(REPLACE "-Zi" "-Zi;-DTEST_Zi" "${var}" "${${var}}") +set(var "CMAKE_CUDA_COMPILE_OPTIONS_MSVC_DEBUG_INFORMATION_FORMAT_EditAndContinue") +string(REPLACE "-ZI" "-ZI;-DTEST_ZI" "${var}" "${${var}}") diff --git a/Tests/MSVCDebugInformationFormat/override-CXX.cmake b/Tests/MSVCDebugInformationFormat/override-CXX.cmake new file mode 100644 index 0000000..caa23fe --- /dev/null +++ b/Tests/MSVCDebugInformationFormat/override-CXX.cmake @@ -0,0 +1,7 @@ +set(var "CMAKE_CXX_COMPILE_OPTIONS_MSVC_DEBUG_INFORMATION_FORMAT_Embedded") +string(REPLACE "-Z7" "-Z7;-DTEST_Z7" "${var}" "${${var}}") +string(REPLACE "-gcodeview" "-gcodeview;-DTEST_Z7" "${var}" "${${var}}") +set(var "CMAKE_CXX_COMPILE_OPTIONS_MSVC_DEBUG_INFORMATION_FORMAT_ProgramDatabase") +string(REPLACE "-Zi" "-Zi;-DTEST_Zi" "${var}" "${${var}}") +set(var "CMAKE_CXX_COMPILE_OPTIONS_MSVC_DEBUG_INFORMATION_FORMAT_EditAndContinue") +string(REPLACE "-ZI" "-ZI;-DTEST_ZI" "${var}" "${${var}}") diff --git a/Tests/MSVCDebugInformationFormat/override-Fortran.cmake b/Tests/MSVCDebugInformationFormat/override-Fortran.cmake new file mode 100644 index 0000000..5d2db58 --- /dev/null +++ b/Tests/MSVCDebugInformationFormat/override-Fortran.cmake @@ -0,0 +1,4 @@ +set(var "CMAKE_Fortran_COMPILE_OPTIONS_MSVC_DEBUG_INFORMATION_FORMAT_Embedded") +string(REPLACE "-Z7" "-Z7;-DTEST_Z7" "${var}" "${${var}}") +set(var "CMAKE_Fortran_COMPILE_OPTIONS_MSVC_DEBUG_INFORMATION_FORMAT_ProgramDatabase") +string(REPLACE "-Zi" "-Zi;-DTEST_Zi" "${var}" "${${var}}") diff --git a/Tests/MSVCDebugInformationFormat/verify.F90 b/Tests/MSVCDebugInformationFormat/verify.F90 new file mode 100644 index 0000000..741bca6 --- /dev/null +++ b/Tests/MSVCDebugInformationFormat/verify.F90 @@ -0,0 +1 @@ +#include "verify.h" diff --git a/Tests/MSVCDebugInformationFormat/verify.c b/Tests/MSVCDebugInformationFormat/verify.c new file mode 100644 index 0000000..741bca6 --- /dev/null +++ b/Tests/MSVCDebugInformationFormat/verify.c @@ -0,0 +1 @@ +#include "verify.h" diff --git a/Tests/MSVCDebugInformationFormat/verify.cu b/Tests/MSVCDebugInformationFormat/verify.cu new file mode 100644 index 0000000..741bca6 --- /dev/null +++ b/Tests/MSVCDebugInformationFormat/verify.cu @@ -0,0 +1 @@ +#include "verify.h" diff --git a/Tests/MSVCDebugInformationFormat/verify.cxx b/Tests/MSVCDebugInformationFormat/verify.cxx new file mode 100644 index 0000000..741bca6 --- /dev/null +++ b/Tests/MSVCDebugInformationFormat/verify.cxx @@ -0,0 +1 @@ +#include "verify.h" diff --git a/Tests/MSVCDebugInformationFormat/verify.h b/Tests/MSVCDebugInformationFormat/verify.h new file mode 100644 index 0000000..4bd6529 --- /dev/null +++ b/Tests/MSVCDebugInformationFormat/verify.h @@ -0,0 +1,29 @@ +#ifdef VERIFY_Z7 +# ifndef TEST_Z7 +# error "TEST_Z7 incorrectly not defined by debug format selection" +# endif +#else +# ifdef TEST_Z7 +# error "TEST_Z7 incorrectly defined by non-debug format selection" +# endif +#endif + +#ifdef VERIFY_Zi +# ifndef TEST_Zi +# error "TEST_Zi incorrectly not defined by debug format selection" +# endif +#else +# ifdef TEST_Zi +# error "TEST_Zi incorrectly defined by non-debug format selection" +# endif +#endif + +#ifdef VERIFY_ZI +# ifndef TEST_ZI +# error "TEST_ZI incorrectly not defined by debug format selection" +# endif +#else +# ifdef TEST_ZI +# error "TEST_ZI incorrectly defined by non-debug format selection" +# endif +#endif -- cgit v0.12