ThirdPartyPackages.cmake 55.7 KB
Newer Older
J
jinhai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

Z
zhiru 已提交
18
set(MEGASEARCH_THIRDPARTY_DEPENDENCIES
J
jinhai 已提交
19

Z
zhiru 已提交
20 21 22 23 24 25 26 27
        BOOST
        BZip2
        Easylogging++
        FAISS
        GTest
        LAPACK
        Lz4
        OpenBLAS
Z
zhiru 已提交
28
        Prometheus
Z
zhiru 已提交
29 30 31 32 33 34 35 36
        RocksDB
        Snappy
        SQLite
        SQLite_ORM
        Thrift
        yaml-cpp
        ZLIB
        ZSTD)
J
jinhai 已提交
37

Z
zhiru 已提交
38
message(STATUS "Using ${MEGASEARCH_DEPENDENCY_SOURCE} approach to find dependencies")
J
jinhai 已提交
39

Z
zhiru 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
# For each dependency, set dependency source to global default, if unset
foreach(DEPENDENCY ${MEGASEARCH_THIRDPARTY_DEPENDENCIES})
    if("${${DEPENDENCY}_SOURCE}" STREQUAL "")
        set(${DEPENDENCY}_SOURCE ${MEGASEARCH_DEPENDENCY_SOURCE})
    endif()
endforeach()

macro(build_dependency DEPENDENCY_NAME)
    if("${DEPENDENCY_NAME}" STREQUAL "BZip2")
        build_bzip2()
    elseif("${DEPENDENCY_NAME}" STREQUAL "Easylogging++")
        build_easyloggingpp()
    elseif("${DEPENDENCY_NAME}" STREQUAL "FAISS")
        build_faiss()
    elseif("${DEPENDENCY_NAME}" STREQUAL "LAPACK")
        build_lapack()
    elseif("${DEPENDENCY_NAME}" STREQUAL "Lz4")
        build_lz4()
    elseif ("${DEPENDENCY_NAME}" STREQUAL "GTest")
        build_gtest()
    elseif ("${DEPENDENCY_NAME}" STREQUAL "OpenBLAS")
        build_openblas()
Z
zhiru 已提交
62 63
    elseif ("${DEPENDENCY_NAME}" STREQUAL "Prometheus")
        build_prometheus()
Z
zhiru 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
    elseif ("${DEPENDENCY_NAME}" STREQUAL "RocksDB")
        build_rocksdb()
    elseif ("${DEPENDENCY_NAME}" STREQUAL "Snappy")
        build_snappy()
    elseif ("${DEPENDENCY_NAME}" STREQUAL "SQLite")
        build_sqlite()
    elseif ("${DEPENDENCY_NAME}" STREQUAL "SQLite_ORM")
        build_sqlite_orm()
    elseif("${DEPENDENCY_NAME}" STREQUAL "Thrift")
        build_thrift()
    elseif("${DEPENDENCY_NAME}" STREQUAL "yaml-cpp")
        build_yamlcpp()
    elseif("${DEPENDENCY_NAME}" STREQUAL "ZLIB")
        build_zlib()
    elseif("${DEPENDENCY_NAME}" STREQUAL "ZSTD")
        build_zstd()
    else()
        message(FATAL_ERROR "Unknown thirdparty dependency to build: ${DEPENDENCY_NAME}")
    endif ()
endmacro()
J
jinhai 已提交
84

Z
zhiru 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97 98
macro(resolve_dependency DEPENDENCY_NAME)
    if (${DEPENDENCY_NAME}_SOURCE STREQUAL "AUTO")
        #message(STATUS "Finding ${DEPENDENCY_NAME} package")
#        find_package(${DEPENDENCY_NAME} QUIET)
#        if (NOT ${DEPENDENCY_NAME}_FOUND)
            #message(STATUS "${DEPENDENCY_NAME} package not found")
        build_dependency(${DEPENDENCY_NAME})
#        endif ()
    elseif (${DEPENDENCY_NAME}_SOURCE STREQUAL "BUNDLED")
        build_dependency(${DEPENDENCY_NAME})
    elseif (${DEPENDENCY_NAME}_SOURCE STREQUAL "SYSTEM")
        find_package(${DEPENDENCY_NAME} REQUIRED)
    endif ()
endmacro()
J
jinhai 已提交
99

Z
zhiru 已提交
100 101
# ----------------------------------------------------------------------
# ExternalProject options
J
jinhai 已提交
102

Z
zhiru 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
string(TOUPPER ${CMAKE_BUILD_TYPE} UPPERCASE_BUILD_TYPE)

set(EP_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_${UPPERCASE_BUILD_TYPE}}")
set(EP_C_FLAGS "${CMAKE_C_FLAGS} ${CMAKE_C_FLAGS_${UPPERCASE_BUILD_TYPE}}")

if(NOT MSVC)
    # Set -fPIC on all external projects
    set(EP_CXX_FLAGS "${EP_CXX_FLAGS} -fPIC")
    set(EP_C_FLAGS "${EP_C_FLAGS} -fPIC")
endif()

# CC/CXX environment variables are captured on the first invocation of the
# builder (e.g make or ninja) instead of when CMake is invoked into to build
# directory. This leads to issues if the variables are exported in a subshell
# and the invocation of make/ninja is in distinct subshell without the same
# environment (CC/CXX).
set(EP_COMMON_TOOLCHAIN -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
        -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER})

if(CMAKE_AR)
    set(EP_COMMON_TOOLCHAIN ${EP_COMMON_TOOLCHAIN} -DCMAKE_AR=${CMAKE_AR})
endif()

if(CMAKE_RANLIB)
    set(EP_COMMON_TOOLCHAIN ${EP_COMMON_TOOLCHAIN} -DCMAKE_RANLIB=${CMAKE_RANLIB})
endif()

# External projects are still able to override the following declarations.
# cmake command line will favor the last defined variable when a duplicate is
# encountered. This requires that `EP_COMMON_CMAKE_ARGS` is always the first
# argument.
set(EP_COMMON_CMAKE_ARGS
        ${EP_COMMON_TOOLCHAIN}
        -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
        -DCMAKE_C_FLAGS=${EP_C_FLAGS}
        -DCMAKE_C_FLAGS_${UPPERCASE_BUILD_TYPE}=${EP_C_FLAGS}
        -DCMAKE_CXX_FLAGS=${EP_CXX_FLAGS}
        -DCMAKE_CXX_FLAGS_${UPPERCASE_BUILD_TYPE}=${EP_CXX_FLAGS})

if(NOT MEGASEARCH_VERBOSE_THIRDPARTY_BUILD)
    set(EP_LOG_OPTIONS LOG_CONFIGURE 1 LOG_BUILD 1 LOG_INSTALL 1 LOG_DOWNLOAD 1)
else()
    set(EP_LOG_OPTIONS)
endif()

# Ensure that a default make is set
if("${MAKE}" STREQUAL "")
    if(NOT MSVC)
        find_program(MAKE make)
    endif()
endif()

155 156 157 158 159 160 161 162 163 164
set(MAKE_BUILD_ARGS "-j4")

## Using make -j in sub-make is fragile
## see discussion https://github.com/apache/MEGASEARCH/pull/2779
#if(${CMAKE_GENERATOR} MATCHES "Makefiles")
#    set(MAKE_BUILD_ARGS "")
#else()
#    # limit the maximum number of jobs for ninja
#    set(MAKE_BUILD_ARGS "-j4")
#endif()
Z
zhiru 已提交
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222

# ----------------------------------------------------------------------
# Find pthreads

set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)

# ----------------------------------------------------------------------
# Versions and URLs for toolchain builds, which also can be used to configure
# offline builds

# Read toolchain versions from cpp/thirdparty/versions.txt
set(THIRDPARTY_DIR "${MEGASEARCH_SOURCE_DIR}/thirdparty")
file(STRINGS "${THIRDPARTY_DIR}/versions.txt" TOOLCHAIN_VERSIONS_TXT)
foreach(_VERSION_ENTRY ${TOOLCHAIN_VERSIONS_TXT})
    # Exclude comments
    if(NOT _VERSION_ENTRY MATCHES "^[^#][A-Za-z0-9-_]+_VERSION=")
        continue()
    endif()

    string(REGEX MATCH "^[^=]*" _LIB_NAME ${_VERSION_ENTRY})
    string(REPLACE "${_LIB_NAME}=" "" _LIB_VERSION ${_VERSION_ENTRY})

    # Skip blank or malformed lines
    if(${_LIB_VERSION} STREQUAL "")
        continue()
    endif()

    # For debugging
    #message(STATUS "${_LIB_NAME}: ${_LIB_VERSION}")

    set(${_LIB_NAME} "${_LIB_VERSION}")
endforeach()

if(DEFINED ENV{MEGASEARCH_BOOST_URL})
    set(BOOST_SOURCE_URL "$ENV{MEGASEARCH_BOOST_URL}")
else()
    string(REPLACE "." "_" BOOST_VERSION_UNDERSCORES ${BOOST_VERSION})
    set(BOOST_SOURCE_URL
            "https://dl.bintray.com/boostorg/release/${BOOST_VERSION}/source/boost_${BOOST_VERSION_UNDERSCORES}.tar.gz"
    )
endif()

if(DEFINED ENV{MEGASEARCH_BZIP2_URL})
    set(BZIP2_SOURCE_URL "$ENV{MEGASEARCH_BZIP2_URL}")
else()
    set(BZIP2_SOURCE_URL "https://fossies.org/linux/misc/bzip2-${BZIP2_VERSION}.tar.gz")
endif()

if(DEFINED ENV{MEGASEARCH_EASYLOGGINGPP_URL})
    set(EASYLOGGINGPP_SOURCE_URL "$ENV{MEGASEARCH_EASYLOGGINGPP_URL}")
else()
    set(EASYLOGGINGPP_SOURCE_URL "https://github.com/zuhd-org/easyloggingpp/archive/${EASYLOGGINGPP_VERSION}.tar.gz")
endif()

if(DEFINED ENV{MEGASEARCH_FAISS_URL})
    set(FAISS_SOURCE_URL "$ENV{MEGASEARCH_FAISS_URL}")
