-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCMakeLists.txt
115 lines (94 loc) · 3.59 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
cmake_minimum_required(VERSION 3.6)
project(libjapi VERSION 0.4)
set(SOVERSION 1)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD_REQUIRED ON)
# 'cmake -DCMAKE_BUILD_TYPE=Debug ../' from build folder for debug output #
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release")
endif()
file(GLOB SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/*)
file(GLOB HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/include/*.h)
##############################################
# Create shared and static libjapi libraries #
# configure libraries
find_package(PkgConfig)
pkg_search_module(JSONC REQUIRED IMPORTED_TARGET json-c)
# create an object lib to avoid double compilation
add_library(japi_objs OBJECT ${SOURCES} ${HEADERS})
if(DEFINED CREADLINE_MAX_LINE_SIZE)
if(CREADLINE_MAX_LINE_SIZE LESS 1024)
message(FATAL_ERROR "Minimal linesize is CREADLINE_BLOCK_SIZE=1024 bytes")
endif()
target_compile_definitions(japi_objs PRIVATE CREADLINE_MAX_LINE_SIZE=${CREADLINE_MAX_LINE_SIZE})
endif()
target_compile_options(japi_objs PUBLIC "-pthread" "-D_POSIX_C_SOURCE=200809L")
target_include_directories(japi_objs PUBLIC include/ ${JSONC_INCLUDE_DIRS})
set_property(TARGET japi_objs PROPERTY POSITION_INDEPENDENT_CODE ON)
# set up actual libraries
add_library(japi SHARED $<TARGET_OBJECTS:japi_objs>)
add_library(japi-static STATIC $<TARGET_OBJECTS:japi_objs>)
set_target_properties(japi PROPERTIES PUBLIC_HEADER "${PUBLIC_HEADERS}"
SOVERSION ${SOVERSION})
target_link_libraries(japi PkgConfig::JSONC)
target_link_libraries(japi-static PkgConfig::JSONC)
# only install libjapi.so because the static version is usually linked directly
# from the build directory
install(TARGETS japi
RUNTIME DESTINATION bin
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
PUBLIC_HEADER DESTINATION include
)
################################
# Create Doxygen documentation #
find_package(Doxygen)
if(DOXYGEN_FOUND)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/doxydir/Doxyfile.in
${CMAKE_CURRENT_BINARY_DIR}/Doxyfile @ONLY)
add_custom_target(
doc
${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
COMMENT "Generating API documentation with Doxygen" VERBATIM
)
endif(DOXYGEN_FOUND)
################################
# Create unit test excecutable #
option(LIBJAPI_ENABLE_TESTING "Enable testing with googletest" ON)
if(LIBJAPI_ENABLE_TESTING)
cmake_minimum_required(VERSION 3.14)
enable_testing()
# Include GoogleTest in the project
include(FetchContent)
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG release-1.8.0)
FetchContent_MakeAvailable(googletest)
include(GoogleTest)
# Define test executable
add_executable(testsuite test/japi_test.cc)
target_compile_options(testsuite PUBLIC "-pthread")
target_link_libraries(testsuite gtest_main japi)
# Include directories
target_include_directories(testsuite PUBLIC include/ src/
${JSONC_INCLUDE_DIRS})
gtest_discover_tests(testsuite)
################################
# Test code coverage #
if(CMAKE_BUILD_TYPE MATCHES "Debug")
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
include(${CMAKE_SOURCE_DIR}/cmake/CodeCoverage.cmake)
set(COVERAGE_EXCLUDES
"doxydir"
"/usr/include"
"_deps/googletest"
"test/*"
)
append_coverage_compiler_flags()
setup_target_for_coverage_lcov(NAME coverage EXECUTABLE testsuite)
endif()
endif(LIBJAPI_ENABLE_TESTING)