diff --git a/CMakeLists.txt b/CMakeLists.txt index eb15a36..37ed67a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,17 +1,86 @@ -cmake_minimum_required(VERSION 2.8) +cmake_minimum_required(VERSION 3.13) -project(anyoption) +project(anyoption VERSION 1.0.0 LANGUAGES CXX) -set(anyoption_SRCS - anyoption.cpp - ) +set(CMAKE_CXX_STANDARD 17) +list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake") +include(catch2) -set(anyoption_HDRS - anyoption.h - ) +option(WARNING_AS_ERRORS "Treat any warning as compile time error" OFF) +option(STATIC_ANALYZERS "Run static analyzers (clang-tidy, cppcheck) during compilation" ON) +option(CODE_COVERAGE "Enable code coverage reports; requires compilation with clamg/llvm" ON) -include_directories( - . - ) +if(WARNING_AS_ERRORS) + set(COMPILE_WARNING_AS_ERROR "-Werror") +endif(WARNING_AS_ERRORS) -add_library(anyoption SHARED ${anyoption_SRCS} ${anyoption_HDRS}) +if(STATIC_ANALYZERS) + find_program(CPPCHECK_COMMAND NAMES cppcheck) + set(CMAKE_CXX_CPPCHECK "${CPPCHECK_COMMAND};--quiet;--template=gcc;--suppress=*:*/*_autogen/*") +endif(STATIC_ANALYZERS) + +add_library(${PROJECT_NAME}) +add_library(${PROJECT_NAME}::${PROJECT_NAME} ALIAS ${PROJECT_NAME}) + +target_include_directories(${PROJECT_NAME} + PUBLIC + src/ +) + +target_compile_options(${PROJECT_NAME} + PRIVATE + ${COMPILE_WARNING_AS_ERROR} +# -Wall +# -Wextra +# -Wshadow +# -Wnon-virtual-dtor +# -pedantic +# -Wold-style-cast +# -Wcast-align +# -Wunused +# -Woverloaded-virtual +# -Wpedantic +# -Wconversion +# -Wsign-conversion +# -Wnull-dereference +# -Wdouble-promotion +) + +target_sources(${PROJECT_NAME} + PRIVATE + src/anyoption.cpp + src/anyoption.h +) + +#------------------------------------------------------------------------------------------- +add_executable(${PROJECT_NAME}-test) + +target_sources(${PROJECT_NAME}-test + PRIVATE + tests/test.cpp +) + +target_link_libraries(${PROJECT_NAME}-test + PUBLIC + anyoption::anyoption + catch2::catch2 +) + +if( STATIC_ANALYZERS ) + set_target_properties(${PROJECT_NAME}-test PROPERTIES CXX_CPPCHECK "") +endif(STATIC_ANALYZERS) + +add_test(${PROJECT_NAME}-test ${PROJECT_NAME}-test) + +#------------------------------------------------------------------------------------------- +add_executable(${PROJECT_NAME}-demo) + +target_sources(${PROJECT_NAME}-demo + PRIVATE + demo/demo.cpp +) + +target_link_libraries(${PROJECT_NAME}-demo + PUBLIC + anyoption::anyoption +) diff --git a/appveyor.yml b/appveyor.yml index 7f4d6f8..7aab143 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -4,5 +4,5 @@ platform: x64 build_script: - curl -fsSL -o catch.hpp https://github.com/catchorg/Catch2/releases/download/v2.2.3/catch.hpp - call "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvars64.bat" - - cmd: cl /EHsc /W4 test.cpp anyoption.cpp + - cmd: cl /EHsc /W4 tests\test.cpp anyoption.cpp - cmd: test.exe diff --git a/cmake/catch2.cmake b/cmake/catch2.cmake new file mode 100644 index 0000000..7f617fb --- /dev/null +++ b/cmake/catch2.cmake @@ -0,0 +1,25 @@ +cmake_minimum_required(VERSION 3.13) + +include(ExternalProject) + +set(repo_catch2 "https://github.com/catchorg/Catch2.git") +message(STATUS "Repository for catch2: ${repo_catch2}") + +ExternalProject_add(extern-catch2 + PREFIX ${CMAKE_BINARY_DIR}/catch2 + GIT_REPOSITORY ${repo_catch2} + TIMEOUT 10 + CONFIGURE_COMMAND "" + BUILD_COMMAND "" + INSTALL_COMMAND "" + LOG_DOWNLOAD ON + BUILD_BYPRODUCTS ${CMAKE_CURRENT_BINARY_DIR}/catch2/lib/${CMAKE_STATIC_LIBRARY_PREFIX}catch2${CMAKE_STATIC_LIBRARY_SUFFIX} +) + +ExternalProject_Get_Property(extern-catch2 source_dir) +add_library(catch INTERFACE) +add_library(catch2::catch2 ALIAS catch) + +target_include_directories(catch INTERFACE ${source_dir}/single_include) + +add_dependencies(catch extern-catch2) diff --git a/demo.cpp b/demo/demo.cpp similarity index 100% rename from demo.cpp rename to demo/demo.cpp diff --git a/anyoption.cpp b/src/anyoption.cpp similarity index 100% rename from anyoption.cpp rename to src/anyoption.cpp diff --git a/anyoption.h b/src/anyoption.h similarity index 100% rename from anyoption.h rename to src/anyoption.h diff --git a/test.cpp b/tests/test.cpp similarity index 99% rename from test.cpp rename to tests/test.cpp index becfa60..90f332e 100644 --- a/test.cpp +++ b/tests/test.cpp @@ -7,7 +7,7 @@ * */ #include "anyoption.h" -#include "catch.hpp" +#include "catch2/catch.hpp" #include #include diff --git a/test.sh b/tests/test.sh similarity index 86% rename from test.sh rename to tests/test.sh index a8b6a73..49b3a2e 100755 --- a/test.sh +++ b/tests/test.sh @@ -8,7 +8,7 @@ cppcheck --error-exitcode=1 --enable=warning,performance,information,style *.cpp *.h wget -q https://github.com/catchorg/Catch2/releases/download/v2.2.3/catch.hpp -g++ -g -o0 -coverage -std=c++11 -Wall -Wextra -Werror test.cpp anyoption.cpp -o test +g++ -g -o0 -coverage -std=c++11 -Wall -Wextra -Werror tests/test.cpp src/anyoption.cpp -o test valgrind --error-exitcode=1 --leak-check=full --show-leak-kinds=all ./test lcov --directory . --capture --output-file coverage.info lcov --remove coverage.info 'catch.hpp' '/usr/*' --output-file coverage.info