From 0da3f42953c5845625811071ef409f245bc7b8c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20Lindel=C3=B6w?= Date: Sun, 12 Apr 2026 18:42:38 +0200 Subject: [PATCH] Replace m4 with cmake script --- .github/workflows/ci.yml | 19 ++++++------- CMakeLists.txt | 12 ++++----- cmake/GenerateHeader.cmake | 11 ++++++++ include/tinycsocket.h | 2 +- src/{tinycsocket.h.m4 => tinycsocket.h.in} | 11 ++++---- src/tinycsocket_internal.h | 2 +- tools/build_msvc.bat | 31 +++++++++++----------- tools/is_header_up_to_date.sh | 10 ++++--- 8 files changed, 53 insertions(+), 45 deletions(-) create mode 100644 cmake/GenerateHeader.cmake rename src/{tinycsocket.h.m4 => tinycsocket.h.in} (75%) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 49b02c4..13acc3f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -73,11 +73,8 @@ jobs: runs-on: windows-latest steps: - uses: actions/checkout@v4 - - name: Install m4 - run: choco install gnuwin32-m4 --version=1.4.14 -y - name: Build x86 run: | - $env:PATH += ";C:\Program Files (x86)\GnuWin32\bin" cmake -G "Visual Studio 17 2022" -A "win32" -B build32 -DTCS_ENABLE_TESTS=ON -DTCS_ENABLE_EXAMPLES=ON -DTCS_WARNINGS_AS_ERRORS=ON cmake --build build32 --target ALL_BUILD --config Debug cmake --build build32 --target ALL_BUILD --config Release @@ -111,7 +108,7 @@ jobs: - uses: actions/checkout@v4 - name: Build run: | - apk add cmake g++ ninja m4 linux-headers + apk add cmake g++ ninja linux-headers mkdir build && cd build cmake -G "Ninja" ../ -DCMAKE_VERBOSE_MAKEFILE=ON -DTCS_ENABLE_TESTS=ON -DTCS_ENABLE_EXAMPLES=ON -DTCS_WARNINGS_AS_ERRORS=ON -DCMAKE_EXE_LINKER_FLAGS=-static cmake --build . @@ -134,7 +131,7 @@ jobs: -v ${{ github.workspace }}:/workspace \ -w /workspace \ i386/alpine:3.23.3 sh -c ' - apk add cmake g++ ninja m4 linux-headers && + apk add cmake g++ ninja linux-headers && mkdir build && cd build && cmake -G "Ninja" ../ -DCMAKE_VERBOSE_MAKEFILE=ON -DTCS_ENABLE_TESTS=ON -DTCS_ENABLE_EXAMPLES=ON -DTCS_WARNINGS_AS_ERRORS=ON -DCMAKE_EXE_LINKER_FLAGS=-static && cmake --build . && @@ -176,7 +173,7 @@ jobs: -v ${{ github.workspace }}:/workspace \ -w /workspace \ i386/debian:bookworm sh -c ' - apt-get update && apt-get install -y cmake ninja-build g++ m4 && + apt-get update && apt-get install -y cmake ninja-build g++ && mkdir build && cd build && cmake -G "Ninja" ../ -DCMAKE_VERBOSE_MAKEFILE=ON -DTCS_ENABLE_TESTS=ON -DTCS_ENABLE_EXAMPLES=ON -DTCS_WARNINGS_AS_ERRORS=ON && cmake --build . && @@ -235,7 +232,7 @@ jobs: - uses: actions/checkout@v4 - name: Build run: | - apk add cmake mingw-w64-gcc ninja m4 + apk add cmake mingw-w64-gcc ninja mkdir build && cd build cmake -G "Ninja" ../ -DCMAKE_TOOLCHAIN_FILE="../toolchain_mingw_x64_winblue.cmake" -DCMAKE_VERBOSE_MAKEFILE=ON -DTCS_ENABLE_TESTS=ON -DTCS_ENABLE_EXAMPLES=ON -DTCS_WARNINGS_AS_ERRORS=ON cmake --build . @@ -258,7 +255,7 @@ jobs: -v ${{ github.workspace }}:/workspace \ -w /workspace \ i386/alpine:3.23.3 sh -c ' - apk add cmake mingw-w64-gcc ninja m4 && + apk add cmake mingw-w64-gcc ninja && mkdir build && cd build && cmake -G "Ninja" ../ -DCMAKE_TOOLCHAIN_FILE="../toolchain_mingw_x86_winblue.cmake" -DCMAKE_VERBOSE_MAKEFILE=ON -DTCS_ENABLE_TESTS=ON -DTCS_ENABLE_EXAMPLES=ON -DTCS_WARNINGS_AS_ERRORS=ON && cmake --build . && @@ -318,7 +315,7 @@ jobs: with: usesh: true prepare: | - pkg install -y cmake ninja m4 + pkg install -y cmake ninja run: | mkdir build && cd build cmake -G "Ninja" ../ -DCMAKE_VERBOSE_MAKEFILE=ON -DTCS_ENABLE_TESTS=ON -DTCS_ENABLE_EXAMPLES=ON -DTCS_WARNINGS_AS_ERRORS=ON @@ -341,7 +338,7 @@ jobs: with: usesh: true prepare: | - pkg_add cmake ninja m4 + pkg_add cmake ninja run: | mkdir build && cd build cmake -G "Ninja" ../ -DCMAKE_VERBOSE_MAKEFILE=ON -DTCS_ENABLE_TESTS=ON -DTCS_ENABLE_EXAMPLES=ON -DTCS_WARNINGS_AS_ERRORS=ON @@ -413,7 +410,7 @@ jobs: - uses: actions/checkout@v4 - uses: egor-tensin/setup-cygwin@v4 with: - packages: cmake gcc-g++ ninja m4 + packages: cmake gcc-g++ ninja - name: Build shell: C:\tools\cygwin\bin\bash.exe --login -eo pipefail -o igncr {0} run: | diff --git a/CMakeLists.txt b/CMakeLists.txt index b9c63b7..6775b99 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -57,15 +57,13 @@ add_custom_target( ) set_target_properties(generate_version PROPERTIES FOLDER tinycsocket) -# Header generation (requires GNU m4 for undivert with filename) -find_program(M4_EXECUTABLE gm4 m4 REQUIRED) +# Header generation (concatenates source files into single-header via cmake) add_custom_command( OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/include/tinycsocket.h - COMMAND - ${M4_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/src/tinycsocket.h.m4 > - ${CMAKE_CURRENT_SOURCE_DIR}/include/tinycsocket.h - DEPENDS ${TINYCSOCKET_SRC} ${CMAKE_CURRENT_SOURCE_DIR}/src/tinycsocket.h.m4 - WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/src + COMMAND ${CMAKE_COMMAND} -DSRC_DIR=${CMAKE_CURRENT_SOURCE_DIR} + -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/GenerateHeader.cmake + DEPENDS ${TINYCSOCKET_SRC} ${CMAKE_CURRENT_SOURCE_DIR}/src/tinycsocket.h.in + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} ) add_custom_target( generate_header diff --git a/cmake/GenerateHeader.cmake b/cmake/GenerateHeader.cmake new file mode 100644 index 0000000..8f05aa9 --- /dev/null +++ b/cmake/GenerateHeader.cmake @@ -0,0 +1,11 @@ +file(READ "${SRC_DIR}/src/tinycsocket_internal.h" TINYCSOCKET_INTERNAL_H) +file(READ "${SRC_DIR}/src/tinydatastructures.h" TINYDATASTRUCTURES_H) +file(READ "${SRC_DIR}/src/tinycsocket_posix.c" TINYCSOCKET_POSIX_C) +file(READ "${SRC_DIR}/src/tinycsocket_win32.c" TINYCSOCKET_WIN32_C) +file(READ "${SRC_DIR}/src/tinycsocket_common.c" TINYCSOCKET_COMMON_C) + +if(NOT OUTPUT) + set(OUTPUT "${SRC_DIR}/include/tinycsocket.h") +endif() + +configure_file("${SRC_DIR}/src/tinycsocket.h.in" "${OUTPUT}" @ONLY) diff --git a/include/tinycsocket.h b/include/tinycsocket.h index b06ef0c..df6717b 100644 --- a/include/tinycsocket.h +++ b/include/tinycsocket.h @@ -29,7 +29,7 @@ #ifndef TINYCSOCKET_INTERNAL_H_ #define TINYCSOCKET_INTERNAL_H_ -static const char* const TCS_VERSION_TXT = "v0.3.60"; +static const char* const TCS_VERSION_TXT = "v0.3.61"; static const char* const TCS_LICENSE_TXT = "Copyright 2018 Markus Lindelöw\n" "\n" diff --git a/src/tinycsocket.h.m4 b/src/tinycsocket.h.in similarity index 75% rename from src/tinycsocket.h.m4 rename to src/tinycsocket.h.in index 5eb9aa5..98b5721 100644 --- a/src/tinycsocket.h.m4 +++ b/src/tinycsocket.h.in @@ -1,27 +1,26 @@ -dnl generate with m4 program `m4 tinycsocket.h.m4 > ../include/tinycsocket.h` /* Warning! This file is generated by the tinycsocket project. */ #ifndef TINYCSOCKET_HEADER_H_ #define TINYCSOCKET_HEADER_H_ /**********************************/ /********* tinycsocket.h **********/ /**********************************/ -undivert(tinycsocket_internal.h) +@TINYCSOCKET_INTERNAL_H@ #ifdef TINYCSOCKET_IMPLEMENTATION /**********************************/ /****** tinydatastructures.h ******/ /**********************************/ -undivert(tinydatastructures.h) +@TINYDATASTRUCTURES_H@ /**********************************/ /****** tinycsocket_posix.h *******/ /**********************************/ -undivert(tinycsocket_posix.c) +@TINYCSOCKET_POSIX_C@ /**********************************/ /****** tinycsocket_win32.h *******/ /**********************************/ -undivert(tinycsocket_win32.c) +@TINYCSOCKET_WIN32_C@ /**********************************/ /****** tinycsocket_common.h ******/ /**********************************/ -undivert(tinycsocket_common.c) +@TINYCSOCKET_COMMON_C@ #endif #endif diff --git a/src/tinycsocket_internal.h b/src/tinycsocket_internal.h index 3c4db85..e9db88e 100644 --- a/src/tinycsocket_internal.h +++ b/src/tinycsocket_internal.h @@ -23,7 +23,7 @@ #ifndef TINYCSOCKET_INTERNAL_H_ #define TINYCSOCKET_INTERNAL_H_ -static const char* const TCS_VERSION_TXT = "v0.3.60"; +static const char* const TCS_VERSION_TXT = "v0.3.61"; static const char* const TCS_LICENSE_TXT = "Copyright 2018 Markus Lindelöw\n" "\n" diff --git a/tools/build_msvc.bat b/tools/build_msvc.bat index 92e617a..bdfd1a6 100644 --- a/tools/build_msvc.bat +++ b/tools/build_msvc.bat @@ -1,16 +1,15 @@ -choco feature enable -n allowGlobalConfirmation -choco install cmake -choco install gnuwin32-m4 --version=1.4.14 -call RefreshEnv.cmd -set PATH=%PATH%;C:\Program Files\CMake\bin\ -mkdir build32 -cd build32 -cmake.exe -G "Visual Studio 17 2022" -A "win32" ../ -DTCS_ENABLE_TESTS=ON -DTCS_ENABLE_EXAMPLES=ON -DTCS_WARNINGS_AS_ERRORS=OFF -cmake.exe --build . --target ALL_BUILD --config Debug -cmake.exe --build . --target ALL_BUILD --config Release -cd .. -mkdir build64 -cd build64 -cmake.exe -G "Visual Studio 17 2022" -A "x64" ../ -DTCS_ENABLE_TESTS=ON -DTCS_ENABLE_EXAMPLES=ON -DTCS_WARNINGS_AS_ERRORS=OFF -cmake.exe --build . --target ALL_BUILD --config Debug -cmake.exe --build . --target ALL_BUILD --config Release +choco feature enable -n allowGlobalConfirmation +choco install cmake +call RefreshEnv.cmd +set PATH=%PATH%;C:\Program Files\CMake\bin\ +mkdir build32 +cd build32 +cmake.exe -G "Visual Studio 17 2022" -A "win32" ../ -DTCS_ENABLE_TESTS=ON -DTCS_ENABLE_EXAMPLES=ON -DTCS_WARNINGS_AS_ERRORS=OFF +cmake.exe --build . --target ALL_BUILD --config Debug +cmake.exe --build . --target ALL_BUILD --config Release +cd .. +mkdir build64 +cd build64 +cmake.exe -G "Visual Studio 17 2022" -A "x64" ../ -DTCS_ENABLE_TESTS=ON -DTCS_ENABLE_EXAMPLES=ON -DTCS_WARNINGS_AS_ERRORS=OFF +cmake.exe --build . --target ALL_BUILD --config Debug +cmake.exe --build . --target ALL_BUILD --config Release diff --git a/tools/is_header_up_to_date.sh b/tools/is_header_up_to_date.sh index df9f7a9..49caff1 100755 --- a/tools/is_header_up_to_date.sh +++ b/tools/is_header_up_to_date.sh @@ -1,5 +1,9 @@ #!/bin/sh -# Returns 0 if up to date +# Returns 0 if the committed header matches what would be generated PROJPATH="$(dirname "$(readlink -f "$0")")"/.. -cd $PROJPATH/src -m4 tinycsocket.h.m4 | diff $PROJPATH/include/tinycsocket.h - +TMPFILE=$(mktemp) +trap "rm -f $TMPFILE" EXIT + +# Generate to temp file +cmake -DSRC_DIR=$PROJPATH -DOUTPUT=$TMPFILE -P $PROJPATH/cmake/GenerateHeader.cmake +diff $PROJPATH/include/tinycsocket.h $TMPFILE