Skip to content

Commit

Permalink
Add support for overriding the Windows Kernel lib name (awslabs#520)
Browse files Browse the repository at this point in the history
  • Loading branch information
ColdenCullen authored Oct 7, 2019
1 parent aeab88e commit 2c1c17c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
14 changes: 9 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ option(PERFORM_HEADER_CHECK "Performs compile-time checks that each header can b
option(AWS_NUM_CPU_CORES "Number of CPU cores of the target machine. Useful when cross-compiling." 0)

if (WIN32)
set(WINDOWS_KERNEL_LIB "Kernel32" CACHE STRING "The name of the kernel library to link against (default: Kernel32)")

file(GLOB AWS_COMMON_OS_HEADERS
"include/aws/common/windows/*"
)
Expand All @@ -69,7 +71,8 @@ if (WIN32)
source_group("Source Files\\windows" FILES ${AWS_COMMON_OS_SRC})
endif ()

set(PLATFORM_LIBS BCrypt Kernel32 Ws2_32)
list(APPEND PLATFORM_DEFINES WINDOWS_KERNEL_LIB=${WINDOWS_KERNEL_LIB})
list(APPEND PLATFORM_LIBS BCrypt ${WINDOWS_KERNEL_LIB} Ws2_32)
else ()
file(GLOB AWS_COMMON_OS_HEADERS
"include/aws/common/posix/*"
Expand All @@ -85,13 +88,13 @@ else ()
message(FATAL_ERROR "Core Foundation not found")
endif ()

set(PLATFORM_LIBS Threads::Threads ${CORE_FOUNDATION_LIB})
list(APPEND PLATFORM_LIBS Threads::Threads ${CORE_FOUNDATION_LIB})
elseif (${CMAKE_SYSTEM_NAME} STREQUAL "Linux") # Android does not link to libpthread nor librt, so this is fine
set(PLATFORM_LIBS m Threads::Threads rt)
list(APPEND PLATFORM_LIBS m Threads::Threads rt)
elseif(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
set(PLATFORM_LIBS m thr execinfo)
list(APPEND PLATFORM_LIBS m thr execinfo)
elseif(CMAKE_SYSTEM_NAME STREQUAL "NetBSD")
set(PLATFORM_LIBS m Threads::Threads execinfo)
list(APPEND PLATFORM_LIBS m Threads::Threads execinfo)
endif()

endif()
Expand Down Expand Up @@ -127,6 +130,7 @@ endif()

aws_add_sanitizers(${CMAKE_PROJECT_NAME} BLACKLIST "sanitizer-blacklist.txt")
target_link_libraries(${CMAKE_PROJECT_NAME} PUBLIC ${PLATFORM_LIBS})
target_compile_definitions(${CMAKE_PROJECT_NAME} PRIVATE ${PLATFORM_DEFINES})

if (AWS_NUM_CPU_CORES)
target_compile_definitions(${CMAKE_PROJECT_NAME} PRIVATE -DAWS_NUM_CPU_CORES=${AWS_NUM_CPU_CORES})
Expand Down
8 changes: 6 additions & 2 deletions source/windows/clock.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,17 @@ typedef VOID WINAPI timefunc_t(LPFILETIME);
static VOID WINAPI s_get_system_time_func_lazy_init(LPFILETIME filetime_p);
static timefunc_t *volatile s_p_time_func = s_get_system_time_func_lazy_init;

/* Convert a string from a macro to a wide string */
#define WIDEN2(s) L#s
#define WIDEN(s) WIDEN2(s)

static BOOL CALLBACK s_get_system_time_init_once(PINIT_ONCE init_once, PVOID param, PVOID *context) {
(void)init_once;
(void)param;
(void)context;

HMODULE hkernel32 = GetModuleHandleW(L"kernel32.dll");
timefunc_t *time_func = (timefunc_t *)GetProcAddress(hkernel32, "GetSystemTimePreciseAsFileTime");
HMODULE kernel = GetModuleHandleW(WIDEN(WINDOWS_KERNEL_LIB) L".dll");
timefunc_t *time_func = (timefunc_t *)GetProcAddress(kernel, "GetSystemTimePreciseAsFileTime");

if (time_func == NULL) {
time_func = GetSystemTimeAsFileTime;
Expand Down

0 comments on commit 2c1c17c

Please sign in to comment.