Check availability of compile flags at cmake time
authorJoe Jordan <ejjordan12@gmail.com>
Mon, 25 Jan 2021 12:48:56 +0000 (12:48 +0000)
committerJoe Jordan <ejjordan12@gmail.com>
Mon, 25 Jan 2021 12:48:56 +0000 (12:48 +0000)
Fixes #3821

Ensures that compile flag can be used before using it. Gives a
message if flag cannot be used.

api/nblib/samples/tests/CMakeLists.txt

index 972c252fac8fc9a820441a9b9a8cb6718ce58488..b4b0576dbcd67f1c0d940be9007c62791e1cbfb6 100644 (file)
@@ -48,8 +48,14 @@ gmx_add_gtest_executable(
     # files with code for tests
         argon-sample.cpp
     )
-#The following line is a not so nice hack to prevent compiler warnings that are not relevant for the sample scripts
-set_source_files_properties(argon-sample.cpp PROPERTIES COMPILE_FLAGS "-Wno-header-hygiene")
-target_link_libraries(nblib-samples-test PRIVATE nblib)
-gmx_register_gtest_test(${testname} ${exename})
-add_dependencies(check-nblib ${exename})
+#The following compiler option prevents compiler warnings that are not relevant for the sample scripts
+set(sampleCompileFlag "-Wno-header-hygiene")
+check_cxx_compiler_flag(${sampleCompileFlag} CanCompileSamples)
+if(CanCompileSamples)
+    set_source_files_properties(argon-sample.cpp PROPERTIES COMPILE_FLAGS ${sampleCompileFlag})
+    target_link_libraries(nblib-samples-test PRIVATE nblib)
+    gmx_register_gtest_test(${testname} ${exename})
+    add_dependencies(check-nblib ${exename})
+else()
+    message(WARNING "Failed to test compile with flag ${sampleCompileFlag}, so samples cannot be compiled.")
+endif()
\ No newline at end of file