Skip to content

Commit ffa3576

Browse files
committed
vcpkg and CMake refinements
Decouple from snmalloc headers and add shared library (DLL) support - Introduce include/trieste/compiler.h with self-contained TRIESTE_SLOW_PATH, TRIESTE_FAST_PATH, TRIESTE_LIKELY/UNLIKELY, and TRIESTE_USED_FUNCTION macros, replacing all uses of snmalloc/ds_core/defines.h macros throughout the headers (ast.h, intrusive_ptr.h, logging.h, pass.h, rewrite.h, wf.h). - Make snmalloc an optional dependency controlled by TRIESTE_USE_SNMALLOC. When disabled, the core library no longer fetches or links snmalloc at all. The vcpkg port moves snmalloc from a hard dependency to an opt-in "snmalloc" feature. - Add DLL export support for the JSON and YAML parser libraries: - Use CMake's GenerateExportHeader to produce json_export.h / yaml_export.h. - Annotate all public API functions with JSON_EXPORT / YAML_EXPORT. - Define JSON_STATIC_DEFINE / YAML_STATIC_DEFINE for static builds. - Rename output libraries to trieste-json / trieste-yaml. - Add a Windows shared-libs CI job (BUILD_SHARED_LIBS=ON). - CMake modernisation: - Use native EXCLUDE_FROM_ALL on CMake 3.28+ instead of always fetching cmake_utils; emit a deprecation warning for CMake < 3.28. - Guard header installs on non-Debug builds to avoid installing into Debug prefixes. - Only define the clangformat target when snmalloc is available (it provides the clangformat_targets macro). - vcpkg port fixes: - Prefix release tag refs with "v" to match actual Git tags. - Drop redundant PACKAGE_NAME from vcpkg_cmake_config_fixup. - Add "supports": "!x86" platform constraint. - Fix C++17 compatibility in yaml reader (replace std::set::contains with find/end). - Bump VERSION to 1.1.0. Signed-off-by: Matthew A Johnson <matjoh@microsoft.com>
1 parent 2874d0d commit ffa3576

18 files changed

Lines changed: 332 additions & 207 deletions

File tree

.github/workflows/buildtest.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ jobs:
3535
variant: "build-parser-tests"
3636
build-type: "Release"
3737
cmake-options: "-DTRIESTE_BUILD_PARSER_TESTS=1"
38+
39+
- platform: "windows-latest"
40+
variant: "shared-libs"
41+
build-type: "Release"
42+
cmake-options: "-DBUILD_SHARED_LIBS=ON"
3843

3944
- platform: "macos-latest"
4045
variant: "build-parser-tests"

CMakeLists.txt

