Skip to content

Commit cc66503

Browse files
committed
Improve CMake
1 parent 100b370 commit cc66503

File tree

3 files changed

+54
-10
lines changed

3 files changed

+54
-10
lines changed

CMakeLists.txt

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
# This file is subject to the license terms in the LICENSE file
33
# found in the top-level directory of this distribution.
44

5-
cmake_minimum_required(VERSION 3.1)
6-
project(array)
5+
cmake_minimum_required(VERSION 3.8)
6+
project(foonathan_array VERSION 0.0.0)
77

8+
# source files
89
set(detail_header_files
910
${CMAKE_CURRENT_SOURCE_DIR}/include/foonathan/array/detail/all_of.hpp
1011
${CMAKE_CURRENT_SOURCE_DIR}/include/foonathan/array/detail/is_trivial.hpp
@@ -39,14 +40,37 @@ set(header_files
3940
${CMAKE_CURRENT_SOURCE_DIR}/include/foonathan/array/small_bag.hpp
4041
${CMAKE_CURRENT_SOURCE_DIR}/include/foonathan/array/variant_bag.hpp
4142
)
43+
44+
# main target
4245
add_library(foonathan_array INTERFACE)
43-
target_sources(foonathan_array INTERFACE ${detail_header_files} ${header_files})
44-
target_include_directories(foonathan_array INTERFACE include)
45-
if(MSVC)
46-
target_compile_definitions(foonathan_array INTERFACE "-D_SCL_SECURE_NO_WARNINGS")
46+
add_library(foonathan::foonathan_array ALIAS foonathan_array)
47+
target_sources(foonathan_array INTERFACE $<BUILD_INTERFACE:${detail_header_files} ${header_files}>)
48+
target_compile_features(foonathan_array INTERFACE cxx_std_11)
49+
target_include_directories(foonathan_array SYSTEM INTERFACE
50+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/>
51+
$<INSTALL_INTERFACE:include/>)
52+
53+
# installation
54+
if(NOT dependency_via_submodule)
55+
include(CMakePackageConfigHelpers)
56+
write_basic_package_version_file(foonathan_array-config-version.cmake COMPATIBILITY ExactVersion)
57+
install(TARGETS foonathan_array EXPORT foonathan_array_targets
58+
INCLUDES DESTINATION include)
59+
install(EXPORT foonathan_array_targets
60+
DESTINATION lib/cmake/foonathan_array
61+
FILE foonathan_array-targets.cmake
62+
NAMESPACE foonathan::)
63+
install(DIRECTORY include/
64+
DESTINATION include)
65+
install(FILES foonathan_array-config.cmake ${CMAKE_CURRENT_BINARY_DIR}/foonathan_array-config-version.cmake
66+
DESTINATION lib/cmake/foonathan_array)
67+
else()
68+
message(STATUS "Dependency installed via submodule, installation unavailable")
4769
endif()
4870

49-
if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
71+
# subdirectories
72+
option(FOONATHAN_ARRAY_BUILD_TEST "build tests of foonathan/array" OFF)
73+
if(${FOONATHAN_ARRAY_BUILD_TEST} OR (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR))
5074
enable_testing()
5175
add_subdirectory(test)
5276
endif()

foonathan_array-config.cmake

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Copyright (C) 2018 Jonathan Müller <[email protected]>
2+
# This file is subject to the license terms in the LICENSE file
3+
# found in the top-level directory of this distribution.
4+
5+
include(CMakeFindDependencyMacro)
6+
include("${CMAKE_CURRENT_LIST_DIR}/foonathan_array-targets.cmake")

test/CMakeLists.txt

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# This file is subject to the license terms in the LICENSE file
33
# found in the top-level directory of this distribution.
44

5+
# get Catch
56
if(NOT EXISTS ${CMAKE_CURRENT_BINARY_DIR}/catch.hpp)
67
file(DOWNLOAD
78
https://raw.githubusercontent.com/catchorg/Catch2/master/single_include/catch2/catch.hpp
@@ -18,6 +19,7 @@ if(NOT EXISTS ${CMAKE_CURRENT_BINARY_DIR}/catch.hpp)
1819
endif()
1920
endif()
2021

22+
# unit tests
2123
set(tests
2224
array.cpp
2325
array_view.cpp
@@ -47,8 +49,20 @@ add_executable(foonathan_array_test
4749
equal_checker.hpp
4850
leak_checker.hpp
4951
${tests})
50-
target_include_directories(foonathan_array_test SYSTEM PUBLIC ${CMAKE_CURRENT_BINARY_DIR})
51-
target_link_libraries(foonathan_array_test PUBLIC foonathan_array)
52-
set_target_properties(foonathan_array_test PROPERTIES CXX_STANDARD 11)
52+
target_include_directories(foonathan_array_test PUBLIC
53+
${CMAKE_CURRENT_SOURCE_DIR}
54+
${CMAKE_CURRENT_SOURCE_DIR}/../include
55+
SYSTEM PUBLIC
56+
${CMAKE_CURRENT_BINARY_DIR})
57+
target_compile_features(foonathan_array_test PUBLIC cxx_std_11)
58+
target_compile_options(foonathan_array_test PUBLIC
59+
# clang/GCC warnings
60+
$<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:GNU>>:
61+
-pedantic-errors -Werror -Wall -Wextra -Wconversion -Wsign-conversion>
62+
# disable noexcept type warning on GCC
63+
$<$<CXX_COMPILER_ID:GNU>: -Wno-noexcept-type>
64+
# MSVC warnings
65+
$<$<CXX_COMPILER_ID:MSVC>:
66+
/WX /W4 /D_SCL_SECURE_NO_WARNINGS>)
5367

5468
add_test(NAME test COMMAND foonathan_array_test)

0 commit comments

Comments
 (0)