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..0a914d671 100755 --- a/build.sh +++ b/build.sh @@ -111,6 +111,18 @@ function do_init cd $current_dir } +function do_musl_init +{ + git clone https://github.com/ronchaine/libexecinfo deps/3rd/libexecinfo || return + current_dir=$PWD + + MAKE_COMMAND="make --silent" + cd ${TOPDIR}/deps/3rd/libexecinfo && \ + ${MAKE_COMMAND} install && \ + ${MAKE_COMMAND} clean && rm ${TOPDIR}/deps/3rd/libexecinfo/libexecinfo.so.* && \ + cd ${current_dir} +} + function prepare_build_dir { TYPE=$1 @@ -161,6 +173,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..b2fb58a20 100644 --- a/src/observer/net/ring_buffer.h +++ b/src/observer/net/ring_buffer.h @@ -14,6 +14,8 @@ See the Mulan PSL v2 for more details. */ #pragma once +#include + #include "common/rc.h" #include "common/lang/vector.h" @@ -90,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 +};