Modernise CMake
authorDavid Seifert <soap@gentoo.org>
Sun, 11 Mar 2018 08:40:40 +0000 (09:40 +0100)
committerDavid Seifert <soap@gentoo.org>
Sun, 11 Mar 2018 08:40:40 +0000 (09:40 +0100)
* Use `GNUInstallDirs` to override allow
  overriding installation directories.
* `CTest` provides a standardized variable
  for enabling tests `BUILD_TESTING`.

CMakeLists.txt

index 40e6972..8518f16 100644 (file)
@@ -1,14 +1,26 @@
-cmake_minimum_required(VERSION 2.8)
-project(cppcodec)
+cmake_minimum_required(VERSION 2.8.5)
+project(cppcodec CXX)
 
-option(build_tests "Build tests (run as 'test' target)" ON)
+include(GNUInstallDirs)
+include(CTest)
 
 # These flags are for binaries built by this particular CMake project (test_cppcodec, base64enc, etc.).
 # In your own project that uses cppcodec, you might want to specify a different standard or error level.
 if (MSVC)
   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W3")
 else()
-  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -pedantic")
+  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -pedantic")
+
+  # Request C++11
+  if(${CMAKE_VERSION} VERSION_LESS 3.1)
+    # CMake versions before 3.1 do not understand CMAKE_CXX_STANDARD
+    # remove this block once CMake >=3.1 has fixated in the ecosystem
+    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
+  else()
+    set(CMAKE_CXX_STANDARD 11)
+    set(CMAKE_CXX_STANDARD_REQUIRED ON)
+    set(CMAKE_CXX_EXTENSIONS OFF)
+  endif()
 endif()
 
 set(PUBLIC_HEADERS
@@ -46,12 +58,11 @@ add_library(cppcodec OBJECT ${PUBLIC_HEADERS}) # unnecessary for building, but m
 set_target_properties(cppcodec PROPERTIES LINKER_LANGUAGE CXX)
 add_subdirectory(tool)
 
-if (build_tests)
-    enable_testing()
+if (BUILD_TESTING)
     add_subdirectory(test)
 endif()
 
 foreach(h ${PUBLIC_HEADERS})
-    get_filename_component(HEADER_INCLUDE_DIRECTORY include/${h} PATH) # use DIRECTORY instead of PATH once requiring CMake 3.0
-    install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/${h} DESTINATION ${HEADER_INCLUDE_DIRECTORY} COMPONENT "headers")
+    get_filename_component(FINAL_PATH ${h} PATH) # use DIRECTORY instead of PATH once requiring CMake 3.0
+    install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/${h} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${FINAL_PATH} COMPONENT "headers")
 endforeach()