Skip to content

Commit

Permalink
Show the real problems with boost
Browse files Browse the repository at this point in the history
If boost is installed, it works.
If not, it will have a night mare!
  • Loading branch information
ClausKlein committed Jan 1, 2024
1 parent bc445c5 commit 456c593
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 49 deletions.
100 changes: 60 additions & 40 deletions examples/boost/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.16...3.28)
cmake_minimum_required(VERSION 3.21...3.28)

project(
CPMExampleBoost
Expand All @@ -8,6 +8,7 @@ project(

set(stageDir ${CMAKE_CURRENT_BINARY_DIR}/stage)
include(GNUInstallDirs)

if(NOT CMAKE_RUNTIME_OUTPUT_DIRECTORY)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${stageDir}/${CMAKE_INSTALL_BINDIR})
endif()
Expand All @@ -18,7 +19,7 @@ if(NOT CMAKE_ARCHIVE_OUTPUT_DIRECTORY)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${stageDir}/${CMAKE_INSTALL_LIBDIR})
endif()

set(CMAKE_INSTALL_MESSAGE LAZY)
option(CPM_USE_LOCAL_PACKAGES "yes/no" YES)
option(BUILD_SHARED_LIBS "yes/no" YES)
if(BUILD_SHARED_LIBS)
if(APPLE)
Expand All @@ -31,11 +32,10 @@ if(BUILD_SHARED_LIBS)
${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}
)
set(CMAKE_INSTALL_RPATH ${base} ${base}/${relDir})

# FIXME: install(DIRECTORY "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/" DESTINATION
# ${CMAKE_INSTALL_LIBDIR})
endif()

set(CMAKE_INSTALL_MESSAGE LAZY)

# ---- Create binary ----

add_executable(CPMExampleBoost main.cpp)
Expand All @@ -45,56 +45,76 @@ target_compile_features(CPMExampleBoost PRIVATE cxx_std_17)

include(../../cmake/CPM.cmake)

# XXX list(APPEND BOOST_INCLUDE_LIBRARIES headers asio date_time filesystem thread)
CPMAddPackage(
NAME Boost
VERSION 1.74
URL https://github.com/boostorg/boost/releases/download/boost-1.84.0/boost-1.84.0.tar.xz
URL_MD5 893b5203b862eb9bbd08553e24ff146a
FIND_PACKAGE_ARGUMENTS "COMPONENTS thread\;date_time\;filesystem"
EXCLUDE_FROM_ALL ON
SYSTEM ON
)

target_link_libraries(CPMExampleBoost PUBLIC Boost::headers Boost::date_time Boost::filesystem)

if(PROJECT_IS_TOP_LEVEL)
enable_testing()
add_test(NAME CPMExampleBoost COMMAND CPMExampleBoost)
endif()

# ---- Create a library ----

add_library(scoped_lock scoped_lock.cpp scoped_lock.hpp)
target_compile_features(scoped_lock PUBLIC cxx_std_17)

install(FILES scoped_lock.hpp TYPE INCLUDE)

# ---- Install the library ----

target_link_libraries(scoped_lock PUBLIC Boost::thread)
install(
TARGETS scoped_lock
boost_thread # XXX ...
boost_atomic
boost_chrono
boost_container
RUNTIME_DEPENDENCY_SET
libDeps
)
install(RUNTIME_DEPENDENCY_SET libDeps POST_INCLUDE_REGEXES
[[${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/libboost_.*]]
target_include_directories(
scoped_lock PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}>
)

# ---- Install the binary and its runtime dependency set ----
if(NOT CMAKE_SKIP_INSTALL_RULES)

target_link_libraries(CPMExampleBoost PUBLIC Boost::asio Boost::date_time Boost::filesystem)
# ---- Install the binary and its runtime dependency set ----
install(
TARGETS CPMExampleBoost
# boost_headers boost_date_time boost_filesystem # XXX ... boost_context boost_coroutine
RUNTIME_DEPENDENCY_SET appDeps EXPORT scoped_lockTargets
)
if(BUILD_SHARED_LIBS)
install(RUNTIME_DEPENDENCY_SET appDeps POST_INCLUDE_REGEXES
[[${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/libboost_.*]] EXPORT scoped_lockTargets
)
endif()

install(
TARGETS CPMExampleBoost
boost_asio
boost_date_time
boost_filesystem # XXX ...
boost_context
boost_coroutine
RUNTIME_DEPENDENCY_SET
appDeps
)
install(RUNTIME_DEPENDENCY_SET appDeps POST_INCLUDE_REGEXES
[[${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/libboost_.*]]
)
# ---- Install the library ----
install(FILES scoped_lock.hpp DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME})
if(NOT CPM_USE_LOCAL_PACKAGES)
install(TARGETS boost_headers EXPORT scoped_lockTargets)
elseif(BOOST_SRC_DIR)
install(TARGETS boost_thread EXPORT scoped_lockTargets)
endif()
install(TARGETS scoped_lock RUNTIME_DEPENDENCY_SET libDeps)

# PackageProject.cmake will be used to export our cmake config packages
CPMAddPackage("gh:TheLartians/PackageProject.cmake@1.11.0")

packageProject(
NAME scoped_lock
VERSION ${PROJECT_VERSION}
NAMESPACE ${PROJECT_NAME}
BINARY_DIR ${PROJECT_BINARY_DIR}
COMPATIBILITY AnyNewerVersion
DISABLE_VERSION_SUFFIX YES
DEPENDENCIES "Boost 1.74"
)

set(CPACK_PACKAGE_INSTALL_DIRECTORY /)
set(CPACK_GENERATOR TBZ2)
include(CPack)
if(BUILD_SHARED_LIBS)
install(RUNTIME_DEPENDENCY_SET libDeps POST_INCLUDE_REGEXES
[[${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/libboost_.*]] EXPORT scoped_lockTargets
)
endif()

set(CPACK_PACKAGE_INSTALL_DIRECTORY /)
set(CPACK_GENERATOR TBZ2)
include(CPack)
endif()
6 changes: 2 additions & 4 deletions examples/boost/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ namespace fs = boost::filesystem;
void printPathInfo() {
std::cout << "Current path is " << fs::current_path() << '\n';
fs::current_path(fs::temp_directory_path());
std::cout << "Current path is " << fs::current_path() << '\n';
std::cout << "The TMP path is " << fs::current_path() << '\n';
}

void print(const boost::system::error_code& /*e*/) {
printPathInfo();
}
void print(const boost::system::error_code& /*e*/) { printPathInfo(); }

int main() {
std::cout << "Hello, world! ...\n";
Expand Down
1 change: 0 additions & 1 deletion examples/boost/scoped_lock.cpp
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
#include "scoped_lock.hpp"

7 changes: 3 additions & 4 deletions examples/boost/scoped_lock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@

// Helper class to lock and unlock a singleton automatically.

template <class T> class scoped_lock : private boost::noncopyable
{
template <class T> class scoped_lock : private boost::noncopyable {
public:
explicit scoped_lock() { T::lock(); }
~scoped_lock() { T::unlock(); }
explicit scoped_lock() { T::lock(); }
~scoped_lock() { T::unlock(); }
};

#endif

0 comments on commit 456c593

Please sign in to comment.