mirror of
https://github.com/nemtrif/utfcpp.git
synced 2025-04-06 05:55:08 +00:00
Refactor CMake files and add Conan's support
- Add a finder for Gtest and use targets set by the finder (compatibility cmake >=3.5) - Refactor unittests using function add_unittest() (cmake >=3.4) - If conanbuildinfo.cmake is not found, fall back on googletest imported by git-submodule
This commit is contained in:
parent
e4f1ed4808
commit
65a2b2674e
4 changed files with 116 additions and 35 deletions
|
@ -1,10 +1,71 @@
|
|||
cmake_minimum_required (VERSION 3.0.2)
|
||||
project (utf8cpp VERSION 3.1 LANGUAGES CXX)
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
project (utf8cpp
|
||||
DESCRIPTION "UTF-8 with C++ in a Portable Way"
|
||||
VERSION 3.1
|
||||
LANGUAGES CXX)
|
||||
|
||||
option(UTF8_TESTS "Enable tests for UTF8-CPP" On)
|
||||
option(UTF8_INSTALL "Enable installation for UTF8-CPP" On)
|
||||
option(UTF8_SAMPLES "Enable building samples for UTF8-CPP" On)
|
||||
|
||||
###############
|
||||
# Conan support
|
||||
###############
|
||||
|
||||
if (UTF8_TESTS)
|
||||
if (EXISTS "${CMAKE_BINARY_DIR}/conanbuildinfo.cmake")
|
||||
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
|
||||
message(STATUS "conan_basic_setup()")
|
||||
conan_basic_setup(TARGETS)
|
||||
set(USE_CONAN True)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
###############################
|
||||
# Check compiler's capabilities
|
||||
###############################
|
||||
|
||||
include (CheckCXXCompilerFlag)
|
||||
|
||||
if(CMAKE_C_COMPILER_ID MATCHES "Clang")
|
||||
set (CMAKE_COMPILER_IS_CLANG true)
|
||||
elseif(CMAKE_C_COMPILER_ID MATCHES "MSVC")
|
||||
set (CMAKE_COMPILER_IS_MSVC true)
|
||||
endif()
|
||||
|
||||
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_COMPILER_IS_CLANG)
|
||||
find_program (LINKER_BIN gold)
|
||||
if(LINKER_BIN)
|
||||
set(LINKER_BIN "gold")
|
||||
else()
|
||||
find_program (LINKER_BIN ld.gold)
|
||||
if(LINKER_BIN)
|
||||
set(LINKER_BIN "ld.gold")
|
||||
else()
|
||||
set(LINKER_BIN "ld")
|
||||
endif()
|
||||
endif()
|
||||
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -W")
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall")
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wextra")
|
||||
#set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror")
|
||||
|
||||
CHECK_CXX_COMPILER_FLAG("-fuse-ld=${LINKER_BIN}" USE_LINKER_FLAG)
|
||||
if(USE_LINKER_FLAG)
|
||||
set (CMAKE_SHARED_LINKER_FLAGS "-fuse-ld=${LINKER_BIN} ${CMAKE_SHARED_LINKER_FLAGS}")
|
||||
endif()
|
||||
elseif(CMAKE_COMPILER_IS_MSVC)
|
||||
#set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_SECURE_SCL=0")
|
||||
#set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_CRT_SECURE_NO_DEPRECATE")
|
||||
#set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_CRT_NONSTDC_NO_DEPRECATE")
|
||||
#set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_SCL_SECURE_NO_DEPRECATE")
|
||||
#set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Zi")
|
||||
endif()
|
||||
|
||||
message(STATUS "CMAKE_INSTALL_PREFIX: ${CMAKE_INSTALL_PREFIX}")
|
||||
set (CMAKE_INSTALL_INCLUDEDIR "${CMAKE_INSTALL_PREFIX}/include")
|
||||
|
||||
add_library(utf8cpp INTERFACE)
|
||||
target_include_directories(utf8cpp INTERFACE
|
||||
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/source>"
|
||||
|
@ -26,12 +87,13 @@ if(UTF8_INSTALL)
|
|||
endif()
|
||||
|
||||
if(UTF8_SAMPLES)
|
||||
add_executable(docsample ${PROJECT_SOURCE_DIR}/samples/docsample.cpp)
|
||||
target_link_libraries(docsample PRIVATE utf8::cpp)
|
||||
add_subdirectory(samples)
|
||||
endif()
|
||||
|
||||
if(UTF8_TESTS)
|
||||
enable_testing()
|
||||
add_subdirectory(extern/gtest)
|
||||
if (NOT USE_CONAN)
|
||||
add_subdirectory(extern/gtest)
|
||||
endif()
|
||||
add_subdirectory(tests)
|
||||
endif()
|
||||
|
|
5
conanfile.txt
Normal file
5
conanfile.txt
Normal file
|
@ -0,0 +1,5 @@
|
|||
[build_requires]
|
||||
gtest/1.8.1@bincrafters/stable
|
||||
|
||||
[generators]
|
||||
cmake
|
3
samples/CMakeLists.txt
Normal file
3
samples/CMakeLists.txt
Normal file
|
@ -0,0 +1,3 @@
|
|||
add_executable(docsample
|
||||
"docsample.cpp")
|
||||
target_link_libraries(docsample PRIVATE utf8::cpp)
|
|
@ -1,35 +1,46 @@
|
|||
add_executable(negative ${PROJECT_SOURCE_DIR}/tests/negative.cpp)
|
||||
add_executable(cpp11 ${PROJECT_SOURCE_DIR}/tests/test_cpp11.cpp)
|
||||
add_executable(apitests
|
||||
${PROJECT_SOURCE_DIR}/tests/test_checked_api.cpp
|
||||
${PROJECT_SOURCE_DIR}/tests/test_unchecked_api.cpp
|
||||
${PROJECT_SOURCE_DIR}/tests/test_checked_iterator.cpp
|
||||
${PROJECT_SOURCE_DIR}/tests/test_unchecked_iterator.cpp
|
||||
)
|
||||
find_package (GTest REQUIRED)
|
||||
|
||||
add_executable(noexceptionstests
|
||||
${PROJECT_SOURCE_DIR}/tests/test_unchecked_api.cpp
|
||||
${PROJECT_SOURCE_DIR}/tests/test_unchecked_iterator.cpp
|
||||
)
|
||||
#include(CMakeParseArguments)
|
||||
function(add_unittest)
|
||||
set(options _empty)
|
||||
set(oneValueArgs TESTNAME)
|
||||
set(multiValueArgs SOURCES LIBRARIES ARGUMENTS)
|
||||
cmake_parse_arguments(PARSE_ARGV 0 "impl" "${options}" "${oneValueArgs}" "${multiValueArgs}")
|
||||
|
||||
target_link_libraries(negative PRIVATE utf8::cpp)
|
||||
target_link_libraries(cpp11 PRIVATE
|
||||
utf8::cpp
|
||||
gtest_main
|
||||
)
|
||||
target_link_libraries(apitests PRIVATE
|
||||
utf8::cpp
|
||||
gtest_main
|
||||
)
|
||||
add_executable(${impl_TESTNAME} ${impl_SOURCES})
|
||||
target_link_libraries(${impl_TESTNAME} PRIVATE
|
||||
utf8::cpp
|
||||
${impl_LIBRARIES}
|
||||
)
|
||||
if (NOT ${impl_ARGUMENTS})
|
||||
add_test(NAME "${impl_TESTNAME}_test"
|
||||
COMMAND "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${impl_TESTNAME}" ${impl_ARGUMENTS})
|
||||
else()
|
||||
add_test(NAME "${impl_TESTNAME}_test"
|
||||
COMMAND "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${impl_TESTNAME}")
|
||||
endif()
|
||||
endfunction()
|
||||
|
||||
target_link_libraries(noexceptionstests PRIVATE
|
||||
utf8::cpp
|
||||
gtest_main
|
||||
)
|
||||
target_compile_options(noexceptionstests PUBLIC -fno-exceptions)
|
||||
add_unittest(TESTNAME negative
|
||||
SOURCES "negative.cpp"
|
||||
ARGUMENTS "${PROJECT_SOURCE_DIR}/tests/test_data/utf8_invalid.txt")
|
||||
|
||||
add_test(negative_test negative ${PROJECT_SOURCE_DIR}/tests/test_data/utf8_invalid.txt)
|
||||
add_test(cpp11_test cpp11)
|
||||
add_test(api_test apitests)
|
||||
add_test(noexceptions_test noexceptionstests)
|
||||
add_unittest(TESTNAME cpp11
|
||||
SOURCES "test_cpp11.cpp"
|
||||
LIBRARIES GTest::Main)
|
||||
|
||||
add_unittest(TESTNAME apitests
|
||||
SOURCES
|
||||
"test_checked_api.cpp"
|
||||
"test_unchecked_api.cpp"
|
||||
"test_checked_iterator.cpp"
|
||||
"test_unchecked_iterator.cpp"
|
||||
LIBRARIES GTest::Main)
|
||||
|
||||
add_unittest(TESTNAME noexceptionstests
|
||||
SOURCES
|
||||
"test_unchecked_api.cpp"
|
||||
"test_unchecked_iterator.cpp"
|
||||
LIBRARIES GTest::Main)
|
||||
target_compile_options(noexceptionstests PUBLIC "-fno-exceptions")
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue