[mimalloc,vcpkg-ci-mimalloc] Fix exported usage, add test port (#37499)

Resolves #37489.
This commit is contained in:
Kai Pastor 2024-03-23 00:28:56 +01:00 committed by GitHub
parent 112d277d10
commit 247dc608bf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 208 additions and 6 deletions

View File

@ -1,6 +1,19 @@
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 2bcd1ef..351817c 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -380,11 +380,11 @@ target_link_libraries(mimalloc PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/bin/mimalloc-redirect${MIMALLOC_REDIRECT_SUFFIX}.lib)
@@ -297,6 +297,10 @@ if (MSVC AND MSVC_VERSION GREATER_EQUAL 1914)
list(APPEND mi_cflags /Zc:__cplusplus)
endif()
+if(MINGW)
+ add_definitions(-D_WIN32_WINNT=0x600)
+endif()
+
# extra needed libraries
if(WIN32)
list(APPEND mi_libraries psapi shell32 user32 advapi32 bcrypt)
@@ -412,10 +416,10 @@ if(MI_BUILD_SHARED)
add_custom_command(TARGET mimalloc POST_BUILD
COMMAND "${CMAKE_COMMAND}" -E copy "${CMAKE_CURRENT_SOURCE_DIR}/bin/mimalloc-redirect${MIMALLOC_REDIRECT_SUFFIX}.dll" $<TARGET_FILE_DIR:mimalloc>
COMMENT "Copy mimalloc-redirect${MIMALLOC_REDIRECT_SUFFIX}.dll to output directory")
@ -9,8 +22,7 @@
endif()
- install(TARGETS mimalloc EXPORT mimalloc DESTINATION ${mi_install_libdir} LIBRARY)
+ install(TARGETS mimalloc EXPORT mimalloc ARCHIVE DESTINATION ${mi_install_libdir} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} LIBRARY DESTINATION ${mi_install_libdir} NAMELINK_SKIP)
+ install(TARGETS mimalloc EXPORT mimalloc ARCHIVE DESTINATION ${mi_install_libdir} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} LIBRARY DESTINATION ${mi_install_libdir})
install(EXPORT mimalloc DESTINATION ${mi_install_cmakedir})
endif()
# static library

View File

