From 5031564c7f5e00d285ec3d2c94a0cdbb1fa39413 Mon Sep 17 00:00:00 2001 From: bLueriVerLHR Date: Thu, 3 Oct 2024 10:58:00 +0000 Subject: [PATCH 1/3] feat: support system base on musl libc --- CMakeLists.txt | 10 ++++++++++ build.sh | 14 ++++++++++++++ deps/common/CMakeLists.txt | 6 ++++++ deps/common/os/process.cpp | 2 +- deps/common/time/datetime.cpp | 21 ++++++++++++++++++++- src/observer/net/buffered_writer.cpp | 4 ++++ src/observer/net/ring_buffer.h | 4 ++++ 7 files changed, 59 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1f7aeb5f6..31b5917f6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,6 +22,7 @@ OPTION(ENABLE_NOPIE "Enable no pie" OFF) OPTION(CONCURRENCY "Support concurrency operations" OFF) OPTION(STATIC_STDLIB "Link std library static or dynamic, such as libgcc, libstdc++, libasan" OFF) OPTION(USE_SIMD "Use SIMD" OFF) +OPTION(USE_MUSL_LIBC "Use musl libc" OFF) MESSAGE(STATUS "HOME dir: $ENV{HOME}") #SET(ENV{变量名} 值) @@ -80,6 +81,15 @@ IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" AND ${STATIC_STDLIB}) ADD_LINK_OPTIONS(-static-libgcc -static-libstdc++) ENDIF() +IF(USE_MUSL_LIBC) + ADD_DEFINITIONS(-D__MUSL__) + MESSAGE(STATUS "musl libc use pthread in default") + SET(CMAKE_THREAD_LIBS_INIT "-lpthread") + + MESSAGE(AUTHOR_WARNING "Sanitizer and musl libc not support each other for now") + SET(ENABLE_ASAN OFF) +ENDIF(USE_MUSL_LIBC) + IF (ENABLE_ASAN) MESSAGE(STATUS "Instrumenting with Address Sanitizer") SET(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS} -fno-omit-frame-pointer -fsanitize=address -fsanitize-address-use-after-scope") diff --git a/build.sh b/build.sh index b85901dfe..787c935e4 100755 --- a/build.sh +++ b/build.sh @@ -111,6 +111,17 @@ function do_init cd $current_dir } +function do_musl_init +{ + git submodule add https://github.com/ronchaine/libexecinfo deps/3rd/libexecinfo || return + current_dir=$PWD + + MAKE_COMMAND="make --silent" + cd ${TOPDIR}/deps/3rd/libexecinfo && \ + make install && \ + make clean && rm ${TOPDIR}/deps/3rd/libexecinfo/libexecinfo.so.* +} + function prepare_build_dir { TYPE=$1 @@ -161,6 +172,9 @@ function main init) do_init ;; + musl) + do_musl_init + ;; clean) do_clean ;; diff --git a/deps/common/CMakeLists.txt b/deps/common/CMakeLists.txt index 7e27ef7e4..742ba3de0 100644 --- a/deps/common/CMakeLists.txt +++ b/deps/common/CMakeLists.txt @@ -9,6 +9,12 @@ FILE(GLOB_RECURSE ALL_SRC *.cpp) #STATIC,静态库 ADD_LIBRARY(common STATIC ${ALL_SRC} ) + +IF(USE_MUSL_LIBC) + MESSAGE(STATUS "musl libc need manually link libexecinfo") + TARGET_LINK_LIBRARIES(common execinfo) +ENDIF(USE_MUSL_LIBC) + # 编译静态库时,自动会把同名的动态库给删除, 因此需要临时设置一下 SET_TARGET_PROPERTIES(common PROPERTIES CLEAN_DIRECT_OUTPUT 1) diff --git a/deps/common/os/process.cpp b/deps/common/os/process.cpp index 039e5e1f5..38d3a77de 100644 --- a/deps/common/os/process.cpp +++ b/deps/common/os/process.cpp @@ -26,7 +26,7 @@ See the Mulan PSL v2 for more details. */ namespace common { -#ifdef __MACH__ +#if defined(__MACH__) or defined(__MUSL__) #include #endif diff --git a/deps/common/time/datetime.cpp b/deps/common/time/datetime.cpp index 9e1c06620..372f7c157 100644 --- a/deps/common/time/datetime.cpp +++ b/deps/common/time/datetime.cpp @@ -349,7 +349,26 @@ string Now::unique() uint64_t temp; static uint64_t last_unique = 0; #if defined(LINUX) - static pthread_mutex_t mutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP; + #if defined(__MUSL__) + #define MUTEX_INITIALIZER(__mutex, __type) \ + do { \ + static pthread_mutexattr_t __attr; \ + static pthread_mutexattr_t *__p_attr = nullptr; \ + if (nullptr == __p_attr) { \ + __p_attr = &__attr; \ + pthread_mutexattr_init(__p_attr); \ + pthread_mutexattr_settype(__p_attr, __type); \ + pthread_mutex_init(&__mutex, __p_attr); \ + } \ + } while (0) + + static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; + MUTEX_INITIALIZER(mutex, PTHREAD_MUTEX_ERRORCHECK); + + #undef MUTEX_INITIALIZER + #else + static pthread_mutex_t mutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP; + #endif #elif defined(__MACH__) static pthread_mutex_t mutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER; #endif diff --git a/src/observer/net/buffered_writer.cpp b/src/observer/net/buffered_writer.cpp index de33799a8..e77fa4793 100644 --- a/src/observer/net/buffered_writer.cpp +++ b/src/observer/net/buffered_writer.cpp @@ -13,7 +13,11 @@ See the Mulan PSL v2 for more details. */ // #include +#ifdef __MUSL__ +#include +#else #include +#endif #include #include "net/buffered_writer.h" diff --git a/src/observer/net/ring_buffer.h b/src/observer/net/ring_buffer.h index 5968e8858..7ed4c9038 100644 --- a/src/observer/net/ring_buffer.h +++ b/src/observer/net/ring_buffer.h @@ -14,6 +14,10 @@ See the Mulan PSL v2 for more details. */ #pragma once +#ifdef __MUSL__ +#include +#endif + #include "common/rc.h" #include "common/lang/vector.h" From 1fc23d6760802de7e115b466e892a793b0d88ced Mon Sep 17 00:00:00 2001 From: wangyunlai Date: Tue, 8 Oct 2024 09:57:05 +0800 Subject: [PATCH 2/3] Update ring_buffer.h --- src/observer/net/ring_buffer.h | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/observer/net/ring_buffer.h b/src/observer/net/ring_buffer.h index 7ed4c9038..b2fb58a20 100644 --- a/src/observer/net/ring_buffer.h +++ b/src/observer/net/ring_buffer.h @@ -14,9 +14,7 @@ See the Mulan PSL v2 for more details. */ #pragma once -#ifdef __MUSL__ -#include -#endif +#include #include "common/rc.h" #include "common/lang/vector.h" @@ -94,4 +92,4 @@ class RingBuffer vector buffer_; ///< 缓存使用的内存,使用vector方便管理 int32_t data_size_ = 0; ///< 已经写入的数据量 int32_t write_pos_ = 0; ///< 当前写指针的位置,范围不会超出[0, capacity) -}; \ No newline at end of file +}; From 7214eaa4363d0e910ad65e617f5613c1c2b79940 Mon Sep 17 00:00:00 2001 From: wangyunlai Date: Tue, 8 Oct 2024 10:11:49 +0800 Subject: [PATCH 3/3] Update build.sh --- build.sh | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/build.sh b/build.sh index 787c935e4..0a914d671 100755 --- a/build.sh +++ b/build.sh @@ -113,13 +113,14 @@ function do_init function do_musl_init { - git submodule add https://github.com/ronchaine/libexecinfo deps/3rd/libexecinfo || return + git clone https://github.com/ronchaine/libexecinfo deps/3rd/libexecinfo || return current_dir=$PWD MAKE_COMMAND="make --silent" cd ${TOPDIR}/deps/3rd/libexecinfo && \ - make install && \ - make clean && rm ${TOPDIR}/deps/3rd/libexecinfo/libexecinfo.so.* + ${MAKE_COMMAND} install && \ + ${MAKE_COMMAND} clean && rm ${TOPDIR}/deps/3rd/libexecinfo/libexecinfo.so.* && \ + cd ${current_dir} } function prepare_build_dir