ThirdPartyBoost.cmake 8.44 KB
Newer Older
Dave Moxey's avatar
Dave Moxey committed
1 2 3 4 5 6 7 8
########################################################################
#
# ThirdParty configuration for Nektar++
#
# Boost
#
########################################################################

9
#If the user has not set BOOST_ROOT, look in a couple common places first.
10
MESSAGE(STATUS "Searching for Boost:")
Dave Moxey's avatar
Dave Moxey committed
11

12 13 14 15 16 17 18 19 20 21 22 23 24 25
# Macro to parse major and minor version from version string, since boost
# requires e.g. gcc-4.8 as a toolset.
MACRO(MAJOR_MINOR_VERSION VAR VERSIONSTR)
    STRING(REPLACE "." ";" VERLIST ${VERSIONSTR})
    LIST(GET VERLIST 0 ${VAR}_MAJOR)
    LIST(GET VERLIST 1 ${VAR}_MINOR)
    LIST(GET VERLIST 2 ${VAR}_PATCH)
    SET(${VAR} ${${VAR}_MAJOR}.${${VAR}_MINOR})
    UNSET(VERLIST)
    UNSET(${VAR}_MAJOR)
    UNSET(${VAR}_MINOR)
    UNSET(${VAR}_PATCH)
ENDMACRO()

26
# Minimum version and boost libraries required
27
SET(MIN_VER "1.56.0")
28
SET(NEEDED_BOOST_LIBS thread iostreams filesystem system program_options regex)
Dave Moxey's avatar
Dave Moxey committed
29

30
SET(Boost_NO_BOOST_CMAKE ON)
31
IF( BOOST_ROOT )
32
    SET(Boost_NO_SYSTEM_PATHS ON)
Dave Moxey's avatar
Dave Moxey committed
33
    FIND_PACKAGE( Boost ${MIN_VER} QUIET COMPONENTS ${NEEDED_BOOST_LIBS})
34 35 36 37 38
ELSE ()
    SET(TEST_ENV1 $ENV{BOOST_HOME})
    SET(TEST_ENV2 $ENV{BOOST_DIR})
    IF (DEFINED TEST_ENV1)
        SET(BOOST_ROOT $ENV{BOOST_HOME})
39
        SET(Boost_NO_SYSTEM_PATHS ON)
40
        FIND_PACKAGE( Boost ${MIN_VER} QUIET COMPONENTS ${NEEDED_BOOST_LIBS} )
41 42
    ELSEIF (DEFINED TEST_ENV2)
        SET(BOOST_ROOT $ENV{BOOST_DIR})
43
        SET(Boost_NO_SYSTEM_PATHS ON)
44
        FIND_PACKAGE( Boost ${MIN_VER} QUIET COMPONENTS ${NEEDED_BOOST_LIBS} )
45 46
    ELSE ()
        SET(BOOST_ROOT ${TPDIST})
47
        FIND_PACKAGE( Boost ${MIN_VER} QUIET COMPONENTS ${NEEDED_BOOST_LIBS} )
48 49 50 51 52 53
    ENDIF()
ENDIF()

# Check what we found and determine if we need to build boost
FOREACH(FOUND_VAR ${NEEDED_BOOST_LIBS})
    STRING(TOUPPER ${FOUND_VAR} FOUND_VAR_UPPER)
54
    IF (Boost_${FOUND_VAR_UPPER}_FOUND)
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
        MESSAGE(STATUS "-- Found Boost ${FOUND_VAR} library: "
                "${Boost_${FOUND_VAR_UPPER}_LIBRARY}")
    ELSE ()
        MESSAGE(STATUS "-- Pre-installed Boost ${FOUND_VAR} library not found")
    ENDIF()
ENDFOREACH()

IF (NOT Boost_FOUND)
    SET(BUILD_BOOST ON)
ELSE()
    SET(BUILD_BOOST OFF)
