From 2c15e8be6a7605b6fb68091773581b79ec3e6ecc Mon Sep 17 00:00:00 2001 From: lqx Date: Sat, 8 Jun 2024 00:30:31 +0800 Subject: [PATCH] feat:Supports compilation on FreeBSD14 --- CMakeLists.txt | 5 ++++- include/pika_server.h | 4 +++- src/net/CMakeLists.txt | 2 +- src/net/src/net_interfaces.cc | 8 ++++++-- src/pika_monotonic_time.cc | 15 +++++++++++++-- src/pika_rsync_service.cc | 4 ++++ src/pstd/src/rsync.cc | 5 +++++ src/storage/src/coding.h | 7 +++++-- 8 files changed, 41 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6a263cd599..41b2d897f0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -56,6 +56,9 @@ endif() if(CMAKE_SYSTEM_NAME MATCHES "Darwin") set(CMAKE_CXX_FLAGS "-pthread") add_definitions(-DOS_MACOSX) +elseif (CMAKE_SYSTEM_NAME MATCHES "FreeBSD") + set(CMAKE_CXX_FLAGS "-pthread") + add_definitions(-DOS_FREEBSD) elseif(CMAKE_SYSTEM_NAME MATCHES "Linux") if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang") set(CMAKE_EXE_LINKER_FLAGS "-stdlib=libc++ -fuse-ld=lld -lc++ -lc++abi ${CMAKE_EXE_LINKER_FLAGS}") @@ -66,7 +69,7 @@ elseif(CMAKE_SYSTEM_NAME MATCHES "Linux") endif() add_definitions(-DOS_LINUX) else() - message(FATAL_ERROR "only support linux or macOs") + message(FATAL_ERROR "only support linux or macOs or FreeBSD") endif() if(HOST_ARCH MATCHES "x86_64" OR HOST_ARCH MATCHES "i386") diff --git a/include/pika_server.h b/include/pika_server.h index 2e26678d05..4811c54045 100644 --- a/include/pika_server.h +++ b/include/pika_server.h @@ -7,12 +7,14 @@ #define PIKA_SERVER_H_ #include -#if defined(__APPLE__) + +#if defined(__APPLE__) || defined(__FreeBSD__) # include # include #else # include #endif + #include #include diff --git a/src/net/CMakeLists.txt b/src/net/CMakeLists.txt index d25c06163e..dc38d0d3d8 100644 --- a/src/net/CMakeLists.txt +++ b/src/net/CMakeLists.txt @@ -15,7 +15,7 @@ add_subdirectory(examples) if(${CMAKE_SYSTEM_NAME} MATCHES "Linux") list(FILTER DIR_SRCS EXCLUDE REGEX ".net_kqueue.*") -elseif(${CMAKE_SYSTEM_NAME} MATCHES "Darwin") +elseif(${CMAKE_SYSTEM_NAME} MATCHES "Darwin" OR ${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD") list(FILTER DIR_SRCS EXCLUDE REGEX ".net_epoll.*") endif() diff --git a/src/net/src/net_interfaces.cc b/src/net/src/net_interfaces.cc index fd8b1a7906..89061dd5b1 100644 --- a/src/net/src/net_interfaces.cc +++ b/src/net/src/net_interfaces.cc @@ -12,10 +12,14 @@ #include #include -#if defined(__APPLE__) +#if defined(__APPLE__) || defined(__FreeBSD__) +# include +# include # include +# include # include # include +# include # include # include "pstd/include/pstd_defer.h" @@ -31,7 +35,7 @@ #include "pstd/include/xdebug.h" std::string GetDefaultInterface() { -#if defined(__APPLE__) +#if defined(__APPLE__) || defined(__FreeBSD__) std::string name("lo0"); int fd = socket(AF_INET, SOCK_DGRAM, 0); diff --git a/src/pika_monotonic_time.cc b/src/pika_monotonic_time.cc index e1c8c51496..1c3f6e820d 100644 --- a/src/pika_monotonic_time.cc +++ b/src/pika_monotonic_time.cc @@ -3,7 +3,7 @@ // LICENSE file in the root directory of this source tree. An additional grant // of patent rights can be found in the PATENTS file in the same directory. -#ifdef __APPLE__ // Mac +#if defined(__APPLE__) // Mac #include #include "include/pika_monotonic_time.h" @@ -17,7 +17,18 @@ monotime getMonotonicUs() { return nanos / 1000; } -#elif __linux__ // Linux +#elif defined(__FreeBSD__) // FreeBSD +#include + +#include "include/pika_monotonic_time.h" + +monotime getMonotonicUs() { + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + return (ts.tv_sec * 1000000) + (ts.tv_nsec / 1000); +} + +#elif defined(__linux__) // Linux #ifdef __x86_64__ // x86_64 diff --git a/src/pika_rsync_service.cc b/src/pika_rsync_service.cc index 5f1d8c0e6b..5071a1cfc1 100644 --- a/src/pika_rsync_service.cc +++ b/src/pika_rsync_service.cc @@ -15,6 +15,10 @@ #include "include/pika_conf.h" #include "include/pika_define.h" +#ifdef __FreeBSD__ +# include +#endif + extern std::unique_ptr g_pika_conf; PikaRsyncService::PikaRsyncService(const std::string& raw_path, const int port) : raw_path_(raw_path), port_(port) { diff --git a/src/pstd/src/rsync.cc b/src/pstd/src/rsync.cc index 44ca330aff..5748cfa5ac 100644 --- a/src/pstd/src/rsync.cc +++ b/src/pstd/src/rsync.cc @@ -8,6 +8,11 @@ #include "pstd/include/rsync.h" #include "pstd/include/xdebug.h" +#ifdef __FreeBSD__ +# include +# include +#endif + namespace pstd { // Clean files for rsync info, such as the lock, log, pid, conf file static bool CleanRsyncInfo(const std::string& path) { return pstd::DeleteDirIfExist(path + kRsyncSubDir); } diff --git a/src/storage/src/coding.h b/src/storage/src/coding.h index 001e9d76ee..824bf7a080 100644 --- a/src/storage/src/coding.h +++ b/src/storage/src/coding.h @@ -6,17 +6,20 @@ #ifndef SRC_CODING_H_ #define SRC_CODING_H_ +#undef STORAGE_PLATFORM_IS_LITTLE_ENDIAN + #if defined(__APPLE__) # include // __BYTE_ORDER # define __BYTE_ORDER __DARWIN_BYTE_ORDER # define __LITTLE_ENDIAN __DARWIN_LITTLE_ENDIAN #elif defined(__FreeBSD__) -# include // __BYTE_ORDER +# include +# include +# define STORAGE_PLATFORM_IS_LITTLE_ENDIAN (_BYTE_ORDER == _LITTLE_ENDIAN) #else # include // __BYTE_ORDER #endif -#undef STORAGE_PLATFORM_IS_LITTLE_ENDIAN #ifndef STORAGE_PLATFORM_IS_LITTLE_ENDIAN # define STORAGE_PLATFORM_IS_LITTLE_ENDIAN (__BYTE_ORDER == __LITTLE_ENDIAN) #endif