# 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()

set(DESKTOP_APP_REQUIRE_JEMALLOC ON CACHE BOOL "Always prefer jemalloc by FreeBSD or use system memory allocator?")
if (DESKTOP_APP_REQUIRE_JEMALLOC)
    target_compile_definitions(external_jemalloc INTERFACE DESKTOP_APP_REQUIRE_JEMALLOC)
endif()

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

    if (JEMALLOC_FOUND)
        if (DESKTOP_APP_REQUIRE_JEMALLOC)
            target_link_libraries(external_jemalloc
            INTERFACE
                -Wl,--no-as-needed
                PkgConfig::JEMALLOC
                ${JEMALLOC_LINK_LIBRARIES}
                -Wl,--as-needed
            )
        else()
            target_compile_definitions(external_jemalloc INTERFACE _GNU_SOURCE)
            target_compile_options(external_jemalloc INTERFACE ${JEMALLOC_CFLAGS})
        endif()
    endif()
endif()

if (NOT JEMALLOC_FOUND)
    add_library(external_jemalloc_bundled STATIC IMPORTED GLOBAL)
    set(jemalloc_loc ${third_party_loc}/jemalloc)

    include(ExternalProject)
    ExternalProject_Add(jemalloc
        SOURCE_DIR ${jemalloc_loc}
        CONFIGURE_COMMAND cd "${jemalloc_loc}" && CC=clang CXX=clang++ EXTRA_CFLAGS=-fPIE EXTRA_CXXFLAGS=-fPIE ./autogen.sh
        INSTALL_COMMAND :
        BUILD_IN_SOURCE 1
        BUILD_BYPRODUCTS ${jemalloc_loc}/lib/libjemalloc.a
    )

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

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

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

    target_link_libraries(external_jemalloc_bundled
    INTERFACE
        ${CMAKE_DL_LIBS}
        pthread
    )

    add_dependencies(external_jemalloc_bundled jemalloc)

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