ENDIF ()


OPTION(THIRDPARTY_BUILD_BOOST "Build Boost libraries" ${BUILD_BOOST})
70 71 72
SET(Boost_USE_MULTITHREADED ON CACHE BOOL
    "Search for multithreaded boost libraries")
MARK_AS_ADVANCED(Boost_USE_MULTITHREADED)
73

74

75 76 77 78
IF (WIN32)
    ADD_DEFINITIONS("-DBOOST_ALL_NO_LIB")
ENDIF()

79 80
IF (THIRDPARTY_BUILD_BOOST)
    INCLUDE(ExternalProject)
81 82

    # Only build the libraries we need
83 84 85
    FOREACH(boostlib ${NEEDED_BOOST_LIBS})
 	LIST(APPEND BOOST_LIB_LIST --with-${boostlib})
    ENDFOREACH()
Dave Moxey's avatar
Dave Moxey committed
86

Dave Moxey's avatar
Dave Moxey committed
87 88
    IF (NOT WIN32)
        # We need -fPIC for 64-bit builds
89 90 91
        IF( CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64" )
            SET(BOOST_FLAGS cxxflags=-fPIC cflags=-fPIC linkflags=-fPIC)
        ENDIF ()
92 93
    ENDIF()

94 95 96
    # Build Boost: first need to select toolset. Some will have specific
    # versions.
    SET(TOOLSET_VERSION "")
97
    IF (APPLE)
98
        # macOS should have the darwin toolset regardless of gcc/clang.
99
        SET(TOOLSET darwin)
100 101
    ELSEIF (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
        SET(TOOLSET msvc)
Dave Moxey's avatar
Dave Moxey committed
102
        IF (MSVC_VERSION EQUAL 1600)
103
            SET(TOOLSET_VERSION 10.0) # Visual Studio 2010
Dave Moxey's avatar
Dave Moxey committed
104
        ELSEIF (MSVC_VERSION EQUAL 1700)
105
            SET(TOOLSET_VERSION 11.0) # Visual Studio 2012
Dave Moxey's avatar
Dave Moxey committed
106
        ELSEIF (MSVC_VERSION EQUAL 1800)
107
            SET(TOOLSET_VERSION 12.0) # Visual Studio 2013
Dave Moxey's avatar
Dave Moxey committed
108
        ELSEIF (MSVC_VERSION EQUAL 1900)
109
            SET(TOOLSET_VERSION 14.0) # Visual Studio 2015
Dave Moxey's avatar
Dave Moxey committed
110
        ELSEIF (MSVC_VERSION GREATER 1909 OR MSVC_VERSION LESS 1920)
111 112 113
            SET(TOOLSET_VERSION 14.1) # Visual Studio 2017
        ELSEIF (MSVC_VERSION GREATER 1919 OR MSVC_VERSION LESS 1930)
            SET(TOOLSET_VERSION 14.2) # Visual Studio 2019
Dave Moxey's avatar
Dave Moxey committed
114
        ENDIF()
115
    ELSEIF(CMAKE_CXX_COMPILER_ID STREQUAL "Cray")
Dave Moxey's avatar
Dave Moxey committed
116
        SET(TOOLSET cray)
117 118 119 120
    ELSEIF(CMAKE_CXX_COMPILER_ID STREQUAL "Intel")
        SET(TOOLSET intel)
    ELSEIF(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
        SET(TOOLSET gcc)
121
        MAJOR_MINOR_VERSION(TOOLSET_VERSION ${CMAKE_CXX_COMPILER_VERSION})
122 123
    ELSEIF(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
        SET(TOOLSET clang)
124
        MAJOR_MINOR_VERSION(TOOLSET_VERSION ${CMAKE_CXX_COMPILER_VERSION})
125
    ELSE()
126 127 128 129 130 131 132 133 134
        MESSAGE(STATUS "Unknown compiler for boost build, assuming gcc toolset")
        SET(TOOLSET gcc)
        SET(TOOLSET_VERSION ${CMAKE_CXX_COMPILER_VERSION})
    ENDIF()

    IF (TOOLSET_VERSION STREQUAL "")
        SET(TOOLSET_CMDLINE ${TOOLSET})
    ELSE()
        SET(TOOLSET_CMDLINE ${TOOLSET}-${TOOLSET_VERSION})
135
    ENDIF()
Chris Cantwell's avatar
Chris Cantwell committed
136

Dave Moxey's avatar
Dave Moxey committed
137
    IF (NOT WIN32)
Dave Moxey's avatar
Dave Moxey committed
138 139 140
        EXTERNALPROJECT_ADD(
            boost
            PREFIX ${TPSRC}
Chris Cantwell's avatar
Chris Cantwell committed
141 142
            URL ${TPURL}/boost_1_57_0.tar.bz2
            URL_MD5 "1be49befbdd9a5ce9def2983ba3e7b76"
Dave Moxey's avatar
Dave Moxey committed
143 144 145 146 147 148
            STAMP_DIR ${TPBUILD}/stamp
            DOWNLOAD_DIR ${TPSRC}
            SOURCE_DIR ${TPBUILD}/boost
            BINARY_DIR ${TPBUILD}/boost
            TMP_DIR ${TPBUILD}/boost-tmp
            INSTALL_DIR ${TPDIST}
Michael Turner's avatar
Michael Turner committed
149
            CONFIGURE_COMMAND ./bootstrap.sh
150
            BUILD_COMMAND NO_BZIP2=1 ./b2
Dave Moxey's avatar
Dave Moxey committed
151 152 153
                variant=release
                link=shared
                include=${TPDIST}/include
154
                cxxflags="-w"
Dave Moxey's avatar
Dave Moxey committed
155 156
                linkflags="-L${TPDIST}/lib"
                ${BOOST_FLAGS} ${BOOST_LIB_LIST}
Michael Turner's avatar
Michael Turner committed
157
		--prefix=${TPDIST}
158
                --layout=system toolset=${TOOLSET_CMDLINE} install
159
            INSTALL_COMMAND ""
Dave Moxey's avatar
Dave Moxey committed
160 161 162 163 164 165 166
            )
    ELSE ()
        IF (CMAKE_CL_64)
            SET(ADDRESS_MODEL 64)
        ELSE()
            SET(ADDRESS_MODEL 32)
        ENDIF()
167 168 169
        EXTERNALPROJECT_ADD(
            boost
            PREFIX ${TPSRC}
Chris Cantwell's avatar
Chris Cantwell committed
170 171
            URL ${TPURL}/boost_1_57_0.tar.bz2
            URL_MD5 "1be49befbdd9a5ce9def2983ba3e7b76"
172
            STAMP_DIR ${TPBUILD}/stamp
173
            DOWNLOAD_DIR ${TPSRC}
174
            SOURCE_DIR ${TPBUILD}/boost
175 176 177
            BINARY_DIR ${TPBUILD}/boost
            TMP_DIR ${TPBUILD}/boost-tmp
            INSTALL_DIR ${TPDIST}
178 179
            CONFIGURE_COMMAND call bootstrap.bat
            BUILD_COMMAND b2 variant=release
180
                toolset=${TOOLSET_CMDLINE}
Dave Moxey's avatar
Dave Moxey committed
181 182 183 184 185 186 187 188 189 190
                address-model=${ADDRESS_MODEL}
                link=shared
                runtime-link=shared
                -s NO_BZIP2=1
                -s ZLIB_BINARY=zlib
                -s ZLIB_INCLUDE=${TPDIST}/include
                -s ZLIB_LIBPATH=${TPDIST}/lib
                ${BOOST_LIB_LIST}
                --layout=system
                --prefix=${TPDIST} install
191
            INSTALL_COMMAND ""
Dave Moxey's avatar
Dave Moxey committed
192 193
            )
    ENDIF()
Dave Moxey's avatar
Dave Moxey committed
194

195 196
    IF (APPLE)
        EXTERNALPROJECT_ADD_STEP(boost patch-install-path
197
            COMMAND sed -i ".bak" "s|-install_name \"|&${TPDIST}/lib/|" ${TPBUILD}/boost/tools/build/src/tools/darwin.jam
198 199 200 201
            DEPENDERS build
            DEPENDEES download)
    ENDIF (APPLE)

202 203
    # Write to jamfile to use appropriate toolset.
    SET(cmd_string "using ${TOOLSET} : ${TOOLSET_VERSION} : ${CMAKE_CXX_COMPILER} \\\;")
Michael Turner's avatar
Michael Turner committed
204 205
    IF (UNIX)
	EXTERNALPROJECT_ADD_STEP(boost conf-project-conf
206
            COMMAND cmake -E echo "${cmd_string}" > ${TPBUILD}/boost/tools/build/src/user-config.jam
Michael Turner's avatar
Michael Turner committed
207 208 209 210
            DEPENDERS build
            DEPENDEES configure)
    ENDIF()

211 212 213 214 215 216
    # If building ThirdParty zlib, force zlib build before boost
    IF (THIRDPARTY_BUILD_ZLIB)
        ADD_DEPENDENCIES(boost zlib-1.2.7)
    ENDIF(THIRDPARTY_BUILD_ZLIB)

    # Set up CMake variables
217 218 219 220 221 222 223
    FOREACH(BOOSTLIB ${NEEDED_BOOST_LIBS})
        STRING(TOUPPER ${BOOSTLIB} BOOSTLIB_UPPER)
        THIRDPARTY_LIBRARY(Boost_${BOOSTLIB_UPPER}_LIBRARY
            SHARED boost_${BOOSTLIB} DESCRIPTION "Boost ${BOOSTLIB} library")
        MARK_AS_ADVANCED(Boost_${BOOSTLIB_UPPER}_LIBRARY)
        LIST(APPEND Boost_LIBRARIES ${Boost_${BOOSTLIB_UPPER}_LIBRARY})
    ENDFOREACH()
224

225
    SET(Boost_INCLUDE_DIRS ${TPSRC}/dist/include)
226
    SET(Boost_CONFIG_INCLUDE_DIR ${TPINC})
227
    SET(Boost_LIBRARY_DIRS ${TPSRC}/dist/lib)
228
    SET(Boost_CONFIG_LIBRARY_DIR ${TPLIB})
229

Chris Cantwell's avatar
Chris Cantwell committed
230
    INCLUDE_DIRECTORIES(SYSTEM ${TPDIST}/include)
231

232 233
    STRING(REPLACE ";" ", " NEEDED_BOOST_LIBS_STRING "${NEEDED_BOOST_LIBS}")
    MESSAGE(STATUS "Build boost libs: ${NEEDED_BOOST_LIBS_STRING}")
Dave Moxey's avatar
Dave Moxey committed
234
ELSE (THIRDPARTY_BUILD_BOOST)
235
    ADD_CUSTOM_TARGET(boost ALL)
236 237 238
    IF (BOOST_THREAD_LIBRARY)
        MARK_AS_ADVANCED(BOOST_THREAD_LIBRARY)
    ENDIF (BOOST_THREAD_LIBRARY)
239 240
    SET(Boost_CONFIG_INCLUDE_DIR ${Boost_INCLUDE_DIRS})
    SET(Boost_CONFIG_LIBRARY_DIR ${Boost_LIBRARY_DIRS})
241
ENDIF (THIRDPARTY_BUILD_BOOST)
242

Chris Cantwell's avatar
Chris Cantwell committed
243
INCLUDE_DIRECTORIES(SYSTEM ${Boost_INCLUDE_DIRS})