Skip to content

Commit

Permalink
🎨 Revamp CMake to be correct and easy to understand (#69)
Browse files Browse the repository at this point in the history
🎨 Cleanup include path for catch2

✨ Add basic .deb generation support
  WiX and RPM support is still needed (if even desired?)

⬆️ Upgrade minimum CMake version to 3.14
⬆️ Upgrade Catch to Catch v2.9.2
📌 Pin Catch to v2.9.2
  • Loading branch information
bruxisma authored and TartanLlama committed Oct 25, 2019
1 parent fd96e45 commit 0ca73ee
Show file tree
Hide file tree
Showing 17 changed files with 150 additions and 12,082 deletions.
9 changes: 3 additions & 6 deletions .appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ os:
- Visual Studio 2017

build_script:
- git submodule update --init --recursive
- mkdir build
- cd build
- cmake ..
- cmake --build .
- C:\projects\expected\build\Debug\tests.exe
- cmake -Bbuild -S.
- cmake --build build
- cmake --build build --target RUN_TESTS
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
\#*
.\#*
/build/
27 changes: 19 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
language: cpp

dist: xenial
sudo: false

matrix:
include:
Expand Down Expand Up @@ -87,8 +86,9 @@ matrix:
addons:
apt:
sources:
- llvm-toolchain-precise-3.8
- ubuntu-toolchain-r-test
- sourceline: "deb http://apt.llvm.org/xenial/ llvm-toolchain-xenial-3.8 main"
key_url: "http://apt.llvm.org/llvm-snapshot.gpg.key"
- ubuntu-toolchain-r-test
packages:
- clang++-3.8
- libc++-dev
Expand Down Expand Up @@ -201,8 +201,9 @@ matrix:
addons:
apt:
sources:
- llvm-toolchain-precise-3.8
- ubuntu-toolchain-r-test
- sourceline: "deb http://apt.llvm.org/xenial/ llvm-toolchain-xenial-3.8 main"
key_url: "http://apt.llvm.org/llvm-snapshot.gpg.key"
- ubuntu-toolchain-r-test
packages:
- clang++-3.8
- libc++-dev
Expand Down Expand Up @@ -249,9 +250,19 @@ matrix:
- libc++-dev
env: COMPILER=clang++-6.0 CXXSTD=14

before_install:
- sudo apt update
- sudo apt install -y apt-transport-https ca-certificates gnupg software-properties-common
- curl -L https://apt.kitware.com/keys/kitware-archive-latest.asc | sudo apt-key add -
- sudo apt-add-repository 'deb https://apt.kitware.com/ubuntu/ xenial main'
- sudo apt update

install:
- if [ "$CXX" = "clang++" ]; then export CXX="$COMPILER -stdlib=libc++"; fi
- if [ "$CXX" = "g++" ]; then export CXX="$COMPILER"; fi
- if [ "$CXX" = "clang++" ]; then export CXX="$COMPILER -stdlib=libc++"; fi
- if [ "$CXX" = "g++" ]; then export CXX="$COMPILER"; fi
- sudo apt install -y cmake

script: mkdir build && cd build && cmake -DCXXSTD=$CXXSTD .. && make && ./tests
script:
- /usr/bin/cmake -B build -S . "-DCMAKE_CXX_STANDARD=$CXXSTD"
- /usr/bin/cmake --build build
- /usr/bin/cmake --build build --target test
153 changes: 112 additions & 41 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,46 +1,117 @@
cmake_minimum_required(VERSION 3.11)
cmake_minimum_required(VERSION 3.14)
project(tl-expected
HOMEPAGE_URL https://tl.tartanllama.xyz
DESCRIPTION "C++11/14/17 std::expected with functional-style extensions"
VERSION 1.0.0
LANGUAGES CXX)

project(tl-expected VERSION 1.0.0 LANGUAGES CXX)
include(CMakePackageConfigHelpers)
include(CMakeDependentOption)
include(GNUInstallDirs)
include(FetchContent)
include(CTest)

if (NOT DEFINED CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 14)
endif()

option(EXPECTED_ENABLE_TESTS "Enable tests." ON)
option(EXPECTED_BUILD_PACKAGE "Build package files as well" ON)

include(FetchContent)
FetchContent_Declare(
tl_cmake
GIT_REPOSITORY https://github.com/TartanLlama/tl-cmake.git
)
FetchContent_GetProperties(tl_cmake)
if(NOT tl_cmake_POPULATED)
FetchContent_Populate(tl_cmake)
set(CMAKE_MODULE_PATH ${tl_cmake_SOURCE_DIR} ${CMAKE_MODULE_PATH})
cmake_dependent_option(EXPECTED_BUILD_TESTS
"Enable tl::expected tests" ON
"BUILD_TESTING" OFF)

cmake_dependent_option(EXPECTED_BUILD_PACKAGE_DEB
"Create a DEB" ON
"EXPECTED_BUILD_PACKAGE" OFF)

add_library(expected INTERFACE)
target_include_directories(expected
INTERFACE
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>)

if (NOT CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR)
add_executable(tl::expected ALIAS expected)
endif()

# Installation help
configure_package_config_file(
"${PROJECT_SOURCE_DIR}/cmake/${PROJECT_NAME}-config.cmake.in"
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}-config.cmake"
INSTALL_DESTINATION "share/cmake/${PROJECT_NAME}")

