Skip to content

Commit 139180c

Browse files
authored
Adding vcpkg integration (#825)
* Adding the vcpkg integration files Signed-off-by: Matthew A Johnson <matjoh@microsoft.com>
1 parent c2f0d1c commit 139180c

File tree

10 files changed

+226
-2
lines changed

10 files changed

+226
-2
lines changed

.github/workflows/main.yml

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,80 @@ jobs:
446446
cd ${{github.workspace}}/build
447447
ctest --parallel --output-on-failure
448448
449+
# ============================================================================
450+
# vcpkg integration
451+
# ============================================================================
452+
vcpkg-integration:
453+
strategy:
454+
fail-fast: false
455+
matrix:
456+
os: [ ubuntu-latest, macos-latest, windows-latest ]
457+
feature: [ "default", "static-shim" ]
458+
runs-on: ${{ matrix.os }}
459+
name: vcpkg - ${{ matrix.os }} ${{ matrix.feature }}
460+
steps:
461+
- uses: actions/checkout@v4
462+
463+
- name: Bootstrap vcpkg
464+
shell: bash
465+
run: |
466+
git clone https://github.com/microsoft/vcpkg.git "${{ runner.temp }}/vcpkg" --depth 1
467+
if [ "$RUNNER_OS" = "Windows" ]; then
468+
"${{ runner.temp }}/vcpkg/bootstrap-vcpkg.bat" -disableMetrics
469+
else
470+
"${{ runner.temp }}/vcpkg/bootstrap-vcpkg.sh" -disableMetrics
471+
fi
472+
473+
- name: Create overlay port pointing at local source
474+
shell: bash
475+
run: |
476+
overlay="${{ runner.temp }}/overlay/snmalloc"
477+
mkdir -p "$overlay"
478+
# Copy the real port files
479+
cp ports/snmalloc/vcpkg.json "$overlay/"
480+
cp ports/snmalloc/usage "$overlay/"
481+
# Normalize workspace path to forward slashes for CMake (Windows compat)
482+
ws_path=$(echo "${{ github.workspace }}" | sed 's|\\|/|g')
483+
# Rewrite portfile to use local source instead of vcpkg_from_github
484+
{
485+
echo 'if(NOT "static-shim" IN_LIST FEATURES)'
486+
echo ' set(VCPKG_BUILD_TYPE release)'
487+
echo 'endif()'
488+
echo ''
489+
echo "set(SOURCE_PATH \"${ws_path}\")"
490+
echo ''
491+
# Keep everything from vcpkg_check_features onwards (see comment in
492+
# ports/snmalloc/portfile.cmake documenting this coupling)
493+
sed -n '/^vcpkg_check_features/,$ p' ports/snmalloc/portfile.cmake
494+
} > "$overlay/portfile.cmake"
495+
496+
- name: Install snmalloc via vcpkg
497+
shell: bash
498+
# Run from runner.temp to avoid the workspace vcpkg.json triggering
499+
# manifest mode (which forbids positional package arguments).
500+
working-directory: ${{ runner.temp }}
501+
run: |
502+
features=""
503+
if [ "${{ matrix.feature }}" = "static-shim" ]; then
504+
features="[static-shim]"
505+
fi
506+
"${{ runner.temp }}/vcpkg/vcpkg" install "snmalloc${features}" \
507+
--overlay-ports="${{ runner.temp }}/overlay"
508+
509+
- name: Build consumer project
510+
shell: bash
511+
run: |
512+
cmake -B "${{ runner.temp }}/consumer-build" \
513+
-S test/vcpkg-consumer \
514+
-DCMAKE_TOOLCHAIN_FILE="${{ runner.temp }}/vcpkg/scripts/buildsystems/vcpkg.cmake"
515+
cmake --build "${{ runner.temp }}/consumer-build" --config Release
516+
517+
- name: Run tests
518+
shell: bash
519+
run: |
520+
ctest --test-dir "${{ runner.temp }}/consumer-build" \
521+
--build-config Release --output-on-failure
522+
449523
# ============================================================================
450524
# Final gate check
451525
# ============================================================================
@@ -456,7 +530,8 @@ jobs:
456530
freebsd, netbsd,
457531
qemu-crossbuild,
458532
windows,
459-
format
533+
format,
534+
vcpkg-integration
460535
]
461536
runs-on: ubuntu-24.04
462537
steps:

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ endif()
292292
# Add header paths.
293293
target_include_directories(snmalloc
294294
INTERFACE
295-
$<INSTALL_INTERFACE:include/snmalloc>
295+
$<INSTALL_INTERFACE:include>
296296
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>)
297297

298298
if(NOT MSVC)