else()
223
    set(FAISS_SOURCE_URL "https://github.com/JinHai-CN/faiss/archive/${FAISS_VERSION}.tar.gz")
Z
zhiru 已提交
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
endif()

if (DEFINED ENV{MEGASEARCH_GTEST_URL})
    set(GTEST_SOURCE_URL "$ENV{MEGASEARCH_GTEST_URL}")
else ()
    set(GTEST_SOURCE_URL
            "https://github.com/google/googletest/archive/release-${GTEST_VERSION}.tar.gz")
endif()

if(DEFINED ENV{MEGASEARCH_LAPACK_URL})
    set(LAPACK_SOURCE_URL "$ENV{MEGASEARCH_LAPACK_URL}")
else()
    set(LAPACK_SOURCE_URL "https://github.com/Reference-LAPACK/lapack/archive/${LAPACK_VERSION}.tar.gz")
endif()

if(DEFINED ENV{MEGASEARCH_LZ4_URL})
    set(LZ4_SOURCE_URL "$ENV{MEGASEARCH_LZ4_URL}")
else()
    set(LZ4_SOURCE_URL "https://github.com/lz4/lz4/archive/${LZ4_VERSION}.tar.gz")
endif()

if (DEFINED ENV{MEGASEARCH_OPENBLAS_URL})
    set(OPENBLAS_SOURCE_URL "$ENV{MEGASEARCH_OPENBLAS_URL}")
else ()
    set(OPENBLAS_SOURCE_URL
            "https://github.com/xianyi/OpenBLAS/archive/${OPENBLAS_VERSION}.tar.gz")
endif()

Z
zhiru 已提交
252 253 254 255 256 257 258
if (DEFINED ENV{MEGASEARCH_PROMETHEUS_URL})
    set(PROMETHEUS_SOURCE_URL "$ENV{PROMETHEUS_OPENBLAS_URL}")
else ()
    set(PROMETHEUS_SOURCE_URL
            "https://github.com/JinHai-CN/prometheus-cpp/archive/${PROMETHEUS_VERSION}.tar.gz")
endif()

Z
zhiru 已提交
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
if (DEFINED ENV{MEGASEARCH_ROCKSDB_URL})
    set(ROCKSDB_SOURCE_URL "$ENV{MEGASEARCH_ROCKSDB_URL}")
else ()
    set(ROCKSDB_SOURCE_URL
            "https://github.com/facebook/rocksdb/archive/${ROCKSDB_VERSION}.tar.gz")
endif()

if(DEFINED ENV{MEGASEARCH_SNAPPY_URL})
    set(SNAPPY_SOURCE_URL "$ENV{MEGASEARCH_SNAPPY_URL}")
else()
    set(SNAPPY_SOURCE_URL
            "https://github.com/google/snappy/archive/${SNAPPY_VERSION}.tar.gz")
endif()

if(DEFINED ENV{MEGASEARCH_SQLITE_URL})
    set(SQLITE_SOURCE_URL "$ENV{MEGASEARCH_SQLITE_URL}")
else()
    set(SQLITE_SOURCE_URL
            "https://www.sqlite.org/2019/sqlite-autoconf-${SQLITE_VERSION}.tar.gz")
endif()

if(DEFINED ENV{MEGASEARCH_SQLITE_ORM_URL})
    set(SQLITE_ORM_SOURCE_URL "$ENV{MEGASEARCH_SQLITE_ORM_URL}")
else()
    set(SQLITE_ORM_SOURCE_URL
            "https://github.com/fnc12/sqlite_orm/archive/${SQLITE_ORM_VERSION}.zip")
endif()

if(DEFINED ENV{MEGASEARCH_THRIFT_URL})
    set(THRIFT_SOURCE_URL "$ENV{MEGASEARCH_THRIFT_URL}")
else()
    set(THRIFT_SOURCE_URL
J
jinhai 已提交
291
            "https://github.com/apache/thrift/archive/${THRIFT_VERSION}.tar.gz")
Z
zhiru 已提交
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
endif()

if(DEFINED ENV{MEGASEARCH_YAMLCPP_URL})
    set(YAMLCPP_SOURCE_URL "$ENV{MEGASEARCH_YAMLCPP_URL}")
else()
    set(YAMLCPP_SOURCE_URL "https://github.com/jbeder/yaml-cpp/archive/yaml-cpp-${YAMLCPP_VERSION}.tar.gz")
endif()

if(DEFINED ENV{MEGASEARCH_ZLIB_URL})
    set(ZLIB_SOURCE_URL "$ENV{MEGASEARCH_ZLIB_URL}")
else()
    set(ZLIB_SOURCE_URL "https://github.com/madler/zlib/archive/${ZLIB_VERSION}.tar.gz")
endif()

if(DEFINED ENV{MEGASEARCH_ZSTD_URL})
    set(ZSTD_SOURCE_URL "$ENV{MEGASEARCH_ZSTD_URL}")
else()
    set(ZSTD_SOURCE_URL "https://github.com/facebook/zstd/archive/${ZSTD_VERSION}.tar.gz")
endif()

# ----------------------------------------------------------------------
# Add Boost dependencies (code adapted from Apache Kudu (incubating))

set(Boost_USE_MULTITHREADED ON)
if(MSVC AND MEGASEARCH_USE_STATIC_CRT)
    set(Boost_USE_STATIC_RUNTIME ON)
endif()
set(Boost_ADDITIONAL_VERSIONS
        "1.70.0"
        "1.70"
        "1.69.0"
        "1.69"
        "1.68.0"
        "1.68"
        "1.67.0"
        "1.67"
        "1.66.0"
        "1.66"
        "1.65.0"
        "1.65"
        "1.64.0"
        "1.64"
        "1.63.0"
        "1.63"
        "1.62.0"
        "1.61"
        "1.61.0"
        "1.62"
        "1.60.0"
        "1.60")

if(MEGASEARCH_BOOST_VENDORED)
    set(BOOST_PREFIX "${CMAKE_CURRENT_BINARY_DIR}/boost_ep-prefix/src/boost_ep")
    set(BOOST_LIB_DIR "${BOOST_PREFIX}/stage/lib")
    set(BOOST_BUILD_LINK "static")
    set(BOOST_STATIC_SYSTEM_LIBRARY
            "${BOOST_LIB_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}boost_system${CMAKE_STATIC_LIBRARY_SUFFIX}"
    )
    set(BOOST_STATIC_FILESYSTEM_LIBRARY
            "${BOOST_LIB_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}boost_filesystem${CMAKE_STATIC_LIBRARY_SUFFIX}"
    )
    set(BOOST_STATIC_SERIALIZATION_LIBRARY
            "${BOOST_LIB_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}boost_serialization${CMAKE_STATIC_LIBRARY_SUFFIX}"
    )
    set(BOOST_SYSTEM_LIBRARY boost_system_static)
    set(BOOST_FILESYSTEM_LIBRARY boost_filesystem_static)
    set(BOOST_SERIALIZATION_LIBRARY boost_serialization_static)

    if(MEGASEARCH_BOOST_HEADER_ONLY)
        set(BOOST_BUILD_PRODUCTS)
        set(BOOST_CONFIGURE_COMMAND "")
        set(BOOST_BUILD_COMMAND "")
J
jinhai 已提交
364
    else()
Z
zhiru 已提交
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
        set(BOOST_BUILD_PRODUCTS ${BOOST_STATIC_SYSTEM_LIBRARY}
                ${BOOST_STATIC_FILESYSTEM_LIBRARY} ${BOOST_STATIC_SERIALIZATION_LIBRARY})
        set(BOOST_CONFIGURE_COMMAND "./bootstrap.sh" "--prefix=${BOOST_PREFIX}"
                "--with-libraries=filesystem,serialization,system")
        if("${CMAKE_BUILD_TYPE}" STREQUAL "DEBUG")
            set(BOOST_BUILD_VARIANT "debug")
        else()
            set(BOOST_BUILD_VARIANT "release")
        endif()
        set(BOOST_BUILD_COMMAND
                "./b2"
                "link=${BOOST_BUILD_LINK}"
                "variant=${BOOST_BUILD_VARIANT}"
                "cxxflags=-fPIC")

        add_thirdparty_lib(boost_system STATIC_LIB "${BOOST_STATIC_SYSTEM_LIBRARY}")

        add_thirdparty_lib(boost_filesystem STATIC_LIB "${BOOST_STATIC_FILESYSTEM_LIBRARY}")

        add_thirdparty_lib(boost_serialization STATIC_LIB "${BOOST_STATIC_SERIALIZATION_LIBRARY}")

        set(MEGASEARCH_BOOST_LIBS ${BOOST_SYSTEM_LIBRARY} ${BOOST_FILESYSTEM_LIBRARY} ${BOOST_STATIC_SERIALIZATION_LIBRARY})
J
jinhai 已提交
387
    endif()
Z
zhiru 已提交
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
    externalproject_add(boost_ep
            URL
            ${BOOST_SOURCE_URL}
            BUILD_BYPRODUCTS
            ${BOOST_BUILD_PRODUCTS}
            BUILD_IN_SOURCE
            1
            CONFIGURE_COMMAND
            ${BOOST_CONFIGURE_COMMAND}
            BUILD_COMMAND
            ${BOOST_BUILD_COMMAND}
            INSTALL_COMMAND
            ""
            ${EP_LOG_OPTIONS})
    set(Boost_INCLUDE_DIR "${BOOST_PREFIX}")
    set(Boost_INCLUDE_DIRS "${BOOST_INCLUDE_DIR}")
    add_dependencies(boost_system_static boost_ep)
    add_dependencies(boost_filesystem_static boost_ep)
    add_dependencies(boost_serialization_static boost_ep)
J
jinhai 已提交
407

Z
zhiru 已提交
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
else()
    if(MSVC)
        # disable autolinking in boost
        add_definitions(-DBOOST_ALL_NO_LIB)
    endif()

