Skip to content

Instantly share code, notes, and snippets.

@thiagowfx
Last active February 22, 2023 15:46
Show Gist options
  • Star 25 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save thiagowfx/970e3931387ed7db9a39709a8a130ee9 to your computer and use it in GitHub Desktop.
Save thiagowfx/970e3931387ed7db9a39709a8a130ee9 to your computer and use it in GitHub Desktop.
FindBoost.cmake (Note: DO NOT DELETE as it's linked from Stack Overflow)
# - Find Boost
#
# Copyright (c) 2016 Thiago Barroso Perrotta
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
# This module finds if Boost is installed and determines where the
# executables are. It sets the following variables:
#
# BOOST_FOUND : boolean - system has Boost
# BOOST_LIBRARIES : list(filepath) - the libraries needed to use Boost
# BOOST_INCLUDE_DIRS : list(path) - the Boost include directories
#
# If Boost is not found, this module downloads it according to the
# following variables:
#
# BOOST_ROOT_DIR : path - the Path where Boost will be installed on
# BOOST_REQUESTED_VERSION : string - the Boost version to be downloaded
#
# You can also specify its components:
#
# find_package(Boost COMPONENTS program_options system)
#
# which are stored in Boost_FIND_COMPONENTS : list(string)
#
# You can also specify its behavior:
#
# BOOST_USE_STATIC_LIBS : boolean (default: OFF)
if(NOT Boost_FIND_COMPONENTS)
message(FATAL_ERROR "No COMPONENTS specified for Boost")
endif()
set(BOOST_USE_STATIC_LIBS false)
# Set the library prefix and library suffix properly.
if(BOOST_USE_STATIC_LIBS)
set(CMAKE_FIND_LIBRARY_PREFIXES ${CMAKE_STATIC_LIBRARY_PREFIX})
set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_STATIC_LIBRARY_SUFFIX})
set(LIBRARY_PREFIX ${CMAKE_STATIC_LIBRARY_PREFIX})
set(LIBRARY_SUFFIX ${CMAKE_STATIC_LIBRARY_SUFFIX})
else()
set(CMAKE_FIND_LIBRARY_PREFIXES ${CMAKE_SHARED_LIBRARY_PREFIX})
set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_SHARED_LIBRARY_SUFFIX})
set(LIBRARY_PREFIX ${CMAKE_SHARED_LIBRARY_PREFIX})
set(LIBRARY_SUFFIX ${CMAKE_SHARED_LIBRARY_SUFFIX})
endif()
# Create a list(string) for the build command (e.g. --with-program_options;--with-system)
# and assigns it to BOOST_COMPONENTS_FOR_BUILD
foreach(component ${Boost_FIND_COMPONENTS})
list(APPEND BOOST_COMPONENTS_FOR_BUILD --with-${component})
endforeach()
# Create a string for the first component (e.g. boost_program_options)
# and assigns it to Boost_FIND_COMPONENTS
list(GET Boost_FIND_COMPONENTS 0 BOOST_FIRST_COMPONENT)
set(BOOST_FIRST_COMPONENT "boost_${BOOST_FIRST_COMPONENT}")
include(FindPackageHandleStandardArgs)
macro(DO_FIND_BOOST_SYSTEM)
find_path(BOOST_INCLUDE_DIR boost/config.hpp
PATHS /usr/local/include /usr/include
)
find_library(BOOST_LIBRARY
NAMES ${BOOST_FIRST_COMPONENT}
PATHS /usr/local/lib /usr/lib
)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(Boost DEFAULT_MSG
BOOST_INCLUDE_DIR BOOST_LIBRARY
)
set(BOOST_LIBRARIES ${BOOST_LIBRARY})
set(BOOST_INCLUDE_DIRS ${BOOST_INCLUDE_DIR})
mark_as_advanced(BOOST_LIBRARIES BOOST_INCLUDE_DIRS)
endmacro()
macro(DO_FIND_BOOST_ROOT)
if(NOT BOOST_ROOT_DIR)
message(STATUS "BOOST_ROOT_DIR is not defined, using binary directory.")
set(BOOST_ROOT_DIR ${CURRENT_CMAKE_BINARY_DIR} CACHE PATH "")
endif()
find_path(BOOST_INCLUDE_DIR boost/config.hpp ${BOOST_ROOT_DIR}/include)
find_library(BOOST_LIBRARY ${BOOST_FIRST_COMPONENT} HINTS ${BOOST_ROOT_DIR}/lib)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(Boost DEFAULT_MSG
BOOST_INCLUDE_DIR BOOST_LIBRARY
)
set(BOOST_LIBRARIES ${BOOST_LIBRARY})
set(BOOST_INCLUDE_DIRS ${BOOST_INCLUDE_DIR})
mark_as_advanced(BOOST_LIBRARIES BOOST_INCLUDE_DIRS)
endmacro()
macro(DO_FIND_BOOST_DOWNLOAD)
if(NOT BOOST_REQUESTED_VERSION)
message(FATAL_ERROR "BOOST_REQUESTED_VERSION is not defined.")
endif()
string(REPLACE "." "_" BOOST_REQUESTED_VERSION_UNDERSCORE ${BOOST_REQUESTED_VERSION})
set(BOOST_MAYBE_STATIC)
if(BOOST_USE_STATIC_LIBS)
set(BOOST_MAYBE_STATIC "link=static")
endif()
include(ExternalProject)
ExternalProject_Add(
Boost
URL https://downloads.sourceforge.net/project/boost/boost/${BOOST_REQUESTED_VERSION}/boost_${BOOST_REQUESTED_VERSION_UNDERSCORE}.zip
UPDATE_COMMAND ""
CONFIGURE_COMMAND ./bootstrap.sh --prefix=${BOOST_ROOT_DIR}
BUILD_COMMAND ./b2 ${BOOST_MAYBE_STATIC} --prefix=${BOOST_ROOT_DIR} ${BOOST_COMPONENTS_FOR_BUILD} install
BUILD_IN_SOURCE true
INSTALL_COMMAND ""
INSTALL_DIR ${BOOST_ROOT_DIR}
)
ExternalProject_Get_Property(Boost install_dir)
set(BOOST_INCLUDE_DIRS ${install_dir}/include)
macro(libraries_to_fullpath varname)
set(${varname})
foreach(component ${Boost_FIND_COMPONENTS})
list(APPEND ${varname} ${BOOST_ROOT_DIR}/lib/${LIBRARY_PREFIX}boost_${component}${LIBRARY_SUFFIX})
endforeach()
endmacro()
libraries_to_fullpath(BOOST_LIBRARIES)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(Boost DEFAULT_MSG
BOOST_INCLUDE_DIRS BOOST_LIBRARIES
)
mark_as_advanced(BOOST_LIBRARIES BOOST_INCLUDE_DIRS)
endmacro()
if(NOT BOOST_FOUND)
DO_FIND_BOOST_ROOT()
endif()
if(NOT BOOST_FOUND)
DO_FIND_BOOST_SYSTEM()
endif()
if(NOT BOOST_FOUND)
DO_FIND_BOOST_DOWNLOAD()
endif()
@bryan-lunt
Copy link

