CMakeLists.txt 10.7 KB
Newer Older
1
cmake_minimum_required(VERSION 3.0)
2 3 4

project(biboumi)

louiz’'s avatar
louiz’ committed
5
set(${PROJECT_NAME}_VERSION_MAJOR 5)
6
set(${PROJECT_NAME}_VERSION_MINOR 0)
louiz’'s avatar
louiz’ committed
7
set(${PROJECT_NAME}_VERSION_SUFFIX "~dev")
8

9 10 11
find_library(LIBASAN NAMES asan libasan.so.3 libasan.so.2 libasan.so.1)
find_library(LIBUBSAN NAMES ubsan libubsan.so.0)

louiz’'s avatar
louiz’ committed
12
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++1y -pedantic -Wall -Wextra")
13 14 15
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
  set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -fprofile-arcs -ftest-coverage --coverage")
endif()
16 17 18 19 20 21 22 23 24 25 26 27 28
if(LIBASAN)
  message(STATUS "Libasan found.")
  set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -fsanitize=address")
else()
  message(STATUS "Libasan NOT found.")
endif()
if(LIBUBSAN)
  message(STATUS "Libubsan found.")
  set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -fsanitize=undefined")
else()
  message(STATUS "Libubsan NOT found.")
endif()

29

30 31 32
#
## Look for external libraries
#
33
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules/")
34

35 36 37
#
## Get the software version
#
louiz’'s avatar
louiz’ committed
38
set(ARCHIVE_NAME ${CMAKE_PROJECT_NAME}-${${PROJECT_NAME}_VERSION_MAJOR}.${${PROJECT_NAME}_VERSION_MINOR})
39 40
set(RPM_VERSION ${${PROJECT_NAME}_VERSION_MAJOR}.${${PROJECT_NAME}_VERSION_MINOR})

louiz’'s avatar
louiz’ committed
41
if(${PROJECT_NAME}_VERSION_SUFFIX MATCHES ".+")
42
  set(ARCHIVE_NAME ${ARCHIVE_NAME}${${PROJECT_NAME}_VERSION_SUFFIX})
43
  set(RPM_VERSION ${RPM_VERSION}${${PROJECT_NAME}_VERSION_SUFFIX})
louiz’'s avatar
louiz’ committed
44 45 46
endif()

if(${PROJECT_NAME}_VERSION_SUFFIX MATCHES "^~dev$")
47 48 49
  # If we are on a dev version, append the hash of the current git HEAD to
  # the version
  include(FindGit)
louiz’'s avatar
louiz’ committed
50
  if(GIT_FOUND AND EXISTS "${CMAKE_SOURCE_DIR}/.git")
51 52 53 54 55
    execute_process(COMMAND git --git-dir=${CMAKE_SOURCE_DIR}/.git rev-parse --short HEAD
      OUTPUT_VARIABLE GIT_REVISION
      OUTPUT_STRIP_TRAILING_WHITESPACE)
    if(GIT_REVISION)
      set(${PROJECT_NAME}_VERSION_SUFFIX "${${PROJECT_NAME}_VERSION_SUFFIX} (${GIT_REVISION})")
56
      set(ARCHIVE_NAME ${ARCHIVE_NAME}${GIT_REVISION})
57
      set(RPM_VERSION ${RPM_VERSION}${GIT_REVISION})
58 59 60 61
    endif()
  endif()
endif()

62
set(SOFTWARE_VERSION
63 64
  ${${PROJECT_NAME}_VERSION_MAJOR}.${${PROJECT_NAME}_VERSION_MINOR}${${PROJECT_NAME}_VERSION_SUFFIX})

65 66
include(CheckFunctionExists)
check_function_exists(ppoll HAVE_PPOLL_FUNCTION)
67 68

# To be able to include the config.h and other files generated by cmake
69
include_directories("${CMAKE_CURRENT_BINARY_DIR}/src/")
70
include_directories("${CMAKE_CURRENT_SOURCE_DIR}/src/")
71
include_directories("${CMAKE_CURRENT_BINARY_DIR}/")
72

73 74 75
#
## Documentation
#
76 77
execute_process(COMMAND "date" "+%Y-%m-%d" OUTPUT_VARIABLE DOC_DATE
                OUTPUT_STRIP_TRAILING_WHITESPACE)
