CMake - how to build Boost after downloading it with FetchContent? - c++

My goal is to download the Boost repository if it isn't found and then build it the default way, i.e. using boostrap and b2 tools.
I know, I can download it like this:
include(FetchContent)
FetchContent_Declare(
Boost
PREFIX external_dependencies/boost
GIT_REPOSITORY https://github.com/boostorg/boost.git
GIT_SUBMODULES libs/system libs/serialization libs/random
libs/function libs/config libs/headers libs/assert libs/core libs/integer
libs/type_traits libs/mpl libs/throw_exception libs/preprocessor libs/utility
libs/static_assert libs/smart_ptr libs/predef libs/move libs/io libs/iterator
libs/detail libs/spirit libs/optional libs/type_index libs/container_hash
libs/array libs/bind
tools/build tools/boost_install
)
FetchContent_GetProperties(Boost)
FetchContent_Populate(Boost)
But how can I build it now correctly? I'd like to run following commands:
./bootstrap.sh
./b2 headers
./b2 -q cxxflags="-fPIC" --layout=system variant=${BUILD_TYPE} link=${LINK_TYPE} address-model=64 --with-system --with-serialization --with-random
I'm thinking about add_custom_target() and add_custom_command() functions, but I'm not sure, if it's the recommended way to do this.

add_custom_target is probably better because it is declaring a target that is convenient to depend on,
using add_dependencies.
You may need one target per command, and that becomes quickly annoying. So instead I would try (I did not) writing a script performing the build, let's call it build-boost.sh:
#!/bin/bash
# This is meant to be run from the source directory of Boost.
./bootstrap.sh
./b2 headers
./b2 -q cxxflags="-fPIC" --layout=system variant=$2 link=$3 address-model=64 --with-system --with-serialization --with-random
./b2 install --prefix=$4
In your CMake code, you would be calling it this way:
add_custom_target(
build-boost
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/build-boost.sh ${BUILD_TYPE} ${LINK_TYPE} ${CMAKE_CURRENT_BINARY_DIR}/external_dependencies/boost-installation
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/external_dependencies/boost
)
After that, you are not done yet.
You should still export all the variables FindBoost usually exports, and create all expected directories in advance (under ${CMAKE_CURRENT_BINARY_DIR}/external_dependencies/boost-installation), since at configuration time the binaries and headers are not present yet.
If you complete this work, please let the community know, since this can be helpful.

Related

Use CMake to build dependency (oneTBB as git submodule) as dynamic library?

