Skip to content

Feature request: cmake support #303

Open

Description

@namreeb

CMake support would be wonderful for cross-platform support. It would allow easy integration into existing cross-platform projects.

Activity

  1. rustyx commented on Feb 26, 2016

    @rustyx
    Contributor

    +1. Not to mention CMake is much more BSD-friendly license-wise.

  2. yuslepukhin commented on Feb 26, 2016

    @yuslepukhin

    I build internally with CMake but there are 3 Windows specific places which can be fixed/augumented. However, I do not have other platforms handy to make it gneric.

  3. jasone commented on Feb 27, 2016

    @jasone
    Member

    I'm open to switching over to CMake, but it's way down my list of things to do right now. If someone besides me is passionate about converting all the portability logic in configure.ac and making the new build system function reliably on at least Linux, FreeBSD, OS X, and Windows, I'm willing to do my part to learn more about CMake and help maintain it as the sole build system.

  4. wbierman commented on Feb 29, 2016

    @wbierman

    Is there an overview of the different build environments jemalloc currently supports? I might be willing to undertake this.

  5. jasone commented on Feb 29, 2016

    @jasone
    Member

    @wbierman, most of the supported platforms are enumerated in configure.ac.

  6. photex commented on Feb 29, 2016

    @photex

    +1 I just started to work on this for Linux and OSX. :)

  7. yuslepukhin commented on Feb 29, 2016

    @yuslepukhin

    +1 I think a good idea would be to issue a fairly generic CMake and then a few people could take a platform each and fix it up. That would parallelize the effort and would not tax a specific individual trying to support everything that is in configure.ac, This is a high bar to get something out that might benefit people immediatly.

    However, that would require a short transition period where CMake file would exist in parallel with the existing system. If @jasone would soften his stand on having 4 platform support at the first iteration, although those 4 may become available almost immediately due to the nature of CMake.

  8. jasone commented on Feb 29, 2016

    @jasone
    Member

    I'm fine with an iterative approach to CMake transition, but I don't want to do a release with both build systems. Incremental changes (and refactoring diffs that touch files outside the new CMake-related files) need to be self-contained so that we can cherry-pick around them for bugfix releases, and leave them out entirely if the effort isn't complete in time for the 4.2.0 release.

    Re: 4.2.0 release date, pretty much all of the features we're currently considering are non-trivial, and it's likely to be at least a couple months before we have enough functionality to justify release.

  9. rustyx commented on Feb 29, 2016

    @rustyx
    Contributor

    Sounds like a good candidate for a feature branch.

  10. jasone commented on Feb 29, 2016

    @jasone
    Member

    Good idea.

  11. wbierman commented on Feb 29, 2016

    @wbierman

    I was going to suggest perhaps another repository under this organization so that those of who are interested in contributing might be able to do so more easily? @photex mentioned already having started on Linux and OSX. I can work on Windows and would be willing to setup virtual machines to work on the more obscure platforms if it helps to get the change adopted.

  12. yuslepukhin commented on Feb 29, 2016

    @yuslepukhin

    @wbierman I can share my Windows CMake build as a starting point or just as a reference if there interest.

  13. wbierman commented on Feb 29, 2016

    @wbierman

    Sounds good to me.

    @jasone would you be willing to create a 'jemalloc-cmake' repository and add myself, @yuslepukhin, @rustyx, @photex to it?

  14. jasone commented on Feb 29, 2016

    @jasone
    Member

    Okay, https://github.com/jemalloc/jemalloc-cmake is set up. GitHub doesn't directly support forks within the same project, so I had to do the following to create the repo:

    git clone [email protected]:jemalloc/jemalloc-cmake.git
    git remote add -m dev upstream https://github.com/jemalloc/jemalloc.git
    git pull upstream dev
    git push origin master
    

    It looks like you will manually need to add the remote to your cloned repo so that you can merge the upstream into the jemalloc-cmake repo master branch from time to time.

  15. 58 remaining items

  16. madebr commented on Feb 4, 2020

    @madebr

    @leezu
    I just tried your cmake-2019 branch for building jemalloc on Windows using Visual Studio.

    I had to make some changes to make it configure and partially compile, but not fully.

    DetectOSFeatures fails to detect the Windows platform.

    During build, the final step failed because nm and awk are generally not available on Windows.
    The warning that is shown during this failure is also awkward.
    Because ${CMAKE_NM} is empty, cmake thinks -a is the name of the application.
    Also, because JEMALLOC_AWK_EXEC is undefined, JEMALLOC_AWK_EXEC-NOTFOUND is shown in the log.
    The configure step should fail when these 2 variables are undefined.
    But better, the default configuration options should be that it just works.

    I would like to suggest some additional changes:

    • Add support for install.
    • Make use of cmake targets. So no global include_directories or add_definitions.
      This also allows the install step to install cmake scripts that export these cmake targets.
    • No use of linux specific utilities. The current sln project can build jemalloc on Windows, so I don't see why cmake cannot. cmake has a script mode which might be useful.
    • Wrap all calls to option and store the decsription, value and default in a list(s). This allows cmake to print a summary at the end of configuration. SDL2 does something similar.
    • CI for Windows, as well as Linux and Macos (using appveyor and travis-ci)?
    Patch to get things going on Visual Studio 2017

    These changes are just hacks to get things working. I did not use the proper cmake variables.

    diff --git a/CMakeLists.txt b/CMakeLists.txt
    index 49d5439c..693c667a 100644
    --- a/CMakeLists.txt
    +++ b/CMakeLists.txt
    @@ -16,6 +16,9 @@ include(${PROJECT_SOURCE_DIR}/build-aux/DetectCompilerFeatures.cmake) # 2
     
     # Tell CMake where to find default headers:
     include_directories(${PROJECT_SOURCE_DIR}/include)
    +if(MSVC)
    +    include_directories(${PROJECT_SOURCE_DIR}/include/msvc_compat)
    +endif()
     
     # Also tell CMake where to find out-of-source generated headers:
     include_directories(${PROJECT_BINARY_DIR}/include)
    diff --git a/build-aux/DetectCompilerFeatures.cmake b/build-aux/DetectCompilerFeatures.cmake
    index d764428c..822a271b 100644
    --- a/build-aux/DetectCompilerFeatures.cmake
    +++ b/build-aux/DetectCompilerFeatures.cmake
    @@ -151,7 +151,7 @@ int main(void) {
         mach_absolute_time();
         return 0;
     }" JEMALLOC_HAVE_MACH_ABSOLUTE_TIME)
    -
    +set(CMAKE_REQUIRED_INCLUDES "${PROJECT_SOURCE_DIR}/include/msvc_compat")
     check_c_source_runs("
     #include <assert.h>
     #include <strings.h>
    @@ -166,13 +166,14 @@ if(HAS_BUILTIN_FFSL)
         set(JEMALLOC_INTERNAL_FFSL __builtin_ffsl)
         set(JEMALLOC_INTERNAL_FFS __builtin_ffs)
     else()
    +    set(CMAKE_REQUIRED_INCLUDES "${PROJECT_SOURCE_DIR}/include/msvc_compat")
         check_c_source_runs("
         #include <assert.h>
         #include <strings.h>
         int main() {
             int rv = ffsl(0x08);
             assert(rv == 4);
    -        return 0);
    +        return 0;
         }" HAS_FFSL)
     
         if(HAS_FFSL)
    diff --git a/build-aux/DetectOSFeatures.cmake b/build-aux/DetectOSFeatures.cmake
    index 523db5ff..de0a5911 100644
    --- a/build-aux/DetectOSFeatures.cmake
    +++ b/build-aux/DetectOSFeatures.cmake
    @@ -76,8 +76,7 @@ else()
     endif()
     
     # Only run check if we haven't run it before
    -if(CMAKE_SYSTEM_PROCESSOR MATCHES "i686" OR
    -   CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64")
    +if(CMAKE_SYSTEM_PROCESSOR MATCHES "(i686|x86_64|AMD64)")
         check_c_source_compiles("
     int main(void) {
         __asm__ volatile(\"pause\");
    @@ -118,7 +117,7 @@ endif()
     if(NOT LG_VADDR)
         if(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64") # maybe "arm64" too?
             set(LG_VADDR 48)
    -    elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64")
    +    elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "(x86_64|AMD64)")
             GetSystemAddrBits(LG_VADDR)
             # Cache result so we don't run the check every time
             set(LG_VADDR ${LG_VADDR} CACHE INTERNAL "System Address Bits")
    diff --git a/build-aux/GetSystemSizes.cmake b/build-aux/GetSystemSizes.cmake
    index 0baf3296..35d3a924 100644
    --- a/build-aux/GetSystemSizes.cmake
    +++ b/build-aux/GetSystemSizes.cmake
    @@ -41,7 +41,7 @@ int main() {
             SYSTEM_LOGICAL_PROCESSOR_INFORMATION *buffer = 0;
     
             GetSystemInfo(&si);
    -        result = si.dwSizes;
    +        result = si.dwPageSize;
         #else
             result = sysconf(_SC_PAGESIZE);
         #endif
    diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
    index a3cceb4d..bd3d56f0 100644
    --- a/src/CMakeLists.txt
    +++ b/src/CMakeLists.txt
    @@ -59,7 +59,7 @@ if(NOT JEMALLOC_CXX_DISABLE)
         # C++14 support exists, so add C++ file and configure C++ options
         list(APPEND JEMALLOC_CMAKE_SOURCES
             jemalloc_cpp.cpp)
    -    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -g3 -O3")
    +    #set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -g3 -O3")
     endif()
     
     if(enable_valgrind)
    @@ -75,7 +75,7 @@ find_program(JEMALLOC_AWK_EXEC awk)
     function(ExtractSymbolsGenerateNamespaceHeader target top-target symExt awk dir hdr)
     # Don't compile -syms with optimization so it builds faster.
     # The target is only for symbol extraction so we don't care about performance.
    -target_compile_options(${target} PRIVATE "-O0")
    +# target_compile_options(${target} PRIVATE "-O0")
     target_compile_definitions(${target} PRIVATE
         JEMALLOC_NO_PRIVATE_NAMESPACE
         _GNU_SOURCE)
    diff --git a/src/jemalloc_cpp.cpp b/src/jemalloc_cpp.cpp
    index da0441a7..01abe32d 100644
    --- a/src/jemalloc_cpp.cpp
    +++ b/src/jemalloc_cpp.cpp
    @@ -67,7 +67,7 @@ handleOOM(std::size_t size, bool nothrow) {
     	}
     
     	if (ptr == nullptr && !nothrow)
    -		std::__throw_bad_alloc();
    +		std::bad_alloc();
     	return ptr;
     }
     

    These changes can be summed up as:

    • include include/msvc_compat as well when using the MSVC compiler
    • when calling check_c_source_compiles and check_c_source_runs, set CMAKE_REQUIRED_* variables. I suggest wrapping these functions.
    • CMAKE_SYSTEM_PROCESSOR is AMD64 on Windows using MSVC
    • 2 bugs in C code (embedded in cmake)
    • not adding gcc/clang options to CMAKE_{LANG}_FLAGS
    • std::__throw_bad_alloc in src/jemalloc_cpp.cpp is not defined in MSVC2017.

    Great work!!

  17. leezu commented on Feb 4, 2020

    @leezu

    @madebr I only did a very small modification to @mattsta's work to verify a special use-case. @mattsta is driving this work and may be able to help.

  18. curoky commented on Jun 6, 2020

    @curoky

    @mattsta Thanks for your work on adding CMake support!
    I added a patch for the equivalent of --disable-initial-exec-tls at https://github.com/leezu/jemalloc/tree/add/cmake-2019
    Also I noticed that the Debug build generates an error: multiple rules generate 3rdparty/jemalloc/src/syms/hash.c.o.sym.jet [-w dupbuild=err] (at least with cmake -GNinja)

    @leezu It seems that some files was included twice.

    set(JEMALLOC_CMAKE_SOURCES
            ...
            hash.c
            prng.c
            ticker.c
        )
    
    if(CMAKE_BUILD_TYPE MATCHES Debug)
        list(APPEND JEMALLOC_CMAKE_SOURCES
            hash.c
            prng.c
            ticker.c)
       ...
    endif()

    Perhaps, we can directly delete the second APPEND in judgment for CMAKE_BUILD_TYPE.

  19. fwsGonzo commented on Jun 27, 2020

    @fwsGonzo

    What is the status on this? I would like to start using CMake with jemalloc as automake is just hopeless to work with. Thanks for your hard work so far!

  20. davidtgoldblatt commented on Jun 27, 2020

    @davidtgoldblatt
    Contributor

    I've been actively working on converting to a cmake-based build for the past week or so. I think I've already got plans for most of the things that have the potential to cause problems.

  21. fwsGonzo commented on Jun 27, 2020

    @fwsGonzo

    Have you tried using it as a library in another project? I would have liked to be able to use add_subdirectory(jemalloc). :) 👍

  22. tormardi commented on May 18, 2021

    @tormardi

    How to configure with cmake to get rid of the 'je_' prefix?
    The library generated by autoconf has:
    $nm ./lib/libjemalloc.so | grep -w malloc
    000000000000d740 T malloc

    The library generated by cmake has:
    nm ./build/src/libjemalloc.so | grep -w je_malloc
    00000000000551d0 T je_malloc

  23. jackpoz commented on Jul 18, 2021

    @jackpoz

    It would be nice to see this feature merged but after 6 years my hopes are diminishing

  24. tormardi commented on Aug 12, 2021

    @tormardi

    OK, too bad. I'm the build/cmake guy at mysql, and I was able to integrate your stuff into our cmake code. I gave up though, when I realized the jemalloc C codebase is gcc-only, will not compile with Visual Studio.

  25. JackBoosY commented on Mar 4, 2022

    @JackBoosY

    @davidtgoldblatt Any progress there? I think I can give some help.
    Since there are some issues about using the latest autoconf, microsoft/vcpkg#22627 was blocked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions