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 BEFORE PUBLIC include) 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 70INSTALL(TARGETS clog 71 LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" 72 ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" 73 PUBLIC_HEADER DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}") 74 75# ---[ clog tests 76IF(CLOG_BUILD_TESTS) 77 # ---[ Build google test 78 IF(NOT TARGET gtest) 79 IF(MSVC AND NOT CLOG_RUNTIME_TYPE STREQUAL "static") 80 SET(gtest_force_shared_crt ON CACHE BOOL "" FORCE) 81 ENDIF() 82 ADD_SUBDIRECTORY( 83 "${GOOGLETEST_SOURCE_DIR}" 84 "${CONFU_DEPENDENCIES_BINARY_DIR}/googletest") 85 ENDIF() 86 87 ADD_EXECUTABLE(clog-test test/clog.cc) 88 SET_TARGET_PROPERTIES(clog-test PROPERTIES 89 CXX_STANDARD 11 90 CXX_EXTENSIONS NO) 91 CLOG_TARGET_RUNTIME_LIBRARY(clog-test) 92 TARGET_LINK_LIBRARIES(clog-test PRIVATE clog gtest gtest_main) 93 ADD_TEST(clog-test clog-test) 94ENDIF() 95