I have a project that depends on Intel's oneTBB. My project is structured as follows:
external/
| - CMakeLists.txt
| - oneTBB/ (this is a git submodule)
| - ...
include/
lib/
include/
CMakeLists.txt
I currently get things to compile by manually building oneTBB and installing it inside a prefix directory located at external/oneTBB/prefix by running the following (bash) commands:
cd oneTBB
mkdir -p prefix
mkdir -p build
cd build
cmake -DCMAKE_INSTALL_PREFIX=../prefix -DTBB_TEST=OFF ..
cmake --build .
cmake --install .
I then simply include and link using this prefix. (I got this from following the oneTBB READMEs)
While this works without issue, I'm currently trying to clean up my CMake such that its easier to build on Windows as well. Ideally, I'm looking to get to a point where I can simply run:
mkdir build
cd build
cmake ..
cmake --build .
and my project will build itself and all dependencies.
I got this working with other dependencies such as glfw and eigen by simply adding (to the CMakeLists.txt in external/:
add_subdirectory(glfw)
add_subdirectory(eigen)
But adding add_subdirectory(oneTBB) throws a LOT of warnings, starting with:
CMake Warning at external/oneTBB/CMakeLists.txt:116 (message):
You are building oneTBB as a static library. This is highly discouraged
and such configuration is not supported. Consider building a dynamic
library to avoid unforeseen issues.
-- TBBBind build targets are disabled due to unsupported environment
-- Configuring done
CMake Warning (dev) at external/oneTBB/src/tbb/CMakeLists.txt:15 (add_library):
Policy CMP0069 is not set: INTERPROCEDURAL_OPTIMIZATION is enforced when
enabled. Run "cmake --help-policy CMP0069" for policy details. Use the
cmake_policy command to set the policy and suppress this warning.
INTERPROCEDURAL_OPTIMIZATION property will be ignored for target 'tbb'.
This warning is for project developers. Use -Wno-dev to suppress it.
I have no need to build oneTBB as a static library.
Am I doing something wrong in my attempt? Really all I need is for oneTBB to be built as a dynamic library and placed somewhere I can link it to (without installing it on the system overall)
Similar question:
I am also including the METIS library which depends on GKlib. Currently I'm doing this in a similar way to what I did for oneTBB where I manually build each using the following script:
# Setup the GKlib library:
cd GKlib
mkdir -p prefix
mkdir -p build
cd build
cmake -DCMAKE_INSTALL_PREFIX=../prefix ..
cmake --build . -j
cmake --install .
cd ../../
# Setup the METIS library:
cd METIS
mkdir -p prefix
make config prefix=../prefix gklib_path=../GKlib/prefix #(GKLib path is done from root, install path done relative to build)
make install -j
cd ../
When I try to add them using:
add_subdirectory(GKlib)
add_subdirectory(METIS)
it throws errors that METIS cannot find GKlib:
CMake Error at external/METIS/CMakeLists.txt:50 (add_subdirectory):
add_subdirectory given source "build/xinclude" which is not an existing
directory.
While I recognize this is a separate issue, I figured to include it here as it is related to my issues with add_subdirectory()
Many projects expect that you invoke CMake on them separately instead of adding them into an existing project with add_subdirectory. While there might be a way to make add_subdirectory work with oneTBB, there is an easier way.
CMake has a function that allows you to download, build, and install external projects at build time: ExternalProject_Add.
Here's an example for spdlog, taken straight from one of my own projects:
# project_root/thirdparty/spdlog/CMakeLists.txt
string(TOUPPER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE_UPPER)
ExternalProject_Add(spdlog-project
GIT_REPOSITORY https://github.com/gabime/spdlog
GIT_TAG edc51df1bdad8667b628999394a1e7c4dc6f3658
GIT_SUBMODULES_RECURSE ON
GIT_REMOTE_UPDATE_STRATEGY CHECKOUT
INSTALL_DIR "${CMAKE_CURRENT_BINARY_DIR}/install"
LIST_SEPARATOR |
CMAKE_CACHE_ARGS
"-DCMAKE_CXX_FLAGS_${CMAKE_BUILD_TYPE_UPPER}:STRING=${CMAKE_CXX_FLAGS_${CMAKE_BUILD_TYPE_UPPER}}"
"-DCMAKE_C_FLAGS_${CMAKE_BUILD_TYPE_UPPER}:STRING=${CMAKE_C_FLAGS_${CMAKE_BUILD_TYPE_UPPER}}"
"-DCMAKE_EXE_LINKER_FLAGS_${CMAKE_BUILD_TYPE_UPPER}:STRING=${CMAKE_EXE_LINKER_FLAGS_${CMAKE_BUILD_TYPE_UPPER}}"
"-DCMAKE_SHARED_LINKER_FLAGS_${CMAKE_BUILD_TYPE_UPPER}:STRING=${CMAKE_SHARED_LINKER_FLAGS_${CMAKE_BUILD_TYPE_UPPER}}"
"-DCMAKE_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE}"
"-DCMAKE_INSTALL_PREFIX:STRING=<INSTALL_DIR>"
"-DSPDLOG_BUILD_EXAMPLE:BOOL=OFF"
)
add_library(ext-spdlog INTERFACE)
add_dependencies(ext-spdlog spdlog-project)
ExternalProject_Get_property(spdlog-project INSTALL_DIR)
target_include_directories(ext-spdlog SYSTEM INTERFACE "${INSTALL_DIR}/include")
target_link_directories(ext-spdlog INTERFACE "${INSTALL_DIR}/lib")
target_link_libraries(ext-spdlog INTERFACE spdlog$<$<CONFIG:Debug>:d>)
After that, my project simply links against the created library target:
target_link_libraries(my_project ext-spdlog)
To adapt this for oneTBB, you have to switch out the repository URL and commit hash, and add your own CMake definitions (i.e. "-DTBB_TEST=OFF"). Depending on how oneTBB has its include and library directories set up, you may also have to change the target_include_directories and/or target_link_directories lines. I haven't looked this up yet, but I'm sure you can figure it out.
This works regardless of whether the external project is built as a static or shared library. You shouldn't use git submodules, though - instead, let CMake do the downloading. (It'll only download and build once; subsequent builds will not re-build the external project if it's already built and up-to-date.)
I have no need to build oneTBB as a static library. Am I doing something wrong in my attempt? Really all I need is for oneTBB to be built as a dynamic library and placed somewhere I can link it to (without installing it on the system overall)
All your diagnostic messages indicate that it's actually being configured to be built as a static library, and additional clues point to the probability that you've set BUILD_SHARED_LIBS to false in the scope where you add_subdirectory(oneTBB).
CMake Warning at external/oneTBB/CMakeLists.txt:116 (message):
You are building oneTBB as a static library. This is highly discouraged
and such configuration is not supported. Consider building a dynamic
library to avoid unforeseen issues.
If you look in oneTBB's CMakeLists.txt file, you'll the following:
if (NOT DEFINED BUILD_SHARED_LIBS)
set(BUILD_SHARED_LIBS ON)
endif()
if (NOT BUILD_SHARED_LIBS)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
message(WARNING "You are building oneTBB as a static library. This is highly discouraged and such configuration is not supported. Consider building a dynamic library to avoid unforeseen issues.")
endif()
And then right after that, you get
-- TBBBind build targets are disabled due to unsupported environment
The corresponding section of oneTBB's CMakeLists.txt file is:
if (TBB_FIND_PACKAGE OR TBB_DIR)
...
else()
if (APPLE OR NOT BUILD_SHARED_LIBS)
message(STATUS "TBBBind build targets are disabled due to unsupported environment")
else()
add_subdirectory(src/tbbbind)
endif()
...
Both of these clues indicate that in the variable scope at which you add_subdirectory(oneTBB), BUILD_SHARED_LIBS is set to a falsy value.
Set BUILD_SHARED_LIBS it to a truthy value (Ex. 1, TRUE, YES, ON, etc.) before doing add_subdirectory(oneTBB) and then restore the previous value afterward.
Ex.
set(BUILD_SHARED_LIBS_TEMP "${BUILD_SHARED_LIBS}")
set(BUILD_SHARED_LIBS YES)
add_subdirectory(oneTBB)
set(BUILD_SHARED_LIBS "${BUILD_SHARED_LIBS_TEMP}")
unset(BUILD_SHARED_LIBS_TEMP)

Installing Boost to custom directory

I have tried following the tutorial Boost gives on their documentation for installing boost and have looked at a few other questions here to try to determine why I can't install Boost at a custom location. Perhaps I'm misunderstanding, but the --prefix option is supposed to specify where the Boost headers and libs will go, then bootstrapper.sh creates a .jam file that is used when b2 or bjam is run.
When I issue the following command
./bootstrap.sh --prefix="$HOME/dev/emulator/src/boost" --includedir=headers --libdir=dist --with-libraries=date_time
I see that the correct lines are added to the generated project-config.jam file
option.set prefix : /home/liam/dev/emulator/src/boost ;
option.set exec-prefix : /home/liam/dev/emulator/src/boost ;
option.set libdir : dist ;
option.set includedir : headers ;
However, when I run ./b2 as instructed by the documentation, it installs the Boost libraries to the source folder; i.e.
The following directory should be added to compiler include paths:
/home/liam/Downloads/brave/boost_1_66_0
The following directory should be added to linker library paths:
/home/liam/Downloads/brave/boost_1_66_0/stage/lib
And running ./b2 install gives me no file output to the intended directory either.
You need to use your directory in both steps:
DST_DIR=${HOME}/dev/emulator/src/boost
./bootstrap.sh --prefix=${DST_DIR} --includedir=headers --libdir=dist --with-libraries=date_time
./b2 --prefix=${DST_DIR} install
Try
./bootstrap.sh --prefix=path/to/installation/prefix
./b2 install
So that means first bootstrap b2 and then use it to build and install boost.

Generator expressions cmake: copying works in debug but not release mode

I am trying to figure out how to copy some libs depending on the config in cmake.
I tried this:
add_custom_command(TARGET Myapp
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
$<$<CONFIG:Debug>:${_LIBS_DEBUG}>
$<$<CONFIG:Release>:${_LIBS_RELEASE}>
$<TARGET_FILE_DIR:MyApp>)
It copies libs in Debug but not in release:
Is this supposed to be legal and should work?
If it is not legal (I do not get error), how can I achieve the same effect?
Turning my comments into an answer
What I normally do to debug those case is to add another COMMAND before the actual line in question that just echos the command line. In your case:
COMMAND ${CMAKE_COMMAND} -E echo
$<$<CONFIG:Debug>:${_LIBS_DEBUG}>
$<$<CONFIG:Release>:${_LIBS_RELEASE}>
I've run this a few tests and you will see that the $<1:...> and $<0:...> expressions are not evaluated.
So seeing this I was searching CMake's bug tracker database and this is a known issue and yet (as for CMake 3.5.2) unresolved: 0009974: CMake should support custom commands that can vary by configuration.
There are several ways proposed in this ticket that do work with existing versions of CMake.
In your case - until this issue is resolved and if you want to have it shell independent - I would do it the "old way" and call a CMake script:
CopyLibsByConfig.cmake.in
if (_CONFIG STREQUAL "Debug")
file(COPY #_LIBS_DEBUG# DESTINATION "${_DEST_PATH}")
else()
file(COPY #_LIBS_RELEASE# DESTINATION "${_DEST_PATH}")
endif()
CMakeLists.txt
...
configure_file(CopyLibsByConfig.cmake.in CopyLibsByConfig.cmake #ONLY)
add_custom_command(TARGET MyApp
POST_BUILD
COMMAND ${CMAKE_COMMAND}
-D _CONFIG=$<CONFIG>
-D _DEST_PATH="$<TARGET_FILE_DIR:MyApp>"
-P "${CMAKE_CURRENT_BINARY_DIR}/CopyLibsByConfig.cmake"
)
But the solution can very much depend on the files you want to copy to your binary output folder. And there are a lot of way doing it, like using install():
install(FILES ${_LIBS_DEBUG} CONFIGURATIONS Debug DESTINATION $<TARGET_FILE_DIR:MyApp>)
install(FILES ${_LIBS_RELEASE} CONFIGURATIONS Release DESTINATION $<TARGET_FILE_DIR:MyApp>)
set(CMAKE_VS_INCLUDE_INSTALL_TO_DEFAULT_BUILD 1)
Obviously that's not the way install() is meant to be used, so consider using the INSTALL or PACKAGE targets properly to distribute your application and all its dependencies.
And if we are talking about Visual Studio runtime DLLs you most likely want to take a look at the InstallRequiredSystemLibraries CMake module.
Other solution is to use generator expression.
For example I have cppzmq (shared library) and cppzmq-static (static library with static dependencies). I would like to have faster debug builds so I use cppzmq in Debug build and in (other) e.g. release I want one big fat exec.
target_link_libraries(CommunicationCommonLib PUBLIC
$<IF:$<CONFIG:Debug>,cppzmq,cppzmq-static>
Dexode::EventBus
gcpp
protobuf::libprotobuf
)

Building boost and changing output dir/filename

I'm trying to build boost via Boost.Build from the cmd line - https://boostorg.github.io/build/manual/develop/index.html#bbv2.overview.invocation
I want to have boost output my files at a certain folder and depending on address model + variant (debug/release 32/64) have a different name.
I tried something like this:
b2 --libdir=C:\ serialization filesystem system toolset=msvc-12.0 variant=debug,release threading=multi link=static runtime-link=static address-model=64,32 architecture=x86
It just builds to the default output path... and there seems to be no option to rename output files.
Also tried "--build-dir" with no success.
Any ideas?

Building a subset of boost libraries

I'm trying to build only a subset of boost libraries. For example, I have this code:
test.cpp:
#include <boost/thread.hpp>
int main (){
return 0;
}
I then do
./bcp --scan test.cpp ~/dev/boost_compact/
So the dependencies files are copied to ~/dev/boost_compact/boost.
Then, following this answer, I copy all files at the root of a regular boost and also the tools directory and run
./bootstrap
./bjam
./bjam install
This does copy all the headers to a destination directory, but it does not build/copy the libraries. This same set of actions does work in the full boost. What am I doing wrong?
Solved the problem. The reason the libraries were not being copied was that I was using the wrong boost directory, that is
./bcp --scan --boost=<path to boost build directory> test.cpp ~/dev/boost_compact/
when I should be using
./bcp --scan --boost=<path to boost source directory> test.cpp ~/dev/boost_compact/
If now you run
./bootstrap
./bjam
./bjam install
The libraries will be build.
Maybe a permission issue?
or
Perhaps try explicitly setting the libdir?
bjam --libdir=path/to/lib install