Skip to content

Commit 1fd0085

Browse files
authored
Allow using mimalloc with dynamic linking (#7391)
With dynamic linking, build and link mimalloc's dynamic library, and include it in the installation (this also brings along the headers and CMake files, but it seemed like more trouble than it was worth to try to manually install just the library or remove the extras). The static build remains the same. Remove the restriction that mimalloc can only be linked into a static-lib build.
1 parent d8b4c56 commit 1fd0085

File tree

4 files changed

+27
-14
lines changed

4 files changed

+27
-14
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ jobs:
250250

251251
- name: cmake
252252
run: |
253-
./alpine.sh cmake . -G Ninja -DCMAKE_INSTALL_PREFIX=out/install -DCMAKE_CXX_FLAGS="-static" -DCMAKE_C_FLAGS="-static" -DCMAKE_BUILD_TYPE=Release -DBUILD_STATIC_LIB=ON -DMIMALLOC_STATIC=ON -DCMAKE_INSTALL_PREFIX=install
253+
./alpine.sh cmake . -G Ninja -DCMAKE_INSTALL_PREFIX=out/install -DCMAKE_CXX_FLAGS="-static" -DCMAKE_C_FLAGS="-static" -DCMAKE_BUILD_TYPE=Release -DBUILD_STATIC_LIB=ON -DBUILD_MIMALLOC=ON -DCMAKE_INSTALL_PREFIX=install
254254
255255
- name: build
256256
run: |

.github/workflows/create_release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ jobs:
140140

141141
- name: cmake
142142
run: |
143-
./alpine.sh cmake . -G Ninja -DCMAKE_CXX_FLAGS="-static" -DCMAKE_C_FLAGS="-static" -DCMAKE_BUILD_TYPE=Release -DBUILD_STATIC_LIB=ON -DMIMALLOC_STATIC=ON -DCMAKE_INSTALL_PREFIX=install
143+
./alpine.sh cmake . -G Ninja -DCMAKE_CXX_FLAGS="-static" -DCMAKE_C_FLAGS="-static" -DCMAKE_BUILD_TYPE=Release -DBUILD_STATIC_LIB=ON -DBUILD_MIMALLOC=ON -DCMAKE_INSTALL_PREFIX=install
144144
145145
- name: build
146146
run: |

CMakeLists.txt

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,10 @@ endif()
5656
# Advised to turn on when statically linking against musl libc (e.g., in the
5757
# Alpine Linux build we use for producing official Linux binaries), because
5858
# musl libc's allocator has very bad performance on heavily multi-threaded
59-
# workloads / high core count machines.
59+
# workloads / high core count machines. But it also works with dynamic linking
60+
# with a small performance advantage in some cases over the glibc allocator.
6061
# See https://github.com/WebAssembly/binaryen/issues/5561.
61-
option(MIMALLOC_STATIC "Build with statically linked mimalloc allocator" OFF)
62+
option(BUILD_MIMALLOC "Build with mimalloc allocator" OFF)
6263

6364
# Turn this off to install only tools and not static/dynamic libs.
6465
option(INSTALL_LIBS "Install libraries" ON)
@@ -467,12 +468,16 @@ if(BUILD_LLVM_DWARF)
467468
target_link_libraries(binaryen llvm_dwarf)
468469
endif()
469470

470-
if(MIMALLOC_STATIC)
471-
if(NOT(CMAKE_SYSTEM_NAME STREQUAL "Linux" AND BUILD_STATIC_LIB) OR EMSCRIPTEN)
472-
message(FATAL_ERROR "Statically linking mimalloc is only supported when building as a native, statically linked library on Linux.")
471+
if(BUILD_MIMALLOC)
472+
if(NOT(CMAKE_SYSTEM_NAME STREQUAL "Linux") OR EMSCRIPTEN)
473+
message(FATAL_ERROR "Linking mimalloc is only supported on Linux.")
474+
endif()
475+
message(STATUS "Building with mimalloc allocator.")
476+
if(BUILD_STATIC_LIB)
477+
target_link_libraries(binaryen mimalloc-static)
478+
else()
479+
target_link_libraries(binaryen mimalloc)
473480
endif()
474-
message(STATUS "Building with statically linked mimalloc allocator.")
475-
target_link_libraries(binaryen mimalloc-static)
476481
endif()
477482

478483
add_subdirectory(src/ir)

third_party/CMakeLists.txt

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,23 @@ if(BUILD_LLVM_DWARF)
1616
add_subdirectory(llvm-project)
1717
endif()
1818

19-
if(MIMALLOC_STATIC)
20-
# We only need the static library, nothing else.
21-
set(MI_BUILD_STATIC ON)
22-
set(MI_BUILD_SHARED OFF)
19+
if(BUILD_MIMALLOC)
20+
# Match static/dynamic linking between libbinaryen and mimalloc
21+
set(MI_BUILD_STATIC ${BUILD_STATIC_LIB})
22+
if (BUILD_STATIC_LIB)
23+
set(MI_BUILD_SHARED OFF)
24+
endif()
2325
set(MI_BUILD_OBJECT OFF)
2426
set(MI_BUILD_TESTS OFF)
27+
set(MI_INSTALL_TOPLEVEL ON)
2528
# Do not show debug and warning messages of the allocator by default.
2629
# (They can still be enabled via MIMALLOC_VERBOSE=1 wasm-opt ...)
2730
add_compile_definitions(MI_DEBUG=0)
2831

29-
add_subdirectory(mimalloc EXCLUDE_FROM_ALL)
32+
if(BUILD_STATIC_LIB)
33+
# No need to install libmimalloc.a when it's linked statically into the tools.
34+
add_subdirectory(mimalloc EXCLUDE_FROM_ALL)
35+
else()
36+
add_subdirectory(mimalloc)
37+
endif()
3038
endif()

0 commit comments

Comments
 (0)