Skip to content

Commit 09ee2f4

Browse files
feat: support system base on musl libc (#454)
### What problem were solved in this pull request? Issue Number: close #453 Problem: Compatibility issues when implementing miniob on system based on musl libc like alpine linux. ### What is changed and how it works? Install libexecinfo as musl libc not support this feature. ``` bash # `bash build.sh init` first bash build.sh musl ``` Turn on support at miniob/CMakeLists.txt#L25. Then build the project. ``` bash bash build.sh ``` ### Other information This fix initially aims for developing and building miniob in docker containers using alpine linux. NO Address Sanitizer Support in This PR. --------- Co-authored-by: wangyunlai <hnwyllmm@126.com>
1 parent 50d3a28 commit 09ee2f4

File tree

7 files changed

+59
-3
lines changed

7 files changed

+59
-3
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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,18 @@ function do_init
111111
cd $current_dir
112112
}
113113

114+
function do_musl_init
115+
{
116+
git clone 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_COMMAND} install && \
122+
${MAKE_COMMAND} clean && rm ${TOPDIR}/deps/3rd/libexecinfo/libexecinfo.so.* && \
123+
cd ${current_dir}
124+
}
125+
114126
function prepare_build_dir
115127
{
116128
TYPE=$1
@@ -161,6 +173,9 @@ function main
161173
init)
162174
do_init
163175
;;
176+
musl)
177+
do_musl_init
178+
;;
164179
clean)
165180
do_clean
166181
;;

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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ See the Mulan PSL v2 for more details. */
1414

1515
#pragma once
1616

17+
#include <stdint.h>
18+
1719
#include "common/rc.h"
1820
#include "common/lang/vector.h"
1921

@@ -90,4 +92,4 @@ class RingBuffer
9092
vector<char> buffer_; ///< 缓存使用的内存,使用vector方便管理
9193
int32_t data_size_ = 0; ///< 已经写入的数据量
9294
int32_t write_pos_ = 0; ///< 当前写指针的位置,范围不会超出[0, capacity)
93-
};
95+
};

0 commit comments

Comments
 (0)