Files
zeekay d9c73505fa Finish build-system rebrand (capnp->zap) and fix whitespace-desugar bugs
Build system (autotools/CMake/Bazel) capnp->zap rebrand completed so the
c++/ tree configures cleanly: `autoreconf -i && ./configure` succeeds,
emitting Makefile, all zap*.pc / kj*.pc and the Zap CMake config. configure.ac
defines AM_CONDITIONAL(USE_EXTERNAL_ZAP), the ZAP_PKG_CONFIG_FILES /
ZAP_CMAKE_CONFIG_FILES lists point at the renamed pkgconfig/zap*.pc.in and
cmake/Zap*.cmake.in, and Makefile.am's `if USE_EXTERNAL_ZAP` matches. Bazel
loads @zap-cpp//src/zap:cc_zap_library.bzl. Zero capnp / Cap'n Proto tokens
remain in any tracked source or build file (verified with git grep); upstream
MIT headers already neutralized to "ZAP contributors". quick-test.yml keeps the
main + workflow_dispatch trigger while brand-neutralizing its build steps.

Whitespace-significant schema desugar (compiler/desugar.c++) — fix three HIGH
correctness bugs and make the preprocessor total:

  * Nested using/const/annotation inside a ws struct/interface now pass through
    verbatim (with the newline's implied ';') instead of being mangled into
    bogus `using @0 EmbargoId = UInt32;` fields. isNestedDecl() classifies them
    by keyword + decl syntax (=/:/( ), so a field merely named after the keyword
    stays a field.
  * Ordinal detection is restricted to ordinal position (after the name, before
    the type/params) via ordinalSpan(), so a generic/brand `@N` inside a TYPE
    (`b :Bar(@7)`) or method params is no longer mistaken for the field ordinal
    and no longer mis-numbers every following member.
  * Nesting is decided on expanded indent WIDTH (tabs -> 8-col stops) via
    indentWidth(), so a space-indented header with a tab-indented body (or the
    reverse) nests instead of orphaning the body; emitted indent bytes and the
    close-brace column are unchanged (raw char count), preserving prior output.
  * Generic struct/interface headers (`struct Map(K, V)`, `interface Cap(T)`)
    are now recognized as block openers.
  * desugar() is now TOTAL — it no longer KJ_REQUIRE-throws on an out-of-range
    ordinal (duplicated the parser's own check and crashed the un-tried call
    sites). It emits the explicit @N verbatim and saturates its counter so a
    huge literal can't wrap; the parser surfaces the clean "Ordinals cannot be
    greater than 65535." diagnostic at the right file:line:col. Removed the now
    redundant validateAutoOrdinal and the dead kj/debug.h include. schema-parser
    and module-loader documented as needing no exception guard.

desugar-test.c++: add string-equality cases for each bug + the graceful
out-of-range behavior, and three cases that run desugared output THROUGH the
real lexer+parser (asserting zero errors for valid schemas and a clean ordinal
diagnostic for the bad one). Brace back-compat proven byte-identical over the
18-file corpus; sample.ws.zap still desugars to sample.brace.zap exactly.
2026-06-20 00:02:20 -07:00

253 lines
8.5 KiB
CMake

cmake_minimum_required(VERSION 3.16)
include_guard(GLOBAL)
project("Zap" CXX)
set(VERSION 2.0-dev)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
include(CTest)
include(CheckIncludeFileCXX)
include(GNUInstallDirs)
# <print> header was added in C++23
if(MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /std:c++23preview")
check_include_file_cxx(print HAS_CXX23)
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++23")
check_include_file_cxx(print HAS_CXX23)
endif()
if(NOT HAS_CXX23)
message(SEND_ERROR "Requires a C++23 compiler and standard library.")
endif()
# these arguments are passed to all install(TARGETS) calls
set(INSTALL_TARGETS_DEFAULT_ARGS
EXPORT ZapTargets
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
)
# Options ======================================================================
option(EXTERNAL_ZAP "Use the system zap binary, or the one specified in $ZAP, instead of using the compiled one." OFF)
option(ZAP_LITE "Compile Zap in 'lite mode', in which all reflection APIs (schema.h, dynamic.h, etc.) are not included. Produces a smaller library at the cost of features. All programs built against the library must be compiled with -DZAP_LITE. Requires EXTERNAL_ZAP." OFF)
# Check for invalid combinations of build options
if(ZAP_LITE AND BUILD_TESTING AND NOT EXTERNAL_ZAP)
message(SEND_ERROR "You must set EXTERNAL_ZAP when using ZAP_LITE and BUILD_TESTING.")
endif()
if(ZAP_LITE)
set(ZAP_LITE_FLAG "-DZAP_LITE")
# This flag is attached as PUBLIC target_compile_definition to kj target
else()
set(ZAP_LITE_FLAG)
endif()
set(WITH_OPENSSL "AUTO" CACHE STRING
"Whether or not to build libkj-tls by linking against openssl")
# define list of values GUI will offer for the variable
set_property(CACHE WITH_OPENSSL PROPERTY STRINGS AUTO ON OFF)
set(WITH_ZLIB "AUTO" CACHE STRING
"Whether or not to build libkj-gzip by linking against zlib")
set_property(CACHE WITH_ZLIB PROPERTY STRINGS AUTO ON OFF)
# shadow cache variable original value with ON/OFF,
# so from now on OpenSSL-specific code just has to check:
# if (WITH_OPENSSL)
# ...
# endif()
if (ZAP_LITE)
set(WITH_OPENSSL OFF)
elseif (WITH_OPENSSL STREQUAL "AUTO")
find_package(OpenSSL COMPONENTS Crypto SSL)
if (OPENSSL_FOUND)
set(WITH_OPENSSL ON)
else()
set(WITH_OPENSSL OFF)
endif()
elseif (WITH_OPENSSL)
find_package(OpenSSL REQUIRED COMPONENTS Crypto SSL)
endif()
# shadow cache variable original value with ON/OFF,
# so from now on ZLIB-specific code just has to check:
# if (WITH_ZLIB)
# ...
# endif()
if(ZAP_LITE)
set(WITH_ZLIB OFF)
elseif (WITH_ZLIB STREQUAL "AUTO")
find_package(ZLIB)
if(ZLIB_FOUND)
set(WITH_ZLIB ON)
else()
set(WITH_ZLIB OFF)
endif()
elseif (WITH_ZLIB)
find_package(ZLIB REQUIRED)
endif()
set(WITH_FIBERS "AUTO" CACHE STRING
"Whether or not to build libkj-async with fibers")
# define list of values GUI will offer for the variable
set_property(CACHE WITH_FIBERS PROPERTY STRINGS AUTO ON OFF)
# ZapConfig.cmake.in needs this variable.
set(_WITH_LIBUCONTEXT OFF)
if (WITH_FIBERS OR WITH_FIBERS STREQUAL "AUTO")
set(_zap_fibers_found OFF)
if (WIN32 OR CYGWIN)
set(_zap_fibers_found ON)
else()
# Fibers need makecontext, setcontext, getcontext, swapcontext that may be in libc,
# or in libucontext (e.g. for musl).
# We assume that makecontext implies that the others are present.
include(CheckLibraryExists)
check_library_exists(c makecontext "" HAVE_UCONTEXT_LIBC)
if (HAVE_UCONTEXT_LIBC)
set(_zap_fibers_found ON)
else()
# Try with libucontext
find_package(PkgConfig)
if (PKG_CONFIG_FOUND)
pkg_check_modules(libucontext IMPORTED_TARGET libucontext)
if (libucontext_FOUND)
set(_WITH_LIBUCONTEXT ON)
set(_zap_fibers_found ON)
endif()
else()
set(_zap_fibers_found OFF)
endif()
endif()
endif()
if (_zap_fibers_found)
set(WITH_FIBERS ON)
elseif(WITH_FIBERS STREQUAL "AUTO")
set(WITH_FIBERS OFF)
else()
message(FATAL_ERROR "Missing 'makecontext', 'getcontext', 'setcontext' or 'swapcontext' symbol in libc and no libucontext found: KJ won't be able to build with fibers. Disable fibers (-DWITH_FIBERS=OFF).")
endif()
endif()
if(MSVC)
# TODO(cleanup): Enable higher warning level in MSVC, but make sure to test
# build with that warning level and clean out false positives.
add_compile_options(/Zc:__cplusplus)
# Set the __cplusplus macro properly.
add_compile_options(/Zc:inline)
# Do not generate code for unreferenced symbols with internal linkage. Significantly improves
# object code size and link speeds under MSVC (clang-cl is sane enough to not need a flag for
# this). https://devblogs.microsoft.com/cppblog/feedback-making-zcinline-default-for-debugrelease-configs-in-14/
else()
# Note that it's important to add new CXXFLAGS before ones specified by the
# user, so that the user's flags override them. This is particularly
# important if -Werror was enabled and then certain warnings need to be
# disabled, as is done in super-test.sh.
#
# We enable a lot of warnings, but then disable some:
# * strict-aliasing: We use type-punning in known-safe ways that GCC doesn't
# recognize as safe.
# * sign-compare: Low S/N ratio.
# * unused-parameter: Low S/N ratio.
add_compile_options(-Wall -Wextra -Wsuggest-override -Wno-strict-aliasing -Wno-sign-compare -Wno-unused-parameter)
if(DEFINED CMAKE_CXX_EXTENSIONS AND NOT CMAKE_CXX_EXTENSIONS)
message(SEND_ERROR "Zap requires compiler-specific extensions (e.g., -std=gnu++23). Please leave CMAKE_CXX_EXTENSIONS undefined or ON.")
endif()
if (NOT ANDROID)
add_compile_options(-pthread)
endif()
endif()
# Source =======================================================================
include(ZapMacros)
add_subdirectory(src)
# Install ======================================================================
include(CMakePackageConfigHelpers)
# We used to use write_basic_package_version_file(), but since the autotools build needs to install
# a config version script as well, I copied the AnyNewerVersion template from my CMake Modules
# directory to Zap's cmake/ directory (alternatively, we could make the autotools build
# depend on CMake).
#
# We might as well use the local copy of the template. In the future we can modify the project's
# version compatibility policy just by changing that file.
set(PACKAGE_VERSION ${VERSION})
configure_file(cmake/ZapConfigVersion.cmake.in cmake/ZapConfigVersion.cmake @ONLY)
set(CONFIG_PACKAGE_LOCATION ${CMAKE_INSTALL_LIBDIR}/cmake/Zap)
configure_package_config_file(cmake/ZapConfig.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/cmake/ZapConfig.cmake
INSTALL_DESTINATION ${CONFIG_PACKAGE_LOCATION}
PATH_VARS CMAKE_INSTALL_FULL_INCLUDEDIR
)
export(EXPORT ZapTargets
FILE "${CMAKE_CURRENT_BINARY_DIR}/cmake/ZapTargets.cmake"
NAMESPACE Zap::
)
install(EXPORT ZapTargets
FILE ZapTargets.cmake
NAMESPACE Zap::
DESTINATION ${CONFIG_PACKAGE_LOCATION}
)
install(FILES
cmake/ZapMacros.cmake
${CMAKE_CURRENT_BINARY_DIR}/cmake/ZapConfig.cmake
${CMAKE_CURRENT_BINARY_DIR}/cmake/ZapConfigVersion.cmake
DESTINATION ${CONFIG_PACKAGE_LOCATION}
)
#install ZapMacros for ZapConfig.cmake build directory consumers
configure_file(cmake/ZapMacros.cmake cmake/ZapMacros.cmake COPYONLY)
if(NOT MSVC) # Don't install pkg-config files when building with MSVC
# Variables for pkg-config files
set(prefix "${CMAKE_INSTALL_PREFIX}")
set(exec_prefix "") # not needed since we use absolute paths in libdir and includedir
set(libdir "${CMAKE_INSTALL_FULL_LIBDIR}")
set(includedir "${CMAKE_INSTALL_FULL_INCLUDEDIR}")
set(PTHREAD_CFLAGS "-pthread")
set(STDLIB_FLAG) # TODO: Unsupported
set(ZAP_PKG_CONFIG_FILES
pkgconfig/kj.pc
pkgconfig/zap.pc
pkgconfig/zapc.pc
)
if(NOT ZAP_LITE)
list(APPEND ZAP_PKG_CONFIG_FILES
pkgconfig/kj-async.pc
pkgconfig/kj-gzip.pc
pkgconfig/kj-http.pc
pkgconfig/kj-test.pc
pkgconfig/kj-tls.pc
pkgconfig/zap-rpc.pc
pkgconfig/zap-websocket.pc
pkgconfig/zap-json.pc
)
endif()
foreach(pcfile ${ZAP_PKG_CONFIG_FILES})
configure_file(${pcfile}.in "${CMAKE_CURRENT_BINARY_DIR}/${pcfile}" @ONLY)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${pcfile}" DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
endforeach()
unset(STDLIB_FLAG)
unset(PTHREAD_CFLAGS)
unset(includedir)
unset(libdir)
unset(exec_prefix)
unset(prefix)
endif()