#    if(DEFINED ENV{BOOST_ROOT} OR DEFINED BOOST_ROOT)
#        # In older versions of CMake (such as 3.2), the system paths for Boost will
#        # be looked in first even if we set $BOOST_ROOT or pass -DBOOST_ROOT
#        set(Boost_NO_SYSTEM_PATHS ON)
#    endif()

    if(MEGASEARCH_BOOST_USE_SHARED)
        # Find shared Boost libraries.
        set(Boost_USE_STATIC_LIBS OFF)
        set(BUILD_SHARED_LIBS_KEEP ${BUILD_SHARED_LIBS})
        set(BUILD_SHARED_LIBS ON)

        if(MSVC)
            # force all boost libraries to dynamic link
            add_definitions(-DBOOST_ALL_DYN_LINK)
        endif()

        if(MEGASEARCH_BOOST_HEADER_ONLY)
            find_package(Boost REQUIRED)
        else()
            find_package(Boost COMPONENTS serialization system filesystem REQUIRED)
            set(BOOST_SYSTEM_LIBRARY Boost::system)
            set(BOOST_FILESYSTEM_LIBRARY Boost::filesystem)
            set(BOOST_SERIALIZATION_LIBRARY Boost::serialization)
            set(MEGASEARCH_BOOST_LIBS ${BOOST_SYSTEM_LIBRARY} ${BOOST_FILESYSTEM_LIBRARY})
        endif()
        set(BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS_KEEP})
        unset(BUILD_SHARED_LIBS_KEEP)
    else()
        # Find static boost headers and libs
        # TODO Differentiate here between release and debug builds
        set(Boost_USE_STATIC_LIBS ON)
        if(MEGASEARCH_BOOST_HEADER_ONLY)
            find_package(Boost REQUIRED)
        else()
            find_package(Boost COMPONENTS serialization system filesystem REQUIRED)
            set(BOOST_SYSTEM_LIBRARY Boost::system)
            set(BOOST_FILESYSTEM_LIBRARY Boost::filesystem)
            set(BOOST_SERIALIZATION_LIBRARY Boost::serialization)
            set(MEGASEARCH_BOOST_LIBS ${BOOST_SYSTEM_LIBRARY} ${BOOST_FILESYSTEM_LIBRARY})
        endif()
    endif()
endif()

#message(STATUS "Boost include dir: " ${Boost_INCLUDE_DIR})
#message(STATUS "Boost libraries: " ${Boost_LIBRARIES})

include_directories(SYSTEM ${Boost_INCLUDE_DIR})
link_directories(SYSTEM ${BOOST_LIB_DIR})

# ----------------------------------------------------------------------
# bzip2

macro(build_bzip2)
    message(STATUS "Building BZip2-${BZIP2_VERSION} from source")
    set(BZIP2_PREFIX "${CMAKE_CURRENT_BINARY_DIR}/bzip2_ep-prefix/src/bzip2_ep")
    set(BZIP2_INCLUDE_DIR "${BZIP2_PREFIX}/include")
    set(BZIP2_STATIC_LIB
            "${BZIP2_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}bz2${CMAKE_STATIC_LIBRARY_SUFFIX}")

    externalproject_add(bzip2_ep
                        ${EP_LOG_OPTIONS}
                        CONFIGURE_COMMAND
                        ""
                        BUILD_IN_SOURCE
                        1
                        BUILD_COMMAND
                        ${MAKE}
                        ${MAKE_BUILD_ARGS}
                        CFLAGS=${EP_C_FLAGS}
                        INSTALL_COMMAND
                        ${MAKE}
                        install
                        PREFIX=${BZIP2_PREFIX}
                        CFLAGS=${EP_C_FLAGS}
                        INSTALL_DIR
                        ${BZIP2_PREFIX}
                        URL
                        ${BZIP2_SOURCE_URL}
                        BUILD_BYPRODUCTS
                        "${BZIP2_STATIC_LIB}")

    file(MAKE_DIRECTORY "${BZIP2_INCLUDE_DIR}")
    add_library(bzip2 STATIC IMPORTED)
    set_target_properties(
            bzip2
            PROPERTIES IMPORTED_LOCATION "${BZIP2_STATIC_LIB}"
            INTERFACE_INCLUDE_DIRECTORIES "${BZIP2_INCLUDE_DIR}")

    add_dependencies(bzip2 bzip2_ep)
endmacro()

if(MEGASEARCH_WITH_BZ2)
    resolve_dependency(BZip2)

    if(NOT TARGET bzip2)
        add_library(bzip2 UNKNOWN IMPORTED)
        set_target_properties(bzip2
                PROPERTIES IMPORTED_LOCATION "${BZIP2_LIBRARIES}"
                INTERFACE_INCLUDE_DIRECTORIES "${BZIP2_INCLUDE_DIR}")
    endif()
    link_directories(SYSTEM ${BZIP2_PREFIX}/lib/)
    include_directories(SYSTEM "${BZIP2_INCLUDE_DIR}")
endif()

# ----------------------------------------------------------------------
# Easylogging++

macro(build_easyloggingpp)
    message(STATUS "Building Easylogging++-${EASYLOGGINGPP_VERSION} from source")
    set(EASYLOGGINGPP_PREFIX "${CMAKE_CURRENT_BINARY_DIR}/easyloggingpp_ep-prefix/src/easyloggingpp_ep")
    set(EASYLOGGINGPP_INCLUDE_DIR "${EASYLOGGINGPP_PREFIX}/include")
    set(EASYLOGGINGPP_STATIC_LIB
            "${EASYLOGGINGPP_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}easyloggingpp${CMAKE_STATIC_LIBRARY_SUFFIX}")

    set(EASYLOGGINGPP_CMAKE_ARGS
            ${EP_COMMON_CMAKE_ARGS}
            "-DCMAKE_INSTALL_PREFIX=${EASYLOGGINGPP_PREFIX}"
            -DCMAKE_INSTALL_LIBDIR=lib
            -Dtest=OFF
            -Dbuild_static_lib=ON)

    externalproject_add(easyloggingpp_ep
            URL
            ${EASYLOGGINGPP_SOURCE_URL}
            ${EP_LOG_OPTIONS}
            CMAKE_ARGS
            ${EASYLOGGINGPP_CMAKE_ARGS}
542 543 544
            BUILD_COMMAND
            ${MAKE}
            ${MAKE_BUILD_ARGS}
Z
zhiru 已提交
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
            BUILD_BYPRODUCTS
            ${EASYLOGGINGPP_STATIC_LIB})

    file(MAKE_DIRECTORY "${EASYLOGGINGPP_INCLUDE_DIR}")
    add_library(easyloggingpp STATIC IMPORTED)
    set_target_properties(
            easyloggingpp
            PROPERTIES IMPORTED_LOCATION "${EASYLOGGINGPP_STATIC_LIB}"
            INTERFACE_INCLUDE_DIRECTORIES "${EASYLOGGINGPP_INCLUDE_DIR}")

    add_dependencies(easyloggingpp easyloggingpp_ep)
endmacro()

if(MEGASEARCH_WITH_EASYLOGGINGPP)
    resolve_dependency(Easylogging++)

    get_target_property(EASYLOGGINGPP_INCLUDE_DIR easyloggingpp INTERFACE_INCLUDE_DIRECTORIES)
Z
zhiru 已提交
562
    link_directories(SYSTEM "${EASYLOGGINGPP_PREFIX}/lib")
Z
zhiru 已提交
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
    include_directories(SYSTEM "${EASYLOGGINGPP_INCLUDE_DIR}")
endif()

# ----------------------------------------------------------------------
# OpenBLAS

macro(build_openblas)
    message(STATUS "Building OpenBLAS-${OPENBLAS_VERSION} from source")
    set(OPENBLAS_PREFIX "${CMAKE_CURRENT_BINARY_DIR}/openblas_ep-prefix/src/openblas_ep")
    set(OPENBLAS_INCLUDE_DIR "${OPENBLAS_PREFIX}/include")
    set(OPENBLAS_STATIC_LIB
            "${OPENBLAS_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}openblas${CMAKE_STATIC_LIBRARY_SUFFIX}")

    externalproject_add(openblas_ep
            URL
            ${OPENBLAS_SOURCE_URL}
            ${EP_LOG_OPTIONS}
            CONFIGURE_COMMAND
            ""
            BUILD_IN_SOURCE
            1
            BUILD_COMMAND
585 586
            ${MAKE}
            ${MAKE_BUILD_ARGS}
Z
zhiru 已提交
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
            INSTALL_COMMAND
            ${MAKE}
            PREFIX=${OPENBLAS_PREFIX}
            install
            BUILD_BYPRODUCTS
            ${OPENBLAS_STATIC_LIB})

    file(MAKE_DIRECTORY "${OPENBLAS_INCLUDE_DIR}")
    add_library(openblas STATIC IMPORTED)
    set_target_properties(
            openblas
            PROPERTIES IMPORTED_LOCATION "${OPENBLAS_STATIC_LIB}"
            INTERFACE_INCLUDE_DIRECTORIES "${OPENBLAS_INCLUDE_DIR}")

    add_dependencies(openblas openblas_ep)
endmacro()

#if(MEGASEARCH_WITH_OPENBLAS)
#    resolve_dependency(OpenBLAS)
#
#    get_target_property(OPENBLAS_INCLUDE_DIR openblas INTERFACE_INCLUDE_DIRECTORIES)
#    include_directories(SYSTEM "${OPENBLAS_INCLUDE_DIR}")
#endif()

# ----------------------------------------------------------------------
# LAPACK

