mirror of
https://github.com/shadowsocks/shadowsocks-libev.git
synced 2026-10-27 03:03:43 +00:00
* Make headers and leaf modules compile as C++
Wrap the declarations of every project header in extern "C" (after the
system includes, which must keep C++ linkage), so converted modules and
the remaining C files agree on linkage.
In the leaf modules, cast allocator and memchr results explicitly,
rename identifiers called new, replace the getpwnam_r VLA with the
fixed 16 KiB buffer the loop already capped itself at, and make the
rlimit initialiser conversion explicit. All of this is still valid C.
* Compile the leaf modules as C++
Rename base64, json, ipset, process, ppbloom, rule, ssurl and cache from
.c to .cpp without touching their contents, and update the CMake source
lists, debian/copyright and the docs.
Ported code keeps -Wall; -Wextra is now opt-in through
ss_strict_warnings() for code written as C++ (the core tests).
check_no_heap.py gains a ratchet baseline for the converted modules,
which still allocate the way the C code did. A new check_cxx_runtime
test verifies with nm that the programs and libshadowsocks-c.a still
need no C++ runtime library, so C consumers are unaffected.
* Fix the Windows branches of process.cpp for C++
Cast allocator results and the thread argument explicitly and
value-initialise JOBOBJECT_EXTENDED_LIMIT_INFORMATION with {}. Found by
the zig x86_64-windows-gnu cross build.
* Keep clang-tidy clean for the converted modules
Once the C headers are included from C++ translation units,
bugprone-dynamic-static-initializers reports each extern variable
declaration in them; none is defined in a header, so disable the check.
Mark the two bounded memchr searches in ssurl.cpp, which
bugprone-not-null-terminated-result misreads as string copies.
* check_cxx_runtime: ignore glibc symbol versions
nm on glibc prints __cxa_atexit@GLIBC_2.2.5, which the allowlist for the
libc-provided __cxa_atexit/__cxa_finalize did not match.
* Keep GCC's C++-only warnings off for modules converted from C
GCC's C++ -Wall includes -Wsign-compare and -Wclass-memaccess, which C's
-Wall does not, so the converted modules failed -Werror on GCC (Linux,
UCRT64) although nothing in them changed. They keep the warning set they
were written against; code under ss_strict_warnings() re-enables both.
73 lines
3.3 KiB
CMake
73 lines
3.3 KiB
CMake
include(CheckCCompilerFlag)
|
|
option(DISABLE_SSP "Disable stack protector" OFF)
|
|
if(NOT DISABLE_SSP AND NOT MSVC)
|
|
check_c_compiler_flag(-fstack-protector-strong HAS_STACK_PROTECTOR)
|
|
endif()
|
|
|
|
function(ss_project_options target)
|
|
set_property(TARGET ${target} PROPERTY NO_SYSTEM_FROM_IMPORTED TRUE)
|
|
target_include_directories(${target} BEFORE PRIVATE ${MBEDTLS_INCLUDE_DIRS})
|
|
if(WIN32)
|
|
target_compile_definitions(${target} PRIVATE WIN32_LEAN_AND_MEAN __USE_MINGW_ANSI_STDIO=1)
|
|
# MinGW can otherwise pull in libwinpthread/libgcc from the toolchain,
|
|
# even when all explicitly selected dependencies are static archives.
|
|
get_target_property(ss_target_type ${target} TYPE)
|
|
# Zig already supplies its own runtime for DLLs; its -static switch
|
|
# changes the output kind instead of just selecting static libraries.
|
|
if(NOT ss_target_type STREQUAL "SHARED_LIBRARY" OR CMAKE_C_COMPILER_ID STREQUAL "GNU")
|
|
target_link_options(${target} PRIVATE -static)
|
|
endif()
|
|
endif()
|
|
if(MSVC)
|
|
target_compile_options(${target} PRIVATE /W4)
|
|
if(SS_WARNINGS_AS_ERRORS)
|
|
target_compile_options(${target} PRIVATE /WX)
|
|
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). New C++ code also opts
|
|
# into -Wextra through ss_strict_warnings(); modules ported from C get
|
|
# it when they are rewritten.
|
|
target_compile_options(${target} PRIVATE
|
|
$<$<COMPILE_LANGUAGE:CXX>:-fno-exceptions -fno-rtti -fno-threadsafe-statics>)
|
|
# GCC's C++ -Wall adds -Wsign-compare and -Wclass-memaccess, which C's
|
|
# -Wall does not have. Modules converted from C keep the warning set
|
|
# they were written against; ss_strict_warnings() turns both back on.
|
|
target_compile_options(${target} PRIVATE
|
|
$<$<COMPILE_LANG_AND_ID:CXX,GNU>:-Wno-sign-compare -Wno-class-memaccess>)
|
|
if(SS_WARNINGS_AS_ERRORS)
|
|
target_compile_options(${target} PRIVATE -Werror)
|
|
endif()
|
|
if(HAS_STACK_PROTECTOR)
|
|
target_compile_options(${target} PRIVATE -fstack-protector-strong)
|
|
endif()
|
|
if(MINGW OR CYGWIN)
|
|
target_compile_options(${target} PRIVATE -mno-ms-bitfields)
|
|
endif()
|
|
endif()
|
|
if(LINUX)
|
|
target_compile_definitions(${target} PRIVATE _GNU_SOURCE)
|
|
endif()
|
|
if(ENABLE_SANITIZERS)
|
|
if(MSVC)
|
|
message(FATAL_ERROR "ASan + UBSan requires GCC or Clang")
|
|
endif()
|
|
target_compile_options(${target} PRIVATE -fsanitize=address,undefined -fno-sanitize-recover=all -fno-omit-frame-pointer)
|
|
target_link_options(${target} PRIVATE -fsanitize=address,undefined)
|
|
endif()
|
|
if(ENABLE_COVERAGE)
|
|
target_compile_options(${target} PRIVATE -O0 --coverage)
|
|
target_link_options(${target} PRIVATE --coverage)
|
|
endif()
|
|
endfunction()
|
|
|
|
# Extra warnings for code written as C++ from the start (src/core and its
|
|
# tests).
|
|
function(ss_strict_warnings target)
|
|
if(NOT MSVC)
|
|
target_compile_options(${target} PRIVATE $<$<COMPILE_LANGUAGE:CXX>:-Wextra>
|
|
$<$<COMPILE_LANG_AND_ID:CXX,GNU>:-Wsign-compare -Wclass-memaccess>)
|
|
endif()
|
|
endfunction()
|