diff --git a/CMakeLists.txt b/CMakeLists.txt index 44954c1..bb8789c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,6 +5,9 @@ # https://github.com/desktop-app/legal/blob/master/LEGAL add_subdirectory(external) +if (LINUX AND NOT DESKTOP_APP_DISABLE_JEMALLOC) + add_subdirectory(linux_jemalloc_helper) +endif() if (DESKTOP_APP_USE_ALLOCATION_TRACER) add_subdirectory(linux_allocation_tracer) endif() diff --git a/external/CMakeLists.txt b/external/CMakeLists.txt index 34181cc..d9af92a 100644 --- a/external/CMakeLists.txt +++ b/external/CMakeLists.txt @@ -38,6 +38,9 @@ if (add_hunspell_library) add_checked_subdirectory(hunspell) endif() add_checked_subdirectory(iconv) +if (LINUX AND NOT DESKTOP_APP_DISABLE_JEMALLOC) + add_checked_subdirectory(jemalloc) +endif() add_checked_subdirectory(jpeg) add_checked_subdirectory(kcoreaddons) add_checked_subdirectory(kimageformats) @@ -54,9 +57,6 @@ add_checked_subdirectory(qr_code_generator) add_checked_subdirectory(ranges) add_checked_subdirectory(rlottie) add_checked_subdirectory(rnnoise) -if (LINUX AND NOT DESKTOP_APP_DISABLE_SCUDO) - add_checked_subdirectory(scudo) -endif() add_checked_subdirectory(ton) add_checked_subdirectory(variant) add_checked_subdirectory(vpx) diff --git a/external/jemalloc/CMakeLists.txt b/external/jemalloc/CMakeLists.txt new file mode 100644 index 0000000..9b87c00 --- /dev/null +++ b/external/jemalloc/CMakeLists.txt @@ -0,0 +1,65 @@ +# 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 jemalloc) + + if (JEMALLOC_FOUND) + target_include_directories(external_jemalloc SYSTEM + INTERFACE + ${JEMALLOC_INCLUDE_DIRS} + ) + + target_link_libraries(external_jemalloc + INTERFACE + -Wl,--push-state,--no-as-needed,${JEMALLOC_LINK_LIBRARIES},--pop-state + ) + + return() + endif() +endif() + +include(ProcessorCount) +ProcessorCount(N) + +include(ExternalProject) +ExternalProject_Add(jemalloc + URL ${third_party_loc}/jemalloc + CONFIGURE_COMMAND env + CC=clang + CXX=clang++ + "$>,$>,EXTRA_CFLAGS=-fno-omit-frame-pointer -fstack-protector-all -fstack-clash-protection -fcf-protection -DNDEBUG -D_FORTIFY_SOURCE=3 -D_GLIBCXX_ASSERTIONS,=>" + "$>,$>,EXTRA_CXXFLAGS=-fno-omit-frame-pointer -fstack-protector-all -fstack-clash-protection -fcf-protection -DNDEBUG -D_FORTIFY_SOURCE=3 -D_GLIBCXX_ASSERTIONS,=>" + ./autogen.sh --disable-shared + BUILD_COMMAND make $<$>:-j${N}> + BUILD_IN_SOURCE 1 + STEP_TARGETS build + EXCLUDE_FROM_ALL TRUE + BUILD_BYPRODUCTS /lib/libjemalloc_pic.a +) + +ExternalProject_Get_property(jemalloc SOURCE_DIR) +file(MAKE_DIRECTORY "${SOURCE_DIR}/include") + +target_include_directories(external_jemalloc SYSTEM +INTERFACE + ${SOURCE_DIR}/include +) + +target_link_libraries(external_jemalloc +INTERFACE + -Wl,--push-state,--whole-archive,${SOURCE_DIR}/lib/libjemalloc_pic.a,--pop-state +) + +add_dependencies(external_jemalloc jemalloc-build) diff --git a/external/scudo/CMakeLists.txt b/external/scudo/CMakeLists.txt deleted file mode 100644 index 3fc79fb..0000000 --- a/external/scudo/CMakeLists.txt +++ /dev/null @@ -1,35 +0,0 @@ -# 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_scudo STATIC) -add_library(desktop-app::external_scudo ALIAS external_scudo) -init_target(external_scudo "(external)") - -set(scudo_loc ${third_party_loc}/scudo) - -# https://llvm.org/docs/ScudoHardenedAllocator.html#library -file(GLOB scudo_srcs ${scudo_loc}/*.cpp) -target_sources(external_scudo -PRIVATE - ${scudo_srcs} -) - -target_include_directories(external_scudo SYSTEM -PRIVATE - ${scudo_loc}/include -) - -add_library(external_scudo_options INTERFACE) -target_compile_options(external_scudo_options -INTERFACE - -fno-lto - -Wno-unused-result -) - -target_link_libraries(external_scudo -PRIVATE - external_scudo_options -) diff --git a/init_target.cmake b/init_target.cmake index 549865e..f022292 100644 --- a/init_target.cmake +++ b/init_target.cmake @@ -27,14 +27,6 @@ function(init_target target_name) # init_target(my_target [cxx_std_..] folder_na set_property(TARGET ${target_name} APPEND_STRING PROPERTY STATIC_LIBRARY_OPTIONS "$,,/LTCG>") endif() - if (LINUX AND NOT DESKTOP_APP_DISABLE_SCUDO AND NOT target_name STREQUAL external_scudo) - add_dependencies(${target_name} desktop-app::external_scudo) - target_link_options(${target_name} - PRIVATE - -Wl,--push-state,--whole-archive,$,--pop-state - ) - endif() - target_link_libraries(${target_name} PRIVATE desktop-app::common_options) set_target_properties(${target_name} PROPERTIES XCODE_ATTRIBUTE_CLANG_ENABLE_OBJC_WEAK YES diff --git a/linux_jemalloc_helper/CMakeLists.txt b/linux_jemalloc_helper/CMakeLists.txt new file mode 100644 index 0000000..0466d8a --- /dev/null +++ b/linux_jemalloc_helper/CMakeLists.txt @@ -0,0 +1,19 @@ +# 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(linux_jemalloc_helper OBJECT) +init_target(linux_jemalloc_helper "(external)") +add_library(desktop-app::linux_jemalloc_helper ALIAS linux_jemalloc_helper) + +nice_target_sources(linux_jemalloc_helper ${CMAKE_CURRENT_SOURCE_DIR} +PRIVATE + linux_jemalloc_helper.cpp +) + +target_link_libraries(linux_jemalloc_helper +PRIVATE + desktop-app::external_jemalloc +) diff --git a/linux_jemalloc_helper/linux_jemalloc_helper.cpp b/linux_jemalloc_helper/linux_jemalloc_helper.cpp new file mode 100644 index 0000000..76bc863 --- /dev/null +++ b/linux_jemalloc_helper/linux_jemalloc_helper.cpp @@ -0,0 +1,25 @@ +// 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 +// +#ifdef __FreeBSD__ +#include +#else // __FreeBSD__ +#include +#endif // !__FreeBSD__ + +namespace { + +class JemallocInitializer { +public: + JemallocInitializer() { + auto backgroundThread = true; + mallctl("background_thread", nullptr, nullptr, &backgroundThread, sizeof(bool)); + } +}; + +static const JemallocInitializer initializer; + +} // namespace diff --git a/options_linux.cmake b/options_linux.cmake index cd49f8c..b74b493 100644 --- a/options_linux.cmake +++ b/options_linux.cmake @@ -99,6 +99,14 @@ if (NOT DESKTOP_APP_USE_PACKAGED OR DESKTOP_APP_SPECIAL_TARGET) ) endif() +if (NOT DESKTOP_APP_DISABLE_JEMALLOC) + target_link_libraries(common_options + INTERFACE + $ + $ + ) +endif() + if (DESKTOP_APP_USE_ALLOCATION_TRACER) target_link_options(common_options INTERFACE diff --git a/variables.cmake b/variables.cmake index 595f1d8..65b1e4e 100644 --- a/variables.cmake +++ b/variables.cmake @@ -32,7 +32,7 @@ option(DESKTOP_APP_ASAN "Enable address sanitizer" OFF) cmake_dependent_option(DESKTOP_APP_USE_ENCHANT "Use Enchant instead of bundled Hunspell." OFF LINUX OFF) cmake_dependent_option(DESKTOP_APP_USE_CLD3 "Disable system text language recognition and use bundled cld3 only." OFF APPLE ON) cmake_dependent_option(DESKTOP_APP_NO_PDB "Disable PDB file generation." OFF WIN32 OFF) -cmake_dependent_option(DESKTOP_APP_DISABLE_SCUDO "Disable scudo, use system malloc." OFF "LINUX; NOT DESKTOP_APP_ASAN" ON) +cmake_dependent_option(DESKTOP_APP_DISABLE_JEMALLOC "Disable jemalloc, use system malloc." OFF "LINUX; NOT DESKTOP_APP_ASAN" ON) if (APPLE AND NOT DEFINED DESKTOP_APP_MAC_ARCH) if (DEFINED CMAKE_OSX_ARCHITECTURES)