Skip to content

Commit

Permalink
Zig CC cross-compilation support for Windows on Linux host. (#128)
Browse files Browse the repository at this point in the history
Zig CC cross-compilation support for Windows on Linux host
  • Loading branch information
svladykin authored Dec 21, 2023
1 parent c729344 commit 2fa40dd
Show file tree
Hide file tree
Showing 20 changed files with 67 additions and 18 deletions.
23 changes: 22 additions & 1 deletion .github/workflows/.ubuntu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,25 @@ jobs:
mkdir build-debug && cd build-debug
cmake .. -DCMAKE_C_FLAGS=-m32 -DSANITIZER=undefined
make -j
make check
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/[email protected]
- 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
2 changes: 1 addition & 1 deletion buffer/buf_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ void fail_test(void)
sc_buf_term(&buf);
}
#else
void fail_test()
void fail_test(void)
{
}
#endif
Expand Down
2 changes: 1 addition & 1 deletion condition/cond_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ void fail_test(void)
}

#else
void fail_test()
void fail_test(void)
{
}
#endif
Expand Down
3 changes: 3 additions & 0 deletions condition/sc_cond.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@
#include <string.h>

#if defined(_WIN32) || defined(_WIN64)

#ifdef _MSC_VER
#pragma warning(disable : 4996)
#endif

int sc_cond_init(struct sc_cond *c)
{
Expand Down
2 changes: 1 addition & 1 deletion heap/heap_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ void fail_test(void)
sc_heap_term(&heap);
}
#else
void fail_test()
void fail_test(void)
{
}
#endif
Expand Down
2 changes: 1 addition & 1 deletion ini/ini_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <stdlib.h>
#include <string.h>

#if defined(_WIN32) || defined(_WIN64)
#ifdef _MSC_VER
#pragma warning(disable : 4996)
#endif

Expand Down
2 changes: 1 addition & 1 deletion ini/sc_ini.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
#include <ctype.h>
#include <string.h>

#if defined(_WIN32) || defined(_WIN64)
#ifdef _MSC_VER
#pragma warning(disable : 4996)
#endif

Expand Down
2 changes: 1 addition & 1 deletion logger/log_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <stdarg.h>
#include <time.h>

#if defined(_WIN32) || defined(_WIN64)
#ifdef _MSC_VER
#pragma warning(disable : 4996)
#endif

Expand Down
3 changes: 3 additions & 0 deletions logger/sc_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 <windows.h>
Expand Down
2 changes: 1 addition & 1 deletion memory-map/mmap_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ void fail_test(void)
fail_posix_fallocate = UINT32_MAX;
}
#else
void fail_test()
void fail_test(void)
{
}
#endif
Expand Down
2 changes: 2 additions & 0 deletions memory-map/sc_mmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@
#include <io.h>
#include <stdint.h>

#ifdef _MSC_VER
#pragma warning(disable : 4996)
#endif

static void sc_mmap_errstr(struct sc_mmap *m)
{
Expand Down
8 changes: 8 additions & 0 deletions signal/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()
Expand Down Expand Up @@ -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 <execinfo.h>
#include <unistd.h>
Expand Down
9 changes: 6 additions & 3 deletions signal/sc_signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
#endif

#if defined(_WIN32)
#include <WinSock2.h>
#include <winsock2.h>
volatile SOCKET sc_signal_shutdown_fd;
#else
volatile sig_atomic_t sc_signal_shutdown_fd;
Expand Down Expand Up @@ -202,13 +202,15 @@ int sc_signal_snprintf(char *buf, size_t sz, const char *fmt, ...)

#define WIN32_LEAN_AND_MEAN

#include <Ws2tcpip.h>
#include <ws2tcpip.h>
#include <io.h>
#include <signal.h>
#include <windows.h>

#ifdef _MSC_VER
#pragma warning(disable : 4996)
#pragma comment(lib, "Ws2_32.lib")
#endif

BOOL WINAPI sc_console_handler(DWORD type)
{
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion signal/sc_signal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <WinSock2.h>
#include <winsock2.h>
extern volatile SOCKET sc_signal_shutdown_fd;
#else
extern volatile sig_atomic_t sc_signal_shutdown_fd;
Expand Down
8 changes: 8 additions & 0 deletions socket/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()
Expand Down Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion socket/sock_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <assert.h>
#include <stdio.h>
#include <errno.h>

#if defined(_WIN32) || defined(_WIN64)
#include <synchapi.h>
Expand All @@ -25,7 +26,6 @@ struct sc_thread {

#else

#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <stdarg.h>
Expand Down
2 changes: 1 addition & 1 deletion string/str_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <stdlib.h>
#include <string.h>

#if defined(_WIN32) || defined(_WIN64)
#ifdef _MSC_VER
#pragma warning(disable : 4996)
#endif

Expand Down
5 changes: 3 additions & 2 deletions thread/sc_thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 <process.h>

Expand Down Expand Up @@ -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;

Expand All @@ -105,7 +107,6 @@ int sc_thread_join(struct sc_thread *t, void **ret)
rc = -1;
}

val = t->ret;
t->id = 0;

out:
Expand Down
2 changes: 1 addition & 1 deletion uri/sc_uri.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
#include <stdio.h>
#include <string.h>

#if defined(_WIN32) || defined(_WIN64)
#ifdef _MSC_VER
#pragma warning(disable : 4996)
#endif

Expand Down
2 changes: 1 addition & 1 deletion uri/uri_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ void fail_test(void)
fail_second_call = 0;
}
#else
void fail_test()
void fail_test(void)
{
}
#endif
Expand Down

0 comments on commit 2fa40dd

Please sign in to comment.