Enable C++20 alongside C11 in the build

Add CXX to the project with C++20 (no extensions) and build C++ sources
with -Wextra -fno-exceptions -fno-rtti -fno-threadsafe-statics on top of
the existing warning flags. Give every toolchain a C++ compiler: the zig
Windows toolchain, the static macOS/Windows CI jobs, the static musl
image (clang++ plus libstdc++ headers) and the RPM spec.
This commit is contained in:
Max Lv
2026-10-14 12:51:47 +08:00
parent 553cdc7945
commit 0799d3a9aa
6 changed files with 19 additions and 7 deletions
+2 -2
View File
@@ -94,7 +94,7 @@ jobs:
- uses: actions/checkout@v6
- name: Build Clang static-only installation
run: |
cmake -S . -B build-static -DCMAKE_C_COMPILER=clang -DSS_DEPENDENCY_MODE=bundled -DWITH_STATIC=ON -DSS_BUILD_STATIC_LIBRARY=ON -DSS_BUILD_SHARED_LIBRARY=OFF
cmake -S . -B build-static -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DSS_DEPENDENCY_MODE=bundled -DWITH_STATIC=ON -DSS_BUILD_STATIC_LIBRARY=ON -DSS_BUILD_SHARED_LIBRARY=OFF
cmake --build build-static --parallel 4
ctest --test-dir build-static -L 'unit|vendor' --output-on-failure --no-tests=error
cmake --install build-static --prefix "$PWD/install-static"
@@ -135,7 +135,7 @@ jobs:
mingw-w64-clang-x86_64-python
- name: Build Clang static-only installation
run: |
cmake -S . -B build-static -G Ninja -DCMAKE_C_COMPILER=clang -DSS_DEPENDENCY_MODE=bundled -DWITH_STATIC=ON -DSS_BUILD_STATIC_LIBRARY=ON -DSS_BUILD_SHARED_LIBRARY=OFF
cmake -S . -B build-static -G Ninja -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DSS_DEPENDENCY_MODE=bundled -DWITH_STATIC=ON -DSS_BUILD_STATIC_LIBRARY=ON -DSS_BUILD_SHARED_LIBRARY=OFF
cmake --build build-static --parallel 4
ctest --test-dir build-static -L 'unit|vendor' --output-on-failure --no-tests=error
cmake --install build-static --prefix "$PWD/install-static"
+8 -1
View File
@@ -6,7 +6,7 @@ set(PROJECT_VERSION "3.3.6")
set(PROJECT_DESC "a lightweight secured socks5 proxy")
set(PROJECT_URL "https://shadowsocks.org")
set(PROJECT_ISSUES_URL "https://github.com/shadowsocks/shadowsocks-libev")
project(${PROJECT_NAME} VERSION ${PROJECT_VERSION} LANGUAGES C)
project(${PROJECT_NAME} VERSION ${PROJECT_VERSION} LANGUAGES C CXX)
if(MSVC)
message(FATAL_ERROR "MSVC is not supported yet. Use MinGW-w64 (UCRT64) or the Zig Windows toolchain; see docs/modernization.md.")
endif()
@@ -23,6 +23,13 @@ set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS ON)
# The C++20 core (src/core) is being introduced module by module; see
# docs/cpp-core.md. It is built without exceptions or RTTI, so it adds
# no unwinding tables or typeinfo to the static binaries.
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
option(ENABLE_SANITIZERS "Build with AddressSanitizer and UndefinedBehaviorSanitizer" OFF)
option(ENABLE_COVERAGE "Build with coverage instrumentation" OFF)
option(SS_WARNINGS_AS_ERRORS "Treat project warnings as errors" ON)
+4
View File
@@ -25,6 +25,10 @@ function(ss_project_options target)
endif()
else()
target_compile_options(${target} PRIVATE -Wall -Wno-deprecated-declarations -fno-strict-aliasing)
# C++ sources: no exceptions or RTTI, and no locks around function
# statics (the event loop is single-threaded).
target_compile_options(${target} PRIVATE
$<$<COMPILE_LANGUAGE:CXX>:-Wextra -fno-exceptions -fno-rtti -fno-threadsafe-statics>)
if(SS_WARNINGS_AS_ERRORS)
target_compile_options(${target} PRIVATE -Werror)
endif()
+2 -1
View File
@@ -1,9 +1,10 @@
# Cross-compile the C project from a Unix host without Docker or a MinGW SDK.
# Cross-compile the project from a Unix host without Docker or a MinGW SDK.
# cmake -S . -B build-windows -DCMAKE_TOOLCHAIN_FILE=cmake/toolchains/zig-windows.cmake
set(CMAKE_SYSTEM_NAME Windows)
set(CMAKE_SYSTEM_PROCESSOR AMD64)
find_program(SS_ZIG_EXECUTABLE NAMES zig REQUIRED)
set(CMAKE_C_COMPILER "${SS_ZIG_EXECUTABLE}" cc -target x86_64-windows-gnu)
set(CMAKE_CXX_COMPILER "${SS_ZIG_EXECUTABLE}" c++ -target x86_64-windows-gnu)
# CMake's archive tools accept an executable path, not an argv list.
file(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/zig-tools")
foreach(tool ar ranlib)
+2 -2
View File
@@ -2,8 +2,8 @@ ARG ALPINE_IMAGE=alpine:3.24
FROM ${ALPINE_IMAGE} AS build
ARG ALPINE_MIRROR=https://dl-cdn.alpinelinux.org/alpine
RUN sed -i "s|https://dl-cdn.alpinelinux.org/alpine|${ALPINE_MIRROR}|g" /etc/apk/repositories \
&& apk add --no-cache clang lld make musl-dev compiler-rt cmake linux-headers python3 binutils file
ENV CC=clang
&& apk add --no-cache clang lld make musl-dev compiler-rt libstdc++-dev cmake linux-headers python3 binutils file
ENV CC=clang CXX=clang++
WORKDIR /src
COPY . .
ARG SS_MINIMAL=OFF
+1 -1
View File
@@ -27,7 +27,7 @@ License: GPLv3+
URL: https://github.com/shadowsocks/%{name}
Source0: @SOURCE@
BuildRequires: make gcc pcre-devel asciidoc xmlto automake libtool mbedtls-devel libsodium-devel >= 1.0.4 libev-devel c-ares-devel
BuildRequires: make gcc gcc-c++ pcre-devel asciidoc xmlto automake libtool mbedtls-devel libsodium-devel >= 1.0.4 libev-devel c-ares-devel
%if 0%{?suse_version}
BuildRequires: libopenssl-devel
%else