78
set(MAN_PAGE ${CMAKE_CURRENT_BINARY_DIR}/doc/${PROJECT_NAME}.1)
79
set(DOC_PAGE ${CMAKE_CURRENT_SOURCE_DIR}/doc/${PROJECT_NAME}.1.rst)
80 81 82 83 84 85 86 87 88 89 90 91 92 93
if (NOT PANDOC_EXECUTABLE)
  find_program(PANDOC_EXECUTABLE NAMES pandoc
    DOC "The pandoc software, to build the man page from the rst documentation")
  if(PANDOC_EXECUTABLE)
    message(STATUS "Found Pandoc: ${PANDOC_EXECUTABLE}")
    set(WITH_DOC true)
    file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/doc/)
    add_custom_command(OUTPUT ${MAN_PAGE}
      COMMAND ${PANDOC_EXECUTABLE} -M date="${DOC_DATE}" -s -t man ${DOC_PAGE} -o ${MAN_PAGE}
      DEPENDS ${DOC_PAGE})
    add_custom_target(doc ALL DEPENDS ${MAN_PAGE})
  else()
    message(STATUS "Pandoc not found, documentation cannot be built")
  endif()
94
endif()
95
mark_as_advanced(PANDOC_EXECUTABLE)
96

97 98 99 100 101 102 103 104 105 106 107 108
# Look for litesql and enable the database if found
if(WITH_LITESQL)
  find_package(LITESQL REQUIRED)
elseif(NOT WITHOUT_LITESQL)
  find_package(LITESQL)
endif()

if(LITESQL_FOUND)
  LITESQL_GENERATE_CPP("database/database.xml"
    "biboudb"
    LITESQL_GENERATED_SOURCES)

109
  set(source_database src/database/database.cpp ${LITESQL_GENERATED_SOURCES})
110
  set(USE_DATABASE TRUE)
111 112
else()
  set(source_database)
113 114
endif()

115 116
add_subdirectory("louloulibs")
include_directories("louloulibs")
117

118
# If they are found in louloulibs CMakeLists.txt, we inherite these values
119 120 121 122 123 124
if(SYSTEMD_FOUND)
  include_directories(${SYSTEMD_INCLUDE_DIRS})
endif()
if(BOTAN_FOUND)
  include_directories(SYSTEM ${BOTAN_INCLUDE_DIRS})
endif()
louiz’'s avatar
louiz’ committed
125 126
if(UDNS_FOUND)
  include_directories(${UDNS_INCLUDE_DIRS})
127
endif()
128