It would be awesome if there were a mini-project demonstrating using this?

@Blacklock
Copy link

Blacklock commented Jul 14, 2020

Hey, I'm getting the same error as somebody in a StackOverflow comment did. Any clue on what could be causing this?

CMake Error: Error required internal CMake variable not set, cmake may not be built correctly.
Missing variable is:
CMAKE_FIND_LIBRARY_PREFIXES

I'm using find_package() after the project() statement, so I'm not sure where the error is coming from.

@bryan-lunt
Copy link

bryan-lunt commented Jul 14, 2020

Are you using an old version of CMake?

Also, see this answer?

https://stackoverflow.com/a/42018800

@Blacklock
Copy link

Blacklock commented Jul 19, 2020

@bryan-lunt I'm using CMake 3.16 and do call project() beforehand. Here's a minimal example (used with this project).

# ./CMakeLists.txt
cmake_minimum_required(VERSION 3.16)
project(
	DemoServer 
	VERSION 1.0.0 
	LANGUAGES CXX
)

set(CMAKE_CXX_EXTENSIONS OFF)
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")
set(BOOST_REQUESTED_VERSION "1.73.0")

find_package(Boost COMPONENTS accumulators core)

add_subdirectory(src)
# /src/CMakeLists.txt
file(GLOB_RECURSE SRC_FILES
	"${PROJECT_SOURCE_DIR}/src/*.h"
	"${PROJECT_SOURCE_DIR}/src/*.cpp"
)

