summaryrefslogtreecommitdiff
path: root/cmake/AddCFlagIfSupported.cmake
diff options
context:
space:
mode:
authorPatrick Steinhardt <ps@pks.im>2020-04-03 19:51:22 +0200
committerPatrick Steinhardt <ps@pks.im>2020-06-01 14:06:20 +0200
commitbc02bcd920eac0ca5a930a8272737db10fc4be1f (patch)
tree7bf9f6cc739d4b2127adb2704cdd3a8b9c3bdbe0 /cmake/AddCFlagIfSupported.cmake
parent172a28860b3e4743d7ccd4409f6f51bc7c00fdfd (diff)
downloadlibgit2-bc02bcd920eac0ca5a930a8272737db10fc4be1f.tar.gz
cmake: move modules into the "cmake/" top level dir
Our custom CMake module currently live in "cmake/Modules". As the "cmake/" directory doesn't contain anything except the "Modules" directory, it doesn't really make sense to have the additional intermediate directory. So let's instead move the modules one level up into the "cmake/" top level directory.
Diffstat (limited to 'cmake/AddCFlagIfSupported.cmake')
-rw-r--r--cmake/AddCFlagIfSupported.cmake30
1 files changed, 30 insertions, 0 deletions
diff --git a/cmake/AddCFlagIfSupported.cmake b/cmake/AddCFlagIfSupported.cmake
new file mode 100644
index 000000000..b7aaa7910
--- /dev/null
+++ b/cmake/AddCFlagIfSupported.cmake
@@ -0,0 +1,30 @@
+# - Append compiler flag to CMAKE_C_FLAGS if compiler supports it
+# ADD_C_FLAG_IF_SUPPORTED(<flag>)
+# <flag> - the compiler flag to test
+# This internally calls the CHECK_C_COMPILER_FLAG macro.
+
+INCLUDE(CheckCCompilerFlag)
+
+MACRO(ADD_C_FLAG _FLAG)
+ STRING(TOUPPER ${_FLAG} UPCASE)
+ STRING(REGEX REPLACE "[-=]" "_" UPCASE_PRETTY ${UPCASE})
+ STRING(REGEX REPLACE "^_+" "" UPCASE_PRETTY ${UPCASE_PRETTY})
+ CHECK_C_COMPILER_FLAG(${_FLAG} IS_${UPCASE_PRETTY}_SUPPORTED)
+
+ IF(IS_${UPCASE_PRETTY}_SUPPORTED)
+ SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${_FLAG}")
+ ELSE()
+ MESSAGE(FATAL_ERROR "Required flag ${_FLAG} is not supported")
+ ENDIF()
+ENDMACRO()
+
+MACRO(ADD_C_FLAG_IF_SUPPORTED _FLAG)
+ STRING(TOUPPER ${_FLAG} UPCASE)
+ STRING(REGEX REPLACE "[-=]" "_" UPCASE_PRETTY ${UPCASE})
+ STRING(REGEX REPLACE "^_+" "" UPCASE_PRETTY ${UPCASE_PRETTY})
+ CHECK_C_COMPILER_FLAG(${_FLAG} IS_${UPCASE_PRETTY}_SUPPORTED)
+
+ IF(IS_${UPCASE_PRETTY}_SUPPORTED)
+ SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${_FLAG}")
+ ENDIF()
+ENDMACRO()