ports/snmalloc/portfile.cmake

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Without static-shim: INTERFACE only — Release and Debug are identical.
2+
# With static-shim: compiled code is produced — both variants are needed.
3+
if(NOT "static-shim" IN_LIST FEATURES)
4+
set(VCPKG_BUILD_TYPE release)
5+
endif()
6+
7+
vcpkg_from_github(
8+
OUT_SOURCE_PATH SOURCE_PATH
9+
REPO microsoft/snmalloc
10+
REF "v${VERSION}"
11+
SHA512 0 # Placeholder: compute the real SHA512 from the GitHub release tarball before publishing to the vcpkg registry.
12+
HEAD_REF main
13+
)
14+
15+
# NOTE: The CI overlay port (see .github/workflows/main.yml, vcpkg-integration)
16+
# uses sed to extract from this line onwards to build a portfile that points at
17+
# the local checkout. If you reorder code above this line, update the sed
18+
# pattern there.
19+
vcpkg_check_features(OUT_FEATURE_OPTIONS FEATURE_OPTIONS
20+
FEATURES
21+
"static-shim" SNMALLOC_STATIC_LIBRARY
22+
INVERTED_FEATURES
23+
"static-shim" SNMALLOC_HEADER_ONLY_LIBRARY
24+
)
25+
26+
vcpkg_cmake_configure(
27+
SOURCE_PATH "${SOURCE_PATH}"
28+
OPTIONS
29+
-DSNMALLOC_BUILD_TESTING=OFF
30+
${FEATURE_OPTIONS}
31+
)
32+
33+
vcpkg_cmake_install()
34+
35+
vcpkg_cmake_config_fixup(
36+
PACKAGE_NAME snmalloc
37+
CONFIG_PATH share/snmalloc
38+
)
39+
40+
file(REMOVE_RECURSE "${CURRENT_PACKAGES_DIR}/debug/include")
41+
file(REMOVE_RECURSE "${CURRENT_PACKAGES_DIR}/debug/share")
42+
43+
vcpkg_install_copyright(FILE_LIST "${SOURCE_PATH}/LICENSE")
44+
45+
file(INSTALL "${CMAKE_CURRENT_LIST_DIR}/usage"
46+
DESTINATION "${CURRENT_PACKAGES_DIR}/share/${PORT}")

ports/snmalloc/usage

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
snmalloc provides CMake integration via:
2+
3+
find_package(snmalloc CONFIG REQUIRED)
4+
target_link_libraries(<your-target> PRIVATE snmalloc::snmalloc)
5+
6+
If installed with the "static-shim" feature, a compiled static library is also
7+
available that replaces malloc/free with a "sn_" prefix (e.g. sn_malloc, sn_free):
8+
9+
target_link_libraries(<your-target> PRIVATE snmalloc::snmallocshim-static)
10+
11+
On non-Windows, the "static-shim" feature also installs shared library shims
12+
(snmalloc::snmallocshim, snmalloc::snmallocshim-checks, snmalloc::snmalloc-minimal)
13+
that can be used for LD_PRELOAD-based allocator replacement.

ports/snmalloc/vcpkg.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "snmalloc",
3+
"version-semver": "0.7.4",
4+
"description": "A high-performance, message passing based allocator",
5+
"homepage": "https://github.com/microsoft/snmalloc",
6+
"license": "MIT",
7+
"dependencies": [
8+
{
9+
"name": "vcpkg-cmake",
10+
"host": true
11+
},
12+
{
13+
"name": "vcpkg-cmake-config",
14+
"host": true
15+
}
16+
],
17+
"features": {
18+
"static-shim": {
19+
"description": "Build and install snmallocshim-static, a compiled static library that exports malloc/free with a configurable symbol prefix (default: sn_)"
20+
}
21+
}
22+
}

src/snmalloc/pal/pal_windows.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
(WINVER >= _WIN32_WINNT_WIN10) && !defined(USE_SYSTEMATIC_TESTING)
2525
# define PLATFORM_HAS_VIRTUALALLOC2
2626
# define PLATFORM_HAS_WAITONADDRESS
27+
# pragma comment(lib, "mincore.lib")
28+
# pragma comment(lib, "synchronization.lib")
2729
# endif
2830
# endif
2931

test/vcpkg-consumer/CMakeLists.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
cmake_minimum_required(VERSION 3.16)
2+
project(snmalloc-vcpkg-test CXX)
3+
4+
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
5+
set(CMAKE_VERBOSE_MAKEFILE ON)
6+
7+
enable_testing()
8+
9+
find_package(snmalloc CONFIG REQUIRED)
10+
11+
add_executable(test-header-only test_header_only.cpp)
12+
target_link_libraries(test-header-only PRIVATE snmalloc::snmalloc)
13+
add_test(NAME header-only COMMAND test-header-only)
14+
15+
if(TARGET snmalloc::snmallocshim-static)
16+
add_executable(test-static-shim test_static_shim.cpp)
17+
target_link_libraries(test-static-shim PRIVATE snmalloc::snmallocshim-static)
18+
add_test(NAME static-shim COMMAND test-static-shim)
19+
endif()
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Minimal vcpkg integration test for the snmalloc header-only target.
3+
* Verifies that find_package(snmalloc) + snmalloc::snmalloc works.
4+
*/
5+
#include <snmalloc/snmalloc.h>
6+
7+
int main()
8+
{
9+
void* p = snmalloc::libc::malloc(64);
10+
if (p == nullptr)
11+
return 1;
12+
snmalloc::libc::free(p);
13+
return 0;
14+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Minimal vcpkg integration test for the snmallocshim-static target.
3+
* Verifies that find_package(snmalloc) + snmalloc::snmallocshim-static works.
4+
* The static shim replaces malloc/free with the snmalloc implementation.
5+
*/
6+
#include <stdlib.h>
7+
8+
extern "C"
9+
{
10+
void *sn_malloc(size_t);
11+
void sn_free(void *);
12+
}
13+
14+
int main()
15+
{
16+
void* p = sn_malloc(64);
17+
if (p == nullptr)
18+
return 1;
19+
sn_free(p);
20+
return 0;
21+
}

vcpkg.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "snmalloc",
3+
"version-semver": "0.7.4",
4+
"description": "A high-performance, message passing based allocator",
5+
"homepage": "https://github.com/microsoft/snmalloc",
6+
"license": "MIT",
7+
"features": {
8+
"static-shim": {
9+
"description": "Build and install snmallocshim-static, a compiled static library that exports malloc/free with a configurable symbol prefix (default: sn_)"
10+
}
11+
}
12+
}

0 commit comments

Comments
 (0)