Skip to content

Commit 54b3317

Browse files
authored
Enable compiler detection only if not added as subproject (#20)
Compiler detection is dangerous if this project is added via add_subdirectory() or FetchContent as it tries to enable languages which might be disabled on purpose in the super-project. Therefore, compiler detection is disabled if used as a subproject.
1 parent 5ff53ca commit 54b3317

File tree

8 files changed

+61
-34
lines changed

8 files changed

+61
-34
lines changed

CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ include(CTest)
1212
set(REQUIRED_BOOST_VERSION 1.58)
1313
find_package(Boost ${REQUIRED_BOOST_VERSION} REQUIRED)
1414

15-
option(CPP_BINDGEN_GT_LEGACY "Enables the legacy mode for API compatibility with GridTools 1.x" OFF)
16-
mark_as_advanced(CPP_BINDGEN_GT_LEGACY)
15+
if(NOT CPP_BINDGEN_GT_LEGACY)
16+
set(CPP_BINDGEN_GT_LEGACY OFF)
17+
endif()
1718

1819
# if used via FetchContent/add_subdirectory() we need to make the add_bindings_library() available here
1920
include(${CMAKE_CURRENT_LIST_DIR}/cmake/bindings.cmake)

cmake/cpp_bindgen.cmake.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ function(cpp_bindgen_enable_fortran_library target_name)
7070
target_link_libraries(fortran_bindings_handle PUBLIC c_bindings_handle)
7171
target_include_directories(fortran_bindings_handle PUBLIC ${CMAKE_CURRENT_BINARY_DIR})
7272
include(${__C_BINDINGS_CMAKE_DIR}/fortran_helpers.cmake)
73-
gen_enable_fortran_preprocessing_on_target(fortran_bindings_handle)
73+
cpp_bindgen_enable_fortran_preprocessing_on_target(fortran_bindings_handle)
7474
endif()
7575
if(NOT TARGET ${target_name}_fortran)
7676
set_source_files_properties(GT_${${target_name}_fortran_bindings_path} PROPERTIES GENERATED TRUE)

cmake/detect_features.cmake

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,68 @@
1+
if(NOT DEFINED CPP_BINDGEN_ENABLE_COMPILER_DETECTION)
2+
if(${PROJECT_SOURCE_DIR} STREQUAL ${CMAKE_SOURCE_DIR})
3+
# If turned OFF, it will still use the compilers,
4+
# - if they are available from a super-project, or
5+
# - if compilers are forced to on via other options.
6+
set(CPP_BINDGEN_ENABLE_COMPILER_DETECTION OFF)
7+
else()
8+
set(CPP_BINDGEN_ENABLE_COMPILER_DETECTION ON)
9+
endif()
10+
endif()
11+
112
macro(detect_cuda)
2-
if(NOT DEFINED CPP_BINDGEN_TEST_CUDA)
3-
# detect CUDA support
4-
include(CheckLanguage)
5-
check_language(CUDA)
13+
if(CPP_BINDGEN_ENABLE_COMPILER_DETECTION)
14+
if(NOT DEFINED CPP_BINDGEN_TEST_CUDA)
15+
# detect CUDA support
16+
include(CheckLanguage)
17+
check_language(CUDA)
618

7-
if(CMAKE_CUDA_COMPILER)
8-
enable_language (CUDA)
19+
if(CMAKE_CUDA_COMPILER)
20+
enable_language (CUDA)
21+
message(STATUS "Enable CUDA tests")
22+
set(CUDA_AVAILABLE ON)
23+
else()
24+
message(STATUS "Disable CUDA tests")
25+
set(CUDA_AVAILABLE OFF)
26+
endif()
27+
elseif(CPP_BINDGEN_TEST_CUDA)
28+
enable_language(CUDA)
929
message(STATUS "Enable CUDA tests")
1030
set(CUDA_AVAILABLE ON)
1131
else()
1232
message(STATUS "Disable CUDA tests")
1333
set(CUDA_AVAILABLE OFF)
1434
endif()
15-
elseif(CPP_BINDGEN_TEST_CUDA)
16-
enable_language(CUDA)
17-
message(STATUS "Enable CUDA tests")
18-
set(CUDA_AVAILABLE ON)
19-
else()
20-
message(STATUS "Disable CUDA tests")
21-
set(CUDA_AVAILABLE OFF)
2235
endif()
2336
endmacro(detect_cuda)
2437

2538
include (CMakeDependentOption)
2639

2740
macro(detect_fortran_compiler)
28-
CMAKE_DEPENDENT_OPTION (CPP_BINDGEN_REQUIRE_TEST_Fortran "CMake will abort if no Fortran compiler can be found"
29-
OFF "BUILD_TESTING" OFF)
41+
if(CPP_BINDGEN_ENABLE_COMPILER_DETECTION)
42+
CMAKE_DEPENDENT_OPTION (CPP_BINDGEN_REQUIRE_TEST_Fortran "CMake will abort if no Fortran compiler can be found"
43+
OFF "BUILD_TESTING" OFF)
3044

31-
include(CheckLanguage)
32-
check_language(Fortran)
33-
if (CMAKE_Fortran_COMPILER OR CPP_BINDGEN_REQUIRE_TEST_Fortran)
34-
enable_language(Fortran)
35-
else()
36-
message(WARNING "Fortran Compiler has not been found. Tests using Fortran will not be built!")
45+
include(CheckLanguage)
46+
check_language(Fortran)
47+
if (CMAKE_Fortran_COMPILER OR CPP_BINDGEN_REQUIRE_TEST_Fortran)
48+
enable_language(Fortran)
49+
else()
50+
message(WARNING "Fortran Compiler has not been found. Tests using Fortran will not be built!")
51+
endif()
3752
endif()
3853
endmacro(detect_fortran_compiler)
3954

4055
macro(detect_c_compiler)
41-
CMAKE_DEPENDENT_OPTION (CPP_BINDGEN_REQUIRE_TEST_C "CMake will abort if no C compiler can be found"
42-
OFF "BUILD_TESTING" OFF)
56+
if(CPP_BINDGEN_ENABLE_COMPILER_DETECTION)
57+
CMAKE_DEPENDENT_OPTION (CPP_BINDGEN_REQUIRE_TEST_C "CMake will abort if no C compiler can be found"
58+
OFF "BUILD_TESTING" OFF)
4359

44-
include(CheckLanguage)
45-
check_language(C)
46-
if (CMAKE_C_COMPILER OR CPP_BINDGEN_REQUIRE_TEST_C)
47-
enable_language(C)
48-
else()
49-
message(WARNING "C Compiler has not been found. Tests using C will not be built!")
60+
include(CheckLanguage)
61+
check_language(C)
62+
if (CMAKE_C_COMPILER OR CPP_BINDGEN_REQUIRE_TEST_C)
63+
enable_language(C)
64+
else()
65+
message(WARNING "C Compiler has not been found. Tests using C will not be built!")
66+
endif()
5067
endif()
5168
endmacro(detect_c_compiler)

cmake/fortran_helpers.cmake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function(gen_enable_fortran_openacc_on_target target)
1+
function(cpp_bindgen_enable_fortran_openacc_on_target target)
22
# TODO check if find_package(OpenACC) solves this problem
33
if(CMAKE_Fortran_COMPILER_ID STREQUAL "Cray")
44
target_compile_options(${target} PRIVATE $<$<COMPILE_LANGUAGE:Fortran>:-h acc>)
@@ -10,7 +10,7 @@ function(gen_enable_fortran_openacc_on_target target)
1010
endif()
1111
endfunction()
1212

13-
function(gen_enable_fortran_preprocessing_on_target target)
13+
function(cpp_bindgen_enable_fortran_preprocessing_on_target target)
1414
if (CMAKE_Fortran_COMPILER_ID STREQUAL "Cray")
1515
target_compile_options(${target} PRIVATE $<$<COMPILE_LANGUAGE:Fortran>:-eF>)
1616
else()

tests/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ endif()
1616
include(${PROJECT_SOURCE_DIR}/cmake/detect_features.cmake)
1717
detect_cuda()
1818

19+
# If clang+nvcc, we disable the tests, if CUDA < 10.0 because nvcc has problems with C++14 in this combination
20+
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL Clang AND DEFINED CMAKE_CUDA_COMPILER AND "${CMAKE_CUDA_COMPILER_VERSION}" VERSION_LESS "10.0")
21+
message("NVCC < 10.0 is not supported with Clang as host compiler. Disabling CUDA tests.")
22+
set(CUDA_AVAILABLE OFF)
23+
endif()
24+
1925
include(${PROJECT_SOURCE_DIR}/cmake/workaround_thread.cmake)
2026
_fix_threads_flags()
2127

