]> git.proxmox.com Git - ceph.git/blob - ceph/cmake/modules/BuildBoost.cmake
update sources to v12.1.0
[ceph.git] / ceph / cmake / modules / BuildBoost.cmake
1 # This module builds Boost
2 # executables are. It sets the following variables:
3 #
4 # Boost_FOUND : boolean - system has Boost
5 # Boost_LIBRARIES : list(filepath) - the libraries needed to use Boost
6 # Boost_INCLUDE_DIRS : list(path) - the Boost include directories
7 #
8 # Following hints are respected
9 #
10 # Boost_USE_STATIC_LIBS : boolean (default: OFF)
11 # Boost_USE_MULTITHREADED : boolean (default: OFF)
12 # BOOST_J: integer (defanult 1)
13
14 function(do_build_boost version)
15 cmake_parse_arguments(Boost_BUILD "" "" COMPONENTS ${ARGN})
16 set(boost_features "variant=release")
17 if(Boost_USE_MULTITHREADED)
18 list(APPEND boost_features "threading=multi")
19 else()
20 list(APPEND boost_features "threading=single")
21 endif()
22 if(Boost_USE_STATIC_LIBS)
23 list(APPEND boost_features "link=static")
24 else()
25 list(APPEND boost_features "link=shared")
26 endif()
27 if(CMAKE_SIZEOF_VOID_P EQUAL 8)
28 list(APPEND boost_features "address-model=64")
29 else()
30 list(APPEND boost_features "address-model=32")
31 endif()
32 set(BOOST_CXXFLAGS "-fPIC -w") # check on arm, etc <---XXX
33 list(APPEND boost_features "cxxflags=${BOOST_CXXFLAGS}")
34
35 string(REPLACE ";" "," boost_with_libs "${Boost_BUILD_COMPONENTS}")
36 # build b2 and prepare the project-config.jam for boost
37 set(configure_command
38 ./bootstrap.sh --prefix=<INSTALL_DIR>
39 --with-libraries=${boost_with_libs})
40
41 set(b2 ./b2)
42 if(BOOST_J)
43 message(STATUS "BUILDING Boost Libraries at j ${BOOST_J}")
44 list(APPEND b2 -j${BOOST_J})
45 endif()
46 if(CMAKE_VERBOSE_MAKEFILE)
47 list(APPEND b2 -d1)
48 else()
49 list(APPEND b2 -d0)
50 endif()
51
52 if(NOT CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL CMAKE_SYSTEM_PROCESSOR)
53 # we are crosscompiling
54 if(CMAKE_CXX_COMPILER_ID STREQUAL GNU)
55 set(b2_cc gcc)
56 elseif(CMAKE_CXX_COMPILER_ID STREQUAL Clang)
57 set(b2_cc clang)
58 else()
59 message(SEND_ERROR "unknown compiler: ${CMAKE_CXX_COMPILER_ID}")
60 endif()
61 # edit the config.jam so, b2 will be able to use the specified toolset
62 execute_process(
63 COMMAND
64 sed -i
65 "s|using ${b2_cc} ;|using ${b2_cc} : ${CMAKE_SYSTEM_PROCESSOR} : ${CMAKE_CXX_COMPILER} ;|"
66 ${PROJECT_SOURCE_DIR}/src/boost/project-config.jam)
67 # use ${CMAKE_SYSTEM_PROCESSOR} as the version identifier of compiler
68 list(APPEND b2 toolset=${b2_cc}-${CMAKE_SYSTEM_PROCESSOR})
69 endif()
70
71 set(build_command
72 ${b2} headers stage
73 #"--buildid=ceph" # changes lib names--can omit for static
74 ${boost_features})
75 set(install_command
76 ${b2} install)
77 set(boost_root_dir "${CMAKE_BINARY_DIR}/boost")
78 if(EXISTS "${PROJECT_SOURCE_DIR}/src/boost/libs/config/include/boost/config.hpp")
79 message(STATUS "boost already in src")
80 set(source_dir
81 SOURCE_DIR "${PROJECT_SOURCE_DIR}/src/boost")
82 elseif(version VERSION_GREATER 1.63)
83 message(FATAL_ERROR "Unknown BOOST_REQUESTED_VERSION: ${version}")
84 else()
85 message(STATUS "boost will be downloaded from sf.net")
86 set(boost_version 1.63.0)
87 set(boost_md5 1c837ecd990bb022d07e7aab32b09847)
88 string(REPLACE "." "_" boost_version_underscore ${boost_version} )
89 set(boost_url http://downloads.sourceforge.net/project/boost/boost/${boost_version}/boost_${boost_version_underscore}.tar.bz2)
90 set(source_dir
91 URL ${boost_url}
92 URL_MD5 ${boost_md5})
93 if(CMAKE_VERSION VERSION_GREATER 3.0)
94 list(APPEND source_dir DOWNLOAD_NO_PROGRESS 1)
95 endif()
96 endif()
97 # build all components in a single shot
98 include(ExternalProject)
99 ExternalProject_Add(Boost
100 ${source_dir}
101 CONFIGURE_COMMAND CC=${CMAKE_C_COMPILER} CXX=${CMAKE_CXX_COMPILER} ${configure_command}
102 BUILD_COMMAND CC=${CMAKE_C_COMPILER} CXX=${CMAKE_CXX_COMPILER} ${build_command}
103 BUILD_IN_SOURCE 1
104 INSTALL_COMMAND ${install_command}
105 PREFIX "${boost_root_dir}")
106 endfunction()
107
108 macro(build_boost version)
109 do_build_boost(version ${ARGN})
110 ExternalProject_Get_Property(Boost install_dir)
111 set(Boost_INCLUDE_DIRS ${install_dir}/include)
112 set(Boost_INCLUDE_DIR ${install_dir}/include)
113 # create the directory so cmake won't complain when looking at the imported
114 # target
115 file(MAKE_DIRECTORY ${Boost_INCLUDE_DIRS})
116 cmake_parse_arguments(Boost_BUILD "" "" COMPONENTS ${ARGN})
117 foreach(c ${Boost_BUILD_COMPONENTS})
118 string(TOUPPER ${c} upper_c)
119 if(Boost_USE_STATIC_LIBS)
120 add_library(Boost::${c} STATIC IMPORTED)
121 else()
122 add_library(Boost::${c} SHARED IMPORTED)
123 endif()
124 add_dependencies(Boost::${c} Boost)
125 if(Boost_USE_STATIC_LIBS)
126 set(Boost_${upper_c}_LIBRARY
127 ${install_dir}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}boost_${c}${CMAKE_STATIC_LIBRARY_SUFFIX})
128 else()
129 set(Boost_${upper_c}_LIBRARY
130 ${install_dir}/lib/${CMAKE_SHARED_LIBRARY_PREFIX}boost_${c}${CMAKE_SHARED_LIBRARY_SUFFIX})
131 endif()
132 set_target_properties(Boost::${c} PROPERTIES
133 INTERFACE_INCLUDE_DIRECTORIES "${Boost_INCLUDE_DIRS}"
134 IMPORTED_LINK_INTERFACE_LANGUAGES "CXX"
135 IMPORTED_LOCATION "${Boost_${upper_c}_LIBRARY}")
136 list(APPEND Boost_LIBRARIES ${Boost_${upper_c}_LIBRARY})
137 endforeach()
138
139 # for header-only libraries
140 if(CMAKE_VERSION VERSION_LESS 3.3)
141 # only ALIAS and INTERFACE target names allow ":" in it, but
142 # INTERFACE library is not allowed until cmake 3.1
143 add_custom_target(Boost.boost DEPENDS Boost)
144 else()
145 add_library(Boost.boost INTERFACE IMPORTED)
146 set_target_properties(Boost.boost PROPERTIES
147 INTERFACE_INCLUDE_DIRECTORIES "${Boost_INCLUDE_DIRS}")
148 add_dependencies(Boost.boost Boost)
149 endif()
150 find_package_handle_standard_args(Boost DEFAULT_MSG
151 Boost_INCLUDE_DIRS Boost_LIBRARIES)
152 mark_as_advanced(Boost_LIBRARIES BOOST_INCLUDE_DIRS)
153 endmacro()
154
155 function(maybe_add_boost_dep target)
156 get_target_property(imported ${target} IMPORTED)
157 if(imported)
158 return()
159 endif()
160 get_target_property(type ${target} TYPE)
161 if(NOT type MATCHES "OBJECT_LIBRARY|STATIC_LIBRARY|SHARED_LIBRARY|EXECUTABLE")
162 return()
163 endif()
164 get_target_property(sources ${target} SOURCES)
165 foreach(src ${sources})
166 get_filename_component(ext ${src} EXT)
167 # assuming all cxx source files include boost header(s)
168 if(ext MATCHES ".cc|.cpp|.cxx")
169 add_dependencies(${target} Boost.boost)
170 return()
171 endif()
172 endforeach()
173 endfunction()
174
175 # override add_library() to add Boost headers dependency
176 function(add_library target)
177 _add_library(${target} ${ARGN})
178 maybe_add_boost_dep(${target})
179 endfunction()
180
181 function(add_executable target)
182 _add_executable(${target} ${ARGN})
183 maybe_add_boost_dep(${target})
184 endfunction()