Bladeren bron

feat: 添加pthreads模块

SongZihuan 3 jaren geleden
bovenliggende
commit
a6b75f4ae0
8 gewijzigde bestanden met toevoegingen van 94 en 5 verwijderingen
  1. 3 0
      .gitmodules
  2. 7 1
      CMakeLists.txt
  3. 47 0
      deps/cmake/FindPThreadWin32.cmake
  4. 30 1
      deps/deps.cmake
  5. 1 0
      deps/pthread-win32
  6. 1 0
      include/tool/tool.h
  7. 4 2
      lang/hgt.cmake
  8. 1 1
      src/tool/CMakeLists.txt

+ 3 - 0
.gitmodules

@@ -8,3 +8,6 @@
 [submodule "deps/dlfcn"]
 	path = deps/dlfcn
 	url = https://github.com/aFun-org/dlfcn-win32.git
+[submodule "deps/pthread-win32"]
+	path = deps/pthread-win32
+	url = https://github.com/aFun-org/pthread-win32.git

+ 7 - 1
CMakeLists.txt

@@ -127,7 +127,13 @@ set(LIBDIR ${INSTALL_LIBDIR})
 set(CMAKEDIR ${INSTALL_CMAKEDIR})
 set(INCLUDEDIR ${INSTALL_INCLUDEDIR})
 
-set(path_var)
+if (MSVC)
+    set(pthread_INSTALL_DIR ${deps_install_dir})
+    set(pthread_CMAKEDIR pthread_cmake)
+    set(path_var pthread_INSTALL_DIR pthread_CMAKEDIR)
+else()
+    set(path_var)
+endif()
 if (WIN32 AND NOT CYGWIN)  # cygwin 不需要编译 dl
     foreach(tgt dlfcn pcre2 fflags)
         set(${tgt}_INSTALL_DIR ${deps_install_dir})

+ 47 - 0
deps/cmake/FindPThreadWin32.cmake

@@ -0,0 +1,47 @@
+include(FindPackageHandleStandardArgs)
+set(_root ${PThreadWin32_ROOT})
+
+if (NOT _root)
+    find_package_handle_standard_args(PThreadWin32
+                                      FOUND_VAR PThreadWin32_FOUND
+                                      REQUIRED_VARS _root)  # 强制搜不到包
+    unset(_root)
+endif()
+
+# PThreadWin32_ROOT 必须是 PThreadWin32的安装目录
+
+# PThreadWin32
+find_path(pthread_h NAMES pthread.h HINTS ${_root}/include DOC "PThreadWin32 include directory" NO_DEFAULT_PATH)
+find_library(pthread_lib NAMES "libpthreadVC3d.lib" "libpthreadVC3.lib"
+             libPThreadWin32 HINTS ${_root}/lib DOC "PThreadWin32 library" NO_DEFAULT_PATH)
+
+set(pthread_INCLUDE_DIRS ${pthread_h})
+set(pthread_LIBRARIES ${pthread_lib})
+
+unset(pthread_h CACHE)
+unset(pthread_lib CACHE)
+
+message(STATUS "pthread_lib = ${pthread_INCLUDE_DIRS}")
+
+find_package_handle_standard_args(PThreadWin32
+                                  FOUND_VAR PThreadWin32_FOUND
+                                  REQUIRED_VARS
+                                  pthread_INCLUDE_DIRS
+                                  pthread_LIBRARIES)  # 强制搜不到包
+
+if (PThreadWin32_FOUND)
+    add_library(PThreadWin32::pthread STATIC IMPORTED)
+    set_target_properties(PThreadWin32::pthread PROPERTIES
+                          IMPORTED_LOCATION "${pthread_LIBRARIES}"
+                          INTERFACE_INCLUDE_DIRECTORIES "${pthread_INCLUDE_DIRS}"
+                          INTERFACE_SOURCES
+                          "${pthread_INCLUDE_DIRS}/pthread.h"
+                          "${pthread_INCLUDE_DIRS}/sched.h"
+                          "${pthread_INCLUDE_DIRS}/semaphore.h"
+                          "${pthread_INCLUDE_DIRS}/_ptw32.h"
+                          PUBLIC_HEADER
+                          "${pthread_INCLUDE_DIRS}/pthread.h"
+                          "${pthread_INCLUDE_DIRS}/sched.h"
+                          "${pthread_INCLUDE_DIRS}/semaphore.h"
+                          "${pthread_INCLUDE_DIRS}/_ptw32.h")
+endif()

+ 30 - 1
deps/deps.cmake

@@ -7,10 +7,12 @@ set(deps_install_dir ${INSTALL_RESOURCEDIR}/deps)  # 依赖的安装位置
 set(dlfcn_cmake "share/dlfcn-win32")  # dlfcn cmake 安装位置 (相对路径)
 set(pcre2_cmake "cmake")
 set(fflags_cmake "cmake")  # FindFFlags.cmake 不是 fflags 的一部分, 但是会被安装到 cmake 目录下
