CMake external project installation boost

Posted by skyagh on Mon, 24 Jan 2022 22:31:03 +0100

This is the command used to manage third-party libraries in CMake. The CMake version used in this article is 3.22.1.
The text introduces the CMake command of ExternalProject, and boost is installed based on this.

Official documents: https://cmake.org/cmake/help/v3.22/module/ExternalProject.html

This library needs to include (external project).

ExternalProject_Add

Define a target, which includes the following steps:

  1. download
  2. update/patch
  3. configure
  4. build
  5. install
  6. test

These steps are performed sequentially. configure, build and install all have default operations. These default operations are aimed at building third-party libraries that support CMake. If the installed third-party library does not support CMake and has specific configuration and construction processes, these construction steps need to be overwritten.

download

URL

There are many parameters in download, mainly about URL. URL supports specifying the URL of network compressed package and local compressed package.

Here is a case of boost local compressed package (other parameters need to be configured for normal installation of boost, which will be added in later chapters):

include(ExternalProject)
SET(DOWNLOAD_DIR "/root/downloads")
SET(BOOST_PATH ${DOWNLOAD_DIR}/boost_1_78_0.tar.gz)
ExternalProject_Add(boost
  URL               "${BOOST_PATH}"
)

The above URL has only one local path, but according to the document, since version 3.7, the URL can provide multiple URLs. If multiple URLs are provided, they are tried in turn until one of them succeeds.
Because my current network is inconvenient, I won't put more url cases.

URL_MD5

As the name suggests, it is used to verify the MD5 value of the compressed package.

include(ExternalProject)
SET(DOWNLOAD_DIR "/root/downloads")
SET(BOOST_PATH ${DOWNLOAD_DIR}/boost_1_78_0.tar.gz)
SET(BOOST_MD5 c2f6428ac52b0e5a3c9b2e1d8cc832b5)
ExternalProject_Add(boost
  URL               "${BOOST_PATH}"
  URL_MD5           "${BOOST_MD5}"
)

If the compressed package does not match the specified md5 value, an error will be reported.

Installation path

PREFIX

Used to configure download, decompression, installation and other paths.
PREFIX is equivalent to configuring the overall root directory, and other paths will be automatically adapted based on PREFIX.
Of course, other paths can also be configured freely.

BUILD_IN_SOURCE

Such as chapter PREFIX, SOURCE_DIR and BINARY_DIR is a different path.
However, for boost, firstly, boost is not built based on CMake. Secondly, the installation process of boost is roughly as follows:
bootstrap.sh -> b2 install
When b2 install is executed, it will depend on the previous decompression to source_ Dir file (boost-build.jam), so you want b2 (BINARY_DIR) and SOURCE_DIR to be the same path. After executing bootstrap After sh, b2 will be put into binary_ Build in dir_ IN_ The function of source is to make binary_ Dir equals source_ DIR.

Therefore, the current installation contents of boost are:

include(ExternalProject)
SET(DOWNLOAD_DIR "/root/downloads")
SET(BOOST_PATH ${DOWNLOAD_DIR}/boost_1_78_0.tar.gz)
SET(BOOST_MD5 c2f6428ac52b0e5a3c9b2e1d8cc832b5)
ExternalProject_Add(boost
  URL               "${BOOST_PATH}"
  URL_MD5           "${BOOST_MD5}"
  PREFIX            "${CMAKE_CURRENT_BINARY_DIR}"
  # Because when executing b2, you need to read boost build. Exe in the same directory Jam, so BUILD_IN_SOURCE
  BUILD_IN_SOURCE   true
)

COMMAND

As mentioned at the beginning, there will be configure, build and install steps. It corresponds to configuration_ COMMAND,BUILD_COMMAND,INSTALL_COMMAND.
Because boost does not rely on CMake, you need to override these three commands.
The overwrite command is the step of normal installation of boost. Put the complete CMake configuration here first:

include(ExternalProject)
SET(DOWNLOAD_DIR "/root/downloads")
SET(BOOST_PATH ${DOWNLOAD_DIR}/boost_1_78_0.tar.gz)
SET(BOOST_MD5 c2f6428ac52b0e5a3c9b2e1d8cc832b5)
SET(BOOST_CONFIGURE <SOURCE_DIR>/bootstrap.sh --prefix=<SOURCE_DIR>)
SET(BOOST_INSTALL <SOURCE_DIR>/b2 install --prefix=<BINARY_DIR>/../build)
ExternalProject_Add(boost
  URL               "${BOOST_PATH}"
  URL_MD5           "${BOOST_MD5}"
  PREFIX            "${CMAKE_CURRENT_BINARY_DIR}"
  # Because when executing b2, you need to read boost build. Exe in the same directory Jam, so BUILD_IN_SOURCE
  BUILD_IN_SOURCE   true
  CONFIGURE_COMMAND "${BOOST_CONFIGURE}"
  BUILD_COMMAND       ""
  INSTALL_COMMAND   "${BOOST_INSTALL}"
)

1,BOOST_ preifx in install specifies the installation path in the project, instead of allowing it to be installed in / usr/local by default, so that this third-party library does not affect the outside, which can be modified as appropriate.
2,BUILD_ There is no command to use in the command, but in order to invalidate the default command, you also need to overwrite an empty command.
3. This target named boost does not operate in cmake. It can only perform decompression, md5 judgment... And a series of operations after making. (this part of the card has been stuck for a long time, and I was puzzled why cmake didn't respond at that time) of course, it also confirms that I'm not familiar with cmake. Cmake is only a preprocessor, and the real executor is make.

This is basically based on external project_ Add installs the content of boost. Based on this case, a non CMake third-party library is installed to access more content. I believe it will be easier to install CMake library later.

Topics: C++ cmake