### Start Basic Client Source: https://github.com/hexian000/kcptun-libev/blob/master/README.md Command to start the kcptun-libev client using the specified configuration file. ```sh ./kcptun-libev -c client.json ``` -------------------------------- ### Start Basic Server Source: https://github.com/hexian000/kcptun-libev/blob/master/README.md Command to start the kcptun-libev server using the specified configuration file. ```sh ./kcptun-libev -c server.json ``` -------------------------------- ### Install Runtime Dependencies on Debian/Ubuntu Source: https://github.com/hexian000/kcptun-libev/blob/master/README.md Install the necessary runtime libraries for dynamically-linked kcptun-libev on Debian or Ubuntu. ```sh # Debian / Ubuntu sudo apt install libev4 libsodium23 ``` -------------------------------- ### Install Runtime Dependencies on OpenWrt Source: https://github.com/hexian000/kcptun-libev/blob/master/README.md Install the necessary runtime libraries for dynamically-linked kcptun-libev on OpenWrt systems. ```sh # OpenWRT opkg install libev libsodium ``` -------------------------------- ### Install SQM Scripts Source: https://github.com/hexian000/kcptun-libev/wiki/Internet-Setup-Example Commands to clone, install, and configure SQM for traffic shaping on a network interface. ```bash git clone https://github.com/tohojo/sqm-scripts cd sqm-scripts sudo make install # check nic name ip a # assuming the name is "eth0" sudo cp /etc/sqm/default.conf /etc/sqm/eth0.iface.conf sudo nano /etc/sqm/eth0.iface.conf # assuming 50 Mbps uplink # UPLINK=50000 # DOWNLINK=0 # assuming systemd sudo systemctl enable sqm@eth0 sudo systemctl start sqm@eth0 # check status sudo systemctl status sqm@eth0 ``` -------------------------------- ### Install Build Dependencies on Debian/Ubuntu Source: https://github.com/hexian000/kcptun-libev/blob/master/README.md Install the necessary development libraries for building kcptun-libev on Debian or Ubuntu systems. ```sh # Debian / Ubuntu sudo apt install libev-dev libsodium-dev ``` -------------------------------- ### Install Build Dependencies on Alpine Linux Source: https://github.com/hexian000/kcptun-libev/blob/master/README.md Install the required development libraries for building kcptun-libev on Alpine Linux. ```sh # Alpine Linux apk add libev-dev libsodium-dev ``` -------------------------------- ### Install Runtime Dependencies on Alpine Linux Source: https://github.com/hexian000/kcptun-libev/blob/master/README.md Install the required runtime libraries for dynamically-linked kcptun-libev on Alpine Linux. ```sh # Alpine Linux apk add libev libsodium ``` -------------------------------- ### Run kcptun-libev with Root Privileges and Drop Capabilities Source: https://github.com/hexian000/kcptun-libev/blob/master/README.md Example of running kcptun-libev as root and dropping privileges to a specific user and group. This is one way to handle the CAP_NET_RAW requirement. ```sh # run as root and drop privileges after necessary setup sudo ./kcptun-libev -u nobody:nogroup -c server.json ``` -------------------------------- ### Get Git Version Source: https://github.com/hexian000/kcptun-libev/blob/master/CMakeLists.txt A CMake function to retrieve the project's version from Git. It checks for tags, falls back to the short commit hash, and appends a '+' if the working directory is dirty. ```cmake function(get_git_version) find_package(Git) if(NOT GIT_FOUND) return() endif() execute_process( COMMAND "${GIT_EXECUTABLE}" rev-parse --git-dir WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}" RESULT_VARIABLE RESULT OUTPUT_VARIABLE GIT_DIR OUTPUT_STRIP_TRAILING_WHITESPACE) if(RESULT) return() endif() execute_process( COMMAND "${GIT_EXECUTABLE}" tag --points-at HEAD WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}" OUTPUT_VARIABLE GIT_VERSION OUTPUT_STRIP_TRAILING_WHITESPACE) if("${GIT_VERSION}" STREQUAL "") execute_process( COMMAND "${GIT_EXECUTABLE}" rev-parse --short HEAD WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}" OUTPUT_VARIABLE GIT_HEAD OUTPUT_STRIP_TRAILING_WHITESPACE) set(GIT_VERSION "git-${GIT_HEAD}") endif() execute_process( COMMAND "${GIT_EXECUTABLE}" diff --quiet HEAD -- WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}" RESULT_VARIABLE RESULT) if(RESULT) set(GIT_VERSION "${GIT_VERSION}+") endif() set(PROJECT_VERSION_STRING "${GIT_VERSION}" PARENT_SCOPE) endfunction(get_git_version) ``` -------------------------------- ### Calculate Window Size and Run Service Source: https://github.com/hexian000/kcptun-libev/wiki/Internet-Setup-Example Commands for running the service and formulas for calculating optimal window sizes based on RTT. ```bash # server side sudo ./kcptun-libev -c server.json # client side sudo ./kcptun-libev -c client.json -v -v # wait up to 1 minute to read the RTT value in logs ``` ```text rx bandwidth = kcp.rcvwnd * kcp.mtu / RTT tx bandwidth = kcp.sndwnd * kcp.mtu / RTT ``` ```text kcp.*wnd := 1024 packets kcp.mtu := 1400 bytes RTT = 170 ms max bandwidth ~= 8.0 MiB/s ``` ```bash # server side sudo ./kcptun-libev -c server.json # client side sudo ./kcptun-libev -c client.json ``` -------------------------------- ### Configure Server and Client Source: https://github.com/hexian000/kcptun-libev/wiki/Internet-Setup-Example JSON configuration templates for the kcptun-libev server and client instances. ```json { "kcp_bind": ":10080", "connect": "127.0.0.1:1080", "http_listen": "127.0.1.1:8081", "kcp": { "mtu": 1400, "sndwnd": 1024, "rcvwnd": 1024, "nodelay": 2, "interval": 50, "resend": 0, "nc": 1 }, "udp": { "sndbuf": 1048576, "rcvbuf": 1048576 }, "method": "xchacha20poly1305_ietf", "psk": "// paste the key", "loglevel": 3, "obfs": "dpi/tcp-wnd", "user": "nobody" } ``` ```json { "listen": ":1080", "kcp_connect": "203.0.113.1:10080", "kcp": { "mtu": 1400, "sndwnd": 1024, "rcvwnd": 1024, "nodelay": 2, "interval": 50, "resend": 0, "nc": 1 }, "udp": { "sndbuf": 1048576, "rcvbuf": 1048576 }, "method": "xchacha20poly1305_ietf", "psk": "// paste the key", "loglevel": 3, "obfs": "dpi/tcp-wnd", "user": "nobody" } ``` -------------------------------- ### Build kcptun-libev on Unix-like Systems Source: https://github.com/hexian000/kcptun-libev/blob/master/README.md Clone the repository, create a build directory, and use CMake to configure and build kcptun-libev. The build type is set to 'Release'. ```sh git clone https://github.com/hexian000/kcptun-libev.git mkdir -p kcptun-libev-build && cd kcptun-libev-build cmake -DCMAKE_BUILD_TYPE="Release" \ ../kcptun-libev cmake --build . --parallel ``` -------------------------------- ### Add Static Library Source: https://github.com/hexian000/kcptun-libev/blob/master/contrib/kcp/CMakeLists.txt Defines a static library named 'kcp' using the source files 'ikcp.c' and 'ikcp.h'. ```cmake add_library(kcp STATIC ikcp.c ikcp.h) ``` -------------------------------- ### Define Build Options Source: https://github.com/hexian000/kcptun-libev/blob/master/CMakeLists.txt Defines various build options for kcptun-libev, including enabling sanitizers, building static executables, position-independent executables, POSIX compliance, static library linking, and systemd integration. ```cmake option(ENABLE_SANITIZERS "Enable address, leak, undefined sanitizer" OFF) option(BUILD_STATIC "Build a static executable" OFF) option(BUILD_PIE "Build a position independent executable" OFF) option(FORCE_POSIX "Build the core features using POSIX.1 APIs only" OFF) option(LINK_STATIC_LIBS "Link against static libraries" OFF) option(ENABLE_SYSTEMD "Enable systemd integration" OFF) ``` -------------------------------- ### Grant CAP_NET_RAW Capability and Run as Normal User Source: https://github.com/hexian000/kcptun-libev/blob/master/README.md Grant the CAP_NET_RAW capability to the kcptun-libev executable, allowing it to be run by a normal user. This is an alternative to running as root. ```sh # or grant the capability and run as a normal user sudo setcap cap_net_raw+ep kcptun-libev ./kcptun-libev -c server.json ``` -------------------------------- ### NAT Traversal Diagram Source: https://github.com/hexian000/kcptun-libev/blob/master/README.md Illustrates the network flow for NAT traversal using kcptun-libev, showing the initial connection via a rendezvous server and the subsequent direct peer-to-peer connection after hole-punching. ```text client -> NAT1 -> rendezvous server server -> NAT2 -> rendezvous server (after hole-punching) client -> NAT1 -> NAT2 -> server ``` -------------------------------- ### Enable HTTP Monitoring Source: https://github.com/hexian000/kcptun-libev/blob/master/README.md Adds a line to the configuration file to enable the built-in HTTP server for monitoring service status. The server will listen on the specified address and port. ```json "http_listen": "127.0.1.1:8081" ``` -------------------------------- ### Basic Server Configuration Source: https://github.com/hexian000/kcptun-libev/blob/master/README.md Configuration for a kcptun-libev server in basic forwarding mode, suitable for direct connectivity. It binds to a local address and forwards traffic to a specified connect address. ```json { "kcp_bind": "0.0.0.0:12345", "connect": "127.0.0.1:1080", "method": "xchacha20poly1305_ietf", "psk": "// your key here" } ``` -------------------------------- ### Basic Client Configuration Source: https://github.com/hexian000/kcptun-libev/blob/master/README.md Configuration for a kcptun-libev client in basic forwarding mode. It listens on a local address and forwards traffic to the KCP server's address and port. ```json { "listen": "127.0.0.1:1080", "kcp_connect": "203.0.113.1:12345", "method": "xchacha20poly1305_ietf", "psk": "// your key here" } ``` -------------------------------- ### Configure Obfuscation Source: https://github.com/hexian000/kcptun-libev/blob/master/README.md Set the obfuscation method in the configuration file. This feature is available on Linux only and requires CAP_NET_RAW capability. ```json "obfs": "// name here" ``` -------------------------------- ### Set Include Directories Source: https://github.com/hexian000/kcptun-libev/blob/master/contrib/kcp/CMakeLists.txt Adds the current source directory to the include paths for the 'kcp' target, ensuring header files are found. ```cmake target_include_directories(kcp BEFORE PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) ``` -------------------------------- ### Include Subdirectories Source: https://github.com/hexian000/kcptun-libev/blob/master/contrib/csnippets/CMakeLists.txt Includes additional subdirectories for 'algo', 'codec', 'math', 'net', 'os', and 'utils'. ```cmake add_subdirectory(algo) add_subdirectory(codec) add_subdirectory(math) add_subdirectory(net) add_subdirectory(os) add_subdirectory(utils) ``` -------------------------------- ### Set Output Directories Source: https://github.com/hexian000/kcptun-libev/blob/master/CMakeLists.txt Configures the output directories for archives, libraries, and executables. This ensures organized build artifacts. ```cmake set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib") set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib") set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") ``` -------------------------------- ### Configure Encryption Method Source: https://github.com/hexian000/kcptun-libev/blob/master/README.md Specify the encryption method in the configuration file. Ensure encryption is enabled for security and privacy. ```json "method": "// name here" ``` -------------------------------- ### Configure Header File Source: https://github.com/hexian000/kcptun-libev/blob/master/contrib/csnippets/CMakeLists.txt Generates the config.h file from config.h.in, escaping quotes. ```cmake configure_file(config.h.in config.h ESCAPE_QUOTES) ``` -------------------------------- ### Network Acceleration Diagram Source: https://github.com/hexian000/kcptun-libev/blob/master/README.md Shows the data flow when kcptun-libev is used for network acceleration on lossy or congested networks, highlighting the KCP transport layer. ```text client -> kcptun-libev client -> lossy network (carried by KCP) -> kcptun-libev server -> server ``` -------------------------------- ### Set Compile Options Source: https://github.com/hexian000/kcptun-libev/blob/master/contrib/kcp/CMakeLists.txt Applies the '-w' compile option (disable all warnings) to the 'kcp' target. ```cmake target_compile_options(kcp PRIVATE -w) ``` -------------------------------- ### Link Libraries Source: https://github.com/hexian000/kcptun-libev/blob/master/contrib/kcp/CMakeLists.txt Links the 'kcp' target with the 'csnippets' library. ```cmake target_link_libraries(kcp PUBLIC csnippets) ``` -------------------------------- ### Set Compile Definitions Source: https://github.com/hexian000/kcptun-libev/blob/master/contrib/kcp/CMakeLists.txt Defines 'IKCP_FASTACK_CONSERVE=1' for the 'kcp' target during compilation. ```cmake target_compile_definitions(kcp PRIVATE IKCP_FASTACK_CONSERVE=1) ``` -------------------------------- ### Generate Preshared Key Source: https://github.com/hexian000/kcptun-libev/wiki/Internet-Setup-Example Command to generate a key for the xchacha20poly1305_ietf encryption method. ```bash ./kcptun-libev --genpsk xchacha20poly1305_ietf ``` -------------------------------- ### Find Systemd Source: https://github.com/hexian000/kcptun-libev/blob/master/contrib/csnippets/CMakeLists.txt Finds systemd libraries and headers if ENABLE_SYSTEMD is set. If found, it configures the csnippets library to use systemd; otherwise, it issues a warning. ```cmake if(ENABLE_SYSTEMD) find_path(SYSTEMD_INCLUDE_DIR NAMES systemd/sd-daemon.h) find_library(SYSTEMD_LIBRARY NAMES systemd) if((EXISTS ${SYSTEMD_INCLUDE_DIR}) AND (EXISTS ${SYSTEMD_LIBRARY})) set(WITH_SYSTEMD TRUE) message(STATUS "systemd: ${SYSTEMD_LIBRARY}") target_include_directories(csnippets PRIVATE ${SYSTEMD_INCLUDE_DIR}) target_link_libraries(csnippets PUBLIC ${SYSTEMD_LIBRARY}) else() message(WARNING "systemd not found") endif() endif() ``` -------------------------------- ### POSIX Compliance Check Source: https://github.com/hexian000/kcptun-libev/blob/master/CMakeLists.txt Configures the build to use POSIX.1-2008 features if FORCE_POSIX is enabled. Otherwise, it assumes a general Unix-like system. ```cmake if(FORCE_POSIX) message(STATUS "POSIX-compliant system: ${CMAKE_SYSTEM_NAME} ${CMAKE_SYSTEM_VERSION}") list(APPEND CMAKE_REQUIRED_DEFINITIONS "-D_POSIX_C_SOURCE=200809L") else() message(STATUS "Unix-like system: ${CMAKE_SYSTEM_NAME} ${CMAKE_SYSTEM_VERSION}") endif() ``` -------------------------------- ### Monitor Service Stats Source: https://github.com/hexian000/kcptun-libev/blob/master/README.md Command to continuously monitor the service statistics by periodically fetching data from the HTTP stats endpoint. This is useful for observing the health and performance of the kcptun-libev service. ```sh watch curl -sX POST http://127.0.1.1:8081/stats ``` -------------------------------- ### Set Project Version Source: https://github.com/hexian000/kcptun-libev/blob/master/CMakeLists.txt Sets the project version string, defaulting to 'dev' if the Git version cannot be determined. Calls get_git_version() to populate the version. ```cmake if("${PROJECT_VERSION_STRING}" STREQUAL "") set(PROJECT_VERSION_STRING "dev") get_git_version() endif() message(STATUS "Project version: ${PROJECT_VERSION_STRING}") ``` -------------------------------- ### Set C Standard and Required Source: https://github.com/hexian000/kcptun-libev/blob/master/CMakeLists.txt Sets the C standard to C11 and enforces that it is required for the build. This ensures modern C features are available and used. ```cmake set(CMAKE_C_STANDARD 11) set(CMAKE_C_STANDARD_REQUIRED ON) ``` -------------------------------- ### Configure Debug Information Source: https://github.com/hexian000/kcptun-libev/blob/master/contrib/csnippets/CMakeLists.txt Enables debug information if the build type is 'Debug' or 'RelWithDebInfo'. It then attempts to find and link libbacktrace or libunwind. ```cmake if(CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo") set(WITH_DEBUGINFO TRUE) find_backtrace() if(WITH_LIBBACKTRACE) target_include_directories(csnippets PRIVATE ${LIBBACKTRACE_INCLUDE_DIR}) target_link_libraries(csnippets PRIVATE ${LIBBACKTRACE_LIBRARY}) elseif(WITH_LIBUNWIND) target_include_directories(csnippets PRIVATE ${LIBUNWIND_INCLUDE_DIR}) target_link_libraries(csnippets PUBLIC ${LIBUNWIND_LIBRARY}) endif() endif() ``` -------------------------------- ### Rendezvous Server Configuration Source: https://github.com/hexian000/kcptun-libev/blob/master/README.md Configuration for the rendezvous server, which acts as a bootstrap point for NAT traversal. It binds to a specified address and port and uses a pre-shared key for authentication. ```json { "kcp_bind": "0.0.0.0:12345", "method": "xchacha20poly1305_ietf", "psk": "// your key here" } ``` -------------------------------- ### Define csnippets Static Library Source: https://github.com/hexian000/kcptun-libev/blob/master/contrib/csnippets/CMakeLists.txt Defines the csnippets library as a static library and sets its include directories and compile options. ```cmake add_library(csnippets STATIC) target_include_directories(csnippets BEFORE PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) target_compile_options(csnippets PUBLIC "-include${CMAKE_CURRENT_BINARY_DIR}/config.h") target_compile_options(csnippets PRIVATE -pedantic -Wall -Wextra) ``` -------------------------------- ### Rendezvous Client Configuration (NAT Traversal) Source: https://github.com/hexian000/kcptun-libev/blob/master/README.md Configuration for a client behind NAT that connects through a rendezvous server. It specifies the local listening address, rendezvous server address, and a service ID. ```json { "listen": "127.0.0.1:25565", "rendezvous_server": "203.0.113.1:12345", "service_id": "myservice", "method": "xchacha20poly1305_ietf", "psk": "// your key here" } ``` -------------------------------- ### Compiler Flags for GNU and Clang Source: https://github.com/hexian000/kcptun-libev/blob/master/contrib/csnippets/CMakeLists.txt Sets the -ffile-prefix-map compiler flag for GCC and Clang to map source directory prefixes. ```cmake if(CMAKE_C_COMPILER_ID STREQUAL "GNU") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ffile-prefix-map=${CMAKE_CURRENT_SOURCE_DIR}/=") elseif(CMAKE_C_COMPILER_ID MATCHES "Clang") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ffile-prefix-map=${CMAKE_CURRENT_SOURCE_DIR}/=") endif() ``` -------------------------------- ### Define Bloom Library in CMake Source: https://github.com/hexian000/kcptun-libev/blob/master/contrib/libbloom/CMakeLists.txt Configures the bloom library as a static target with include directories and link dependencies. ```cmake add_library(bloom STATIC bloom.c bloom.h) target_include_directories(bloom BEFORE PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) target_link_libraries(bloom PUBLIC csnippets) target_compile_options(bloom PRIVATE -w) ``` -------------------------------- ### Additional Compile Definitions Source: https://github.com/hexian000/kcptun-libev/blob/master/contrib/csnippets/CMakeLists.txt Adds public compile definitions for MCACHE_STATS and SLOG_MT_SAFE. ```cmake target_compile_definitions(csnippets PUBLIC MCACHE_STATS=0 SLOG_MT_SAFE=0) ``` -------------------------------- ### Rendezvous Server Configuration (NAT Traversal) Source: https://github.com/hexian000/kcptun-libev/blob/master/README.md Configuration for a server behind NAT that uses a rendezvous server to accept connections. It specifies the direct connection target, rendezvous server address, and a service ID. ```json { "connect": "127.0.0.1:25565", "rendezvous_server": "203.0.113.1:12345", "service_id": "myservice", "method": "xchacha20poly1305_ietf", "psk": "// your key here" } ``` -------------------------------- ### Find libbacktrace or libunwind Source: https://github.com/hexian000/kcptun-libev/blob/master/contrib/csnippets/CMakeLists.txt A function to find and configure libbacktrace or libunwind for debug information. It prioritizes libbacktrace and falls back to libunwind if libbacktrace is not available or not supported. ```cmake function(find_backtrace) find_path(LIBBACKTRACE_INCLUDE_DIR NAMES backtrace.h) find_library(LIBBACKTRACE_LIBRARY NAMES libbacktrace.a) if(EXISTS "${LIBBACKTRACE_INCLUDE_DIR}/backtrace-supported.h") file(STRINGS "${LIBBACKTRACE_INCLUDE_DIR}/backtrace-supported.h" BACKTRACE_SUPPORTED_STR REGEX "^#define BACKTRACE_SUPPORTED ([^\n]*)$") endif() if((${BACKTRACE_SUPPORTED_STR} MATCHES "#define BACKTRACE_SUPPORTED 1") AND (EXISTS ${LIBBACKTRACE_LIBRARY})) message(STATUS "libbacktrace: ${LIBBACKTRACE_LIBRARY}") set(WITH_LIBBACKTRACE TRUE PARENT_SCOPE) return() endif() if(NOT BUILD_STATIC AND NOT LINK_STATIC_LIBS) find_path(LIBUNWIND_INCLUDE_DIR NAMES libunwind.h) find_library(LIBUNWIND_LIBRARY NAMES unwind) find_library(LIBLZMA_LIBRARY NAMES lzma) if((EXISTS ${LIBUNWIND_INCLUDE_DIR}) AND (EXISTS ${LIBUNWIND_LIBRARY}) AND (EXISTS ${LIBLZMA_LIBRARY})) message(STATUS "libunwind: ${LIBUNWIND_LIBRARY}") message(STATUS "liblzma: ${LIBLZMA_LIBRARY}") set(LIBUNWIND_LIBRARY "${LIBUNWIND_LIBRARY};${LIBLZMA_LIBRARY}") set(WITH_LIBUNWIND TRUE PARENT_SCOPE) return() endif() endif() endfunction(find_backtrace) ``` -------------------------------- ### Generate Encryption Key Source: https://github.com/hexian000/kcptun-libev/blob/master/README.md Generates a pre-shared key (PSK) for encryption using the specified method. This key is essential for securing your KCP tunnel. ```sh ./kcptun-libev --genpsk xchacha20poly1305_ietf ``` -------------------------------- ### GCC Compiler Flags Source: https://github.com/hexian000/kcptun-libev/blob/master/CMakeLists.txt Configures specific compiler and linker flags for the GCC toolchain. Includes options for file path mapping, stripping symbols in release builds, enabling sanitizers, and handling static/PIE linking. ```cmake if(CMAKE_C_COMPILER_ID STREQUAL "GNU") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ffile-prefix-map=${PROJECT_SOURCE_DIR}/=") set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} -s") set(CMAKE_EXE_LINKER_FLAGS_MINSIZEREL "${CMAKE_EXE_LINKER_FLAGS_MINSIZEREL} -s") if(ENABLE_SANITIZERS) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-omit-frame-pointer -fsanitize=address,leak,undefined -fstack-protector-all") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fno-omit-frame-pointer -fsanitize=address,leak,undefined -static-libasan -static-liblsan -static-libubsan") endif() if(CMAKE_INTERPROCEDURAL_OPTIMIZATION) set(CMAKE_C_COMPILE_OPTIONS_IPO -flto=1 -fno-fat-lto-objects -flto-partition=one -fuse-linker-plugin) endif() if(BUILD_STATIC AND BUILD_PIE) set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-pie") elseif(BUILD_STATIC) set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static") elseif(CMAKE_POSITION_INDEPENDENT_CODE) set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pie") endif() if(NOT BUILD_STATIC AND LINK_STATIC_LIBS) set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libgcc") endif() elseif(CMAKE_C_COMPILER_ID MATCHES "Clang") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ffile-prefix-map=${PROJECT_SOURCE_DIR}/=") set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} -s") set(CMAKE_EXE_LINKER_FLAGS_MINSIZEREL "${CMAKE_EXE_LINKER_FLAGS_MINSIZEREL} -s") if(ENABLE_SANITIZERS) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-omit-frame-pointer -fsanitize=address,leak,undefined -fstack-protector-all") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fno-omit-frame-pointer -fsanitize=address,leak,undefined -static-libsan") endif() if(CMAKE_INTERPROCEDURAL_OPTIMIZATION) set(CMAKE_C_COMPILE_OPTIONS_IPO -flto) endif() if(BUILD_STATIC AND BUILD_PIE) set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-pie") elseif(BUILD_STATIC) set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static") elseif(CMAKE_POSITION_INDEPENDENT_CODE) set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pie") endif() if(NOT BUILD_STATIC AND LINK_STATIC_LIBS) set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libgcc") endif() endif() ``` -------------------------------- ### Check System Symbols Source: https://github.com/hexian000/kcptun-libev/blob/master/contrib/csnippets/CMakeLists.txt Uses CheckSymbolExists module to check for the existence of various C library functions and defines corresponding HAVE_* variables. ```cmake include(CheckSymbolExists) list(APPEND CMAKE_REQUIRED_DEFINITIONS "-D_POSIX_C_SOURCE=200809L" "-D_XOPEN_SOURCE=700") check_symbol_exists(clock_gettime "time.h" HAVE_CLOCK_GETTIME) check_symbol_exists(timespec_get "time.h" HAVE_TIMESPEC_GET) check_symbol_exists(syslog "syslog.h" HAVE_SYSLOG) check_symbol_exists(gmtime_r "time.h" HAVE_GMTIME_R) check_symbol_exists(localtime_r "time.h" HAVE_LOCALTIME_R) check_symbol_exists(wcwidth "wchar.h" HAVE_WCWIDTH) check_symbol_exists(backtrace "execinfo.h" HAVE_BACKTRACE) check_symbol_exists(backtrace_symbols "execinfo.h" HAVE_BACKTRACE_SYMBOLS) ``` -------------------------------- ### Conditional Compile Definitions Source: https://github.com/hexian000/kcptun-libev/blob/master/contrib/csnippets/CMakeLists.txt Sets compile definitions based on the FORCE_POSIX flag. If FORCE_POSIX is set, _POSIX_C_SOURCE is defined; otherwise, _GNU_SOURCE is defined. ```cmake if(FORCE_POSIX) target_compile_definitions(csnippets PRIVATE _POSIX_C_SOURCE=200809L) else() target_compile_definitions(csnippets PRIVATE _GNU_SOURCE) endif() ``` -------------------------------- ### Define csnippets Target Sources Source: https://github.com/hexian000/kcptun-libev/blob/master/contrib/csnippets/algo/CMakeLists.txt This snippet shows how to define the source files for the 'csnippets' target in a CMake project. It separates public header files from private implementation files. ```cmake target_sources(csnippets PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/cityhash.h" "${CMAKE_CURRENT_SOURCE_DIR}/hashtable.h" "${CMAKE_CURRENT_SOURCE_DIR}/luahash.h" PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/cityhash.c" "${CMAKE_CURRENT_SOURCE_DIR}/hashtable.c" ) ``` -------------------------------- ### Enable Position Independent Code Source: https://github.com/hexian000/kcptun-libev/blob/master/CMakeLists.txt Enables position-independent code compilation if the BUILD_PIE option is set. This is often required for shared libraries or certain executable types. ```cmake if(BUILD_PIE) set(CMAKE_POSITION_INDEPENDENT_CODE ON) endif() ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.