write_basic_package_version_file(
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}-config-version.cmake"
COMPATIBILITY SameMajorVersion
ARCH_INDEPENDENT)

install(TARGETS expected
EXPORT ${PROJECT_NAME}-targets
INCLUDES DESTINATION "${CMAKE_INSTALL_DATADIR}")

install(EXPORT ${PROJECT_NAME}-targets
DESTINATION "${CMAKE_INSTALL_DATADIR}/cmake/${PROJECT_NAME}"
NAMESPACE tl::
FILE "${PROJECT_NAME}-targets.cmake")

install(FILES
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}-config-version.cmake"
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}-config.cmake"
DESTINATION "${CMAKE_INSTALL_DATADIR}/cmake/${PROJECT_NAME}")

install(DIRECTORY "include/" TYPE INCLUDE)

if(EXPECTED_BUILD_TESTS)
set(CMAKE_POLICY_DEFAULT_CMP0077 NEW)
set(CATCH_INSTALL_HELPERS OFF)
set(CATCH_BUILD_TESTING OFF)
set(CATCH_INSTALL_DOCS OFF)
FetchContent_Declare(Catch2 URL
https://github.com/catchorg/Catch2/archive/v2.9.2.zip)
FetchContent_MakeAvailable(Catch2)

file(GLOB test-sources CONFIGURE_DEPENDS tests/*.cpp)
list(FILTER test-sources EXCLUDE REGEX "tests/test.cpp")
add_executable(${PROJECT_NAME}-tests "${test-sources}")
target_compile_options(${PROJECT_NAME}-tests PRIVATE
$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wall -Wextra>)

target_link_libraries(${PROJECT_NAME}-tests
PRIVATE
Catch2::Catch2
expected)
add_test(NAME tl::expected::tests COMMAND ${PROJECT_NAME}-tests)
endif()

if (NOT EXPECTED_BUILD_PACKAGE)
return()
endif()

list(APPEND source-generators TBZ2 TGZ TXZ ZIP)

if (CMAKE_HOST_WIN32)
list(APPEND binary-generators "WIX")
endif()

if (EXPECTED_BUILD_PACKAGE_DEB)
list(APPEND binary-generators "DEB")
endif()
include(add-tl)

tl_add_library(expected SOURCES
include/tl/expected.hpp)

# Prepare "Catch" library for other executables
set(CATCH_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/test)
add_library(Catch INTERFACE)
target_include_directories(Catch INTERFACE ${CATCH_INCLUDE_DIR})

if(EXPECTED_ENABLE_TESTS)
# Make test executable
set(TEST_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/tests/main.cpp
${CMAKE_CURRENT_SOURCE_DIR}/tests/extensions.cpp
${CMAKE_CURRENT_SOURCE_DIR}/tests/assignment.cpp
${CMAKE_CURRENT_SOURCE_DIR}/tests/emplace.cpp
${CMAKE_CURRENT_SOURCE_DIR}/tests/issues.cpp
${CMAKE_CURRENT_SOURCE_DIR}/tests/bases.cpp
${CMAKE_CURRENT_SOURCE_DIR}/tests/observers.cpp
${CMAKE_CURRENT_SOURCE_DIR}/tests/swap.cpp
${CMAKE_CURRENT_SOURCE_DIR}/tests/constructors.cpp)

add_executable(tests ${TEST_SOURCES})

target_link_libraries(tests Catch expected)

set(CXXSTD 14 CACHE STRING "C++ standard to use, default C++14")
set_property(TARGET tests PROPERTY CXX_STANDARD ${CXXSTD})
set_property(TARGET tests PROPERTY CXX_FLAGS "-Wall -Wextra")

if (EXPECTED_BUILD_RPM)
list(APPEND binary-generators "RPM")
endif()


set(CPACK_SOURCE_GENERATOR ${source-generators})
set(CPACK_GENERATOR ${binary-generators})

set(CPACK_PACKAGE_FILE_NAME "${PROJECT_NAME}-${PROJECT_VERSION}")
set(CPACK_SOURCE_PACKAGE_FILE_NAME "${CPACK_PACKAGE_FILE_NAME}")

set(CPACK_DEBIAN_PACKAGE_MAINTAINER "Sy Brand")

list(APPEND CPACK_SOURCE_IGNORE_FILES /.git/ /build/ .gitignore .DS_Store)

include(CPack)

2 changes: 1 addition & 1 deletion tests/assignment.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "catch.hpp"
#include <catch2/catch.hpp>
#include <tl/expected.hpp>

TEST_CASE("Simple assignment", "[assignment.simple]") {
Expand Down
2 changes: 1 addition & 1 deletion tests/bases.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "catch.hpp"
#include <catch2/catch.hpp>
#include <tl/expected.hpp>

#include <string>
Expand Down
Loading

0 comments on commit 0ca73ee

Please sign in to comment.