@ -45,7 +45,7 @@ file(COPY
)
vcpkg_cmake_config_fixup(CONFIG_PATH lib/cmake/mimalloc)
if(VCPKG_LIBRARY_LINKAGE STREQUAL dynamic)
if(VCPKG_LIBRARY_LINKAGE STREQUAL "dynamic")
vcpkg_replace_string(
"${CURRENT_PACKAGES_DIR}/include/mimalloc.h"
"!defined(MI_SHARED_LIB)"
@ -56,5 +56,16 @@ file(REMOVE_RECURSE "${CURRENT_PACKAGES_DIR}/debug/include")
file(REMOVE_RECURSE "${CURRENT_PACKAGES_DIR}/debug/share")
vcpkg_fixup_pkgconfig()
set(mi_basename "mimalloc")
if(VCPKG_TARGET_IS_WINDOWS AND VCPKG_LIBRARY_LINKAGE STREQUAL "static")
string(APPEND mi_basename "-static")
endif()
if("secure" IN_LIST FEATURES)
string(APPEND mi_basename "-secure")
endif()
vcpkg_replace_string("${CURRENT_PACKAGES_DIR}/lib/pkgconfig/mimalloc.pc" " -lmimalloc" " -l${mi_basename}")
if(NOT VCPKG_BUILD_TYPE)
vcpkg_replace_string("${CURRENT_PACKAGES_DIR}/debug/lib/pkgconfig/mimalloc.pc" " -lmimalloc" " -l${mi_basename}-debug")
endif()
vcpkg_install_copyright(FILE_LIST "${SOURCE_PATH}/LICENSE")

View File

@ -2,3 +2,8 @@ mimalloc provides CMake targets:
find_package(mimalloc CONFIG REQUIRED)
target_link_libraries(main PRIVATE $<IF:$<TARGET_EXISTS:mimalloc-static>,mimalloc-static,mimalloc)
To ensure the mimalloc-override.dll is loaded at runtime on Windows with
dynamic linkage, it is suggested to insert some call to the mimalloc API
in the main function, eg. `mi_version()`. Cf.
https://github.com/microsoft/mimalloc/blob/dev/readme.md#dynamic-override-on-windows

View File

@ -1,5 +1,17 @@
_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_OBJECT_DIR-NOTFOUND") # not installed
set(MIMALLOC_TARGET_DIR "${MIMALLOC_LIBRARY_DIR}")
endif()
if(TARGET mimalloc AND NOT TARGET mimalloc-static)
add_library(mimalloc-static INTERFACE IMPORTED)
set_target_properties(mimalloc-static PROPERTIES INTERFACE_LINK_LIBRARIES mimalloc)

View File

@ -1,7 +1,7 @@
{
"name": "mimalloc",
"version": "2.1.2",
"port-version": 1,
"port-version": 2,
"description": "Compact general purpose allocator with excellent performance",
"homepage": "https://github.com/microsoft/mimalloc",
"license": "MIT",

View File

@ -0,0 +1,33 @@
set(VCPKG_POLICY_EMPTY_PACKAGE enabled)
vcpkg_find_acquire_program(PKGCONFIG)
vcpkg_from_github(
OUT_SOURCE_PATH SOURCE_PATH
REPO microsoft/mimalloc
REF "v${VERSION}"
SHA512 927b046e67783b325a6e41e3a9a6d3d78306fa1c82255defd1f3a7a60a27fd809a601f65b1b27fa38f2064e124f29856d7c0e5ccc33c54c2e4b6ebb9816d74b1
HEAD_REF master
PATCHES
vcpkg-tests.patch
)
# Ensure that the test uses the installed mimalloc only
file(REMOVE_RECURSE
"${SOURCE_PATH}/bin"
"${SOURCE_PATH}/include"
"${SOURCE_PATH}/src"
)
vcpkg_cmake_configure(
SOURCE_PATH "${SOURCE_PATH}/test"
OPTIONS
"-DPKG_CONFIG_EXECUTABLE=${PKGCONFIG}"
"-DVCPKG_TESTS=${CURRENT_PORT_DIR}/vcpkg-tests.cmake"
)
vcpkg_cmake_install(ADD_BIN_TO_PATH)
vcpkg_copy_tools(TOOL_NAMES pkgconfig-override-cxx AUTO_CLEAN)
file(REMOVE_RECURSE "${CURRENT_PACKAGES_DIR}/debug")
vcpkg_install_copyright(FILE_LIST "${SOURCE_PATH}/LICENSE")

View File

@ -0,0 +1,47 @@
find_package(PkgConfig REQUIRED)
# Legacy variables
message(STATUS "MIMALLOC_INCLUDE_DIR: ${MIMALLOC_INCLUDE_DIR}")
message(STATUS "MIMALLOC_LIBRARY_DIR: ${MIMALLOC_LIBRARY_DIR}")
find_file(mimalloc_h NAMES mimalloc.h PATHS "${MIMALLOC_INCLUDE_DIR}" NO_DEFAULT_PATH REQUIRED)
set(names
mimalloc
mimalloc-secure
mimalloc-static
mimalloc-static-secure
mimalloc-debug
mimalloc-secure-debug
mimalloc-static-debug
mimalloc-static-secure-debug
)
find_library(mimalloc_lib NAMES ${names} PATHS "${MIMALLOC_LIBRARY_DIR}" NO_DEFAULT_PATH REQUIRED)
# pkgconfig
pkg_check_modules(PC_MIMALLOC mimalloc IMPORTED_TARGET REQUIRED)
if(EXPECTED_FAILURE_DUE_TO_CXX_LINKAGE_OF_MIMALLOC)
add_executable(pkgconfig-override $<IF:$<BOOL:${BUILD_SHARED_LIBS}>,main-override.c,main-override-static.c>)
target_link_libraries(pkgconfig-override PRIVATE PkgConfig::PC_MIMALLOC)
endif()
add_executable(pkgconfig-override-cxx main-override.cpp)
target_link_libraries(pkgconfig-override-cxx PRIVATE PkgConfig::PC_MIMALLOC Threads::Threads)
# Runtime
if(NOT CMAKE_CROSSCOMPILING)
if(BUILD_SHARED_LIBS)
add_custom_target(run-dynamic-override ALL COMMAND $<TARGET_NAME:dynamic-override>)
add_custom_target(run-dynamic-override-cxx ALL COMMAND $<TARGET_NAME:dynamic-override-cxx>)
else()
add_custom_target(run-static-override ALL COMMAND $<TARGET_NAME:static-override>)
add_custom_target(run-static-override-cxx ALL COMMAND $<TARGET_NAME:static-override-cxx>)
endif()
add_custom_target(run-pkgconfig-override-cxx ALL COMMAND $<TARGET_NAME:pkgconfig-override-cxx>)
endif()
# Deployment
install(TARGETS pkgconfig-override-cxx)

View File

@ -0,0 +1,65 @@
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index e76ffa6..a925a37 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -1,4 +1,4 @@
-cmake_minimum_required(VERSION 3.0)
+cmake_minimum_required(VERSION 3.5)
project(mimalloc-test C CXX)
set(CMAKE_C_STANDARD 11)
@@ -19,21 +19,30 @@ endif()
find_package(mimalloc 2.0 REQUIRED NO_SYSTEM_ENVIRONMENT_PATH)
message(STATUS "Found mimalloc installed at: ${MIMALLOC_LIBRARY_DIR} (${MIMALLOC_VERSION_DIR})")
+set(THREADS_PREFER_PTHREAD_FLAG 1)
+find_package(Threads REQUIRED)
+
+if(BUILD_SHARED_LIBS)
# overriding with a dynamic library
add_executable(dynamic-override main-override.c)
target_link_libraries(dynamic-override PUBLIC mimalloc)
add_executable(dynamic-override-cxx main-override.cpp)
target_link_libraries(dynamic-override-cxx PUBLIC mimalloc)
+target_link_libraries(dynamic-override-cxx PRIVATE Threads::Threads)
+endif()
+if(0)
# overriding with a static object file works reliable as the symbols in the
# object file have priority over those in library files
add_executable(static-override-obj main-override.c ${MIMALLOC_OBJECT_DIR}/mimalloc.o)
target_include_directories(static-override-obj PUBLIC ${MIMALLOC_INCLUDE_DIR})
target_link_libraries(static-override-obj PUBLIC pthread)
+endif()
+if(NOT BUILD_SHARED_LIBS)
# overriding with a static library works too if using the `mimalloc-override.h`
# header to redefine malloc/free. (the library already overrides new/delete)
add_executable(static-override-static main-override-static.c)
@@ -47,6 +56,10 @@ target_link_libraries(static-override PUBLIC mimalloc-static)
add_executable(static-override-cxx main-override.cpp)
target_link_libraries(static-override-cxx PUBLIC mimalloc-static)
+target_link_libraries(static-override-cxx PRIVATE Threads::Threads)
+endif()
+
+include("${VCPKG_TESTS}")
## test memory errors
diff --git a/test/main-override.cpp b/test/main-override.cpp
index c1607b6..9e9824c 100644
--- a/test/main-override.cpp
+++ b/test/main-override.cpp
@@ -19,7 +19,7 @@
#endif
#ifdef _WIN32
-#include <Windows.h>
+#include <windows.h>
static void msleep(unsigned long msecs) { Sleep(msecs); }
#else
#include <unistd.h>

View File

@ -0,0 +1,12 @@
{
"name": "vcpkg-ci-mimalloc",
"version": "2.1.2",
"description": "Validates mimalloc",
"dependencies": [
"mimalloc",
{
"name": "vcpkg-cmake",
"host": true
}
]
}

View File

@ -5694,7 +5694,7 @@
},
"mimalloc": {
"baseline": "2.1.2",
"port-version": 1
"port-version": 2
},
"minc": {
"baseline": "2.4.03",

View File

@ -1,5 +1,10 @@
{
"versions": [
{
"git-tree": "d4b6f2a9a4e1e46eb83fe8ce48b36baa58af4977",
"version": "2.1.2",
"port-version": 2
},
{
"git-tree": "db19c496e6855e8aeb3a8666ffae862910b403c4",
"version": "2.1.2",