+set(pthread_cmake "cmake")
 
 set(dlfcn-win32_MUST_BUILD TRUE CACHE BOOL "Must build dlfcn-win32")
 set(PCRE2_MUST_BUILD TRUE CACHE BOOL "Must build pcre2")
 set(FFlags_MUST_BUILD TRUE CACHE BOOL "Must build FFlags")
+set(PThreadWin32_MUST_BUILD TRUE CACHE BOOL "Must build pthreads-win32")
 
 if (WIN32 AND NOT CYGWIN)  # cygwin 不依赖 dl
     if (_print)
@@ -84,4 +86,31 @@ set(fflags_lib FFlags::fflags)
 
 install(DIRECTORY "${fflags_INCLUDE_DIRS}/" DESTINATION ${INSTALL_INCLUDEDIR} FILES_MATCHING PATTERN "*.h")  # 安装fflags.h
 install(FILES ${CMAKE_CURRENT_LIST_DIR}/cmake/FindFFlags.cmake DESTINATION ${deps_install_dir}/cmake)  # 安装find程序
-cfep_install(FFlags PREFIX ${deps_install_dir})
+cfep_install(FFlags PREFIX ${deps_install_dir})
+
+if (MSVC)
+    if (_print)
+        message(STATUS "Build pthreads-win32...")
+    endif()
+    cfep_find_dir(PThreadWin32
+                  REQUIRED
+                  MODULE  # 使用FindFFlags.cmake文件
+                  SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/pthread-win32
+                  CMAKE_DIR "${CMAKE_CURRENT_LIST_DIR}/cmake"  # FindPThreadWin32.cmake 文件位置
+                  EXTERNAL
+                  BUILD_CMAKE_CACHE_ARGS
+                  -DCMAKE_C_FLAGS:STRING=${CMAKE_C_FLAGS}
+                  BUILD_DIR "pthread")
+    set(pthread_lib PThreadWin32::pthread)
+
+    install(DIRECTORY "${pthread_INCLUDE_DIRS}/" DESTINATION ${INSTALL_INCLUDEDIR} FILES_MATCHING PATTERN "*.h")  # 安装fflags.h
+    install(FILES ${CMAKE_CURRENT_LIST_DIR}/cmake/FindPThreadWin32.cmake DESTINATION ${deps_install_dir}/cmake)  # 安装find程序
+    cfep_install(PThreadWin32 PREFIX ${deps_install_dir})
+else()
+    find_package(Threads REQUIRED)
+    set(pthread_define ${CMAKE_USE_PTHREADS_INIT})
+    if (NOT pthread_define)
+        message(FATAL_ERROR "pthread not found")
+    endif()
+    set(pthread_lib Threads::Threads)
+endif()

+ 1 - 0
deps/pthread-win32

@@ -0,0 +1 @@
+Subproject commit 25925381f8d21c44ca3512bea99eb5c76d9ae20a

+ 1 - 0
include/tool/tool.h

@@ -24,6 +24,7 @@
 #include "log.h"
 
 #include "fflags.h"
+#include "pthread.h"
 #include "aFun_ht.h"
 
 #endif //AFUN_TOOL_H

+ 4 - 2
lang/hgt.cmake

@@ -39,7 +39,7 @@ if (re)
     message(FATAL_ERROR "hgt error[${re}]\n${errput}")
 endif()
 
-add_library(hgt-base STATIC)
+add_library(hgt-base SHARED)
 target_sources(hgt-base
                PRIVATE ${hgt_dir}/${hgt_name}_ht.c
                PUBLIC $<BUILD_INTERFACE:${hgt_dir}/${hgt_name}_ht.h> $<INSTALL_INTERFACE:${INSTALL_INCLUDEDIR}/${hgt_name}_ht.h>)
@@ -53,7 +53,9 @@ generate_export_header(hgt-base EXPORT_FILE_NAME "${hgt_dir}/baseHTExport.h" BAS
 target_include_directories(hgt-base PUBLIC
                            $<BUILD_INTERFACE:${hgt_dir}>
                            $<INSTALL_INTERFACE:${INSTALL_INCLUDEDIR}>)
-set_target_properties(hgt-base PROPERTIES PUBLIC_HEADER "${hgt_dir}/${hgt_name}_ht.h")
+set_target_properties(hgt-base PROPERTIES
+                      PUBLIC_HEADER "${hgt_dir}/${hgt_name}_ht.h"
+                      OUTPUT_NAME "aFunlangTr")
 
 install(TARGETS hgt-base
         EXPORT aFunlang

+ 1 - 1
src/tool/CMakeLists.txt

@@ -34,7 +34,7 @@ foreach(tgt tool-shared tool-static)
     target_include_directories(${tgt} PUBLIC
                                $<BUILD_INTERFACE:${build_include_tool}>
                                $<INSTALL_INTERFACE:${install_include_tool}>)
-    target_link_libraries(${tgt} PUBLIC ${hgt-lib} ${dlfcn_lib} ${pcre2_lib} ${fflags_lib})
+    target_link_libraries(${tgt} PUBLIC ${hgt-lib} ${dlfcn_lib} ${pcre2_lib} ${fflags_lib} ${pthread_lib})
     set_target_properties(${tgt} PROPERTIES
                           PUBLIC_HEADER "${public_h_build}")
     if(_build_mem)