mirror of
https://github.com/TheLartians/ModernCppStarter.git
synced 2025-09-01 06:30:52 +02:00
support dynamic library too
generate export header needed and install it
This commit is contained in:
parent
7f36b7cb86
commit
0bad72b51e
4 changed files with 37 additions and 21 deletions
|
@ -20,7 +20,7 @@ endif()
|
||||||
|
|
||||||
# ---- Project settings ----
|
# ---- 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)
|
if(NOT DEFINED CMAKE_CXX_STANDARD)
|
||||||
set(CMAKE_CXX_STANDARD 20)
|
set(CMAKE_CXX_STANDARD 20)
|
||||||
|
@ -28,6 +28,10 @@ if(NOT DEFINED CMAKE_CXX_STANDARD)
|
||||||
set(CMAKE_CXX_EXTENSIONS NO)
|
set(CMAKE_CXX_EXTENSIONS NO)
|
||||||
endif()
|
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 ----
|
# ---- Add dependencies via CPM ----
|
||||||
# see https://github.com/cpm-cmake/CPM.cmake for more info
|
# 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>)
|
target_link_libraries(Greeter PRIVATE $<BUILD_INTERFACE:fmt::fmt-header-only>)
|
||||||
# OR: target_link_libraries(Greeter PUBLIC fmt::fmt)
|
# OR: target_link_libraries(Greeter PUBLIC fmt::fmt)
|
||||||
|
|
||||||
|
set(INCLUDE_INSTALL_DIR include/${PROJECT_NAME}-${PROJECT_VERSION})
|
||||||
target_include_directories(
|
target_include_directories(
|
||||||
Greeter PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
|
Greeter BEFORE PUBLIC $<BUILD_INTERFACE: ${PROJECT_SOURCE_DIR}/include>
|
||||||
$<INSTALL_INTERFACE:include/${PROJECT_NAME}-${PROJECT_VERSION}>
|
$<INSTALL_INTERFACE:${INCLUDE_INSTALL_DIR}>
|
||||||
)
|
)
|
||||||
|
|
||||||
# ---- Create an installable target ----
|
# ---- 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
|
# the location where the project's version header will be placed should match the project's regular
|
||||||
# header paths
|
# header paths
|
||||||
string(TOLOWER ${PROJECT_NAME}/version.h VERSION_HEADER_LOCATION)
|
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(
|
packageProject(
|
||||||
NAME ${PROJECT_NAME}
|
NAME ${PROJECT_NAME}
|
||||||
|
@ -96,8 +109,8 @@ packageProject(
|
||||||
NAMESPACE ${PROJECT_NAME}
|
NAMESPACE ${PROJECT_NAME}
|
||||||
BINARY_DIR ${PROJECT_BINARY_DIR}
|
BINARY_DIR ${PROJECT_BINARY_DIR}
|
||||||
INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include
|
INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include
|
||||||
INCLUDE_DESTINATION include/${PROJECT_NAME}-${PROJECT_VERSION}
|
INCLUDE_DESTINATION ${INCLUDE_INSTALL_DIR}
|
||||||
VERSION_HEADER "${VERSION_HEADER_LOCATION}"
|
VERSION_HEADER "${VERSION_HEADER_LOCATION}"
|
||||||
COMPATIBILITY SameMajorVersion
|
COMPATIBILITY SameMajorVersion
|
||||||
# NOTE: not needed DEPENDENCIES "fmt 7.1.3"
|
# Note: not needed DEPENDENCIES "fmt 7.1.3"
|
||||||
)
|
)
|
||||||
|
|
29
GNUmakefile
29
GNUmakefile
|
@ -1,25 +1,24 @@
|
||||||
#
|
#
|
||||||
# CURDIR=$(/bin/pwd)
|
# CURDIR=$(/bin/pwd)
|
||||||
# On UNIX one can use the DESTDIR mechanism in order to relocate the whole installation.
|
ROOT?=${CURDIR}/stagedir
|
||||||
DESTDIR?=${CURDIR}/stagedir
|
|
||||||
export DESTDIR
|
|
||||||
|
|
||||||
# GENERATOR?="Unix Makefiles"
|
#XXX GENERATOR?="Unix Makefiles"
|
||||||
GENERATOR?=Ninja
|
GENERATOR?=Ninja
|
||||||
|
|
||||||
export CPM_SOURCE_CACHE=${HOME}/.cache/CPM
|
#XXX export CXX=clang++
|
||||||
export CPM_USE_LOCAL_PACKAGES=1
|
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:
|
all:
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
find . -type d -name build | xargs rm -rf
|
find . -type d -name build | xargs rm -rf
|
||||||
|
|
||||||
distclean: clean
|
distclean: clean
|
||||||
rm -rf build ${DESTDIR}
|
rm -rf build ${ROOT}
|
||||||
|
|
||||||
# update CPM.cmake
|
# update CPM.cmake
|
||||||
update:
|
update:
|
||||||
|
@ -31,25 +30,27 @@ lock: standalone all
|
||||||
cmake --build build/install --target cpm-update-package-lock
|
cmake --build build/install --target cpm-update-package-lock
|
||||||
cmake --build build/standalone --target cpm-update-package-lock
|
cmake --build build/standalone --target cpm-update-package-lock
|
||||||
|
|
||||||
# install the library
|
# install the library to stagedir
|
||||||
install:
|
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
|
cmake --build build/install --target install
|
||||||
|
|
||||||
# test the library
|
# test the library
|
||||||
test: install
|
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
|
||||||
cmake --build build/test --target test
|
cmake --build build/test --target test
|
||||||
|
|
||||||
# all together
|
# all together
|
||||||
all: test
|
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
|
||||||
cmake --build build/all --target test
|
cmake --build build/all --target test
|
||||||
cmake --build build/all --target GenerateDocs
|
|
||||||
cmake --build build/all --target check-format
|
cmake --build build/all --target check-format
|
||||||
|
|
||||||
|
doc: all
|
||||||
|
cmake --build build/all --target GenerateDocs
|
||||||
|
|
||||||
format: distclean
|
format: distclean
|
||||||
find . -name CMakeLists.txt | xargs cmake-format -i
|
find . -name CMakeLists.txt | xargs cmake-format -i
|
||||||
find . -name '*.cmake' | xargs cmake-format -i
|
find . -name '*.cmake' | xargs cmake-format -i
|
||||||
|
@ -57,7 +58,7 @@ format: distclean
|
||||||
find . -name '*.h' | xargs clang-format -i
|
find . -name '*.h' | xargs clang-format -i
|
||||||
|
|
||||||
standalone:
|
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
|
cmake --build build/standalone --target all
|
||||||
|
|
||||||
# check the library
|
# check the library
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <greeter/greeter_export.h>
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
namespace greeter {
|
namespace greeter {
|
||||||
|
@ -10,7 +12,7 @@ namespace greeter {
|
||||||
/**
|
/**
|
||||||
* @brief A class for saying hello in multiple languages
|
* @brief A class for saying hello in multiple languages
|
||||||
*/
|
*/
|
||||||
class Greeter {
|
class GREETER_EXPORT Greeter {
|
||||||
std::string name;
|
std::string name;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
|
@ -16,7 +16,7 @@ TEST_CASE("Greeter") {
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("Greeter version") {
|
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
|
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
|
CHECK(std::string(GREETER_VERSION).starts_with("1.0")); // SameMajorVersion
|
||||||
#else
|
#else
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue