|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# |
| 3 | +# Copyright (C) 2024 The Falco Authors. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
| 6 | +# in compliance with the License. You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software distributed under the License |
| 11 | +# is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 12 | +# or implied. See the License for the specific language governing permissions and limitations under |
| 13 | +# the License. |
| 14 | +# |
| 15 | + |
| 16 | +option(USE_BUNDLED_JEMALLOC "Use bundled jemalloc allocator" ${USE_BUNDLED_DEPS}) |
| 17 | + |
| 18 | +if(JEMALLOC_LIB) |
| 19 | + # we already have JEMALLOC |
| 20 | +elseif(NOT USE_BUNDLED_JEMALLOC) |
| 21 | + if(BUILD_SHARED_LIBS) |
| 22 | + set(JEMALLOC_LIB_SUFFIX ${CMAKE_SHARED_LIBRARY_SUFFIX}) |
| 23 | + else() |
| 24 | + set(JEMALLOC_LIB_SUFFIX ${CMAKE_STATIC_LIBRARY_SUFFIX}) |
| 25 | + endif() |
| 26 | + find_library(JEMALLOC_LIB NAMES jemalloc${JEMALLOC_LIB_SUFFIX}) |
| 27 | + if(JEMALLOC_LIB) |
| 28 | + message(STATUS "Found JEMALLOC: lib: ${JEMALLOC_LIB}") |
| 29 | + else() |
| 30 | + message(FATAL_ERROR "Couldn't find system jemalloc") |
| 31 | + endif() |
| 32 | +else() |
| 33 | + if(BUILD_SHARED_LIBS) |
| 34 | + set(JEMALLOC_LIB_SUFFIX ${CMAKE_SHARED_LIBRARY_SUFFIX}) |
| 35 | + else() |
| 36 | + set(JEMALLOC_LIB_SUFFIX ${CMAKE_STATIC_LIBRARY_SUFFIX}) |
| 37 | + endif() |
| 38 | + set(JEMALLOC_SRC "${PROJECT_BINARY_DIR}/jemalloc-prefix/src") |
| 39 | + set(JEMALLOC_LIB "${JEMALLOC_SRC}/jemalloc/lib/libjemalloc${JEMALLOC_LIB_SUFFIX}") |
| 40 | + ExternalProject_Add( |
| 41 | + jemalloc |
| 42 | + PREFIX "${PROJECT_BINARY_DIR}/jemalloc-prefix" |
| 43 | + URL "https://github.com/jemalloc/jemalloc/archive/refs/tags/5.3.0.tar.gz" |
| 44 | + URL_HASH "SHA256=ef6f74fd45e95ee4ef7f9e19ebe5b075ca6b7fbe0140612b2a161abafb7ee179" |
| 45 | + CONFIGURE_COMMAND ./autogen.sh --enable-prof --disable-libdl |
| 46 | + BUILD_IN_SOURCE 1 |
| 47 | + BUILD_COMMAND make build_lib_static |
| 48 | + INSTALL_COMMAND "" |
| 49 | + UPDATE_COMMAND "" |
| 50 | + BUILD_BYPRODUCTS ${JEMALLOC_LIB} |
| 51 | + ) |
| 52 | + message(STATUS "Using bundled jemalloc: lib: ${JEMALLOC_LIB}") |
| 53 | + install( |
| 54 | + FILES "${JEMALLOC_LIB}" |
| 55 | + DESTINATION "${CMAKE_INSTALL_LIBDIR}/${LIBS_PACKAGE_NAME}" |
| 56 | + COMPONENT "libs-deps" |
| 57 | + ) |
| 58 | +endif() |
| 59 | + |
| 60 | +# We add a custom target, in this way we can always depend on `jemalloc` without distinguishing |
| 61 | +# between "bundled" and "not-bundled" case |
| 62 | +if(NOT TARGET jemalloc) |
| 63 | + add_custom_target(jemalloc) |
| 64 | +endif() |
0 commit comments