Lines changed: 89 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ if(GIT_FOUND)
3434
list(GET TRIESTE_GIT_LOG_LIST 2 TRIESTE_BUILD_DATE)
3535
else()
3636
set(TRIESTE_GIT_HASH "unknown")
37-
set(TRIESTE_GIT_SHORT_HASH "unknown")
38-
set(TRIESTE_BUILD_DATE "unknown")
37+
set(TRIESTE_GIT_SHORT_HASH "v${TRIESTE_VERSION}")
38+
string(TIMESTAMP TRIESTE_BUILD_DATE "%a, %d %b %Y %H:%M:%S %z")
3939
endif()
4040
execute_process(
4141
COMMAND ${GIT_EXECUTABLE} rev-parse --abbrev-ref HEAD
@@ -48,8 +48,8 @@ else()
4848
message(STATUS "Git not found; version metadata will be 'unknown'")
4949
set(TRIESTE_GIT_HASH "unknown")
5050
set(TRIESTE_GIT_BRANCH "unknown")
51-
set(TRIESTE_GIT_SHORT_HASH "unknown")
52-
set(TRIESTE_BUILD_DATE "unknown")
51+
set(TRIESTE_GIT_SHORT_HASH "v${TRIESTE_VERSION}")
52+
string(TIMESTAMP TRIESTE_BUILD_DATE "%a, %d %b %Y %H:%M:%S %z")
5353
endif()
5454

5555
set(TRIESTE_BUILD_TOOLCHAIN "${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION}")
@@ -99,50 +99,85 @@ set(TRIESTE_SANITIZE "" CACHE STRING "Argument to pass to sanitize (disabled by
9999
if(TRIESTE_USE_FETCH_CONTENT)
100100
include(FetchContent)
101101

102-
set(SNMALLOC_BUILD_TESTING OFF CACHE INTERNAL "Turn off snmalloc tests")
103-
set(SNMALLOC_USE_CXX17 ${TRIESTE_USE_CXX17})
104-
105-
# Used to provide
106-
# FetchContent_MakeAvailable_ExcludeFromAll
107-
FetchContent_Declare(
102+
# CMake 3.28+ supports EXCLUDE_FROM_ALL on FetchContent_Declare natively.
103+
# For older versions, we fall back to cmake_utils which provides
104+
# FetchContent_MakeAvailable_ExcludeFromAll.
105+
if(CMAKE_VERSION VERSION_LESS "3.28")
106+
message(WARNING
107+
"CMake < 3.28 detected. Support for CMake versions below 3.28 is "
108+
"deprecated and will be removed in a future release. Please upgrade.")
109+
FetchContent_Declare(
108110
cmake_utils
109111
GIT_REPOSITORY https://github.com/mjp41/cmake_utils
110112
GIT_TAG 2bf98b5773ea7282197c823e205547d8c2e323c0
111113
GIT_SHALLOW FALSE
112-
)
113-
114-
FetchContent_MakeAvailable(cmake_utils)
114+
)
115+
FetchContent_MakeAvailable(cmake_utils)
116+
endif()
115117

116-
FetchContent_Declare(
117-
snmalloc
118-
GIT_REPOSITORY https://github.com/microsoft/snmalloc
119-
GIT_TAG 1c7a93aad54198423944a786da9dad3962134f75
120-
# per https://gitlab.kitware.com/cmake/cmake/-/issues/17770, do not use GIT_SHALLOW here
121-
# or it will silently break new builds days later, once the commit hash above no longer
122-
# matches a named branch, like main.
123-
# Tracking a named branch with GIT_SHALLOW enabled is ok, however.
124-
# GIT_SHALLOW TRUE
125-
)
118+
# Wrapper: use EXCLUDE_FROM_ALL on 3.28+, fall back to cmake_utils otherwise.
119+
macro(trieste_fetch_content_make_available target)
120+
if(CMAKE_VERSION VERSION_LESS "3.28")
121+
FetchContent_MakeAvailable_ExcludeFromAll(${target})
122+
else()
123+
FetchContent_MakeAvailable(${target})
124+
endif()
125+
endmacro()
126+
127+
if(TRIESTE_USE_SNMALLOC)
128+
set(SNMALLOC_BUILD_TESTING OFF CACHE INTERNAL "Turn off snmalloc tests")
129+
set(SNMALLOC_USE_CXX17 ${TRIESTE_USE_CXX17})
130+
131+
set(_snmalloc_extra_args "")
132+
if(NOT CMAKE_VERSION VERSION_LESS "3.28")
133+
list(APPEND _snmalloc_extra_args EXCLUDE_FROM_ALL)
134+
endif()
135+
136+
FetchContent_Declare(
137+
snmalloc
138+
GIT_REPOSITORY https://github.com/microsoft/snmalloc
139+
GIT_TAG 1c7a93aad54198423944a786da9dad3962134f75
140+
${_snmalloc_extra_args}
141+
# per https://gitlab.kitware.com/cmake/cmake/-/issues/17770, do not use GIT_SHALLOW here
142+
# or it will silently break new builds days later, once the commit hash above no longer
143+
# matches a named branch, like main.
144+
# Tracking a named branch with GIT_SHALLOW enabled is ok, however.
145+
# GIT_SHALLOW TRUE
146+
)
147+
148+
trieste_fetch_content_make_available(snmalloc)
149+
150+
set(TRIESTE_SNMALLOC_TARGET snmalloc)
151+
set(TRIESTE_SNMALLOC_OVERRIDE_TARGET snmalloc-new-override)
152+
else()
153+
set(TRIESTE_SNMALLOC_TARGET "")
154+
set(TRIESTE_SNMALLOC_OVERRIDE_TARGET "")
155+
endif()
126156

127-
FetchContent_MakeAvailable_ExcludeFromAll(snmalloc)
157+
set(_cli11_extra_args "")
158+
if(NOT CMAKE_VERSION VERSION_LESS "3.28")
159+
list(APPEND _cli11_extra_args EXCLUDE_FROM_ALL)
160+
endif()
128161

129162
FetchContent_Declare(
130163
cli11
131164
GIT_REPOSITORY https://github.com/CLIUtils/CLI11
132165
GIT_TAG 4160d259d961cd393fd8d67590a8c7d210207348
133166
GIT_SHALLOW TRUE
167+
${_cli11_extra_args}
134168
)
135169

136-
FetchContent_MakeAvailable_ExcludeFromAll(cli11)
137-
138-
set(TRIESTE_SNMALLOC_TARGET snmalloc)
139-
set(TRIESTE_SNMALLOC_OVERRIDE_TARGET snmalloc-new-override)
170+
trieste_fetch_content_make_available(cli11)
140171
else()
141-
find_package(snmalloc CONFIG REQUIRED)
142-
find_package(CLI11 CONFIG REQUIRED)
143-
144-
set(TRIESTE_SNMALLOC_TARGET snmalloc::snmalloc)
172+
if(TRIESTE_USE_SNMALLOC)
173+
find_package(snmalloc CONFIG REQUIRED)
174+
set(TRIESTE_SNMALLOC_TARGET snmalloc::snmalloc)
175+
else()
176+
set(TRIESTE_SNMALLOC_TARGET "")
177+
endif()
145178
set(TRIESTE_SNMALLOC_OVERRIDE_TARGET "")
179+
180+
find_package(CLI11 CONFIG REQUIRED)
146181
endif()
147182

148183
# #############################################
@@ -162,14 +197,17 @@ target_include_directories(trieste
162197
target_link_libraries(trieste
163198
INTERFACE
164199
CLI11::CLI11
165-
${TRIESTE_SNMALLOC_TARGET}
166200
)
167201

168-
if(TRIESTE_USE_SNMALLOC AND TRIESTE_SNMALLOC_OVERRIDE_TARGET)
169-
if(TRIESTE_SANITIZE)
170-
message(WARNING "Sanitizers are not supported with snmalloc, disabling snmalloc.")
171-
else()
172-
target_link_libraries(trieste INTERFACE ${TRIESTE_SNMALLOC_OVERRIDE_TARGET})
202+
if(TRIESTE_USE_SNMALLOC)
203+
target_link_libraries(trieste INTERFACE ${TRIESTE_SNMALLOC_TARGET})
204+
205+
if(TRIESTE_SNMALLOC_OVERRIDE_TARGET)
206+
if(TRIESTE_SANITIZE)
207+
message(WARNING "Sanitizers are not supported with snmalloc, disabling snmalloc.")
208+
else()
209+
target_link_libraries(trieste INTERFACE ${TRIESTE_SNMALLOC_OVERRIDE_TARGET})
210+
endif()
173211
endif()
174212
endif()
175213

@@ -212,9 +250,12 @@ if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
212250
endif()
213251

214252
if(TRIESTE_USE_FETCH_CONTENT)
215-
set(INSTALL_DEPS ${TRIESTE_SNMALLOC_TARGET} CLI11)
216-
if (TRIESTE_USE_SNMALLOC AND TRIESTE_SNMALLOC_OVERRIDE_TARGET)
217-
list(APPEND INSTALL_DEPS ${TRIESTE_SNMALLOC_OVERRIDE_TARGET})
253+
set(INSTALL_DEPS CLI11)
254+
if(TRIESTE_USE_SNMALLOC)
255+
list(APPEND INSTALL_DEPS ${TRIESTE_SNMALLOC_TARGET})
256+
if(TRIESTE_SNMALLOC_OVERRIDE_TARGET)
257+
list(APPEND INSTALL_DEPS ${TRIESTE_SNMALLOC_OVERRIDE_TARGET})
258+
endif()
218259
endif()
219260
else()
220261
set(INSTALL_DEPS "")
@@ -250,11 +291,13 @@ install(FILES ${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake
250291
${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
251292
DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}/cmake)
252293

253-
install(DIRECTORY ${PROJECT_SOURCE_DIR}/include/trieste DESTINATION include
254-
PATTERN "*.in" EXCLUDE)
294+
if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
295+
install(DIRECTORY ${PROJECT_SOURCE_DIR}/include/trieste DESTINATION include
296+
PATTERN "*.in" EXCLUDE)
255297

256-
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/include/trieste/version.h
257-
DESTINATION include/trieste)
298+
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/include/trieste/version.h
299+
DESTINATION include/trieste)
300+
endif()
258301

259302
# #############################################
260303
# # Exporting from the build tree
@@ -267,7 +310,7 @@ export(PACKAGE trieste)
267310

268311
# #############################################
269312
# # Formatting targets
270-
if(TRIESTE_USE_FETCH_CONTENT)
313+
if(TRIESTE_USE_FETCH_CONTENT AND TRIESTE_USE_SNMALLOC)
271314
clangformat_targets(
272315
VERSION 18
273316
INCLUDE_PATTERNS include/*.h include/*.cc parsers/*.cc parsers/*.h samples/*.cc samples/*.h test/*.cc test/*.h

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.0.0
1+
1.1.0

include/trieste/ast.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,7 @@ namespace trieste
778778
/*
779779
* Useful for calling from inside a debugger.
780780
*/
781-
std::string SNMALLOC_USED_FUNCTION str()
781+
std::string TRIESTE_USED_FUNCTION str()
782782
{
783783
std::ostringstream out;
784784
str(out);
@@ -837,7 +837,7 @@ namespace trieste
837837
* passed to the action, but not above.
838838
*/
839839
template<typename Pre, typename Post = NopPost>
840-
SNMALLOC_FAST_PATH void traverse(Pre pre, Post post = NopPost())
840+
TRIESTE_FAST_PATH void traverse(Pre pre, Post post = NopPost())
841841
{
842842
Node root = intrusive_ptr_from_this();
843843
if (!pre(root))

include/trieste/compiler.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#pragma once
2+
3+
/**
4+
* @file compiler.h
5+
* @brief Compiler hint macros for Trieste.
6+
*
7+
* Provides portable macros for inlining hints and branch prediction.
8+
* These are self-contained and do not depend on any external library.
9+
*/
10+
11+
#if defined(_MSC_VER) && !defined(__clang__)
12+
# define TRIESTE_SLOW_PATH __declspec(noinline)
13+
# define TRIESTE_FAST_PATH __forceinline
14+
# define TRIESTE_FAST_PATH_INLINE __forceinline
15+
# define TRIESTE_LIKELY(x) (!!(x))
16+
# define TRIESTE_UNLIKELY(x) (!!(x))
17+
# define TRIESTE_USED_FUNCTION
18+
# if _MSC_VER >= 1927 && _MSVC_LANG > 201703L
19+
# define TRIESTE_FAST_PATH_LAMBDA [[msvc::forceinline]]
20+
# else
21+
# define TRIESTE_FAST_PATH_LAMBDA
22+
# endif
23+
#else
24+
# define TRIESTE_SLOW_PATH __attribute__((noinline))
25+
# define TRIESTE_FAST_PATH __attribute__((always_inline))
26+
# define TRIESTE_FAST_PATH_INLINE __attribute__((always_inline)) inline
27+
# define TRIESTE_LIKELY(x) __builtin_expect(!!(x), 1)
28+
# define TRIESTE_UNLIKELY(x) __builtin_expect(!!(x), 0)
29+
# define TRIESTE_USED_FUNCTION __attribute__((used))
30+
# define TRIESTE_FAST_PATH_LAMBDA __attribute__((always_inline))
31+
#endif
32+
33+
namespace trieste
34+
{
35+
template<typename... Args>
36+
TRIESTE_FAST_PATH_INLINE void UNUSED(Args&&...)
37+
{}
38+
} // namespace trieste

include/trieste/intrusive_ptr.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#pragma once
22

3-
#include "snmalloc/ds_core/defines.h"
3+
#include "compiler.h"
44

55
#include <atomic>
66
#include <cassert>
@@ -265,7 +265,7 @@ namespace trieste
265265
// It's better to have the non-null case dec_ref code all in one place,
266266
// because it's long for something that might be pasted over 10x
267267
// into functions that use intrusive_ptr a lot.
268-
SNMALLOC_SLOW_PATH
268+
TRIESTE_SLOW_PATH
269269
void intrusive_dec_ref()
270270
{
271271
// Atomically subtract 1 from refcount and get the _old value_.

0 commit comments

Comments
 (0)