# This file is part of Desktop App Toolkit,
# a set of libraries for developing nice desktop applications.
#
# For license and copyright information please follow this link:
# https://github.com/desktop-app/legal/blob/master/LEGAL

add_library(external_jemalloc INTERFACE IMPORTED GLOBAL)
add_library(desktop-app::external_jemalloc ALIAS external_jemalloc)

if (CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
    return()
endif()

if (DESKTOP_APP_USE_PACKAGED)
    find_package(PkgConfig REQUIRED)
    pkg_check_modules(JEMALLOC IMPORTED_TARGET jemalloc)

    if (JEMALLOC_FOUND)
        target_link_libraries(external_jemalloc
        INTERFACE
            -Wl,--no-as-needed
            PkgConfig::JEMALLOC
            ${JEMALLOC_LINK_LIBRARIES}
            -Wl,--as-needed
        )
        return()
    endif()
endif()

add_library(external_jemalloc_bundled STATIC IMPORTED)

include(ExternalProject)
ExternalProject_Add(jemalloc
    URL ${third_party_loc}/jemalloc
    CONFIGURE_COMMAND cd "<SOURCE_DIR>" && export EXTRA_CFLAGS=-fstack-protector-all\ -fPIC\ -D_FORTIFY_SOURCE=2 && export EXTRA_CXXFLAGS=-fstack-protector-all\ -fPIC\ -D_FORTIFY_SOURCE=2 && CC=clang CXX=clang++ ./autogen.sh
    BUILD_IN_SOURCE 1
    STEP_TARGETS build
    EXCLUDE_FROM_ALL TRUE
    BUILD_BYPRODUCTS <SOURCE_DIR>/lib/libjemalloc.a
)

ExternalProject_Get_property(jemalloc SOURCE_DIR)
file(MAKE_DIRECTORY "${SOURCE_DIR}/include")

set_target_properties(external_jemalloc_bundled PROPERTIES
    IMPORTED_LOCATION "${SOURCE_DIR}/lib/libjemalloc.a"
)

target_include_directories(external_jemalloc_bundled SYSTEM
INTERFACE
    ${SOURCE_DIR}/include
)

target_link_libraries(external_jemalloc_bundled
INTERFACE
    ${CMAKE_DL_LIBS}
    pthread
)

add_dependencies(external_jemalloc_bundled jemalloc-build)

target_link_libraries(external_jemalloc
INTERFACE
    -Wl,--whole-archive
    $<TARGET_FILE:external_jemalloc_bundled>
    -Wl,--no-whole-archive
    external_jemalloc_bundled
)
