From f76c20da6395c0830ac0de667c105b0b29f84c7a Mon Sep 17 00:00:00 2001 From: Fred Baksik Date: Mon, 20 Jul 2020 09:45:32 -0400 Subject: Toolchain: Test compiler initial settings --- Tests/RunCMake/CMakeLists.txt | 1 + Tests/RunCMake/CompilerArgs/C.cmake | 3 ++ Tests/RunCMake/CompilerArgs/CMakeLists.txt | 3 ++ Tests/RunCMake/CompilerArgs/CXX.cmake | 3 ++ Tests/RunCMake/CompilerArgs/FindCCompiler.cmake | 2 + Tests/RunCMake/CompilerArgs/FindCXXCompiler.cmake | 2 + Tests/RunCMake/CompilerArgs/RunCMakeTest.cmake | 58 +++++++++++++++++++++++ Tests/RunCMake/CompilerArgs/main.c | 10 ++++ Tests/RunCMake/CompilerArgs/main.cxx | 10 ++++ Tests/RunCMake/CompilerArgs/toolchain.cmake.in | 1 + 10 files changed, 93 insertions(+) create mode 100644 Tests/RunCMake/CompilerArgs/C.cmake create mode 100644 Tests/RunCMake/CompilerArgs/CMakeLists.txt create mode 100644 Tests/RunCMake/CompilerArgs/CXX.cmake create mode 100644 Tests/RunCMake/CompilerArgs/FindCCompiler.cmake create mode 100644 Tests/RunCMake/CompilerArgs/FindCXXCompiler.cmake create mode 100644 Tests/RunCMake/CompilerArgs/RunCMakeTest.cmake create mode 100644 Tests/RunCMake/CompilerArgs/main.c create mode 100644 Tests/RunCMake/CompilerArgs/main.cxx create mode 100644 Tests/RunCMake/CompilerArgs/toolchain.cmake.in diff --git a/Tests/RunCMake/CMakeLists.txt b/Tests/RunCMake/CMakeLists.txt index 36b016f..5c35040 100644 --- a/Tests/RunCMake/CMakeLists.txt +++ b/Tests/RunCMake/CMakeLists.txt @@ -624,6 +624,7 @@ if("${CMAKE_GENERATOR}" MATCHES "Make|Ninja") set_property(TEST RunCMake.CompilerLauncher APPEND PROPERTY LABELS "CUDA") add_RunCMake_test(ctest_labels_for_subprojects) + add_RunCMake_test(CompilerArgs) endif() set(cpack_tests diff --git a/Tests/RunCMake/CompilerArgs/C.cmake b/Tests/RunCMake/CompilerArgs/C.cmake new file mode 100644 index 0000000..96b004b --- /dev/null +++ b/Tests/RunCMake/CompilerArgs/C.cmake @@ -0,0 +1,3 @@ +enable_language(C) +set(CMAKE_VERBOSE_MAKEFILE TRUE) +add_executable(main main.c) diff --git a/Tests/RunCMake/CompilerArgs/CMakeLists.txt b/Tests/RunCMake/CompilerArgs/CMakeLists.txt new file mode 100644 index 0000000..18dfd26 --- /dev/null +++ b/Tests/RunCMake/CompilerArgs/CMakeLists.txt @@ -0,0 +1,3 @@ +cmake_minimum_required(VERSION 3.2) +project(${RunCMake_TEST} NONE) +include(${RunCMake_TEST}.cmake) diff --git a/Tests/RunCMake/CompilerArgs/CXX.cmake b/Tests/RunCMake/CompilerArgs/CXX.cmake new file mode 100644 index 0000000..3d2ee00 --- /dev/null +++ b/Tests/RunCMake/CompilerArgs/CXX.cmake @@ -0,0 +1,3 @@ +enable_language(CXX) +set(CMAKE_VERBOSE_MAKEFILE TRUE) +add_executable(main main.cxx) diff --git a/Tests/RunCMake/CompilerArgs/FindCCompiler.cmake b/Tests/RunCMake/CompilerArgs/FindCCompiler.cmake new file mode 100644 index 0000000..aeaaf7f --- /dev/null +++ b/Tests/RunCMake/CompilerArgs/FindCCompiler.cmake @@ -0,0 +1,2 @@ +enable_language(C) +file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/C_comp.cmake" "set(temp_CMAKE_C_COMPILER \"${CMAKE_C_COMPILER}\")\n") diff --git a/Tests/RunCMake/CompilerArgs/FindCXXCompiler.cmake b/Tests/RunCMake/CompilerArgs/FindCXXCompiler.cmake new file mode 100644 index 0000000..663ac83 --- /dev/null +++ b/Tests/RunCMake/CompilerArgs/FindCXXCompiler.cmake @@ -0,0 +1,2 @@ +enable_language(CXX) +file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/CXX_comp.cmake" "set(temp_CMAKE_CXX_COMPILER \"${CMAKE_CXX_COMPILER}\")\n") diff --git a/Tests/RunCMake/CompilerArgs/RunCMakeTest.cmake b/Tests/RunCMake/CompilerArgs/RunCMakeTest.cmake new file mode 100644 index 0000000..9e5a18a --- /dev/null +++ b/Tests/RunCMake/CompilerArgs/RunCMakeTest.cmake @@ -0,0 +1,58 @@ +include(RunCMake) + +function(find_compiler lang) + # Detect the compiler in use in the current environment. + run_cmake(Find${lang}Compiler) + # Use the detected compiler + include(${RunCMake_BINARY_DIR}/Find${lang}Compiler-build/${lang}_comp.cmake) + if(NOT temp_CMAKE_${lang}_COMPILER) + message(FATAL_ERROR "FindCompiler provided no compiler!") + endif() + # Create a toolchain file + set(__test_compiler_var CMAKE_${lang}_COMPILER) + set(__test_compiler "${temp_CMAKE_${lang}_COMPILER}") + configure_file(${RunCMake_SOURCE_DIR}/toolchain.cmake.in + ${RunCMake_BINARY_DIR}/Find${lang}Compiler-build/toolchain_${lang}_comp.cmake @ONLY) +endfunction() + +function(run_compiler_env lang) + # Use the correct compiler + include(${RunCMake_BINARY_DIR}/Find${lang}Compiler-build/${lang}_comp.cmake) + + # Use a single build tree for tests without cleaning. + set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/${lang}-env-build) + set(RunCMake_TEST_NO_CLEAN 1) + file(REMOVE_RECURSE "${RunCMake_TEST_BINARY_DIR}") + file(MAKE_DIRECTORY "${RunCMake_TEST_BINARY_DIR}") + + # Set the compiler + if(lang STREQUAL "C") + set(ENV{CC} "'${temp_CMAKE_${lang}_COMPILER}' -DFOO1 -DFOO2") + else() + set(ENV{${lang}} "'${temp_CMAKE_${lang}_COMPILER}' -DFOO1 -DFOO2") + endif() + + run_cmake(${lang}) + run_cmake_command(${lang}-Build ${CMAKE_COMMAND} --build . ${verbose_args}) +endfunction() + +function(run_compiler_tc lang) + # Use a single build tree for tests without cleaning. + set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/${lang}-tc-build) + set(RunCMake_TEST_NO_CLEAN 1) + file(REMOVE_RECURSE "${RunCMake_TEST_BINARY_DIR}") + file(MAKE_DIRECTORY "${RunCMake_TEST_BINARY_DIR}") + + set(RunCMake_TEST_OPTIONS + -DCMAKE_TOOLCHAIN_FILE=${RunCMake_BINARY_DIR}/Find${lang}Compiler-build/toolchain_${lang}_comp.cmake) + run_cmake(${lang}) + run_cmake_command(${lang}-Build ${CMAKE_COMMAND} --build . ${verbose_args}) +endfunction() + +set(langs C CXX) + +foreach(lang ${langs}) + find_compiler(${lang}) + run_compiler_env(${lang}) + run_compiler_tc(${lang}) +endforeach() diff --git a/Tests/RunCMake/CompilerArgs/main.c b/Tests/RunCMake/CompilerArgs/main.c new file mode 100644 index 0000000..b526135 --- /dev/null +++ b/Tests/RunCMake/CompilerArgs/main.c @@ -0,0 +1,10 @@ +#ifndef FOO1 +# error Missing FOO1 +#endif +#ifndef FOO2 +# error Missing FOO2 +#endif +int main(void) +{ + return 0; +} diff --git a/Tests/RunCMake/CompilerArgs/main.cxx b/Tests/RunCMake/CompilerArgs/main.cxx new file mode 100644 index 0000000..db90e93 --- /dev/null +++ b/Tests/RunCMake/CompilerArgs/main.cxx @@ -0,0 +1,10 @@ +#ifndef FOO1 +# error Missing FOO1 +#endif +#ifndef FOO2 +# error Missing FOO2 +#endif +int main() +{ + return 0; +} diff --git a/Tests/RunCMake/CompilerArgs/toolchain.cmake.in b/Tests/RunCMake/CompilerArgs/toolchain.cmake.in new file mode 100644 index 0000000..ff77639 --- /dev/null +++ b/Tests/RunCMake/CompilerArgs/toolchain.cmake.in @@ -0,0 +1 @@ +set(@__test_compiler_var@ "@__test_compiler@" -DFOO1 -DFOO2) -- cgit v0.12