modern cmake project struct

Added ability to add as a dependency to another cmake project via add_subdirectory
This commit is contained in:
Egor Kuzmin 2020-11-03 18:25:37 +03:00
parent efc6744581
commit 6ac13af81a
3 changed files with 72 additions and 77 deletions

View File

@ -1,83 +1,32 @@
cmake_minimum_required(VERSION 3.7.0)
project(test_uuid CXX)
project(stduuid CXX)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake")
include_directories(${CMAKE_SOURCE_DIR}/include)
include_directories(${CMAKE_SOURCE_DIR}/catch)
option(UUID_BUILD_TESTS "Build the unit tests" ON)
option(UUID_SYSTEM_GENERATOR "Enable operating system uuid generator" ON)
file(GLOB headers ${CMAKE_SOURCE_DIR}/include/*.h)
file(GLOB SOURCES "test/*.cpp" "include/*.cpp")
if(BUILD_TESTS)
include(CTest)
endif()
# Library target
add_library(${PROJECT_NAME} INTERFACE)
target_include_directories(${PROJECT_NAME} INTERFACE ${CMAKE_SOURCE_DIR}/include)
# Using system uuid generator
if (UUID_SYSTEM_GENERATOR)
add_definitions(-DUUID_SYSTEM_GENERATOR)
endif()
if(WIN32)
message(status "Setting MSVC flags")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHc /std:c++17")
add_definitions(-D_SCL_SECURE_NO_WARNINGS)
elseif(APPLE)
message(status "Setting Clang flags")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -fexceptions -g -Wall")
else()
#include_directories(${LIBUUID_INCLUDE_DIR})
message(status "Setting GCC flags")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -fexceptions -g -Wall")
endif()
add_executable(test_uuid ${SOURCES} ${headers})
target_compile_definitions(${PROJECT_NAME} INTERFACE UUID_SYSTEM_GENERATOR)
if (WIN32)
elseif (APPLE)
find_library(CFLIB CoreFoundation)
target_link_libraries(test_uuid ${CFLIB})
find_library(CFLIB CoreFoundation REQUIRED)
target_link_libraries(${PROJECT_NAME} INTERFACE ${CFLIB})
else ()
FIND_PATH(LIBUUID_INCLUDE_DIRS uuid/uuid.h)
FIND_LIBRARY(LIBUUID_LIBRARIES uuid)
IF (LIBUUID_LIBRARIES AND LIBUUID_INCLUDE_DIRS)
SET(LIBUUID_FOUND 1)
IF (NOT LibUuid_FIND_QUIETLY)
MESSAGE(STATUS "Found libuuid: ${LIBUUID_LIBRARIES}")
ENDIF ( NOT LibUuid_FIND_QUIETLY )
ELSE (LIBUUID_LIBRARIES AND LIBUUID_INCLUDE_DIRS)
IF (LibUuid_FIND_REQUIRED)
MESSAGE(SEND_ERROR "Could NOT find libuuid")
ELSE (LibUuid_FIND_REQUIRED)
IF (NOT LIBUUID_FIND_QUIETLY)
MESSAGE(STATUS "Could NOT find libuuid")
ENDIF (NOT LIBUUID_FIND_QUIETLY)
ENDIF (LibUuid_FIND_REQUIRED)
ENDIF (LIBUUID_LIBRARIES AND LIBUUID_INCLUDE_DIRS)
MARK_AS_ADVANCED(LIBUUID_LIBRARIES LIBUUID_INCLUDE_DIRS)
include_directories(${LIBUUID_INCLUDE_DIRS})
#find_package(Libuuid REQUIRED NO_MODULE)
#if (NOT LIBUUID_FOUND)
# message(FATAL_ERROR
# "You might need to run 'sudo apt-get install uuid-dev' or similar")
#endif()
target_link_libraries(test_uuid ${LIBUUID_LIBRARY})
find_package(Libuuid REQUIRED)
if (Libuuid_FOUND)
target_include_directories(${PROJECT_NAME} INTERFACE ${Libuuid_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} INTERFACE ${Libuuid_LIBRARIES})
endif ()
endif ()
endif ()
message(status "** CMAKE_CXX_FLAGS: ${CMAKE_CXX_FLAGS}")
if(BUILD_TESTS)
enable_testing()
add_test(NAME "test_uuid" COMMAND "test_uuid" "-r compact")
set_tests_properties("test_uuid"
PROPERTIES
PASS_REGULAR_EXPRESSION "Passed all.*")
set_tests_properties("test_uuid"
PROPERTIES
FAIL_REGULAR_EXPRESSION "Failed \\d+ test cases")
set_tests_properties("test_uuid"
PROPERTIES
TIMEOUT 120)
# Tests
if (UUID_BUILD_TESTS)
add_subdirectory(test)
endif ()

17
cmake/FindLibuuid.cmake Normal file
View File

@ -0,0 +1,17 @@
find_path(Libuuid_INCLUDE_DIRS uuid/uuid.h)
find_library(Libuuid_LIBRARIES uuid)
if (Libuuid_LIBRARIES AND Libuuid_INCLUDE_DIRS)
set(Libuuid_FOUND YES)
if (NOT Libuuid_FIND_QUIETLY)
message(STATUS "Found libuuid: ${Libuuid_LIBRARIES}")
endif ()
else ()
if (Libuuid_FIND_REQUIRED)
message(SEND_ERROR "Could NOT find libuuid")
else ()
if (NOT Libuuid_FIND_QUIETLY)
message(STATUS "Could NOT find libuuid")
endif ()
endif ()
endif ()

29
test/CMakeLists.txt Normal file
View File

@ -0,0 +1,29 @@
enable_testing()
# Test target
add_executable(test_${PROJECT_NAME} main.cpp test_generators.cpp test_uuid.cpp)
target_include_directories(test_${PROJECT_NAME} PRIVATE ${PROJECT_SOURCE_DIR}/catch)
target_link_libraries(test_${PROJECT_NAME} PRIVATE ${PROJECT_NAME})
set_target_properties(test_${PROJECT_NAME} PROPERTIES CXX_STANDARD 17)
if(WIN32)
target_compile_options(test_${PROJECT_NAME} PRIVATE /EHc)
target_compile_definitions(test_${PROJECT_NAME} PRIVATE _SCL_SECURE_NO_WARNINGS)
elseif(APPLE)
target_compile_options(test_${PROJECT_NAME} PRIVATE -fexceptions -g -Wall)
else()
target_compile_options(test_${PROJECT_NAME} PRIVATE -fexceptions -g -Wall)
endif()
get_target_property(CURRENT_COMPILE_OPTIONS test_${PROJECT_NAME} COMPILE_OPTIONS)
message(STATUS "** ${CMAKE_CXX_COMPILER_ID} flags: ${CURRENT_COMPILE_OPTIONS}")
# Tests
add_test(NAME "test_${PROJECT_NAME}" COMMAND "test_${PROJECT_NAME}" "-r compact")
set_tests_properties("test_${PROJECT_NAME}"
PROPERTIES
PASS_REGULAR_EXPRESSION "Passed all.*")
set_tests_properties("test_${PROJECT_NAME}"
PROPERTIES
FAIL_REGULAR_EXPRESSION "Failed \\d+ test cases")
set_tests_properties("test_${PROJECT_NAME}"
PROPERTIES
TIMEOUT 120)