• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1CMAKE_MINIMUM_REQUIRED(VERSION 3.1 FATAL_ERROR)
2
3INCLUDE(GNUInstallDirs)
4
5# ---[ Project and semantic versioning.
6PROJECT(clog C CXX)
7
8# ---[ Options.
9SET(CLOG_RUNTIME_TYPE "default" CACHE STRING "Type of runtime library (shared, static, or default) to use")
10SET_PROPERTY(CACHE CLOG_RUNTIME_TYPE PROPERTY STRINGS default static shared)
11IF(ANDROID)
12  OPTION(CLOG_LOG_TO_STDIO "Log errors, warnings, and information to stdout/stderr" OFF)
13ELSE()
14  OPTION(CLOG_LOG_TO_STDIO "Log errors, warnings, and information to stdout/stderr" ON)
15ENDIF()
16OPTION(CLOG_BUILD_TESTS "Build clog tests" ON)
17
18# ---[ CMake options
19IF(CLOG_BUILD_TESTS)
20  ENABLE_TESTING()
21ENDIF()
22
23MACRO(CLOG_TARGET_RUNTIME_LIBRARY target)
24  IF(MSVC AND NOT CLOG_RUNTIME_TYPE STREQUAL "default")
25    IF(CLOG_RUNTIME_TYPE STREQUAL "shared")
26      TARGET_COMPILE_OPTIONS(${target} PRIVATE
27        "/MD$<$<CONFIG:Debug>:d>")
28    ELSEIF(CLOG_RUNTIME_TYPE STREQUAL "static")
29      TARGET_COMPILE_OPTIONS(${target} PRIVATE
30        "/MT$<$<CONFIG:Debug>:d>")
31    ENDIF()
32  ENDIF()
33ENDMACRO()
34
35# ---[ Download deps
36SET(CONFU_DEPENDENCIES_SOURCE_DIR ${CMAKE_SOURCE_DIR}/deps
37  CACHE PATH "Confu-style dependencies source directory")
38SET(CONFU_DEPENDENCIES_BINARY_DIR ${CMAKE_BINARY_DIR}/deps
39  CACHE PATH "Confu-style dependencies binary directory")
40
41IF(CLOG_BUILD_TESTS)
42  IF(NOT DEFINED GOOGLETEST_SOURCE_DIR)
43    MESSAGE(STATUS "Downloading Google Test to ${CONFU_DEPENDENCIES_SOURCE_DIR}/googletest (define GOOGLETEST_SOURCE_DIR to avoid it)")
44    CONFIGURE_FILE(cmake/DownloadGoogleTest.cmake "${CONFU_DEPENDENCIES_BINARY_DIR}/googletest-download/CMakeLists.txt")
45    EXECUTE_PROCESS(COMMAND "${CMAKE_COMMAND}" -G "${CMAKE_GENERATOR}" .
46      WORKING_DIRECTORY "${CONFU_DEPENDENCIES_BINARY_DIR}/googletest-download")
47    EXECUTE_PROCESS(COMMAND "${CMAKE_COMMAND}" --build .
48      WORKING_DIRECTORY "${CONFU_DEPENDENCIES_BINARY_DIR}/googletest-download")
49    SET(GOOGLETEST_SOURCE_DIR "${CONFU_DEPENDENCIES_SOURCE_DIR}/googletest" CACHE STRING "Google Test source directory")
50  ENDIF()
51ENDIF()
52
53# ---[ clog library
54ADD_LIBRARY(clog STATIC src/clog.c)
55SET_TARGET_PROPERTIES(clog PROPERTIES
56  C_STANDARD 99
57  C_EXTENSIONS NO)
58CLOG_TARGET_RUNTIME_LIBRARY(clog)
59SET_TARGET_PROPERTIES(clog PROPERTIES PUBLIC_HEADER include/clog.h)
60TARGET_INCLUDE_DIRECTORIES(clog PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
61IF(CLOG_LOG_TO_STDIO)
62  TARGET_COMPILE_DEFINITIONS(clog PRIVATE CLOG_LOG_TO_STDIO=1)
63ELSE()
64  TARGET_COMPILE_DEFINITIONS(clog PRIVATE CLOG_LOG_TO_STDIO=0)
65ENDIF()
66IF(ANDROID AND NOT CLOG_LOG_TO_STDIO)
67  TARGET_LINK_LIBRARIES(clog PRIVATE log)
68ENDIF()
69
70ADD_LIBRARY(cpuinfo::clog ALIAS clog)
71
72INSTALL(TARGETS clog
73  EXPORT cpuinfo-targets
74  LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
75  ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
76  PUBLIC_HEADER DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}")
77
78# ---[ clog tests
79IF(CLOG_BUILD_TESTS)
80  # ---[ Build google test
81  IF(NOT TARGET gtest)
82    IF(MSVC AND NOT CLOG_RUNTIME_TYPE STREQUAL "static")
83      SET(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
84    ENDIF()
85    ADD_SUBDIRECTORY(
86      "${GOOGLETEST_SOURCE_DIR}"
87      "${CONFU_DEPENDENCIES_BINARY_DIR}/googletest")
88  ENDIF()
89
90  ADD_EXECUTABLE(clog-test test/clog.cc)
91  SET_TARGET_PROPERTIES(clog-test PROPERTIES
92    CXX_STANDARD 11
93    CXX_EXTENSIONS NO)
94  CLOG_TARGET_RUNTIME_LIBRARY(clog-test)
95  TARGET_LINK_LIBRARIES(clog-test PRIVATE clog gtest gtest_main)
96  ADD_TEST(clog-test clog-test)
97ENDIF()
98