macro(build_lapack)
    message(STATUS "Building LAPACK-${LAPACK_VERSION} from source")
    set(LAPACK_PREFIX "${CMAKE_CURRENT_BINARY_DIR}/lapack_ep-prefix/src/lapack_ep")
    set(LAPACK_INCLUDE_DIR "${LAPACK_PREFIX}/include")
    set(LAPACK_STATIC_LIB
            "${LAPACK_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}lapack${CMAKE_STATIC_LIBRARY_SUFFIX}")

    set(LAPACK_CMAKE_ARGS
            ${EP_COMMON_CMAKE_ARGS}
            "-DCMAKE_INSTALL_PREFIX=${LAPACK_PREFIX}"
            -DCMAKE_INSTALL_LIBDIR=lib)

    externalproject_add(lapack_ep
            URL
            ${LAPACK_SOURCE_URL}
            ${EP_LOG_OPTIONS}
            CMAKE_ARGS
            ${LAPACK_CMAKE_ARGS}
632 633 634
            BUILD_COMMAND
            ${MAKE}
            ${MAKE_BUILD_ARGS}
Z
zhiru 已提交
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674
            BUILD_BYPRODUCTS
            ${LAPACK_STATIC_LIB})

    file(MAKE_DIRECTORY "${LAPACK_INCLUDE_DIR}")
    add_library(lapack STATIC IMPORTED)
    set_target_properties(
            lapack
            PROPERTIES IMPORTED_LOCATION "${LAPACK_STATIC_LIB}"
            INTERFACE_INCLUDE_DIRECTORIES "${LAPACK_INCLUDE_DIR}")

    add_dependencies(lapack lapack_ep)
endmacro()

#if(MEGASEARCH_WITH_LAPACK)
#    resolve_dependency(LAPACK)
#
#    get_target_property(LAPACK_INCLUDE_DIR lapack INTERFACE_INCLUDE_DIRECTORIES)
#    include_directories(SYSTEM "${LAPACK_INCLUDE_DIR}")
#endif()

# ----------------------------------------------------------------------
# FAISS

macro(build_faiss)
    message(STATUS "Building FAISS-${FAISS_VERSION} from source")
    set(FAISS_PREFIX "${CMAKE_CURRENT_BINARY_DIR}/faiss_ep-prefix/src/faiss_ep")
    set(FAISS_INCLUDE_DIR "${FAISS_PREFIX}/include")
    set(FAISS_STATIC_LIB
            "${FAISS_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}faiss${CMAKE_STATIC_LIBRARY_SUFFIX}")

#    add_custom_target(faiss_dependencies)
#    add_dependencies(faiss_dependencies openblas_ep)
#    add_dependencies(faiss_dependencies openblas)
#    get_target_property(FAISS_OPENBLAS_LIB_DIR openblas IMPORTED_LOCATION)
#    get_filename_component(FAISS_OPENBLAS_LIB "${FAISS_OPENBLAS_LIB_DIR}" DIRECTORY)

    set(FAISS_CONFIGURE_ARGS
            "--prefix=${FAISS_PREFIX}"
            "CFLAGS=${EP_C_FLAGS}"
            "CXXFLAGS=${EP_CXX_FLAGS}"
Z
zhiru 已提交
675
            "LDFLAGS=-L${OPENBLAS_PREFIX}/lib -L${LAPACK_PREFIX}/lib -lopenblas -llapack"
Z
zhiru 已提交
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705
            --without-python)

#    if(OPENBLAS_STATIC_LIB)
#        set(OPENBLAS_LIBRARY ${OPENBLAS_STATIC_LIB})
#    else()
#        set(OPENBLAS_LIBRARY ${OPENBLAS_SHARED_LIB})
#    endif()
#    set(FAISS_DEPENDENCIES ${FAISS_DEPENDENCIES} ${OPENBLAS_LIBRARY})

    if(${MEGASEARCH_WITH_FAISS_GPU_VERSION} STREQUAL "ON")
        set(FAISS_CONFIGURE_ARGS ${FAISS_CONFIGURE_ARGS}
                "--with-cuda=${CUDA_TOOLKIT_ROOT_DIR}"
                "--with-cuda-arch=${MEGASEARCH_FAISS_GPU_ARCH}")
    else()
        set(FAISS_CONFIGURE_ARGS ${FAISS_CONFIGURE_ARGS} --without-cuda)
    endif()

    externalproject_add(faiss_ep
            URL
            ${FAISS_SOURCE_URL}
            ${EP_LOG_OPTIONS}
            CONFIGURE_COMMAND
            "./configure"
            ${FAISS_CONFIGURE_ARGS}
#            BINARY_DIR
#            ${FAISS_PREFIX}
#            INSTALL_DIR
#            ${FAISS_PREFIX}
#            BUILD_COMMAND
#            ${MAKE} ${MAKE_BUILD_ARGS}
706 707
            BUILD_COMMAND
            ${MAKE}
J
jinhai 已提交
708
            ${MAKE_BUILD_ARGS} all
Z
zhiru 已提交
709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
            BUILD_IN_SOURCE
            1
#            INSTALL_DIR
#            ${FAISS_PREFIX}
#            INSTALL_COMMAND
#            ""
            BUILD_BYPRODUCTS
            ${FAISS_STATIC_LIB})
#            DEPENDS
#            ${faiss_dependencies})
    ExternalProject_Add_StepDependencies(faiss_ep build openblas_ep)
    ExternalProject_Add_StepDependencies(faiss_ep build lapack_ep)

    file(MAKE_DIRECTORY "${FAISS_INCLUDE_DIR}")
    add_library(faiss STATIC IMPORTED)
    set_target_properties(
            faiss
            PROPERTIES IMPORTED_LOCATION "${FAISS_STATIC_LIB}"
            INTERFACE_INCLUDE_DIRECTORIES "${FAISS_INCLUDE_DIR}")

    add_dependencies(faiss faiss_ep)
endmacro()

if(MEGASEARCH_WITH_FAISS)

    resolve_dependency(OpenBLAS)
    get_target_property(OPENBLAS_INCLUDE_DIR openblas INTERFACE_INCLUDE_DIRECTORIES)
    include_directories(SYSTEM "${OPENBLAS_INCLUDE_DIR}")
    link_directories(SYSTEM ${OPENBLAS_PREFIX}/lib)

    resolve_dependency(LAPACK)
    get_target_property(LAPACK_INCLUDE_DIR lapack INTERFACE_INCLUDE_DIRECTORIES)
    include_directories(SYSTEM "${LAPACK_INCLUDE_DIR}")
Z
zhiru 已提交
742
    link_directories(SYSTEM "${LAPACK_PREFIX}/lib")
Z
zhiru 已提交
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776

    resolve_dependency(FAISS)
    get_target_property(FAISS_INCLUDE_DIR faiss INTERFACE_INCLUDE_DIRECTORIES)
    include_directories(SYSTEM "${FAISS_INCLUDE_DIR}")
    link_directories(SYSTEM ${FAISS_PREFIX}/lib/)
endif()

# ----------------------------------------------------------------------
# Google gtest

macro(build_gtest)
    message(STATUS "Building gtest-${GTEST_VERSION} from source")
    set(GTEST_VENDORED TRUE)
    set(GTEST_CMAKE_CXX_FLAGS "${EP_CXX_FLAGS}")

    if(APPLE)
        set(GTEST_CMAKE_CXX_FLAGS
                ${GTEST_CMAKE_CXX_FLAGS}
                -DGTEST_USE_OWN_TR1_TUPLE=1
                -Wno-unused-value
                -Wno-ignored-attributes)
    endif()

    set(GTEST_PREFIX "${CMAKE_CURRENT_BINARY_DIR}/googletest_ep-prefix/src/googletest_ep")
    set(GTEST_INCLUDE_DIR "${GTEST_PREFIX}/include")
    set(GTEST_STATIC_LIB
            "${GTEST_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}gtest${CMAKE_STATIC_LIBRARY_SUFFIX}")
    set(GTEST_MAIN_STATIC_LIB
            "${GTEST_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}gtest_main${CMAKE_STATIC_LIBRARY_SUFFIX}")

    set(GTEST_CMAKE_ARGS
            ${EP_COMMON_CMAKE_ARGS}
            "-DCMAKE_INSTALL_PREFIX=${GTEST_PREFIX}"
            "-DCMAKE_INSTALL_LIBDIR=lib"
Z
zhiru 已提交
777 778 779 780 781 782 783 784
            -DCMAKE_CXX_FLAGS=${GTEST_CMAKE_CXX_FLAGS}
            -DCMAKE_BUILD_TYPE=Release)

    set(GMOCK_INCLUDE_DIR "${GTEST_PREFIX}/include")
    set(GMOCK_STATIC_LIB
            "${GTEST_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}gmock${CMAKE_STATIC_LIBRARY_SUFFIX}"
    )

Z
zhiru 已提交
785 786 787 788

    ExternalProject_Add(googletest_ep
                        URL
                        ${GTEST_SOURCE_URL}
789 790 791
                        BUILD_COMMAND
                        ${MAKE}
                        ${MAKE_BUILD_ARGS}
Z
zhiru 已提交
792 793 794
                        BUILD_BYPRODUCTS
                        ${GTEST_STATIC_LIB}
                        ${GTEST_MAIN_STATIC_LIB}
Z
zhiru 已提交
795
                        ${GMOCK_STATIC_LIB}
Z
zhiru 已提交
796 797 798 799 800 801
                        CMAKE_ARGS
                        ${GTEST_CMAKE_ARGS}
                        ${EP_LOG_OPTIONS})

    # The include directory must exist before it is referenced by a target.
    file(MAKE_DIRECTORY "${GTEST_INCLUDE_DIR}")
J
jinhai 已提交
802 803

    add_library(gtest STATIC IMPORTED)
Z
zhiru 已提交
804 805 806
    set_target_properties(gtest
                            PROPERTIES IMPORTED_LOCATION "${GTEST_STATIC_LIB}"
                            INTERFACE_INCLUDE_DIRECTORIES "${GTEST_INCLUDE_DIR}")
J
jinhai 已提交
807 808

    add_library(gtest_main STATIC IMPORTED)
Z
zhiru 已提交
809 810 811 812
    set_target_properties(gtest_main
                            PROPERTIES IMPORTED_LOCATION "${GTEST_MAIN_STATIC_LIB}"
                            INTERFACE_INCLUDE_DIRECTORIES "${GTEST_INCLUDE_DIR}")

Z
zhiru 已提交
813 814 815 816 817
    add_library(gmock STATIC IMPORTED)
    set_target_properties(gmock
            PROPERTIES IMPORTED_LOCATION "${GMOCK_STATIC_LIB}"
            INTERFACE_INCLUDE_DIRECTORIES "${GTEST_INCLUDE_DIR}")

