From 7fe3afe6806f9b5ee4f9a426dff5553b709bd23c Mon Sep 17 00:00:00 2001 From: axmand <815966508@qq.com> Date: Tue, 16 Dec 2025 15:13:56 +0800 Subject: [PATCH] support _win32 --- CMakeLists.txt | 137 ++++++++++++++++++++++++------------------- include/gk_externs.h | 7 +++ src/error.c | 8 ++- src/memory.c | 6 +- src/win32/adapt.c | 4 ++ 5 files changed, 99 insertions(+), 63 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6a9a694..8c43669 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.10) # ... project(GKlib - VERSION 0.0.1 + VERSION 0.0.1 LANGUAGES C) # include required CMake modules @@ -13,9 +13,9 @@ include(CheckIncludeFile) include(CMakePackageConfigHelpers) include(GNUInstallDirs) -#------------------------------------------------------------------------------- +# ------------------------------------------------------------------------------- # OPTIONS -#------------------------------------------------------------------------------- +# ------------------------------------------------------------------------------- option(ASSERT "turn asserts on" OFF) option(ASSERT2 "additional assertions" OFF) option(DEBUG "add debugging support" OFF) @@ -29,6 +29,11 @@ option(VALGRID "enable valgrind support" OFF) option(NO_X86 "enable no-x86 support" OFF) option(SHARED "enable shared support" OFF) +if(MSVC) + add_compile_options(/wd4005) + add_compile_options(/wd4142) +endif() + if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME) option(GKLIB_BUILD_APPS "build the GKlib applications" ON) else() @@ -42,71 +47,76 @@ else() set(GKLIB_LIBRARY_TYPE STATIC) endif(SHARED) -#------------------------------------------------------------------------------- +# ------------------------------------------------------------------------------- # LIBRARY configuration -#------------------------------------------------------------------------------- +# ------------------------------------------------------------------------------- add_library(${PROJECT_NAME} ${GKLIB_LIBRARY_TYPE}) target_sources(${PROJECT_NAME} PRIVATE src/b64.c src/blas.c src/cache.c src/csr.c src/error.c src/evaluate.c - src/fkvkselect.c src/fs.c src/getopt.c src/gk_util.c src/gkregex.c - src/graph.c src/htable.c src/io.c src/itemsets.c src/mcore.c - src/memory.c src/pqueue.c src/random.c src/rw.c src/seq.c src/sort.c - src/string.c src/timers.c src/tokenizer.c - # these are only included below so that they appear when using IDEs - include/GKlib.h include/gk_arch.h include/gk_defs.h - include/gk_externs.h include/gk_getopt.h include/gk_macros.h - include/gk_mkblas.h include/gk_mkmemory.h include/gk_mkpqueue.h - include/gk_mkpqueue2.h include/gk_mkrandom.h include/gk_mksort.h - include/gk_mkutils.h include/gk_proto.h include/gk_struct.h - include/gk_types.h include/gkregex.h include/gk_ms_inttypes.h - include/gk_ms_stat.h include/gk_ms_stdint.h - # the following are shims for win32 systems - $<$:src/win32/adapt.c - include/win32/adapt.h>) + src/fkvkselect.c src/fs.c src/getopt.c src/gk_util.c src/gkregex.c + src/graph.c src/htable.c src/io.c src/itemsets.c src/mcore.c + src/memory.c src/pqueue.c src/random.c src/rw.c src/seq.c src/sort.c + src/string.c src/timers.c src/tokenizer.c + + # these are only included below so that they appear when using IDEs + include/GKlib.h include/gk_arch.h include/gk_defs.h + include/gk_externs.h include/gk_getopt.h include/gk_macros.h + include/gk_mkblas.h include/gk_mkmemory.h include/gk_mkpqueue.h + include/gk_mkpqueue2.h include/gk_mkrandom.h include/gk_mksort.h + include/gk_mkutils.h include/gk_proto.h include/gk_struct.h + include/gk_types.h include/gkregex.h include/gk_ms_inttypes.h + include/gk_ms_stat.h include/gk_ms_stdint.h + + # the following are shims for win32 systems + $<$:src/win32/adapt.c + include/win32/adapt.h>) target_compile_definitions(${PROJECT_NAME} PUBLIC $<$:LINUX>) target_include_directories(${PROJECT_NAME} PUBLIC $ - $) + $) target_link_libraries(${PROJECT_NAME} PUBLIC $<$>:m>) set_target_properties(${PROJECT_NAME} PROPERTIES SOVERSION ${PROJECT_VERSION_MAJOR} - VERSION ${PROJECT_VERSION}) + VERSION ${PROJECT_VERSION}) add_library(GKlib::GKlib ALIAS ${PROJECT_NAME}) -#------------------------------------------------------------------------------- +# ------------------------------------------------------------------------------- # OPTIONS configuration -#------------------------------------------------------------------------------- +# ------------------------------------------------------------------------------- target_compile_definitions(${PROJECT_NAME} PUBLIC $<$>:NDEBUG> - $<$>:NDEBUG2> - $<$:DEBUG> - $<$:GKRAND> - $<$:NO_X86> - ) + $<$>:NDEBUG2> + $<$:DEBUG> + $<$:GKRAND> + $<$:NO_X86> +) -#------------------------------------------------------------------------------- +# ------------------------------------------------------------------------------- # FEATURE AVAILABILITY checks -#------------------------------------------------------------------------------- +# ------------------------------------------------------------------------------- check_include_file(execinfo.h HAVE_EXECINFO_H) check_function_exists(getline HAVE_GETLINE) # regular expressions if(PCRE) check_include_file(pcreposix.h HAVE_PCREPOSIX_H) + if(NOT HAVE_PCREPOSIX_H) message(WARNING "PCRE was requested, but is not available") endif() endif() + if(NOT HAVE_PCREPOSIX_H) check_include_file(regex.h HAVE_REGEX_H) + if(NOT HAVE_REGEX_H) set(USE_GKREGEX ON) endif() @@ -115,6 +125,7 @@ endif() # profiling support if(GPROF) check_c_compiler_flag("-pg" HAVE_GPROF_SUPPORT) + if(NOT HAVE_GPROF_SUPPORT) message(WARNING "GPROF support was requested, but is not available") endif() @@ -123,6 +134,7 @@ endif() # profiling support if(GDB|DEBUG) check_c_compiler_flag("-g" HAVE_GDB_SUPPORT) + if(NOT HAVE_GDB_SUPPORT) message(WARNING "GDB support was requested, but is not available") endif() @@ -131,6 +143,7 @@ endif() # openmp support if(OPENMP) find_package(OpenMP) + if(NOT OpenMP_C_FOUND) message(WARNING "OpenMP was requested, but is not available") endif() @@ -139,11 +152,13 @@ endif() # thread local storage if(NOT DEFINED HAVE_TLS) set(TLS_NAME "" CACHE INTERNAL "Thread local keyword") + foreach(tls_name "__thread" "__declspec(thread)") unset(HAVE_TLS CACHE) check_c_source_compiles("${tls_name} int x; int main(void) { return 0; }" HAVE_TLS) - if (HAVE_TLS) + + if(HAVE_TLS) set(TLS_NAME ${tls_name} CACHE INTERNAL "Thread local keyword") break() else() @@ -153,12 +168,12 @@ endif() target_compile_definitions(${PROJECT_NAME} PUBLIC $<$:HAVE_EXEC_INFO_H> - $<$:USE_PCRE> - $<$,$>:HAVE_PCREPOSIX_H> - $<$:HAVE_REGEX_H> - $<$:USE_GKREGEX> - $<$:HAVE_GETLINE> - __thread=${TLS_NAME}) + $<$:USE_PCRE> + $<$,$>:HAVE_PCREPOSIX_H> + $<$:HAVE_REGEX_H> + $<$:USE_GKREGEX> + $<$:HAVE_GETLINE> + __thread=${TLS_NAME}) target_compile_options(${PROJECT_NAME} PUBLIC $<$,$>:-pg>) @@ -172,34 +187,34 @@ target_compile_options(${PROJECT_NAME} target_link_libraries(${PROJECT_NAME} PUBLIC $<$:OpenMP::OpenMP_C>) -#------------------------------------------------------------------------------- +# ------------------------------------------------------------------------------- # APPS configuration -#------------------------------------------------------------------------------- +# ------------------------------------------------------------------------------- if(GKLIB_BUILD_APPS) add_subdirectory("apps") endif() -#------------------------------------------------------------------------------- +# ------------------------------------------------------------------------------- # PACKAGE configuration -#------------------------------------------------------------------------------- +# ------------------------------------------------------------------------------- # generate files configure_package_config_file(GKlibConfig.cmake.in cmake/GKlibConfig.cmake INSTALL_DESTINATION lib/cmake/GKlib) write_basic_package_version_file(cmake/GKlibConfigVersion.cmake - VERSION ${PROJECT_VERSION} + VERSION ${PROJECT_VERSION} COMPATIBILITY SameMajorVersion) # install library install(TARGETS ${PROJECT_NAME} EXPORT GKlibTargets - RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} - COMPONENT GKlib_Runtime - LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} - COMPONENT GKlib_Runtime - NAMELINK_SKIP - ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} - COMPONENT GKlib_Development - INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + COMPONENT GKlib_Runtime + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + COMPONENT GKlib_Runtime + NAMELINK_SKIP + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} + COMPONENT GKlib_Development + INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) # The previous install() command is repeated here to distinguish installations # that include a namelink versus those that do not. Unfortunately, prior to @@ -207,25 +222,25 @@ install(TARGETS ${PROJECT_NAME} EXPORT GKlibTargets # necessary to get the desired behavior. if(BUILD_SHARED_LIBS) install(TARGETS ${PROJECT_NAME} - LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} - COMPONENT GKlib_Development - NAMELINK_ONLY) + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + COMPONENT GKlib_Development + NAMELINK_ONLY) endif() # install header files install(DIRECTORY "${CMAKE_SOURCE_DIR}/include/" DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} - COMPONENT GKlib_Development) + COMPONENT GKlib_Development) # install files necessary to use find_package() with GKlib install(EXPORT GKlibTargets - FILE GKlibTargets.cmake - NAMESPACE GKlib:: + FILE GKlibTargets.cmake + NAMESPACE GKlib:: DESTINATION lib/cmake/GKlib - COMPONENT GKlib_Development) + COMPONENT GKlib_Development) install( - FILES ${CMAKE_CURRENT_BINARY_DIR}/cmake/GKlibConfig.cmake - ${CMAKE_CURRENT_BINARY_DIR}/cmake/GKlibConfigVersion.cmake + FILES ${CMAKE_CURRENT_BINARY_DIR}/cmake/GKlibConfig.cmake + ${CMAKE_CURRENT_BINARY_DIR}/cmake/GKlibConfigVersion.cmake DESTINATION lib/cmake/GKlib - COMPONENT GKlib_Development) + COMPONENT GKlib_Development) diff --git a/include/gk_externs.h b/include/gk_externs.h index 2c0fdd9..09425d5 100644 --- a/include/gk_externs.h +++ b/include/gk_externs.h @@ -15,10 +15,17 @@ * Extern variable definition. Hopefully, the __thread makes them thread-safe. **************************************************************************/ #ifndef _GK_ERROR_C_ + +#ifdef _WIN32 +extern __declspec(thread) int gk_cur_jbufs; +extern __declspec(thread) jmp_buf gk_jbufs[]; +extern __declspec(thread) jmp_buf gk_jbuf; +#else /* declared in error.c */ extern __thread int gk_cur_jbufs; extern __thread jmp_buf gk_jbufs[]; extern __thread jmp_buf gk_jbuf; +#endif #endif diff --git a/src/error.c b/src/error.c index e2a18cf..5d3572d 100644 --- a/src/error.c +++ b/src/error.c @@ -19,9 +19,15 @@ This file contains functions dealing with error reporting and termination /* These are the jmp_buf for the graceful exit in case of severe errors. Multiple buffers are defined to allow for recursive invokation. */ #define MAX_JBUFS 128 -__thread int gk_cur_jbufs=-1; +#ifdef _WIN32 +__declspec(thread) int gk_cur_jbufs = -1; +__declspec(thread) jmp_buf gk_jbufs[MAX_JBUFS]; +__declspec(thread) jmp_buf gk_jbuf; +#else +__thread int gk_cur_jbufs = -1; __thread jmp_buf gk_jbufs[MAX_JBUFS]; __thread jmp_buf gk_jbuf; +#endif typedef void (*gksighandler_t)(int); diff --git a/src/memory.c b/src/memory.c index e6dc99c..387d594 100644 --- a/src/memory.c +++ b/src/memory.c @@ -16,7 +16,11 @@ can be used to define other memory allocation routines. #include /* This is for the global mcore that tracks all heap allocations */ -static __thread gk_mcore_t *gkmcore = NULL; +#ifdef _WIN32 +static __declspec(thread) gk_mcore_t* gkmcore = NULL; +#else +static __thread gk_mcore_t* gkmcore = NULL; +#endif /*************************************************************************/ diff --git a/src/win32/adapt.c b/src/win32/adapt.c index 546857c..1bb89dd 100644 --- a/src/win32/adapt.c +++ b/src/win32/adapt.c @@ -3,7 +3,11 @@ \brief Implementation of Win32 adaptation of libc functions */ +#ifdef _WIN32 +#include "win32/adapt.h" +#else #include "adapt.h" +#endif pid_t getpid(void) {