Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 76 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,80 @@ jobs:
cd ${{github.workspace}}/build
ctest --parallel --output-on-failure

# ============================================================================
# vcpkg integration
# ============================================================================
vcpkg-integration:
strategy:
fail-fast: false
matrix:
os: [ ubuntu-latest, macos-latest, windows-latest ]
feature: [ "default", "static-shim" ]
runs-on: ${{ matrix.os }}
name: vcpkg - ${{ matrix.os }} ${{ matrix.feature }}
steps:
- uses: actions/checkout@v4

- name: Bootstrap vcpkg
shell: bash
run: |
git clone https://github.com/microsoft/vcpkg.git "${{ runner.temp }}/vcpkg" --depth 1
if [ "$RUNNER_OS" = "Windows" ]; then
"${{ runner.temp }}/vcpkg/bootstrap-vcpkg.bat" -disableMetrics
else
"${{ runner.temp }}/vcpkg/bootstrap-vcpkg.sh" -disableMetrics
fi

- name: Create overlay port pointing at local source
shell: bash
run: |
overlay="${{ runner.temp }}/overlay/snmalloc"
mkdir -p "$overlay"
# Copy the real port files
cp ports/snmalloc/vcpkg.json "$overlay/"
cp ports/snmalloc/usage "$overlay/"
# Normalize workspace path to forward slashes for CMake (Windows compat)
ws_path=$(echo "${{ github.workspace }}" | sed 's|\\|/|g')
# Rewrite portfile to use local source instead of vcpkg_from_github
{
echo 'if(NOT "static-shim" IN_LIST FEATURES)'
echo ' set(VCPKG_BUILD_TYPE release)'
echo 'endif()'
echo ''
echo "set(SOURCE_PATH \"${ws_path}\")"
echo ''
# Keep everything from vcpkg_check_features onwards (see comment in
# ports/snmalloc/portfile.cmake documenting this coupling)
sed -n '/^vcpkg_check_features/,$ p' ports/snmalloc/portfile.cmake
} > "$overlay/portfile.cmake"

- name: Install snmalloc via vcpkg
shell: bash
# Run from runner.temp to avoid the workspace vcpkg.json triggering
# manifest mode (which forbids positional package arguments).
working-directory: ${{ runner.temp }}
run: |
features=""
if [ "${{ matrix.feature }}" = "static-shim" ]; then
features="[static-shim]"
fi
"${{ runner.temp }}/vcpkg/vcpkg" install "snmalloc${features}" \
--overlay-ports="${{ runner.temp }}/overlay"

- name: Build consumer project
shell: bash
run: |
cmake -B "${{ runner.temp }}/consumer-build" \
-S test/vcpkg-consumer \
-DCMAKE_TOOLCHAIN_FILE="${{ runner.temp }}/vcpkg/scripts/buildsystems/vcpkg.cmake"
cmake --build "${{ runner.temp }}/consumer-build" --config Release

- name: Run tests
shell: bash
run: |
ctest --test-dir "${{ runner.temp }}/consumer-build" \
--build-config Release --output-on-failure

# ============================================================================
# Final gate check
# ============================================================================
Expand All @@ -456,7 +530,8 @@ jobs:
freebsd, netbsd,
qemu-crossbuild,
windows,
format
format,
vcpkg-integration
]
runs-on: ubuntu-24.04
steps:
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ endif()
# Add header paths.
target_include_directories(snmalloc
INTERFACE
$<INSTALL_INTERFACE:include/snmalloc>
$<INSTALL_INTERFACE:include>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the bit that has been wrong as an install target.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.

$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>)

if(NOT MSVC)
Expand Down
46 changes: 46 additions & 0 deletions ports/snmalloc/portfile.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Without static-shim: INTERFACE only — Release and Debug are identical.
# With static-shim: compiled code is produced — both variants are needed.
if(NOT "static-shim" IN_LIST FEATURES)
set(VCPKG_BUILD_TYPE release)
endif()

vcpkg_from_github(
OUT_SOURCE_PATH SOURCE_PATH
REPO microsoft/snmalloc
REF "v${VERSION}"
SHA512 0 # Placeholder: compute the real SHA512 from the GitHub release tarball before publishing to the vcpkg registry.
HEAD_REF main
)

# NOTE: The CI overlay port (see .github/workflows/main.yml, vcpkg-integration)
# uses sed to extract from this line onwards to build a portfile that points at
# the local checkout. If you reorder code above this line, update the sed
# pattern there.
vcpkg_check_features(OUT_FEATURE_OPTIONS FEATURE_OPTIONS
FEATURES
"static-shim" SNMALLOC_STATIC_LIBRARY
INVERTED_FEATURES
"static-shim" SNMALLOC_HEADER_ONLY_LIBRARY
)

vcpkg_cmake_configure(
SOURCE_PATH "${SOURCE_PATH}"
OPTIONS
-DSNMALLOC_BUILD_TESTING=OFF
${FEATURE_OPTIONS}
)