129 130 131
#
## utils
#
132
file(GLOB source_src_utils
133 134
    src/utils/*.[hc]pp)

135 136 137
#
## irclib
#
138 139
file(GLOB source_irc
  src/irc/*.[hc]pp)
140 141

#
142
## xmpp
143
#
144 145
file(GLOB source_xmpp
  src/xmpp/*.[hc]pp)
146

louiz’'s avatar
louiz’ committed
147 148 149 150 151 152
#
## identd
#
file(GLOB source_identd
  src/identd/*.[hc]pp)

153 154 155 156 157
#
## bridge
#
file(GLOB source_bridge
  src/bridge/*.[hc]pp)
158

159 160 161
#
## Main executable
#
162
add_executable(${PROJECT_NAME} src/main.cpp
163 164 165 166 167 168 169 170 171 172 173
        ${source_utils}
        ${source_config}
        ${source_logger}
        ${source_network}
        ${source_xmpplib}
        ${source_xmpp}
        ${source_src_utils}
        ${source_bridge}
        ${source_irc}
        ${source_identd}
        ${source_database})
louiz’'s avatar
louiz’ committed
174

175 176 177
#
## Tests
#
louiz’'s avatar
louiz’ committed
178 179
file(GLOB source_tests
  tests/*.cpp)
180
add_executable(test_suite EXCLUDE_FROM_ALL ${source_tests}
181 182 183 184 185 186 187 188 189 190 191
        ${source_utils}
        ${source_config}
        ${source_logger}
        ${source_network}
        ${source_xmpplib}
        ${source_xmpp}
        ${source_src_utils}
        ${source_bridge}
        ${source_irc}
        ${source_identd}
        ${source_database})
192 193 194 195 196

target_link_libraries(${PROJECT_NAME}
        ${EXPAT_LIBRARIES}
        ${LIBUUID_LIBRARIES}
        ${EXPAT_LIBRARIES})
197
target_link_libraries(test_suite
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
        ${EXPAT_LIBRARIES}
        ${LIBUUID_LIBRARIES}
        ${EXPAT_LIBRARIES})
if(SYSTEMD_FOUND)
  target_link_libraries(${PROJECT_NAME} ${SYSTEMD_LIBRARIES})
  target_link_libraries(test_suite ${SYSTEMD_LIBRARIES})
endif()
if(BOTAN_FOUND)
  target_link_libraries(${PROJECT_NAME} ${BOTAN_LIBRARIES})
  target_link_libraries(test_suite ${BOTAN_LIBRARIES})
elseif(GCRYPT_FOUND)
  target_link_libraries(${PROJECT_NAME} ${GCRYPT_LIBRARIES})
  target_link_libraries(test_suite ${GCRYPT_LIBRARIES})
endif()
if(UDNS_FOUND)
  target_link_libraries(${PROJECT_NAME} ${UDNS_LIBRARIES})
  target_link_libraries(test_suite ${UDNS_LIBRARIES})
endif()
if(LIBIDN_FOUND)
  target_link_libraries(${PROJECT_NAME} ${LIBIDN_LIBRARIES})
  target_link_libraries(test_suite ${LIBIDN_LIBRARIES})
endif()
220
if(USE_DATABASE)
221 222
  target_link_libraries(${PROJECT_NAME} ${LITESQL_LIBRARIES})
  target_link_libraries(test_suite ${LITESQL_LIBRARIES})
223
endif()
224

225 226 227 228 229 230 231 232
# Define a __FILENAME__ macro with the relative path (from the base project directory)
# of each source file
file(GLOB_RECURSE source_all src/*.[hc]pp tests/*.[hc]pp)
foreach(file ${source_all})
  file(RELATIVE_PATH shorter_file ${CMAKE_CURRENT_SOURCE_DIR} ${file})
  set_property(SOURCE ${file} APPEND PROPERTY COMPILE_DEFINITIONS __FILENAME__="${shorter_file}")
endforeach()

louiz’'s avatar
louiz’ committed
233 234
include(ExternalProject)
ExternalProject_Add(catch
235
  GIT_REPOSITORY "https://lab.louiz.org/louiz/Catch.git"
louiz’'s avatar
louiz’ committed
236 237 238 239 240
  PREFIX "external"
  UPDATE_COMMAND ""
  CONFIGURE_COMMAND ""
  BUILD_COMMAND ""
  INSTALL_COMMAND ""
241
  )
242
set_target_properties(catch PROPERTIES EXCLUDE_FROM_ALL TRUE)
louiz’'s avatar
louiz’ committed
243
ExternalProject_Get_Property(catch SOURCE_DIR)
244 245 246 247 248 249
if(NOT EXISTS ${CMAKE_SOURCE_DIR}/tests/catch.hpp)
  target_include_directories(test_suite
    PUBLIC "${SOURCE_DIR}/include/"
    )
  add_dependencies(test_suite catch)
endif()
louiz’'s avatar
louiz’ committed
250
add_custom_target(check COMMAND "test_suite"
251
  DEPENDS test_suite biboumi)
252 253
add_custom_target(e2e COMMAND "python3" "${CMAKE_CURRENT_SOURCE_DIR}/tests/end_to_end/"
  DEPENDS biboumi)
louiz’'s avatar
louiz’ committed
254 255
add_custom_target(e2e_valgrind COMMAND "E2E_BIBOUMI_SUPP_DIR=${CMAKE_CURRENT_SOURCE_DIR}/tests/end_to_end/" "E2E_BIBOUMI_VALGRIND=1" "python3" "${CMAKE_CURRENT_SOURCE_DIR}/tests/end_to_end/"
    DEPENDS biboumi)
256

257

louiz’'s avatar
louiz’ committed
258 259 260
#
## Code coverage
#
louiz’'s avatar
louiz’ committed
261
if(CMAKE_BUILD_TYPE MATCHES Debug)
262
  include(CodeCoverage)
263
  SETUP_TARGET_FOR_COVERAGE(coverage_check
264 265 266 267
    ./test_suite
    coverage_test_suite)
  add_dependencies(coverage_check test_suite)

268
  SETUP_TARGET_FOR_COVERAGE(coverage_e2e
269
    python3
270
    coverage_e2e
271 272
    ${CMAKE_CURRENT_SOURCE_DIR}/tests/end_to_end/)
  add_dependencies(coverage_e2e biboumi)
273 274 275 276 277 278 279 280 281

  ADD_CUSTOM_TARGET(coverage
    COMMAND ${LCOV_PATH} -a coverage_e2e.info -a coverage_test_suite.info -o coverage_total.info

    COMMAND ${GENHTML_PATH} -o coverage_total coverage_total.info

    WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
    )

282
endif()
louiz’'s avatar
louiz’ committed
283

louiz’'s avatar
louiz’ committed
284 285 286
#
## Install target
#
287 288
install(TARGETS ${PROJECT_NAME} RUNTIME                     DESTINATION bin)
install(FILES   ${MAN_PAGE}                                 DESTINATION share/man/man1         OPTIONAL COMPONENT documentation)
289
install(FILES   ${CMAKE_CURRENT_BINARY_DIR}/biboumi.service DESTINATION lib/systemd/system     COMPONENT init)
290
install(FILES   conf/biboumi.cfg                            DESTINATION /etc/biboumi           COMPONENT configuration)
louiz’'s avatar
louiz’ committed
291 292 293 294 295

#
## Dist target
## Generate a release tarball from the git sources
#
296
add_custom_command(OUTPUT ${ARCHIVE_NAME}.tar.xz
297
  COMMAND git archive --prefix=${ARCHIVE_NAME}/ --format=tar HEAD^{tree}
298 299 300 301 302 303 304
          > ${CMAKE_CURRENT_BINARY_DIR}/${ARCHIVE_NAME}.tar
  # Append this specific file that is not part of the git repo
  COMMAND tar -rf ${CMAKE_CURRENT_BINARY_DIR}/${ARCHIVE_NAME}.tar -P ${SOURCE_DIR}/single_include/catch.hpp --xform 's|/.*/|${ARCHIVE_NAME}/tests/|g'
  # Remove a potential existing archive
  COMMAND rm -f ${CMAKE_CURRENT_BINARY_DIR}/${ARCHIVE_NAME}.tar.xz
  # Compress the archive
  COMMAND xz ${CMAKE_CURRENT_BINARY_DIR}/${ARCHIVE_NAME}.tar
305
  COMMAND ${CMAKE_COMMAND} -E cmake_echo_color --cyan "${ARCHIVE_NAME}.tar.xz created."
306 307
  WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
  )
