diff --git a/CMakeLists.txt b/CMakeLists.txt index 80650c59..340055ce 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -77,6 +77,12 @@ message("-- Version: ${VERSION}") set(GENERIC_LIB_VERSION ${VERSION}) string(SUBSTRING ${VERSION} 0 1 GENERIC_LIB_SOVERSION) +# C++ feature checks +include(CXXFeatureCheck) +cxx_feature_check(STD_REGEX) +cxx_feature_check(GNU_POSIX_REGEX) +cxx_feature_check(POSIX_REGEX) + # Set up directories include_directories(${PROJECT_SOURCE_DIR}/include) include_directories(${PROJECT_SOURCE_DIR}/src) diff --git a/cmake/AddCXXCompilerFlag.cmake b/cmake/AddCXXCompilerFlag.cmake index d4a2de05..31a284fe 100644 --- a/cmake/AddCXXCompilerFlag.cmake +++ b/cmake/AddCXXCompilerFlag.cmake @@ -1,21 +1,21 @@ -# - Adds a compiler FLAG if it is supported by the compiler +# - Adds a compiler flag if it is supported by the compiler # -# This function checks that the supplied compiler FLAG is supported and then -# adds it to the corresponding compiler FLAGs +# This function checks that the supplied compiler flag is supported and then +# adds it to the corresponding compiler flags # -# add_cxx_compiler_FLAG( []) +# add_cxx_compiler_flag( []) # # - Example # # include(AddCXXCompilerFlag) -# add_cxx_compiler_FLAG(-Wall) -# add_cxx_compiler_FLAG(-no-strict-aliasing RELEASE) +# add_cxx_compiler_flag(-Wall) +# add_cxx_compiler_flag(-no-strict-aliasing RELEASE) # Requires CMake 2.6+ -if(__add_cxx_compiler_FLAG) +if(__add_cxx_compiler_flag) return() endif() -set(__add_cxx_compiler_FLAG INCLUDED) +set(__add_cxx_compiler_flag INCLUDED) include(CheckCXXCompilerFlag) diff --git a/cmake/CXXFeatureCheck.cmake b/cmake/CXXFeatureCheck.cmake new file mode 100644 index 00000000..9d809046 --- /dev/null +++ b/cmake/CXXFeatureCheck.cmake @@ -0,0 +1,35 @@ +# - Compile and run code to check for C++ features +# +# This functions compiles a source file under the `cmake` folder +# and adds the corresponding `HAVE_[FILENAME]` flag to the CMake +# environment +# +# add_cxx_compiler_FLAG( []) +# +# - Example +# +# include(AddCXXCompilerFlag) +# add_cxx_compiler_FLAG(-Wall) +# add_cxx_compiler_FLAG(-no-strict-aliasing RELEASE) +# Requires CMake 2.6+ + +if(__cxx_feature_check_FLAG) + return() +endif() +set(__cxx_feature_check_FLAG INCLUDED) + +function(cxx_feature_check FILE) + string(TOLOWER ${FILE} FILE) + string(TOUPPER ${FILE} VAR) + string(TOUPPER "HAVE_${VAR}" FEATURE) + message("-- Performing Test ${FEATURE}") + try_run(RUN_${FEATURE} COMPILE_${FEATURE} ${CMAKE_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/cmake/${FILE}.cpp) + if(RUN_${FEATURE} EQUAL 0) + message("-- Performing Test ${FEATURE} -- Success") + set(HAVE_${VAR} 1 PARENT_SCOPE) + add_definitions(-DHAVE_${VAR}) + else() + message("-- Performing Test ${FEATURE} -- Failed") + endif() +endfunction() + diff --git a/cmake/gnu_posix_regex.cpp b/cmake/gnu_posix_regex.cpp new file mode 100644 index 00000000..480ab25c --- /dev/null +++ b/cmake/gnu_posix_regex.cpp @@ -0,0 +1,12 @@ +#include +#include +int main() { + std::string str = "test0159"; + regex_t re; + int ec = regcomp(&re, "^[a-z]+[0-9]+$", REG_EXTENDED | REG_NOSUB); + if (ec != 0) { + return ec; + } + return regexec(&re, str.c_str(), 0, NULL, 0) ? -1 : 0; +} + diff --git a/cmake/posix_regex.cpp b/cmake/posix_regex.cpp new file mode 100644 index 00000000..1f7fe07e --- /dev/null +++ b/cmake/posix_regex.cpp @@ -0,0 +1,12 @@ +#include +#include +int main() { + std::string str = "test0159"; + regex_t re; + int ec = regcomp(&re, "^[a-z]+[0-9]+$", REG_EXTENDED | REG_NOSUB); + if (ec != 0) { + return ec; + } + return regexec(&re, str.c_str(), 0, NULL, 0) ? -1 : 0; +} + diff --git a/cmake/std_regex.cpp b/cmake/std_regex.cpp new file mode 100644 index 00000000..4f227d44 --- /dev/null +++ b/cmake/std_regex.cpp @@ -0,0 +1,8 @@ +#include +#include +int main() { + const std::string str = "test0159"; + const std::regex re("^[a-z]+[0-9]+$", std::regex_constants::extended | std::regex_constants::nosubs); + return std::regex_search(str, re) ? 0 : -1; +} +