include_directories(${BOOST_INCLUDE_DIRS})
add_executable(DemoServer ${SRC_FILES})
target_link_libraries(DemoServer PRIVATE ${Boost_LIBRARIES})
set_target_properties(DemoServer PROPERTIES RUNTIME_OUTPUT_DIRECTORY_DEBUG "${PROJECT_BINARY_DIR}/bin/src/")
set_target_properties(DemoServer PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELEASE "${PROJECT_BINARY_DIR}/bin/src/")
target_compile_features(DemoServer PRIVATE cxx_std_17)

I checked (and also tried manually setting) the variables CMAKE_FIND_LIBRARY_<PREFIXES/SUFFIXES>, CMAKE_FIND_LIBRARY_SUFFFIXES, CMAKE_STATIC_LIBRARY_<PREFIX/SUFFIX>, and CMAKE_SHARED_LIBRARY_<PREFIX/SUFFIX>, but CMake appeared to be setting them properly so it didn't help.

Removing these lines from the FindBoost file makes the error disappear:

# Set the library prefix and library suffix properly.
if(BOOST_USE_STATIC_LIBS)
	set(CMAKE_FIND_LIBRARY_PREFIXES ${CMAKE_STATIC_LIBRARY_PREFIX})
	set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_STATIC_LIBRARY_SUFFIX})
	set(LIBRARY_PREFIX ${CMAKE_STATIC_LIBRARY_PREFIX})
	set(LIBRARY_SUFFIX ${CMAKE_STATIC_LIBRARY_SUFFIX})
else()
	set(CMAKE_FIND_LIBRARY_PREFIXES ${CMAKE_SHARED_LIBRARY_PREFIX})
	set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_SHARED_LIBRARY_SUFFIX})
	set(LIBRARY_PREFIX ${CMAKE_SHARED_LIBRARY_PREFIX})
	set(LIBRARY_SUFFIX ${CMAKE_SHARED_LIBRARY_SUFFIX})
endif()

...but removing those lines also makes the library not properly download in the first place and leaves the ${Boost_LIBRARIES} variable empty.

@s1nisteR
Copy link

Doesn't work at all.
Could NOT find Boost (missing: BOOST_INCLUDE_DIR BOOST_LIBRARY)
Call stack
cmake/FindBoost.cmake:101 (FIND_PACKAGE_HANDLE_STANDARD_ARGS)
cmake/FindBoost.cmake:151 (DO_FIND_BOOST_ROOT)
CMakeLists.txt:12 (find_package)

Ironic because the library is supposed to download and set up boost if not found and it just tells me it can't find boost. Well then download it bruh.

@paolostefan
Copy link

Doesn't work at all. Could NOT find Boost (missing: BOOST_INCLUDE_DIR BOOST_LIBRARY) Call stack cmake/FindBoost.cmake:101 (FIND_PACKAGE_HANDLE_STANDARD_ARGS) cmake/FindBoost.cmake:151 (DO_FIND_BOOST_ROOT) CMakeLists.txt:12 (find_package)

Ironic because the library is supposed to download and set up boost if not found and it just tells me it can't find boost. Well then download it bruh.

I've created an updated version here https://gist.github.com/paolostefan/d7db4f6a575da0843b4f346272907d81 (if you want to give it a try).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment