Skip to content

Commit 5031564

Browse files
committed
feat: support system base on musl libc
1 parent 50d3a28 commit 5031564

File tree

7 files changed

+59
-2
lines changed

7 files changed

+59
-2
lines changed

CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ OPTION(ENABLE_NOPIE "Enable no pie" OFF)
2222
OPTION(CONCURRENCY "Support concurrency operations" OFF)
2323
OPTION(STATIC_STDLIB "Link std library static or dynamic, such as libgcc, libstdc++, libasan" OFF)
2424
OPTION(USE_SIMD "Use SIMD" OFF)
25+
OPTION(USE_MUSL_LIBC "Use musl libc" OFF)
2526

2627
MESSAGE(STATUS "HOME dir: $ENV{HOME}")
2728
#SET(ENV{变量名} 值)
@@ -80,6 +81,15 @@ IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" AND ${STATIC_STDLIB})
8081
ADD_LINK_OPTIONS(-static-libgcc -static-libstdc++)
8182
ENDIF()
8283

84+
IF(USE_MUSL_LIBC)
85+
ADD_DEFINITIONS(-D__MUSL__)
86+
MESSAGE(STATUS "musl libc use pthread in default")
87+
SET(CMAKE_THREAD_LIBS_INIT "-lpthread")
88+
89+
MESSAGE(AUTHOR_WARNING "Sanitizer and musl libc not support each other for now")
90+
SET(ENABLE_ASAN OFF)
91+
ENDIF(USE_MUSL_LIBC)
92+
8393
IF (ENABLE_ASAN)
8494
MESSAGE(STATUS "Instrumenting with Address Sanitizer")
8595
SET(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS} -fno-omit-frame-pointer -fsanitize=address -fsanitize-address-use-after-scope")

build.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,17 @@ function do_init
111111
cd $current_dir
112112
}
113113

114+
function do_musl_init
115+
{
116+
git submodule add https://github.com/ronchaine/libexecinfo deps/3rd/libexecinfo || return
117+
current_dir=$PWD
118+
119+
MAKE_COMMAND="make --silent"
120+
cd ${TOPDIR}/deps/3rd/libexecinfo && \
121+
make install && \
122+
make clean && rm ${TOPDIR}/deps/3rd/libexecinfo/libexecinfo.so.*
123+
}
124+
114125
function prepare_build_dir
115126
{
116127
TYPE=$1
@@ -161,6 +172,9 @@ function main
161172
init)
162173
do_init
163174
;;
175+
musl)
176+
do_musl_init
177+
;;
164178
clean)
165179
do_clean
166180
;;

deps/common/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ FILE(GLOB_RECURSE ALL_SRC *.cpp)
99
#STATIC,静态库
1010
ADD_LIBRARY(common STATIC ${ALL_SRC} )
1111

12+
13+
IF(USE_MUSL_LIBC)
14+
MESSAGE(STATUS "musl libc need manually link libexecinfo")
15+
TARGET_LINK_LIBRARIES(common execinfo)
16+
ENDIF(USE_MUSL_LIBC)
17+
1218
# 编译静态库时,自动会把同名的动态库给删除, 因此需要临时设置一下
1319
SET_TARGET_PROPERTIES(common PROPERTIES CLEAN_DIRECT_OUTPUT 1)
1420

deps/common/os/process.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ See the Mulan PSL v2 for more details. */
2626

2727
namespace common {
2828

29-
#ifdef __MACH__
29+
#if defined(__MACH__) or defined(__MUSL__)
3030
#include <libgen.h>
3131
#endif
3232

deps/common/time/datetime.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,26 @@ string Now::unique()
349349
uint64_t temp;
350350
static uint64_t last_unique = 0;
351351
#if defined(LINUX)
352-
static pthread_mutex_t mutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
352+
#if defined(__MUSL__)
353+
#define MUTEX_INITIALIZER(__mutex, __type) \
354+
do { \
355+
static pthread_mutexattr_t __attr; \
356+
static pthread_mutexattr_t *__p_attr = nullptr; \
357+
if (nullptr == __p_attr) { \
358+
__p_attr = &__attr; \
359+
pthread_mutexattr_init(__p_attr); \
360+
pthread_mutexattr_settype(__p_attr, __type); \
361+
pthread_mutex_init(&__mutex, __p_attr); \
362+
} \
363+
} while (0)
364+
365+
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
366+
MUTEX_INITIALIZER(mutex, PTHREAD_MUTEX_ERRORCHECK);
367+
368+
#undef MUTEX_INITIALIZER
369+
#else
370+
static pthread_mutex_t mutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
371+
#endif
353372
#elif defined(__MACH__)
354373
static pthread_mutex_t mutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER;
355374
#endif

src/observer/net/buffered_writer.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ See the Mulan PSL v2 for more details. */
1313
//
1414

1515
#include <algorithm>
16+
#ifdef __MUSL__
17+
#include <errno.h>
18+
#else
1619
#include <sys/errno.h>
20+
#endif
1721
#include <unistd.h>
1822

1923
#include "net/buffered_writer.h"

src/observer/net/ring_buffer.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ See the Mulan PSL v2 for more details. */
1414

1515
#pragma once
1616

17+
#ifdef __MUSL__
18+
#include <cstdint>
19+
#endif
20+
1721
#include "common/rc.h"
1822
#include "common/lang/vector.h"
1923

0 commit comments

Comments
 (0)