Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / 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...")
86     # NOTE: If you change this version number make sure the package is available
87     # at the three URLs below (may involve uploading to download.ceph.com)
88     set(boost_version 1.63.0)
89     set(boost_md5 1c837ecd990bb022d07e7aab32b09847)
90     string(REPLACE "." "_" boost_version_underscore ${boost_version} )
91     set(boost_url 
92       https://dl.bintray.com/boostorg/release/${boost_version}/source/boost_${boost_version_underscore}.tar.bz2)
93     if(CMAKE_VERSION VERSION_GREATER 3.7)
94       set(boost_url
95         "${boost_url} http://downloads.sourceforge.net/project/boost/boost/${boost_version}/boost_${boost_version_underscore}.tar.bz2")
96       set(boost_url
97         "${boost_url} https://download.ceph.com/qa/boost_${boost_version_underscore}.tar.bz2")
98     endif()
99     set(source_dir
100       URL ${boost_url}
101       URL_MD5 ${boost_md5})
102     if(CMAKE_VERSION VERSION_GREATER 3.0)
103       list(APPEND source_dir DOWNLOAD_NO_PROGRESS 1)
104     endif()
105   endif()
106   # build all components in a single shot
107   include(ExternalProject)
108   ExternalProject_Add(Boost
109     ${source_dir}
110     CONFIGURE_COMMAND CC=${CMAKE_C_COMPILER} CXX=${CMAKE_CXX_COMPILER} ${configure_command}
111     BUILD_COMMAND CC=${CMAKE_C_COMPILER} CXX=${CMAKE_CXX_COMPILER} ${build_command}
112     BUILD_IN_SOURCE 1
113     INSTALL_COMMAND ${install_command}
114     PREFIX "${boost_root_dir}")
115 endfunction()
116
117 macro(build_boost version)
118   do_build_boost(version ${ARGN})
119   ExternalProject_Get_Property(Boost install_dir)
120   set(Boost_INCLUDE_DIRS ${install_dir}/include)
121   set(Boost_INCLUDE_DIR ${install_dir}/include)
122   # create the directory so cmake won't complain when looking at the imported
123   # target
124   file(MAKE_DIRECTORY ${Boost_INCLUDE_DIRS})
125   cmake_parse_arguments(Boost_BUILD "" "" COMPONENTS ${ARGN})
126   foreach(c ${Boost_BUILD_COMPONENTS})
127     string(TOUPPER ${c} upper_c)
128     if(Boost_USE_STATIC_LIBS)
129       add_library(Boost::${c} STATIC IMPORTED)
130     else()
131       add_library(Boost::${c} SHARED IMPORTED)
132     endif()
133     add_dependencies(Boost::${c} Boost)
134     if(Boost_USE_STATIC_LIBS)
135       set(Boost_${upper_c}_LIBRARY
136         ${install_dir}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}boost_${c}${CMAKE_STATIC_LIBRARY_SUFFIX})
137     else()
138       set(Boost_${upper_c}_LIBRARY
139         ${install_dir}/lib/${CMAKE_SHARED_LIBRARY_PREFIX}boost_${c}${CMAKE_SHARED_LIBRARY_SUFFIX})
140     endif()
141     set_target_properties(Boost::${c} PROPERTIES
142       INTERFACE_INCLUDE_DIRECTORIES "${Boost_INCLUDE_DIRS}"
143       IMPORTED_LINK_INTERFACE_LANGUAGES "CXX"
144       IMPORTED_LOCATION "${Boost_${upper_c}_LIBRARY}")
145     list(APPEND Boost_LIBRARIES ${Boost_${upper_c}_LIBRARY})
146   endforeach()
147
148   # for header-only libraries
149   if(CMAKE_VERSION VERSION_LESS 3.3)
150     # only ALIAS and INTERFACE target names allow ":" in it, but
151     # INTERFACE library is not allowed until cmake 3.1
152     add_custom_target(Boost.boost DEPENDS Boost)
153   else()
154     add_library(Boost.boost INTERFACE IMPORTED)
155     set_target_properties(Boost.boost PROPERTIES
156       INTERFACE_INCLUDE_DIRECTORIES "${Boost_INCLUDE_DIRS}")
157     add_dependencies(Boost.boost Boost)
158   endif()
159   find_package_handle_standard_args(Boost DEFAULT_MSG
160     Boost_INCLUDE_DIRS Boost_LIBRARIES)
161   mark_as_advanced(Boost_LIBRARIES BOOST_INCLUDE_DIRS)
162 endmacro()
163
164 function(maybe_add_boost_dep target)
165   get_target_property(imported ${target} IMPORTED)
166   if(imported)
167     return()
168   endif()
169   get_target_property(type ${target} TYPE)
170   if(NOT type MATCHES "OBJECT_LIBRARY|STATIC_LIBRARY|SHARED_LIBRARY|EXECUTABLE")
171     return()
172   endif()
173   get_target_property(sources ${target} SOURCES)
174   foreach(src ${sources})
175     get_filename_component(ext ${src} EXT)
176     # assuming all cxx source files include boost header(s)
177     if(ext MATCHES ".cc|.cpp|.cxx")
178       add_dependencies(${target} Boost.boost)
179       return()
180     endif()
181   endforeach()
182 endfunction()
183
184 # override add_library() to add Boost headers dependency
185 function(add_library target)
186   _add_library(${target} ${ARGN})
187   maybe_add_boost_dep(${target})
188 endfunction()
189
190 function(add_executable target)
191   _add_executable(${target} ${ARGN})
192   maybe_add_boost_dep(${target})
193 endfunction()