From 2fa40dd43634473f8c0fa29eeda6029190cfe3fd Mon Sep 17 00:00:00 2001 From: Sergi Vladykin Date: Wed, 20 Dec 2023 23:50:39 -0800 Subject: [PATCH] Zig CC cross-compilation support for Windows on Linux host. (#128) Zig CC cross-compilation support for Windows on Linux host --- .github/workflows/.ubuntu.yml | 23 ++++++++++++++++++++++- buffer/buf_test.c | 2 +- condition/cond_test.c | 2 +- condition/sc_cond.c | 3 +++ heap/heap_test.c | 2 +- ini/ini_test.c | 2 +- ini/sc_ini.c | 2 +- logger/log_test.c | 2 +- logger/sc_log.c | 3 +++ memory-map/mmap_test.c | 2 +- memory-map/sc_mmap.c | 2 ++ signal/CMakeLists.txt | 8 ++++++++ signal/sc_signal.c | 9 ++++++--- signal/sc_signal.h | 2 +- socket/CMakeLists.txt | 8 ++++++++ socket/sock_test.c | 2 +- string/str_test.c | 2 +- thread/sc_thread.c | 5 +++-- uri/sc_uri.c | 2 +- uri/uri_test.c | 2 +- 20 files changed, 67 insertions(+), 18 deletions(-) diff --git a/.github/workflows/.ubuntu.yml b/.github/workflows/.ubuntu.yml index bfa0459..9e587da 100644 --- a/.github/workflows/.ubuntu.yml +++ b/.github/workflows/.ubuntu.yml @@ -129,4 +129,25 @@ jobs: mkdir build-debug && cd build-debug cmake .. -DCMAKE_C_FLAGS=-m32 -DSANITIZER=undefined make -j - make check \ No newline at end of file + make check + ubuntu-zig-windows: + runs-on: ubuntu-latest + if: | + (github.event_name == 'pull_request') || + (github.event_name == 'push') || + (github.event_name == 'workflow_dispatch' || + (github.event_name == 'schedule' && github.repository == 'tezc/sc')) && !contains(github.event.inputs.skip, 'zig-windows') + name: Zig Windows + steps: + - uses: actions/checkout@v2.1.0 + - name: build + run: | + sudo apt update + sudo apt-get install cmake curl tar + mkdir build-debug && cd build-debug + export ZIG=zig-linux-x86_64-0.12.0-dev.1834+f36ac227b + curl -o zig.tar.xz "https://ziglang.org/builds/$ZIG.tar.xz" + tar -xf zig.tar.xz + export CC="$(pwd)/$ZIG/zig cc -target x86_64-windows" + cmake .. -DCMAKE_SYSTEM_NAME=Windows + make -j \ No newline at end of file diff --git a/buffer/buf_test.c b/buffer/buf_test.c index a194cc1..885c113 100644 --- a/buffer/buf_test.c +++ b/buffer/buf_test.c @@ -617,7 +617,7 @@ void fail_test(void) sc_buf_term(&buf); } #else -void fail_test() +void fail_test(void) { } #endif diff --git a/condition/cond_test.c b/condition/cond_test.c index 5b68fec..82f6150 100644 --- a/condition/cond_test.c +++ b/condition/cond_test.c @@ -280,7 +280,7 @@ void fail_test(void) } #else -void fail_test() +void fail_test(void) { } #endif diff --git a/condition/sc_cond.c b/condition/sc_cond.c index 1d8e184..d332c40 100644 --- a/condition/sc_cond.c +++ b/condition/sc_cond.c @@ -40,7 +40,10 @@ #include #if defined(_WIN32) || defined(_WIN64) + +#ifdef _MSC_VER #pragma warning(disable : 4996) +#endif int sc_cond_init(struct sc_cond *c) { diff --git a/heap/heap_test.c b/heap/heap_test.c index 062f4c3..81433ba 100644 --- a/heap/heap_test.c +++ b/heap/heap_test.c @@ -239,7 +239,7 @@ void fail_test(void) sc_heap_term(&heap); } #else -void fail_test() +void fail_test(void) { } #endif diff --git a/ini/ini_test.c b/ini/ini_test.c index df8981f..5027a93 100644 --- a/ini/ini_test.c +++ b/ini/ini_test.c @@ -6,7 +6,7 @@ #include #include -#if defined(_WIN32) || defined(_WIN64) +#ifdef _MSC_VER #pragma warning(disable : 4996) #endif diff --git a/ini/sc_ini.c b/ini/sc_ini.c index abd4638..06eca38 100644 --- a/ini/sc_ini.c +++ b/ini/sc_ini.c @@ -37,7 +37,7 @@ #include #include -#if defined(_WIN32) || defined(_WIN64) +#ifdef _MSC_VER #pragma warning(disable : 4996) #endif diff --git a/logger/log_test.c b/logger/log_test.c index da203ff..2c08d02 100644 --- a/logger/log_test.c +++ b/logger/log_test.c @@ -5,7 +5,7 @@ #include #include -#if defined(_WIN32) || defined(_WIN64) +#ifdef _MSC_VER #pragma warning(disable : 4996) #endif diff --git a/logger/sc_log.c b/logger/sc_log.c index 34c07ba..4630633 100644 --- a/logger/sc_log.c +++ b/logger/sc_log.c @@ -73,7 +73,10 @@ thread_local char sc_name[32] = "Thread"; #if defined(_WIN32) || defined(_WIN64) +#ifdef _MSC_VER #pragma warning(disable : 4996) +#endif + #define strcasecmp _stricmp #define localtime_r(a, b) (localtime_s(b, a) == 0 ? b : NULL) #include diff --git a/memory-map/mmap_test.c b/memory-map/mmap_test.c index 4f5e75e..4d681ef 100644 --- a/memory-map/mmap_test.c +++ b/memory-map/mmap_test.c @@ -297,7 +297,7 @@ void fail_test(void) fail_posix_fallocate = UINT32_MAX; } #else -void fail_test() +void fail_test(void) { } #endif diff --git a/memory-map/sc_mmap.c b/memory-map/sc_mmap.c index 484b483..249ed64 100644 --- a/memory-map/sc_mmap.c +++ b/memory-map/sc_mmap.c @@ -43,7 +43,9 @@ #include #include +#ifdef _MSC_VER #pragma warning(disable : 4996) +#endif static void sc_mmap_errstr(struct sc_mmap *m) { diff --git a/signal/CMakeLists.txt b/signal/CMakeLists.txt index 5d0445a..6a2eff1 100644 --- a/signal/CMakeLists.txt +++ b/signal/CMakeLists.txt @@ -12,6 +12,10 @@ add_library( target_include_directories(sc_signal PUBLIC ${CMAKE_CURRENT_LIST_DIR}) +if (CMAKE_SYSTEM_NAME MATCHES Windows) + target_link_libraries(sc_signal -lws2_32) +endif() + if (NOT CMAKE_C_COMPILER_ID MATCHES "MSVC") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -Wall -Wextra -pedantic -Werror") endif () @@ -41,6 +45,10 @@ if (SC_BUILD_TEST) target_compile_options(${PROJECT_NAME}_test PRIVATE -DSC_SIZE_MAX=1400000ul) + if (CMAKE_SYSTEM_NAME MATCHES Windows) + target_link_libraries(${PROJECT_NAME}_test -lws2_32) + endif() + check_c_source_compiles(" #include #include diff --git a/signal/sc_signal.c b/signal/sc_signal.c index 87c78d8..030ac75 100644 --- a/signal/sc_signal.c +++ b/signal/sc_signal.c @@ -52,7 +52,7 @@ #endif #if defined(_WIN32) -#include +#include volatile SOCKET sc_signal_shutdown_fd; #else volatile sig_atomic_t sc_signal_shutdown_fd; @@ -202,13 +202,15 @@ int sc_signal_snprintf(char *buf, size_t sz, const char *fmt, ...) #define WIN32_LEAN_AND_MEAN -#include +#include #include #include #include +#ifdef _MSC_VER #pragma warning(disable : 4996) #pragma comment(lib, "Ws2_32.lib") +#endif BOOL WINAPI sc_console_handler(DWORD type) { @@ -297,9 +299,10 @@ void sc_signal_std_on_fatal(int sig) void sc_signal_std_on_shutdown(int type) { + (void)type; sc_console_handler(CTRL_C_EVENT); } -int sc_signal_init() +int sc_signal_init(void) { BOOL b; sc_signal_log_fd = -1; diff --git a/signal/sc_signal.h b/signal/sc_signal.h index 16243fd..387c7a2 100644 --- a/signal/sc_signal.h +++ b/signal/sc_signal.h @@ -47,7 +47,7 @@ * e.g., CTRL+C to shutdown, twice CTRL+C means 'I don't want to wait anything'. */ #if defined(_WIN32) -#include +#include extern volatile SOCKET sc_signal_shutdown_fd; #else extern volatile sig_atomic_t sc_signal_shutdown_fd; diff --git a/socket/CMakeLists.txt b/socket/CMakeLists.txt index ce495b5..48694df 100644 --- a/socket/CMakeLists.txt +++ b/socket/CMakeLists.txt @@ -12,6 +12,10 @@ add_library( target_include_directories(sc_socket PUBLIC ${CMAKE_CURRENT_LIST_DIR}) +if (CMAKE_SYSTEM_NAME MATCHES Windows) + target_link_libraries(sc_socket -lws2_32) +endif() + if (NOT CMAKE_C_COMPILER_ID MATCHES "MSVC") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -Wall -Wextra -pedantic -pthread -Werror") endif () @@ -41,6 +45,10 @@ if (SC_BUILD_TEST) target_compile_options(${PROJECT_NAME}_test PRIVATE -DSC_SIZE_MAX=300 -Dsc_fcntl=test_fcntl) + if (CMAKE_SYSTEM_NAME MATCHES Windows) + target_link_libraries(${PROJECT_NAME}_test -lws2_32) + endif() + if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux" AND SC_USE_WRAP) if ("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_C_COMPILER_ID}" STREQUAL "GNU") diff --git a/socket/sock_test.c b/socket/sock_test.c index 6684278..a2fd17f 100644 --- a/socket/sock_test.c +++ b/socket/sock_test.c @@ -6,6 +6,7 @@ #include #include +#include #if defined(_WIN32) || defined(_WIN64) #include @@ -25,7 +26,6 @@ struct sc_thread { #else -#include #include #include #include diff --git a/string/str_test.c b/string/str_test.c index 57c1976..6cbf12b 100644 --- a/string/str_test.c +++ b/string/str_test.c @@ -5,7 +5,7 @@ #include #include -#if defined(_WIN32) || defined(_WIN64) +#ifdef _MSC_VER #pragma warning(disable : 4996) #endif diff --git a/thread/sc_thread.c b/thread/sc_thread.c index cc078cf..7dd1db1 100644 --- a/thread/sc_thread.c +++ b/thread/sc_thread.c @@ -39,7 +39,10 @@ void sc_thread_init(struct sc_thread *t) } #if defined(_WIN32) || defined(_WIN64) + +#ifdef _MSC_VER #pragma warning(disable : 4996) +#endif #include @@ -85,7 +88,6 @@ int sc_thread_start(struct sc_thread *t, void *(*fn)(void *), void *arg) int sc_thread_join(struct sc_thread *t, void **ret) { int rc = 0; - void *val = NULL; DWORD rv; BOOL brc; @@ -105,7 +107,6 @@ int sc_thread_join(struct sc_thread *t, void **ret) rc = -1; } - val = t->ret; t->id = 0; out: diff --git a/uri/sc_uri.c b/uri/sc_uri.c index 0c8b9fd..4c0ce36 100644 --- a/uri/sc_uri.c +++ b/uri/sc_uri.c @@ -35,7 +35,7 @@ #include #include -#if defined(_WIN32) || defined(_WIN64) +#ifdef _MSC_VER #pragma warning(disable : 4996) #endif diff --git a/uri/uri_test.c b/uri/uri_test.c index 24e4dda..cfbb21f 100644 --- a/uri/uri_test.c +++ b/uri/uri_test.c @@ -355,7 +355,7 @@ void fail_test(void) fail_second_call = 0; } #else -void fail_test() +void fail_test(void) { } #endif