Z
zhiru 已提交
818 819
    add_dependencies(gtest googletest_ep)
    add_dependencies(gtest_main googletest_ep)
Z
zhiru 已提交
820
    add_dependencies(gmock googletest_ep)
Z
zhiru 已提交
821 822 823 824 825 826 827 828 829 830 831 832

endmacro()

if (MEGASEARCH_BUILD_TESTS)
    #message(STATUS "Resolving gtest dependency")
    resolve_dependency(GTest)

    if(NOT GTEST_VENDORED)
    endif()

    # TODO: Don't use global includes but rather target_include_directories
    get_target_property(GTEST_INCLUDE_DIR gtest INTERFACE_INCLUDE_DIRECTORIES)
Z
zhiru 已提交
833
    link_directories(SYSTEM "${GTEST_PREFIX}/lib")
Z
zhiru 已提交
834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910
    include_directories(SYSTEM ${GTEST_INCLUDE_DIR})
endif()

# ----------------------------------------------------------------------
# lz4

macro(build_lz4)
    message(STATUS "Building lz4-${LZ4_VERSION} from source")
    set(LZ4_BUILD_DIR "${CMAKE_CURRENT_BINARY_DIR}/lz4_ep-prefix/src/lz4_ep")
    set(LZ4_PREFIX "${CMAKE_CURRENT_BINARY_DIR}/lz4_ep-prefix/")

    if(MSVC)
        if(MEGASEARCH_USE_STATIC_CRT)
            if(${UPPERCASE_BUILD_TYPE} STREQUAL "DEBUG")
                set(LZ4_RUNTIME_LIBRARY_LINKAGE "/p:RuntimeLibrary=MultiThreadedDebug")
            else()
                set(LZ4_RUNTIME_LIBRARY_LINKAGE "/p:RuntimeLibrary=MultiThreaded")
            endif()
        endif()
        set(LZ4_STATIC_LIB
                "${LZ4_BUILD_DIR}/visual/VS2010/bin/x64_${CMAKE_BUILD_TYPE}/liblz4_static.lib")
        set(LZ4_BUILD_COMMAND
                BUILD_COMMAND
                msbuild.exe
                /m
                /p:Configuration=${CMAKE_BUILD_TYPE}
                /p:Platform=x64
                /p:PlatformToolset=v140
                ${LZ4_RUNTIME_LIBRARY_LINKAGE}
                /t:Build
                ${LZ4_BUILD_DIR}/visual/VS2010/lz4.sln)
    else()
        set(LZ4_STATIC_LIB "${LZ4_BUILD_DIR}/lib/liblz4.a")
        #set(LZ4_BUILD_COMMAND BUILD_COMMAND ${CMAKE_SOURCE_DIR}/build-support/build-lz4-lib.sh
        #        "AR=${CMAKE_AR}")
        set(LZ4_BUILD_COMMAND BUILD_COMMAND ${MAKE} ${MAKE_BUILD_ARGS} CFLAGS=${EP_C_FLAGS})
    endif()

    # We need to copy the header in lib to directory outside of the build
    externalproject_add(lz4_ep
            URL
            ${LZ4_SOURCE_URL}
            ${EP_LOG_OPTIONS}
            UPDATE_COMMAND
            ${CMAKE_COMMAND}
            -E
            copy_directory
            "${LZ4_BUILD_DIR}/lib"
            "${LZ4_PREFIX}/include"
            ${LZ4_PATCH_COMMAND}
            CONFIGURE_COMMAND
            ""
            INSTALL_COMMAND
            ""
            BINARY_DIR
            ${LZ4_BUILD_DIR}
            BUILD_BYPRODUCTS
            ${LZ4_STATIC_LIB}
            ${LZ4_BUILD_COMMAND})

    file(MAKE_DIRECTORY "${LZ4_PREFIX}/include")
    add_library(lz4 STATIC IMPORTED)
    set_target_properties(lz4
            PROPERTIES IMPORTED_LOCATION "${LZ4_STATIC_LIB}"
            INTERFACE_INCLUDE_DIRECTORIES "${LZ4_PREFIX}/include")
    add_dependencies(lz4 lz4_ep)
endmacro()

if(MEGASEARCH_WITH_LZ4)
    resolve_dependency(Lz4)

    # TODO: Don't use global includes but rather target_include_directories
    get_target_property(LZ4_INCLUDE_DIR lz4 INTERFACE_INCLUDE_DIRECTORIES)
    link_directories(SYSTEM ${LZ4_BUILD_DIR}/lib/)
    include_directories(SYSTEM ${LZ4_INCLUDE_DIR})
endif()

Z
zhiru 已提交
911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953
# ----------------------------------------------------------------------
# Prometheus

macro(build_prometheus)
    message(STATUS "Building Prometheus-${PROMETHEUS_VERSION} from source")
    set(PROMETHEUS_PREFIX "${CMAKE_CURRENT_BINARY_DIR}/prometheus_ep-prefix/src/prometheus_ep")
    set(PROMETHEUS_STATIC_LIB_NAME prometheus-cpp)
    set(PROMETHEUS_CORE_STATIC_LIB
            "${PROMETHEUS_PREFIX}/core/${CMAKE_STATIC_LIBRARY_PREFIX}${PROMETHEUS_STATIC_LIB_NAME}-core${CMAKE_STATIC_LIBRARY_SUFFIX}"
            )
    set(PROMETHEUS_PUSH_STATIC_LIB
            "${PROMETHEUS_PREFIX}/push/${CMAKE_STATIC_LIBRARY_PREFIX}${PROMETHEUS_STATIC_LIB_NAME}-push${CMAKE_STATIC_LIBRARY_SUFFIX}"
            )
    set(PROMETHEUS_PULL_STATIC_LIB
            "${PROMETHEUS_PREFIX}/pull/${CMAKE_STATIC_LIBRARY_PREFIX}${PROMETHEUS_STATIC_LIB_NAME}-pull${CMAKE_STATIC_LIBRARY_SUFFIX}"
            )

    set(PROMETHEUS_CMAKE_ARGS
            ${EP_COMMON_CMAKE_ARGS}
            -DCMAKE_INSTALL_LIBDIR=lib
            -DBUILD_SHARED_LIBS=OFF
            "-DCMAKE_INSTALL_PREFIX=${PROMETHEUS_PREFIX}")

    externalproject_add(prometheus_ep
            URL
            ${PROMETHEUS_SOURCE_URL}
            ${EP_LOG_OPTIONS}
            CMAKE_ARGS
            ${PROMETHEUS_CMAKE_ARGS}
            BUILD_COMMAND
            ${MAKE}
            ${MAKE_BUILD_ARGS}
            BUILD_IN_SOURCE
            1
            INSTALL_COMMAND
            ${MAKE}
            "DESTDIR=${PROMETHEUS_PREFIX}"
            install
            BUILD_BYPRODUCTS
            "${PROMETHEUS_CORE_STATIC_LIB}"
            "${PROMETHEUS_PUSH_STATIC_LIB}"
            "${PROMETHEUS_PULL_STATIC_LIB}")

954
    file(MAKE_DIRECTORY "${PROMETHEUS_PREFIX}/push/include")
Z
zhiru 已提交
955 956
    add_library(prometheus-cpp-push STATIC IMPORTED)
    set_target_properties(prometheus-cpp-push
957 958
            PROPERTIES IMPORTED_LOCATION "${PROMETHEUS_PUSH_STATIC_LIB}"
            INTERFACE_INCLUDE_DIRECTORIES "${PROMETHEUS_PREFIX}/push/include")
Z
zhiru 已提交
959 960
    add_dependencies(prometheus-cpp-push prometheus_ep)

961
    file(MAKE_DIRECTORY "${PROMETHEUS_PREFIX}/pull/include")
Z
zhiru 已提交
962 963
    add_library(prometheus-cpp-pull STATIC IMPORTED)
    set_target_properties(prometheus-cpp-pull
964 965
            PROPERTIES IMPORTED_LOCATION "${PROMETHEUS_PULL_STATIC_LIB}"
            INTERFACE_INCLUDE_DIRECTORIES "${PROMETHEUS_PREFIX}/pull/include")
Z
zhiru 已提交
966 967
    add_dependencies(prometheus-cpp-pull prometheus_ep)

968
    file(MAKE_DIRECTORY "${PROMETHEUS_PREFIX}/core/include")
Z
zhiru 已提交
969 970
    add_library(prometheus-cpp-core STATIC IMPORTED)
    set_target_properties(prometheus-cpp-core
971 972
            PROPERTIES IMPORTED_LOCATION "${PROMETHEUS_CORE_STATIC_LIB}"
            INTERFACE_INCLUDE_DIRECTORIES "${PROMETHEUS_PREFIX}/core/include")
Z
zhiru 已提交
973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992
    add_dependencies(prometheus-cpp-core prometheus_ep)
endmacro()

if(MEGASEARCH_WITH_PROMETHEUS)

    resolve_dependency(Prometheus)

    # TODO: Don't use global includes but rather target_include_directories
    #get_target_property(PROMETHEUS-core_INCLUDE_DIRS prometheus-core INTERFACE_INCLUDE_DIRECTORIES)

    #get_target_property(PROMETHEUS_PUSH_INCLUDE_DIRS prometheus_push INTERFACE_INCLUDE_DIRECTORIES)
    link_directories(SYSTEM ${PROMETHEUS_PREFIX}/push/)
    include_directories(SYSTEM ${PROMETHEUS_PREFIX}/push/include)

    #get_target_property(PROMETHEUS_PULL_INCLUDE_DIRS prometheus_pull INTERFACE_INCLUDE_DIRECTORIES)
    link_directories(SYSTEM ${PROMETHEUS_PREFIX}/pull/)
    include_directories(SYSTEM ${PROMETHEUS_PREFIX}/pull/include)

    link_directories(SYSTEM ${PROMETHEUS_PREFIX}/core/)
    include_directories(SYSTEM ${PROMETHEUS_PREFIX}/core/include)
Y
yu yunfeng 已提交
993 994

    link_directories(${PROMETHEUS_PREFIX}/civetweb_ep-prefix/src/civetweb_ep)