308 309 310
add_custom_target(dist
  DEPENDS ${ARCHIVE_NAME}.tar.xz
  DEPENDS catch)
311 312

add_custom_target(rpm
313
  DEPENDS dist
314 315 316
  COMMAND mkdir -p rpmbuild/{BUILD,RPMS,SOURCES,SPECS,SRPMS}
  COMMAND rpmbuild --define "_topdir `pwd`/rpmbuild/" --define "_sourcedir `pwd`" -ba biboumi.spec
  )
317

318 319
configure_file(biboumi.h.cmake src/biboumi.h)

320 321
set(SYSTEMD_SERVICE_TYPE_DOCSTRING "The value used as the Type= in the systemd unit file.")
set(WATCHDOG_SEC_DOCSTRING "The value used as WatchdogSec= in the systemd unit file.")
322
if(SYSTEMD_FOUND)
323 324
  set(SYSTEMD_SERVICE_TYPE "notify" CACHE STRING ${SYSTEMD_SERVICE_TYPE_DOCSTRING})
  set(WATCHDOG_SEC "20" CACHE STRING ${WATCHDOG_SEC_DOCSTRING})
325
else()
326 327
  set(SYSTEMD_SERVICE_TYPE "simple" CACHE STRING ${SYSTEMD_SERVICE_TYPE_DOCSTRING})
  set(WATCHDOG_SEC "" CACHE STRING ${WATCHDOG_SEC_DOCSTRING})
328
endif()
329
set(SERVICE_USER_DOCSTRING "The value used as the User= in the systemd unit file.")
330
if(NOT DEFINED SERVICE_USER)
331
  set(SERVICE_USER "nobody" CACHE STRING ${SERVICE_USER_DOCSTRING})
332
endif()
333
set(SERVICE_GROUP_DOCSTRING "The value used as the Group= in the systemd unit file.")
334
if(NOT DEFINED SERVICE_GROUP)
335
  set(SERVICE_GROUP "nobody" CACHE STRING ${SERVICE_GROUP_DOCSTRING})
336
endif()
337
configure_file(unit/biboumi.service.cmake biboumi.service)
338

louiz’'s avatar
louiz’ committed
339
# Force the format of the date output
340
set(ENV{LANG} "C")
341 342
execute_process(COMMAND "date" "+%a %b %d %Y" OUTPUT_VARIABLE RPM_DATE
                OUTPUT_STRIP_TRAILING_WHITESPACE)
343
unset(ENV{LANG})
344 345

configure_file(packaging/biboumi.spec.cmake biboumi.spec)