tests/regression/array/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ add_executable(gen_regression_array_driver_fortran driver.f90)
44
target_link_libraries(gen_regression_array_driver_fortran gen_regression_array_fortran)
55
add_test(NAME gen_regression_array_driver_fortran
66
COMMAND $<TARGET_FILE:gen_regression_array_driver_fortran>)
7+
set_target_properties(gen_regression_array_driver_fortran PROPERTIES LINKER_LANGUAGE Fortran)

tests/regression/array_gt_legacy/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ add_executable(gen_regression_array_gt_legacy_driver_fortran driver.f90)
44
target_link_libraries(gen_regression_array_gt_legacy_driver_fortran gen_regression_array_gt_legacy_fortran)
55
add_test(NAME gen_regression_array_gt_legacy_driver_fortran
66
COMMAND $<TARGET_FILE:gen_regression_array_gt_legacy_driver_fortran>)
7+
set_target_properties(gen_regression_array_gt_legacy_driver_fortran PROPERTIES LINKER_LANGUAGE Fortran)

tests/regression/simple/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ if(CMAKE_Fortran_COMPILER_LOADED)
55
target_link_libraries(gen_regression_simple_driver_fortran gen_regression_simple_fortran)
66
add_test(NAME gen_regression_simple_driver_fortran
77
COMMAND $<TARGET_FILE:gen_regression_simple_driver_fortran>)
8+
set_target_properties(gen_regression_simple_driver_fortran PROPERTIES LINKER_LANGUAGE Fortran)
89
endif()
910

1011
if(CMAKE_C_COMPILER_LOADED)

0 commit comments

Comments
 (0)