Z
zhiru 已提交
995 996
endif()

Z
zhiru 已提交
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
# ----------------------------------------------------------------------
# RocksDB

macro(build_rocksdb)
    message(STATUS "Building RocksDB-${ROCKSDB_VERSION} from source")
    set(ROCKSDB_PREFIX "${CMAKE_CURRENT_BINARY_DIR}/rocksdb_ep-prefix/src/rocksdb_ep")
    set(ROCKSDB_INCLUDE_DIRS "${ROCKSDB_PREFIX}/include")
    set(ROCKSDB_STATIC_LIB_NAME rocksdb)
    set(ROCKSDB_STATIC_LIB
            "${ROCKSDB_PREFIX}/lib/lib/${CMAKE_STATIC_LIBRARY_PREFIX}${ROCKSDB_STATIC_LIB_NAME}${CMAKE_STATIC_LIBRARY_SUFFIX}"
            )

    externalproject_add(rocksdb_ep
            URL
            ${ROCKSDB_SOURCE_URL}
            ${EP_LOG_OPTIONS}
            CONFIGURE_COMMAND
            ""
            BUILD_COMMAND
            ${MAKE}
            ${MAKE_BUILD_ARGS}
            static_lib
            "prefix=${ROCKSDB_PREFIX}"
            BUILD_IN_SOURCE
            1
            INSTALL_COMMAND
            ${MAKE}
            install-static
            "INSTALL_PATH=${ROCKSDB_PREFIX}/lib"
            BUILD_BYPRODUCTS
            "${ROCKSDB_STATIC_LIB}")

    file(MAKE_DIRECTORY "${ROCKSDB_PREFIX}/include")

    add_library(rocksdb STATIC IMPORTED)
    set_target_properties(rocksdb
            PROPERTIES IMPORTED_LOCATION "${ROCKSDB_STATIC_LIB}"
            INTERFACE_INCLUDE_DIRECTORIES "${ROCKSDB_INCLUDE_DIRS}")
    add_dependencies(rocksdb rocksdb_ep)
endmacro()

if(MEGASEARCH_WITH_ROCKSDB)
Z
zhiru 已提交
1039

Z
zhiru 已提交
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066
    resolve_dependency(RocksDB)

    # TODO: Don't use global includes but rather target_include_directories
#    get_target_property(ROCKSDB_INCLUDE_DIRS rocksdb INTERFACE_INCLUDE_DIRECTORIES)
    link_directories(SYSTEM ${ROCKSDB_PREFIX}/lib/lib/)
    include_directories(SYSTEM ${ROCKSDB_INCLUDE_DIRS})
endif()

# ----------------------------------------------------------------------
# Snappy

macro(build_snappy)
    message(STATUS "Building snappy-${SNAPPY_VERSION} from source")
    set(SNAPPY_PREFIX "${CMAKE_CURRENT_BINARY_DIR}/snappy_ep-prefix/src/snappy_ep")
    set(SNAPPY_STATIC_LIB_NAME snappy)
    set(SNAPPY_STATIC_LIB
            "${SNAPPY_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}${SNAPPY_STATIC_LIB_NAME}${CMAKE_STATIC_LIBRARY_SUFFIX}"
    )

    set(SNAPPY_CMAKE_ARGS
            ${EP_COMMON_CMAKE_ARGS}
            -DCMAKE_INSTALL_LIBDIR=lib
            -DSNAPPY_BUILD_TESTS=OFF
            "-DCMAKE_INSTALL_PREFIX=${SNAPPY_PREFIX}")

    externalproject_add(snappy_ep
                        ${EP_LOG_OPTIONS}
1067 1068 1069
                        BUILD_COMMAND
                        ${MAKE}
                        ${MAKE_BUILD_ARGS}
Z
zhiru 已提交
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212
                        BUILD_IN_SOURCE
                        1
                        INSTALL_DIR
                        ${SNAPPY_PREFIX}
                        URL
                        ${SNAPPY_SOURCE_URL}
                        CMAKE_ARGS
                        ${SNAPPY_CMAKE_ARGS}
                        BUILD_BYPRODUCTS
                        "${SNAPPY_STATIC_LIB}")

    file(MAKE_DIRECTORY "${SNAPPY_PREFIX}/include")

    add_library(snappy STATIC IMPORTED)
    set_target_properties(snappy
                        PROPERTIES IMPORTED_LOCATION "${SNAPPY_STATIC_LIB}"
                        INTERFACE_INCLUDE_DIRECTORIES
                        "${SNAPPY_PREFIX}/include")
    add_dependencies(snappy snappy_ep)
endmacro()

if(MEGASEARCH_WITH_SNAPPY)
#    if(Snappy_SOURCE STREQUAL "AUTO")
#        # Normally *Config.cmake files reside in /usr/lib/cmake but Snappy
#        # errornously places them in ${CMAKE_ROOT}/Modules/
#        # This is fixed in 1.1.7 but fedora (30) still installs into the wrong
#        # location.
#        # https://bugzilla.redhat.com/show_bug.cgi?id=1679727
#        # https://src.fedoraproject.org/rpms/snappy/pull-request/1
#        find_package(Snappy QUIET HINTS "${CMAKE_ROOT}/Modules/")
#        if(NOT Snappy_FOUND)
#            find_package(SnappyAlt)
#        endif()
#        if(NOT Snappy_FOUND AND NOT SnappyAlt_FOUND)
#            build_snappy()
#        endif()
#    elseif(Snappy_SOURCE STREQUAL "BUNDLED")
#        build_snappy()
#    elseif(Snappy_SOURCE STREQUAL "SYSTEM")
#        # SnappyConfig.cmake is not installed on Ubuntu/Debian
#        # TODO: Make a bug report upstream
#        find_package(Snappy HINTS "${CMAKE_ROOT}/Modules/")
#        if(NOT Snappy_FOUND)
#            find_package(SnappyAlt REQUIRED)
#        endif()
#    endif()

    resolve_dependency(Snappy)

    # TODO: Don't use global includes but rather target_include_directories
    get_target_property(SNAPPY_INCLUDE_DIRS snappy INTERFACE_INCLUDE_DIRECTORIES)
    link_directories(SYSTEM ${SNAPPY_PREFIX}/lib/)
    include_directories(SYSTEM ${SNAPPY_INCLUDE_DIRS})
endif()

# ----------------------------------------------------------------------
# SQLite

macro(build_sqlite)
    message(STATUS "Building SQLITE-${SQLITE_VERSION} from source")
    set(SQLITE_PREFIX "${CMAKE_CURRENT_BINARY_DIR}/sqlite_ep-prefix/src/sqlite_ep")
    set(SQLITE_INCLUDE_DIR "${SQLITE_PREFIX}/include")
    set(SQLITE_STATIC_LIB
            "${SQLITE_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}sqlite3${CMAKE_STATIC_LIBRARY_SUFFIX}")

    set(SQLITE_CONFIGURE_ARGS
            "--prefix=${SQLITE_PREFIX}"
            "CC=${CMAKE_C_COMPILER}"
            "CXX=${CMAKE_CXX_COMPILER}"
            "CFLAGS=${EP_C_FLAGS}"
            "CXXFLAGS=${EP_CXX_FLAGS}")

    externalproject_add(sqlite_ep
            URL
            ${SQLITE_SOURCE_URL}
            ${EP_LOG_OPTIONS}
            CONFIGURE_COMMAND
            "./configure"
            ${SQLITE_CONFIGURE_ARGS}
            BUILD_COMMAND
            ${MAKE}
            ${MAKE_BUILD_ARGS}
            BUILD_IN_SOURCE
            1
            BUILD_BYPRODUCTS
            "${SQLITE_STATIC_LIB}")

    file(MAKE_DIRECTORY "${SQLITE_INCLUDE_DIR}")
    add_library(sqlite STATIC IMPORTED)
    set_target_properties(
            sqlite
            PROPERTIES IMPORTED_LOCATION "${SQLITE_STATIC_LIB}"
            INTERFACE_INCLUDE_DIRECTORIES "${SQLITE_INCLUDE_DIR}")

    add_dependencies(sqlite sqlite_ep)
endmacro()

if(MEGASEARCH_WITH_SQLITE)
    resolve_dependency(SQLite)
    include_directories(SYSTEM "${SQLITE_INCLUDE_DIR}")
    link_directories(SYSTEM ${SQLITE_PREFIX}/lib/)
endif()

# ----------------------------------------------------------------------
# SQLite_ORM

macro(build_sqlite_orm)
    message(STATUS "Building SQLITE_ORM-${SQLITE_ORM_VERSION} from source")

    set(SQLITE_ORM_PREFIX "${CMAKE_CURRENT_BINARY_DIR}/sqlite_orm_ep-prefix")
    set(SQLITE_ORM_TAR_NAME "${SQLITE_ORM_PREFIX}/sqlite_orm-${SQLITE_ORM_VERSION}.tar.gz")    #sqlite_orm-${SQLITE_ORM_VERSION}.tar.gz
    if (NOT EXISTS ${SQLITE_ORM_TAR_NAME})
        file(MAKE_DIRECTORY ${SQLITE_ORM_PREFIX})
        file(DOWNLOAD https://github.com/fnc12/sqlite_orm/archive/${SQLITE_ORM_VERSION}.tar.gz
                ${SQLITE_ORM_TAR_NAME})
        execute_process(COMMAND ${CMAKE_COMMAND} -E tar -xf ${SQLITE_ORM_TAR_NAME}
                        WORKING_DIRECTORY ${SQLITE_ORM_PREFIX})

    endif ()
    set(SQLITE_ORM_INCLUDE_DIR "${SQLITE_ORM_PREFIX}/sqlite_orm-${SQLITE_ORM_VERSION}/include/sqlite_orm")

    #set(SQLITE_ORM_PREFIX "${CMAKE_CURRENT_BINARY_DIR}/sqlite_orm_ep-prefix/src/sqlite_orm_ep")
    #set(SQLITE_ORM_INCLUDE_DIR "${SQLITE_ORM_PREFIX}/include/sqlite_orm")

