1
0
Fork 0
mirror of https://github.com/TheLartians/ModernCppStarter.git synced 2025-08-30 21:51:12 +02:00

support dynamic library too

generate export header needed and install it
This commit is contained in:
ClausKlein 2021-02-14 23:44:26 +01:00
parent 7f36b7cb86
commit 0bad72b51e
4 changed files with 37 additions and 21 deletions

View file

@ -20,7 +20,7 @@ endif()
# ---- Project settings ----
option(BUILD_SHARED_LIBS "Create shared libraries if ON" OFF)
option(BUILD_SHARED_LIBS "Create shared libraries if ON" YES)
if(NOT DEFINED CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 20)
@ -28,6 +28,10 @@ if(NOT DEFINED CMAKE_CXX_STANDARD)
set(CMAKE_CXX_EXTENSIONS NO)
endif()
# Set default visibility to hidden for all targets
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
set(CMAKE_VISIBILITY_INLINES_HIDDEN YES)
# ---- Add dependencies via CPM ----
# see https://github.com/cpm-cmake/CPM.cmake for more info
@ -78,9 +82,10 @@ endif()
target_link_libraries(Greeter PRIVATE $<BUILD_INTERFACE:fmt::fmt-header-only>)
# OR: target_link_libraries(Greeter PUBLIC fmt::fmt)
set(INCLUDE_INSTALL_DIR include/${PROJECT_NAME}-${PROJECT_VERSION})
target_include_directories(
Greeter PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include/${PROJECT_NAME}-${PROJECT_VERSION}>
Greeter BEFORE PUBLIC $<BUILD_INTERFACE: ${PROJECT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${INCLUDE_INSTALL_DIR}>
)
# ---- Create an installable target ----
@ -89,6 +94,14 @@ target_include_directories(
# the location where the project's version header will be placed should match the project's regular
# header paths
string(TOLOWER ${PROJECT_NAME}/version.h VERSION_HEADER_LOCATION)
string(TOLOWER ${PROJECT_NAME}/greeter_export.h EXPORT_HEADER_LOCATION)
# TODO: should be done in packageProject()
include(GenerateExportHeader)
generate_export_header(
${PROJECT_NAME} EXPORT_FILE_NAME PackageProjectInclude/${EXPORT_HEADER_LOCATION}
)
# Note: the export header will be installed while installing the version header! CK
packageProject(
NAME ${PROJECT_NAME}
@ -96,8 +109,8 @@ packageProject(
NAMESPACE ${PROJECT_NAME}
BINARY_DIR ${PROJECT_BINARY_DIR}
INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include
INCLUDE_DESTINATION include/${PROJECT_NAME}-${PROJECT_VERSION}
INCLUDE_DESTINATION ${INCLUDE_INSTALL_DIR}
VERSION_HEADER "${VERSION_HEADER_LOCATION}"
COMPATIBILITY SameMajorVersion
# NOTE: not needed DEPENDENCIES "fmt 7.1.3"
# Note: not needed DEPENDENCIES "fmt 7.1.3"
)

View file

@ -1,25 +1,24 @@
#
# CURDIR=$(/bin/pwd)
# On UNIX one can use the DESTDIR mechanism in order to relocate the whole installation.
DESTDIR?=${CURDIR}/stagedir
export DESTDIR
ROOT?=${CURDIR}/stagedir
# GENERATOR?="Unix Makefiles"
#XXX GENERATOR?="Unix Makefiles"
GENERATOR?=Ninja
export CPM_SOURCE_CACHE=${HOME}/.cache/CPM
#XXX export CXX=clang++
export CPM_USE_LOCAL_PACKAGES=1
export CPM_SOURCE_CACHE=${HOME}/.cache/CPM
.PHONY: update format all test standalone check clean distclean lock
.PHONY: update format all test standalone doc check clean distclean lock
# the default target does just all
# the default target does just all, but neither standalone nor doc
all:
clean:
find . -type d -name build | xargs rm -rf
distclean: clean
rm -rf build ${DESTDIR}
rm -rf build ${ROOT}
# update CPM.cmake
update:
@ -31,25 +30,27 @@ lock: standalone all
cmake --build build/install --target cpm-update-package-lock
cmake --build build/standalone --target cpm-update-package-lock
# install the library
# install the library to stagedir
install:
cmake -S . -B build/install -G "${GENERATOR}" -DCMAKE_PREFIX_PATH=${DESTDIR} # --trace-expand
cmake -S . -B build/install -G "${GENERATOR}" -DCMAKE_PREFIX_PATH=${ROOT} -DCMAKE_INSTALL_PREFIX=${ROOT} # --trace-expand
cmake --build build/install --target install
# test the library
test: install
cmake -S test -B build/test -G "${GENERATOR}" -DCMAKE_PREFIX_PATH=${DESTDIR} -DTEST_INSTALLED_VERSION=1
cmake -S test -B build/test -G "${GENERATOR}" -DCMAKE_PREFIX_PATH=${ROOT} -DTEST_INSTALLED_VERSION=1
cmake --build build/test
cmake --build build/test --target test
# all together
all: test
cmake -S all -B build/all -G "${GENERATOR}" -DCMAKE_PREFIX_PATH=${DESTDIR} -DTEST_INSTALLED_VERSION=1 -DENABLE_TEST_COVERAGE=1
cmake -S all -B build/all -G "${GENERATOR}" -DCMAKE_PREFIX_PATH=${ROOT} -DTEST_INSTALLED_VERSION=1 -DENABLE_TEST_COVERAGE=1
cmake --build build/all
cmake --build build/all --target test
cmake --build build/all --target GenerateDocs
cmake --build build/all --target check-format
doc: all
cmake --build build/all --target GenerateDocs
format: distclean
find . -name CMakeLists.txt | xargs cmake-format -i
find . -name '*.cmake' | xargs cmake-format -i
@ -57,7 +58,7 @@ format: distclean
find . -name '*.h' | xargs clang-format -i
standalone:
cmake -S standalone -B build/standalone -G "${GENERATOR}" -DCMAKE_PREFIX_PATH=${DESTDIR} -DCMAKE_EXPORT_COMPILE_COMMANDS=1
cmake -S standalone -B build/standalone -G "${GENERATOR}" -DCMAKE_PREFIX_PATH=${ROOT} -DCMAKE_EXPORT_COMPILE_COMMANDS=1
cmake --build build/standalone --target all
# check the library

View file

@ -1,5 +1,7 @@
#pragma once
#include <greeter/greeter_export.h>
#include <string>
namespace greeter {
@ -10,7 +12,7 @@ namespace greeter {
/**
* @brief A class for saying hello in multiple languages
*/
class Greeter {
class GREETER_EXPORT Greeter {
std::string name;
public:

View file

@ -16,7 +16,7 @@ TEST_CASE("Greeter") {
}
TEST_CASE("Greeter version") {
#if ((__cplusplus >= 201907L) || __cpp_lib_starts_ends_with)
#if (__APPLE__ || __cpp_lib_starts_ends_with)
static_assert(std::string_view(GREETER_VERSION).starts_with("1.0")); // TBD C++20 only
CHECK(std::string(GREETER_VERSION).starts_with("1.0")); // SameMajorVersion
#else