vcpkg_cmake_install()

vcpkg_cmake_config_fixup(
PACKAGE_NAME snmalloc
CONFIG_PATH share/snmalloc
)

file(REMOVE_RECURSE "${CURRENT_PACKAGES_DIR}/debug/include")
file(REMOVE_RECURSE "${CURRENT_PACKAGES_DIR}/debug/share")
Comment on lines +40 to +41
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand what these lines are doing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vcpkg only allows you to have one copy of the headers etc. under the install directory. If it didn't remove the debug copies, then the post-validation step would fail.


vcpkg_install_copyright(FILE_LIST "${SOURCE_PATH}/LICENSE")

file(INSTALL "${CMAKE_CURRENT_LIST_DIR}/usage"
DESTINATION "${CURRENT_PACKAGES_DIR}/share/${PORT}")
13 changes: 13 additions & 0 deletions ports/snmalloc/usage
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
snmalloc provides CMake integration via:

find_package(snmalloc CONFIG REQUIRED)
target_link_libraries(<your-target> PRIVATE snmalloc::snmalloc)

If installed with the "static-shim" feature, a compiled static library is also
available that replaces malloc/free with a "sn_" prefix (e.g. sn_malloc, sn_free):

target_link_libraries(<your-target> PRIVATE snmalloc::snmallocshim-static)

On non-Windows, the "static-shim" feature also installs shared library shims
(snmalloc::snmallocshim, snmalloc::snmallocshim-checks, snmalloc::snmalloc-minimal)
that can be used for LD_PRELOAD-based allocator replacement.
22 changes: 22 additions & 0 deletions ports/snmalloc/vcpkg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "snmalloc",
"version-semver": "0.7.4",
"description": "A high-performance, message passing based allocator",
"homepage": "https://github.com/microsoft/snmalloc",
"license": "MIT",
"dependencies": [
{
"name": "vcpkg-cmake",
"host": true
},
{
"name": "vcpkg-cmake-config",
"host": true
}
],
"features": {
"static-shim": {
"description": "Build and install snmallocshim-static, a compiled static library that exports malloc/free with a configurable symbol prefix (default: sn_)"
}
}
}
2 changes: 2 additions & 0 deletions src/snmalloc/pal/pal_windows.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
(WINVER >= _WIN32_WINNT_WIN10) && !defined(USE_SYSTEMATIC_TESTING)
# define PLATFORM_HAS_VIRTUALALLOC2
# define PLATFORM_HAS_WAITONADDRESS
# pragma comment(lib, "mincore.lib")
# pragma comment(lib, "synchronization.lib")
# endif
# endif

Expand Down
19 changes: 19 additions & 0 deletions test/vcpkg-consumer/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
cmake_minimum_required(VERSION 3.16)
project(snmalloc-vcpkg-test CXX)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_VERBOSE_MAKEFILE ON)

enable_testing()

find_package(snmalloc CONFIG REQUIRED)

add_executable(test-header-only test_header_only.cpp)
target_link_libraries(test-header-only PRIVATE snmalloc::snmalloc)
add_test(NAME header-only COMMAND test-header-only)

if(TARGET snmalloc::snmallocshim-static)
add_executable(test-static-shim test_static_shim.cpp)
target_link_libraries(test-static-shim PRIVATE snmalloc::snmallocshim-static)
add_test(NAME static-shim COMMAND test-static-shim)
endif()
14 changes: 14 additions & 0 deletions test/vcpkg-consumer/test_header_only.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Minimal vcpkg integration test for the snmalloc header-only target.
* Verifies that find_package(snmalloc) + snmalloc::snmalloc works.
*/
#include <snmalloc/snmalloc.h>

int main()
{
void* p = snmalloc::libc::malloc(64);
if (p == nullptr)
return 1;
snmalloc::libc::free(p);
return 0;
}
21 changes: 21 additions & 0 deletions test/vcpkg-consumer/test_static_shim.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Minimal vcpkg integration test for the snmallocshim-static target.
* Verifies that find_package(snmalloc) + snmalloc::snmallocshim-static works.
* The static shim replaces malloc/free with the snmalloc implementation.
*/
#include <stdlib.h>

extern "C"
{
void *sn_malloc(size_t);
void sn_free(void *);
}

int main()
{
void* p = sn_malloc(64);
if (p == nullptr)
return 1;
sn_free(p);
return 0;
}
12 changes: 12 additions & 0 deletions vcpkg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "snmalloc",
"version-semver": "0.7.4",
"description": "A high-performance, message passing based allocator",
"homepage": "https://github.com/microsoft/snmalloc",
"license": "MIT",
"features": {
"static-shim": {
"description": "Build and install snmallocshim-static, a compiled static library that exports malloc/free with a configurable symbol prefix (default: sn_)"
}
}
}
Loading