#    set(SQLITE_ORM_STATIC_LIB
#            "${SQLITE_ORM_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}sqlite_orm${CMAKE_STATIC_LIBRARY_SUFFIX}")
#
#    set(SQLITE_ORM_CMAKE_CXX_FLAGS "${EP_CXX_FLAGS} -std=c++14")
#    set(SQLITE_ORM_CMAKE_CXX_FLAGS_DEBUG "${EP_CXX_FLAGS} -std=c++14")
#
#    set(SQLITE_ORM_CMAKE_ARGS
#            ${EP_COMMON_CMAKE_ARGS}
#            "-DCMAKE_INSTALL_PREFIX=${SQLITE_ORM_PREFIX}"
#            #"LDFLAGS=-L${SQLITE_PREFIX}"
#            #"-DCMAKE_PREFIX_PATH=${SQLITE_PREFIX}/include"
#            "-DCMAKE_INCLUDE_PATH=${SQLITE_PREFIX}/include"
#            "-DCMAKE_CXX_FLAGS=${SQLITE_ORM_CMAKE_CXX_FLAGS}"
#            "-DCMAKE_CXX_FLAGS_DEBUG=${SQLITE_ORM_CMAKE_CXX_FLAGS}"
#            -DSqliteOrm_BuildTests=off
#            -DBUILD_TESTING=off)
#    message(STATUS "SQLITE_INCLUDE: ${SQLITE_ORM_CMAKE_ARGS}")
#
#        message(STATUS "SQLITE_ORM_CMAKE_CXX_FLAGS: ${SQLITE_ORM_CMAKE_CXX_FLAGS}")
J
jinhai 已提交
1213

Z
zhiru 已提交
1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258
#    externalproject_add(sqlite_orm_ep
#            URL
#            ${SQLITE_ORM_SOURCE_URL}
#            PREFIX ${CMAKE_CURRENT_BINARY_DIR}/sqlite_orm_ep-prefix
#            CONFIGURE_COMMAND
#            ""
#            BUILD_COMMAND
#            ""
#            INSTALL_COMMAND
#            ""
            #${EP_LOG_OPTIONS}
            #${EP_LOG_OPTIONS}
#            CMAKE_ARGS
#            ${SQLITE_ORM_CMAKE_ARGS}
#            BUILD_COMMAND
#            ${MAKE}
#            ${MAKE_BUILD_ARGS}
#            #"LDFLAGS=-L${SQLITE_PREFIX}"
#            BUILD_IN_SOURCE
#            1
#            BUILD_BYPRODUCTS
#            "${SQLITE_ORM_STATIC_LIB}"
#            )
#    ExternalProject_Add_StepDependencies(sqlite_orm_ep build sqlite_ep)

    #set(SQLITE_ORM_SQLITE_HEADER ${SQLITE_INCLUDE_DIR}/sqlite3.h)
#    file(MAKE_DIRECTORY "${SQLITE_ORM_INCLUDE_DIR}")
#    add_library(sqlite_orm STATIC IMPORTED)
##    message(STATUS "SQLITE_INCLUDE_DIR: ${SQLITE_INCLUDE_DIR}")
#    set_target_properties(
#            sqlite_orm
#            PROPERTIES
#            IMPORTED_LOCATION "${SQLITE_ORM_STATIC_LIB}"
#            INTERFACE_INCLUDE_DIRECTORIES "${SQLITE_ORM_INCLUDE_DIR};${SQLITE_INCLUDE_DIR}")
#    target_include_directories(sqlite_orm INTERFACE ${SQLITE_PREFIX} ${SQLITE_INCLUDE_DIR})
#    target_link_libraries(sqlite_orm INTERFACE sqlite)
#
#    add_dependencies(sqlite_orm sqlite_orm_ep)
endmacro()

if(MEGASEARCH_WITH_SQLITE_ORM)
    resolve_dependency(SQLite_ORM)
#    ExternalProject_Get_Property(sqlite_orm_ep source_dir)
#    set(SQLITE_ORM_INCLUDE_DIR ${source_dir}/sqlite_orm_ep)
    include_directories(SYSTEM "${SQLITE_ORM_INCLUDE_DIR}")
Z
zhiru 已提交
1259
    #message(STATUS "SQLITE_ORM_INCLUDE_DIR: ${SQLITE_ORM_INCLUDE_DIR}")
Z
zhiru 已提交
1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293
endif()

# ----------------------------------------------------------------------
# Thrift

macro(build_thrift)
    message(STATUS "Building Apache Thrift-${THRIFT_VERSION} from source")
    set(THRIFT_PREFIX "${CMAKE_CURRENT_BINARY_DIR}/thrift_ep-prefix/src/thrift_ep")
    set(THRIFT_INCLUDE_DIR "${THRIFT_PREFIX}/include")
    set(THRIFT_COMPILER "${THRIFT_PREFIX}/bin/thrift")
    set(THRIFT_CMAKE_ARGS
            ${EP_COMMON_CMAKE_ARGS}
            "-DCMAKE_INSTALL_PREFIX=${THRIFT_PREFIX}"
            "-DCMAKE_INSTALL_RPATH=${THRIFT_PREFIX}/lib"
            -DBUILD_SHARED_LIBS=OFF
            -DBUILD_TESTING=OFF
            -DBUILD_EXAMPLES=OFF
            -DBUILD_TUTORIALS=OFF
            -DWITH_QT4=OFF
            -DWITH_C_GLIB=OFF
            -DWITH_JAVA=OFF
            -DWITH_PYTHON=OFF
            -DWITH_HASKELL=OFF
            -DWITH_CPP=ON
            -DWITH_STATIC_LIB=ON
            -DWITH_LIBEVENT=OFF
            -DCMAKE_BUILD_TYPE=Release)

    # Thrift also uses boost. Forward important boost settings if there were ones passed.
    if(DEFINED BOOST_ROOT)
        set(THRIFT_CMAKE_ARGS ${THRIFT_CMAKE_ARGS} "-DBOOST_ROOT=${BOOST_ROOT}")
    endif()
    if(DEFINED Boost_NAMESPACE)
        set(THRIFT_CMAKE_ARGS ${THRIFT_CMAKE_ARGS} "-DBoost_NAMESPACE=${Boost_NAMESPACE}")
J
jinhai 已提交
1294
    endif()
Z
zhiru 已提交
1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378

    set(THRIFT_STATIC_LIB_NAME "${CMAKE_STATIC_LIBRARY_PREFIX}thrift")
    if(MSVC)
        if(MEGASEARCH_USE_STATIC_CRT)
            set(THRIFT_STATIC_LIB_NAME "${THRIFT_STATIC_LIB_NAME}")
            set(THRIFT_CMAKE_ARGS ${THRIFT_CMAKE_ARGS} "-DWITH_MT=ON")
        else()
            set(THRIFT_STATIC_LIB_NAME "${THRIFT_STATIC_LIB_NAME}")
            set(THRIFT_CMAKE_ARGS ${THRIFT_CMAKE_ARGS} "-DWITH_MT=OFF")
        endif()
    endif()
    if(${UPPERCASE_BUILD_TYPE} STREQUAL "DEBUG")
        set(THRIFT_STATIC_LIB_NAME "${THRIFT_STATIC_LIB_NAME}")
    endif()
    set(THRIFT_STATIC_LIB
            "${THRIFT_PREFIX}/lib/${THRIFT_STATIC_LIB_NAME}${CMAKE_STATIC_LIBRARY_SUFFIX}")

    if(ZLIB_SHARED_LIB)
        set(THRIFT_CMAKE_ARGS "-DZLIB_LIBRARY=${ZLIB_SHARED_LIB}" ${THRIFT_CMAKE_ARGS})
    else()
        set(THRIFT_CMAKE_ARGS "-DZLIB_LIBRARY=${ZLIB_STATIC_LIB}" ${THRIFT_CMAKE_ARGS})
    endif()
    set(THRIFT_DEPENDENCIES ${THRIFT_DEPENDENCIES} ${ZLIB_LIBRARY})

    if(MSVC)
        set(WINFLEXBISON_VERSION 2.4.9)
        set(WINFLEXBISON_PREFIX
                "${CMAKE_CURRENT_BINARY_DIR}/winflexbison_ep/src/winflexbison_ep-install")
        externalproject_add(
                winflexbison_ep
                URL
                https://github.com/lexxmark/winflexbison/releases/download/v.${WINFLEXBISON_VERSION}/win_flex_bison-${WINFLEXBISON_VERSION}.zip
                URL_HASH
                MD5=a2e979ea9928fbf8567e995e9c0df765
                SOURCE_DIR
                ${WINFLEXBISON_PREFIX}
                CONFIGURE_COMMAND
                ""
                BUILD_COMMAND
                ""
                INSTALL_COMMAND
                ""
                ${EP_LOG_OPTIONS})
        set(THRIFT_DEPENDENCIES ${THRIFT_DEPENDENCIES} winflexbison_ep)

        set(THRIFT_CMAKE_ARGS
                "-DFLEX_EXECUTABLE=${WINFLEXBISON_PREFIX}/win_flex.exe"
                "-DBISON_EXECUTABLE=${WINFLEXBISON_PREFIX}/win_bison.exe"
                "-DZLIB_INCLUDE_DIR=${ZLIB_INCLUDE_DIR}"
                "-DWITH_SHARED_LIB=OFF"
                "-DWITH_PLUGIN=OFF"
                ${THRIFT_CMAKE_ARGS})
    elseif(APPLE)
        # Some other process always resets BISON_EXECUTABLE to the system default,
        # thus we use our own variable here.
        if(NOT DEFINED THRIFT_BISON_EXECUTABLE)
            find_package(BISON 2.5.1)

            # In the case where we cannot find a system-wide installation, look for
            # homebrew and ask for its bison installation.
            if(NOT BISON_FOUND)
                find_program(BREW_BIN brew)
                if(BREW_BIN)
                    execute_process(COMMAND ${BREW_BIN} --prefix bison
                            OUTPUT_VARIABLE BISON_PREFIX
                            OUTPUT_STRIP_TRAILING_WHITESPACE)
                    set(BISON_EXECUTABLE "${BISON_PREFIX}/bin/bison")
                    find_package(BISON 2.5.1)
                    set(THRIFT_BISON_EXECUTABLE "${BISON_EXECUTABLE}")
                endif()
            else()
                set(THRIFT_BISON_EXECUTABLE "${BISON_EXECUTABLE}")
            endif()
        endif()
        set(THRIFT_CMAKE_ARGS "-DBISON_EXECUTABLE=${THRIFT_BISON_EXECUTABLE}"
                ${THRIFT_CMAKE_ARGS})
    endif()

    externalproject_add(thrift_ep
            URL
            ${THRIFT_SOURCE_URL}
            BUILD_BYPRODUCTS
            "${THRIFT_STATIC_LIB}"
            "${THRIFT_COMPILER}"
1379 1380 1381
            BUILD_COMMAND
            ${MAKE}
            ${MAKE_BUILD_ARGS}
Z
zhiru 已提交
1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422
            CMAKE_ARGS
            ${THRIFT_CMAKE_ARGS}
            DEPENDS
            ${THRIFT_DEPENDENCIES}
            ${EP_LOG_OPTIONS})

    add_library(thrift STATIC IMPORTED)
    # The include directory must exist before it is referenced by a target.
    file(MAKE_DIRECTORY "${THRIFT_INCLUDE_DIR}")
    set_target_properties(thrift
            PROPERTIES IMPORTED_LOCATION "${THRIFT_STATIC_LIB}"
            INTERFACE_INCLUDE_DIRECTORIES "${THRIFT_INCLUDE_DIR}")
    add_dependencies(thrift thrift_ep)
