From fc44a62262950069964aab832583b181e7cf6d21 Mon Sep 17 00:00:00 2001 From: HankWu Date: Fri, 27 Jun 2025 15:28:56 +0800 Subject: [PATCH] build: add MXE cross-compilation support with pthread and gettimeofday fallback --- CMakeLists.txt | 18 ++++++++++++++++++ common/pthreads_cross.c | 11 +++++++++-- common/pthreads_cross.h | 4 ++-- common/time_util.h | 8 +++++++- 4 files changed, 36 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4806fdea..2e1ca6c7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,6 +8,24 @@ option(BUILD_SHARED_LIBS "Build shared libraries" ON) option(BUILD_EXAMPLES "Build example executables" ON) option(ASAN "Use AddressSanitizer for debug builds to detect memory issues" OFF) + +include(CheckIncludeFile) +check_include_file(pthread.h HAVE_PTHREAD) +if(HAVE_PTHREAD) + add_definitions(-DHAVE_PTHREAD) + message(STATUS "pthread.h found, using POSIX thread support") +endif() +# gettimeofday +check_include_file(sys/time.h HAVE_SYS_TIME_H) +if(HAVE_SYS_TIME_H) + add_definitions(-DHAVE_SYS_TIME_H) + message(STATUS "sys/time.h found, using gettimeofday()") +else() + message(WARNING "sys/time.h not found, gettimeofday() will not be available") +endif() + + + if (ASAN) set(ASAN_FLAGS "\ -fsanitize=address \ diff --git a/common/pthreads_cross.c b/common/pthreads_cross.c index 04e556cf..5a856075 100644 --- a/common/pthreads_cross.c +++ b/common/pthreads_cross.c @@ -22,7 +22,9 @@ SOFTWARE. #include "common/pthreads_cross.h" -#ifdef _WIN32 + + +#if defined(_WIN32) && !defined(HAVE_PTHREAD) typedef struct { SRWLOCK lock; @@ -238,6 +240,12 @@ unsigned int timespec_to_ms(const struct timespec *abstime) return ((abstime->tv_sec - time(NULL)) * 1000) + (abstime->tv_nsec / 1000000); } +#endif + + +#if defined(_WIN32) +#include + unsigned int pcthread_get_num_procs() { SYSTEM_INFO sysinfo; @@ -245,7 +253,6 @@ unsigned int pcthread_get_num_procs() GetSystemInfo(&sysinfo); return sysinfo.dwNumberOfProcessors; } - #else #include diff --git a/common/pthreads_cross.h b/common/pthreads_cross.h index fc40c9ba..8263f122 100644 --- a/common/pthreads_cross.h +++ b/common/pthreads_cross.h @@ -23,7 +23,7 @@ SOFTWARE. #ifndef __CPTHREAD_H__ #define __CPTHREAD_H__ -#ifdef _WIN32 +#if defined(_WIN32) && !defined(HAVE_PTHREAD) #include #include #else @@ -32,7 +32,7 @@ SOFTWARE. #endif #include -#ifdef _WIN32 +#if defined(_WIN32) && !defined(HAVE_PTHREAD) typedef CRITICAL_SECTION pthread_mutex_t; typedef void pthread_mutexattr_t; diff --git a/common/time_util.h b/common/time_util.h index d778a18f..fcaeb5a3 100644 --- a/common/time_util.h +++ b/common/time_util.h @@ -31,11 +31,17 @@ either expressed or implied, of the Regents of The University of Michigan. #include #include -#ifdef _WIN32 +#if defined(_WIN32) +#if !defined(HAVE_SYS_TIME_H) #include +#endif + +#include +#define sleep(x) Sleep((x)*1000) typedef long long suseconds_t; #endif + #ifdef _MSC_VER inline int gettimeofday(struct timeval* tp, void* tzp)