Skip to content

Commit

Permalink
Support Zig for x86_64-windows-gnu
Browse files Browse the repository at this point in the history
  • Loading branch information
justsmth committed Sep 3, 2024
1 parent 173bd09 commit bea364c
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 12 deletions.
7 changes: 0 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -408,13 +408,6 @@ if(GCC OR CLANG)
endif()
endif()

if(MINGW)
# Some MinGW compilers set _WIN32_WINNT to an older version (Windows Server 2003)
# See: https://learn.microsoft.com/en-us/cpp/porting/modifying-winver-and-win32-winnt?view=msvc-170
# Support Windows 7 and later.
add_definitions(-D_WIN32_WINNT=_WIN32_WINNT_WIN7)
endif()

if(CLANG)
set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wnewline-eof -fcolor-diagnostics")
elseif(CMAKE_C_COMPILER_VERSION VERSION_GREATER "4.1.3")
Expand Down
6 changes: 2 additions & 4 deletions crypto/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,12 @@
#endif
#endif

#if defined(OPENSSL_THREADS) && \
(!defined(OPENSSL_WINDOWS) || defined(__MINGW32__))
#if defined(OPENSSL_THREADS) && !defined(__MSVCRT__)
#include <pthread.h>
#define OPENSSL_PTHREADS
#endif

#if defined(OPENSSL_THREADS) && !defined(OPENSSL_PTHREADS) && \
defined(OPENSSL_WINDOWS)
#if defined(OPENSSL_THREADS) && !defined(OPENSSL_PTHREADS) && defined(__MSVCRT__)
#define OPENSSL_WINDOWS_THREADS
OPENSSL_MSVC_PRAGMA(warning(push, 3))
#include <windows.h>
Expand Down
13 changes: 13 additions & 0 deletions crypto/thread_win.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ static void NTAPI thread_local_destructor(PVOID module, DWORD reason,
// Note, in the prefixed build, |p_thread_callback_boringssl| may be a macro.
#define STRINGIFY(x) #x
#define EXPAND_AND_STRINGIFY(x) STRINGIFY(x)
#ifdef _MSC_VER
#ifdef _WIN64
__pragma(comment(linker, "/INCLUDE:_tls_used"))
__pragma(comment(
Expand All @@ -157,6 +158,7 @@ __pragma(comment(linker, "/INCLUDE:__tls_used"))
__pragma(comment(
linker, "/INCLUDE:_" EXPAND_AND_STRINGIFY(p_thread_callback_boringssl)))
#endif
#endif // _MSC_VER

// .CRT$XLA to .CRT$XLZ is an array of PIMAGE_TLS_CALLBACK pointers that are
// called automatically by the OS loader code (not the CRT) when the module is
Expand All @@ -174,6 +176,7 @@ __pragma(comment(
// reference to this variable with a linker /INCLUDE:symbol pragma to ensure
// that.) If this variable is discarded, the OnThreadExit function will never
// be called.
#ifdef _MSC_VER
#ifdef _WIN64

// .CRT section is merged with .rdata on x64 so it must be constant data.
Expand All @@ -193,6 +196,16 @@ PIMAGE_TLS_CALLBACK p_thread_callback_boringssl = thread_local_destructor;
#pragma data_seg()

#endif // _WIN64
#elif defined(__GNUC__) || defined(__clang__)
// GCC/Clang-specific code
#ifdef _WIN64
const PIMAGE_TLS_CALLBACK p_thread_callback_boringssl
__attribute__((section(".CRT$XLC"))) = thread_local_destructor;
#else
PIMAGE_TLS_CALLBACK p_thread_callback_boringssl
__attribute__((section(".CRT$XLC"))) = thread_local_destructor;
#endif
#endif // _MSC_VER

static void **get_thread_locals(void) {
// |TlsGetValue| clears the last error even on success, so that callers may
Expand Down
2 changes: 1 addition & 1 deletion tool/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function(build_benchmark target_name install_path)
if (libdecrepit-${target_name})
target_link_libraries(${target_name} ${libdecrepit-${target_name}})
endif ()
if(NOT MSVC AND NOT ANDROID)
if(NOT WIN32 AND NOT ANDROID)
target_link_libraries(${target_name} pthread dl)
endif()
target_compile_options(${target_name} PUBLIC -Wno-deprecated-declarations)
Expand Down
5 changes: 5 additions & 0 deletions util/zig/zig-c++.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0 OR ISC

zig c++ "${@}"
5 changes: 5 additions & 0 deletions util/zig/zig-cc.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0 OR ISC

zig cc "${@}"
9 changes: 9 additions & 0 deletions util/zig/zig-x86_64-windows-gnu.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
set(CMAKE_ASM_COMPILER "${CMAKE_CURRENT_SOURCE_DIR}/util/zig/zig-cc.sh")
set(CMAKE_C_COMPILER "${CMAKE_CURRENT_SOURCE_DIR}/util/zig/zig-cc.sh")
set(CMAKE_CXX_COMPILER "${CMAKE_CURRENT_SOURCE_DIR}/util/zig/zig-c++.sh")

set(CMAKE_SYSTEM_NAME "Windows")
set(CMAKE_SYSTEM_PROCESSOR "x86_64")
set(CMAKE_ASM_COMPILER_TARGET "x86_64-windows-gnu")
set(CMAKE_C_COMPILER_TARGET "x86_64-windows-gnu")
set(CMAKE_CXX_COMPILER_TARGET "x86_64-windows-gnu")

0 comments on commit bea364c

Please sign in to comment.