endmacro()

if(MEGASEARCH_WITH_THRIFT)
    resolve_dependency(Thrift)
    # TODO: Don't use global includes but rather target_include_directories
#    MESSAGE(STATUS ${THRIFT_PREFIX}/lib/)
    link_directories(SYSTEM ${THRIFT_PREFIX}/lib/)
    include_directories(SYSTEM ${THRIFT_INCLUDE_DIR})
endif()

# ----------------------------------------------------------------------
# yaml-cpp

macro(build_yamlcpp)
    message(STATUS "Building yaml-cpp-${YAMLCPP_VERSION} from source")
    set(YAMLCPP_PREFIX "${CMAKE_CURRENT_BINARY_DIR}/yaml-cpp_ep-prefix/src/yaml-cpp_ep")
    set(YAMLCPP_STATIC_LIB "${YAMLCPP_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}yaml-cpp${CMAKE_STATIC_LIBRARY_SUFFIX}")
    set(YAMLCPP_CMAKE_ARGS
            ${EP_COMMON_CMAKE_ARGS}
            "-DCMAKE_INSTALL_PREFIX=${YAMLCPP_PREFIX}"
            -DCMAKE_INSTALL_LIBDIR=lib
            -DYAML_CPP_BUILD_TESTS=OFF
            -DYAML_CPP_BUILD_TOOLS=OFF)

    externalproject_add(yaml-cpp_ep
            URL
            ${YAMLCPP_SOURCE_URL}
            ${EP_LOG_OPTIONS}
1423 1424 1425
            BUILD_COMMAND
            ${MAKE}
            ${MAKE_BUILD_ARGS}
Z
zhiru 已提交
1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472
            BUILD_BYPRODUCTS
            "${YAMLCPP_STATIC_LIB}"
            CMAKE_ARGS
            ${YAMLCPP_CMAKE_ARGS})

    file(MAKE_DIRECTORY "${YAMLCPP_PREFIX}/include")

    add_library(yaml-cpp STATIC IMPORTED)
    set_target_properties(yaml-cpp
            PROPERTIES IMPORTED_LOCATION "${YAMLCPP_STATIC_LIB}"
            INTERFACE_INCLUDE_DIRECTORIES "${YAMLCPP_PREFIX}/include")

    add_dependencies(yaml-cpp yaml-cpp_ep)
endmacro()

if(MEGASEARCH_WITH_YAMLCPP)
    resolve_dependency(yaml-cpp)

    # TODO: Don't use global includes but rather target_include_directories
    get_target_property(YAMLCPP_INCLUDE_DIR yaml-cpp INTERFACE_INCLUDE_DIRECTORIES)
    link_directories(SYSTEM ${YAMLCPP_PREFIX}/lib/)
    include_directories(SYSTEM ${YAMLCPP_INCLUDE_DIR})
endif()

# ----------------------------------------------------------------------
# zlib

macro(build_zlib)
    message(STATUS "Building ZLIB-${ZLIB_VERSION} from source")
    set(ZLIB_PREFIX "${CMAKE_CURRENT_BINARY_DIR}/zlib_ep-prefix/src/zlib_ep")
    if(MSVC)
        if(${UPPERCASE_BUILD_TYPE} STREQUAL "DEBUG")
            set(ZLIB_STATIC_LIB_NAME zlibstaticd.lib)
        else()
            set(ZLIB_STATIC_LIB_NAME zlibstatic.lib)
        endif()
    else()
        set(ZLIB_STATIC_LIB_NAME libz.a)
    endif()
    set(ZLIB_STATIC_LIB "${ZLIB_PREFIX}/lib/${ZLIB_STATIC_LIB_NAME}")
    set(ZLIB_CMAKE_ARGS ${EP_COMMON_CMAKE_ARGS} "-DCMAKE_INSTALL_PREFIX=${ZLIB_PREFIX}"
            -DBUILD_SHARED_LIBS=OFF)

    externalproject_add(zlib_ep
            URL
            ${ZLIB_SOURCE_URL}
            ${EP_LOG_OPTIONS}
1473 1474 1475
            BUILD_COMMAND
            ${MAKE}
            ${MAKE_BUILD_ARGS}
Z
zhiru 已提交
1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542
            BUILD_BYPRODUCTS
            "${ZLIB_STATIC_LIB}"
            CMAKE_ARGS
            ${ZLIB_CMAKE_ARGS})

    file(MAKE_DIRECTORY "${ZLIB_PREFIX}/include")

    add_library(zlib STATIC IMPORTED)
    set_target_properties(zlib
            PROPERTIES IMPORTED_LOCATION "${ZLIB_STATIC_LIB}"
            INTERFACE_INCLUDE_DIRECTORIES "${ZLIB_PREFIX}/include")

    add_dependencies(zlib zlib_ep)
endmacro()

if(MEGASEARCH_WITH_ZLIB)
    resolve_dependency(ZLIB)

    # TODO: Don't use global includes but rather target_include_directories
    get_target_property(ZLIB_INCLUDE_DIR zlib INTERFACE_INCLUDE_DIRECTORIES)
    include_directories(SYSTEM ${ZLIB_INCLUDE_DIR})
endif()

# ----------------------------------------------------------------------
# zstd

macro(build_zstd)
    message(STATUS "Building zstd-${ZSTD_VERSION} from source")
    set(ZSTD_PREFIX "${CMAKE_CURRENT_BINARY_DIR}/zstd_ep-prefix/src/zstd_ep")

    set(ZSTD_CMAKE_ARGS
            ${EP_COMMON_TOOLCHAIN}
            "-DCMAKE_INSTALL_PREFIX=${ZSTD_PREFIX}"
            -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
            -DCMAKE_INSTALL_LIBDIR=lib #${CMAKE_INSTALL_LIBDIR}
            -DZSTD_BUILD_PROGRAMS=off
            -DZSTD_BUILD_SHARED=off
            -DZSTD_BUILD_STATIC=on
            -DZSTD_MULTITHREAD_SUPPORT=off)

    if(MSVC)
        set(ZSTD_STATIC_LIB "${ZSTD_PREFIX}/lib/zstd_static.lib")
        if(MEGASEARCH_USE_STATIC_CRT)
            set(ZSTD_CMAKE_ARGS ${ZSTD_CMAKE_ARGS} "-DZSTD_USE_STATIC_RUNTIME=on")
        endif()
    else()
        set(ZSTD_STATIC_LIB "${ZSTD_PREFIX}/lib/libzstd.a")
        # Only pass our C flags on Unix as on MSVC it leads to a
        # "incompatible command-line options" error
        set(ZSTD_CMAKE_ARGS
                ${ZSTD_CMAKE_ARGS}
                -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
                -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
                -DCMAKE_C_FLAGS=${EP_C_FLAGS}
                -DCMAKE_CXX_FLAGS=${EP_CXX_FLAGS})
    endif()

    if(CMAKE_VERSION VERSION_LESS 3.7)
        message(FATAL_ERROR "Building zstd using ExternalProject requires at least CMake 3.7")
    endif()

    externalproject_add(zstd_ep
            ${EP_LOG_OPTIONS}
            CMAKE_ARGS
            ${ZSTD_CMAKE_ARGS}
            SOURCE_SUBDIR
            "build/cmake"
1543 1544 1545
            BUILD_COMMAND
            ${MAKE}
            ${MAKE_BUILD_ARGS}
Z
zhiru 已提交
1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570
            INSTALL_DIR
            ${ZSTD_PREFIX}
            URL
            ${ZSTD_SOURCE_URL}
            BUILD_BYPRODUCTS
            "${ZSTD_STATIC_LIB}")

    file(MAKE_DIRECTORY "${ZSTD_PREFIX}/include")

    add_library(zstd STATIC IMPORTED)
    set_target_properties(zstd
            PROPERTIES IMPORTED_LOCATION "${ZSTD_STATIC_LIB}"
            INTERFACE_INCLUDE_DIRECTORIES "${ZSTD_PREFIX}/include")

    add_dependencies(zstd zstd_ep)
endmacro()

if(MEGASEARCH_WITH_ZSTD)
    resolve_dependency(ZSTD)

    # TODO: Don't use global includes but rather target_include_directories
    get_target_property(ZSTD_INCLUDE_DIR zstd INTERFACE_INCLUDE_DIRECTORIES)
    link_directories(SYSTEM ${ZSTD_PREFIX}/lib)
    include_directories(SYSTEM ${ZSTD_INCLUDE_DIR})
endif()