diff --git a/contrib/vcpkg/portfile.cmake b/contrib/vcpkg/portfile.cmake new file mode 100644 index 00000000..47bf30cb --- /dev/null +++ b/contrib/vcpkg/portfile.cmake @@ -0,0 +1,63 @@ +vcpkg_from_github( + OUT_SOURCE_PATH SOURCE_PATH + REPO microsoft/mimalloc + HEAD_REF master + + # The "REF" can be a commit hash, branch name (dev2), or a version (v2.2.1). + # REF "v${VERSION}" + REF 3f91c9f937d9a7b26c130393925e7364a26c7880 + + # The sha512 is the hash of the tar.gz bundle. + # (To get the sha512, run `./vcpkg install mimalloc --overlay-ports=` and copy the sha from the error message.) + SHA512 bb29283e95786c064ce6fa5a36488dafa85f7e979afe3d8fc1361997c978d13557ed27904dda9b8d29c576ad32d7dff5c674dc60c7a5891224e3f1f56bbae73c +) + +vcpkg_check_features(OUT_FEATURE_OPTIONS FEATURE_OPTIONS + FEATURES + c MI_NO_USE_CXX + guarded MI_GUARDED + secure MI_SECURE + override MI_OVERRIDE + xmalloc MI_XMALLOC + asm MI_SEE_ASM +) +string(COMPARE EQUAL "${VCPKG_LIBRARY_LINKAGE}" "static" MI_BUILD_STATIC) +string(COMPARE EQUAL "${VCPKG_LIBRARY_LINKAGE}" "dynamic" MI_BUILD_SHARED) + +vcpkg_cmake_configure( + SOURCE_PATH "${SOURCE_PATH}" + OPTIONS_RELEASE + -DMI_OPT_ARCH=ON + OPTIONS + -DMI_USE_CXX=ON + -DMI_BUILD_TESTS=OFF + -DMI_BUILD_OBJECT=ON + ${FEATURE_OPTIONS} + -DMI_BUILD_STATIC=${MI_BUILD_STATIC} + -DMI_BUILD_SHARED=${MI_BUILD_SHARED} + -DMI_INSTALL_TOPLEVEL=ON +) + +vcpkg_cmake_install() +vcpkg_copy_pdbs() + +file(COPY + "${CMAKE_CURRENT_LIST_DIR}/vcpkg-cmake-wrapper.cmake" + "${CMAKE_CURRENT_LIST_DIR}/usage" + DESTINATION "${CURRENT_PACKAGES_DIR}/share/${PORT}" +) +vcpkg_cmake_config_fixup(CONFIG_PATH lib/cmake/mimalloc) + +if(VCPKG_LIBRARY_LINKAGE STREQUAL "dynamic") + # todo: why is this needed? + vcpkg_replace_string( + "${CURRENT_PACKAGES_DIR}/include/mimalloc.h" + "!defined(MI_SHARED_LIB)" + "0 // !defined(MI_SHARED_LIB)" + ) +endif() +file(REMOVE_RECURSE "${CURRENT_PACKAGES_DIR}/debug/include") +file(REMOVE_RECURSE "${CURRENT_PACKAGES_DIR}/debug/share") + +vcpkg_fixup_pkgconfig() +vcpkg_install_copyright(FILE_LIST "${SOURCE_PATH}/LICENSE") diff --git a/contrib/vcpkg/readme.md b/contrib/vcpkg/readme.md new file mode 100644 index 00000000..d1b366a5 --- /dev/null +++ b/contrib/vcpkg/readme.md @@ -0,0 +1,36 @@ +# Vcpkg support + +This directory is meant to provide the sources for the official [vcpkg port] +of mimalloc, but can also be used to override the official port with +your own variant. + +For example, you can edit the [`portfile.cmake`](portfile.cmake) +to check out a specific commit, version, or branch of mimalloc, or set further options. +You can install such custom port as: +```sh +$ vcpkg install mimalloc --overlay-ports=./contrib/vcpkg +``` + +This will also show the correct sha512 hash if you use a custom version. +See the vcpkg [documentation](https://learn.microsoft.com/en-us/vcpkg/produce/update-package-version) for more information. + + +# Using mimalloc from vcpkg + +When using [cmake with vcpkg](https://learn.microsoft.com/en-us/vcpkg/get_started/get-started?pivots=shell-powershell), +you can use mimalloc from the `CMakeLists.txt` as: + +```cmake +find_package(mimalloc CONFIG REQUIRED) +target_link_libraries(main PRIVATE mimalloc) +``` + + +# Acknowledgements + +The original port for vckpg was contributed by various people, including: @vicroms, @myd7349, @PhoubeHui, @LilyWangL, +@JonLiu1993, @RT2Code, Remy Tassoux, @wangao, @BillyONeal, @jiayuehua, @dg0yt, @gerar-ryan-immersaview, @nickdademo, +and @jimwang118 -- Thank you so much! + + +[vcpkg port]: https://github.com/microsoft/vcpkg/tree/master/ports/mimalloc diff --git a/contrib/vcpkg/usage b/contrib/vcpkg/usage new file mode 100644 index 00000000..08f79c97 --- /dev/null +++ b/contrib/vcpkg/usage @@ -0,0 +1,21 @@ +Use the following CMake targets to import mimalloc: + + find_package(mimalloc CONFIG REQUIRED) + target_link_libraries(main PRIVATE mimalloc) + +And use mimalloc in your sources as: + + #include + #include + int main(int argc, char** argv) { + int* p = mi_malloc_tp(int); + *p = mi_version(); + printf("mimalloc version: %d\n", *p); + mi_free(p); + return 0; + } + +When dynamically overriding on Windows, ensure `mimalloc.dll` is linked +through some call to the mimalloc API, e.g. `mi_version()`, and that +the `mimalloc-redirect.dll` is in the same directory. +See https://github.com/microsoft/mimalloc/blob/dev/bin/readme.md for detailed information. diff --git a/contrib/vcpkg/vcpkg-cmake-wrapper.cmake b/contrib/vcpkg/vcpkg-cmake-wrapper.cmake new file mode 100644 index 00000000..1b355722 --- /dev/null +++ b/contrib/vcpkg/vcpkg-cmake-wrapper.cmake @@ -0,0 +1,20 @@ +_find_package(${ARGS}) + +if(CMAKE_CURRENT_LIST_DIR STREQUAL "${MIMALLOC_CMAKE_DIR}/${MIMALLOC_VERSION_DIR}") + set(MIMALLOC_INCLUDE_DIR "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/include") + # As in vcpkg.cmake + if(NOT DEFINED CMAKE_BUILD_TYPE OR CMAKE_BUILD_TYPE MATCHES "^[Dd][Ee][Bb][Uu][Gg]$") + set(MIMALLOC_LIBRARY_DIR "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/debug/lib") + else() + set(MIMALLOC_LIBRARY_DIR "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/lib") + endif() + set(MIMALLOC_OBJECT_DIR "${MIMALLOC_LIBRARY_DIR}") + set(MIMALLOC_TARGET_DIR "${MIMALLOC_LIBRARY_DIR}") +endif() + +# vcpkg always configures either a static or dynamic library. +# ensure to always expose the mimalloc target as either the static or dynamic build. +if(TARGET mimalloc-static AND NOT TARGET mimalloc) + add_library(mimalloc INTERFACE IMPORTED) + set_target_properties(mimalloc PROPERTIES INTERFACE_LINK_LIBRARIES mimalloc-static) +endif() diff --git a/contrib/vcpkg/vcpkg.json b/contrib/vcpkg/vcpkg.json new file mode 100644 index 00000000..69a72edf --- /dev/null +++ b/contrib/vcpkg/vcpkg.json @@ -0,0 +1,36 @@ +{ + "name": "mimalloc", + "version": "1.9.2", + "port-version": 2, + "description": "Compact general purpose allocator with excellent performance", + "homepage": "https://github.com/microsoft/mimalloc", + "license": "MIT", + "supports": "!uwp", + "dependencies": [ + { + "name": "vcpkg-cmake", + "host": true + }, + { + "name": "vcpkg-cmake-config", + "host": true + } + ], + "features": { + "c": { + "description": "Use C11 compilation (this can still override new/delete)" + }, + "override": { + "description": "Override the standard malloc/free interface" + }, + "secure": { + "description": "Use full security mitigations (like guard pages and randomization)" + }, + "xmalloc": { + "description": "If out-of-memory, call abort() instead of returning NULL" + }, + "asm": { + "description": "Generate assembly files" + } + } +} \ No newline at end of file