Skip to content

Commit

Permalink
Merge pull request #4873 from ye-luo/more-compiler-check
Browse files Browse the repository at this point in the history
Add more compilation tests in CMake.
  • Loading branch information
prckent committed Dec 13, 2023
2 parents e95609a + b5e485d commit 1fe3871
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
18 changes: 18 additions & 0 deletions CMake/TestCXXMainCompiles.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Check that the configured compiler works on a C++ main function
# Note: whitespaces not allowed in STAGE_NAME
function(TestCXXMainCompiles STAGE_NAME)
if(STAGE_NAME MATCHES " ")
message(FATAL_ERROR "TestCXXMainCompiles whitespaces not allowed in the stage name. The given value is '${STAGE_NAME}'.")
endif()
set(TEST_CXX_COMPILE_MAIN_DIR ${PROJECT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp)
file(WRITE ${TEST_CXX_COMPILE_MAIN_DIR}/try_cxx_main.cpp "int main(){}")
set(TEST_RESULT_VAR_NAME TEST_RESULT_${STAGE_NAME})
try_compile(
${TEST_RESULT_VAR_NAME}
${TEST_CXX_COMPILE_MAIN_DIR}
SOURCES ${TEST_CXX_COMPILE_MAIN_DIR}/try_cxx_main.cpp
OUTPUT_VARIABLE COMPILE_OUTPUT)
if(NOT ${TEST_RESULT_VAR_NAME})
message(FATAL_ERROR "Failed in compiling a main() function in stage ${STAGE_NAME}. Output:\n${COMPILE_OUTPUT}")
endif()
endfunction()
18 changes: 5 additions & 13 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,8 @@ endif(QMC_OMP)
#-------------------------------------------------------------------------------
# Set vendor specific compiler options
#-------------------------------------------------------------------------------
include(TestCXXMainCompiles)
TestCXXMainCompiles("Before_Customization")
if(CMAKE_TOOLCHAIN_FILE)
message(STATUS "Using ${CMAKE_TOOLCHAIN_FILE} toolchain ")
else(CMAKE_TOOLCHAIN_FILE)
Expand Down Expand Up @@ -278,7 +280,7 @@ else(CMAKE_TOOLCHAIN_FILE)
endif()

include(inspectCompiler)

TestCXXMainCompiles("After_Customization")
endif(CMAKE_TOOLCHAIN_FILE)

if(NOT CMAKE_CXX_COMPILER_ID STREQUAL CMAKE_C_COMPILER_ID)
Expand Down Expand Up @@ -326,7 +328,7 @@ if(NOT "${ENABLE_SANITIZER}" STREQUAL "none")
endif()

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_SAN}")

TestCXXMainCompiles("With_Sanitizers")
endif()

#-----------------------------------------------------------------------
Expand Down Expand Up @@ -403,6 +405,7 @@ if(QMC_OMP)
option(ENABLE_OFFLOAD_CLANG_DEBUG_O3 "build OMP target kernels with -O3 in the build type Debug" ON)
endif()
mark_as_advanced(ENABLE_OFFLOAD_CLANG_DEBUG_O3)
TestCXXMainCompiles("With_OpenMP")
endif()

#-------------------------------------------------------------------------------------
Expand All @@ -418,17 +421,6 @@ if(QMC_EXP_THREADING)
add_definitions(-DQMC_EXP_THREADING)
endif(QMC_EXP_THREADING)

#--------------------------------------------------------------
# final test for compile options before searching for libraries
#--------------------------------------------------------------
include(CheckCXXSourceCompiles)
check_cxx_source_compiles("int main(){}" TEST_CXX_COMPILE_MAIN)
if(NOT TEST_CXX_COMPILE_MAIN)
unset(TEST_CXX_COMPILE_MAIN CACHE)
message(FATAL_ERROR "Failed in compiling a main() function likely due to incorrect compile options. "
"Check error in \"${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/CMakeError.log\".")
endif()

######################################################################
# Check external libraries.
######################################################################
Expand Down

0 comments on commit 1fe3871

Please sign in to comment.