Skip to content

Commit

Permalink
feat: support system base on musl libc (#454)
Browse files Browse the repository at this point in the history
### 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 <[email protected]>
  • Loading branch information
bLueriVerLHR and hnwyllmm authored Oct 8, 2024
1 parent 50d3a28 commit 09ee2f4
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 3 deletions.
10 changes: 10 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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{变量名} 值)
Expand Down Expand Up @@ -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")
Expand Down
15 changes: 15 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -161,6 +173,9 @@ function main
init)
do_init
;;
musl)
do_musl_init
;;
clean)
do_clean
;;
Expand Down
6 changes: 6 additions & 0 deletions deps/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion deps/common/os/process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ See the Mulan PSL v2 for more details. */

namespace common {

#ifdef __MACH__
#if defined(__MACH__) or defined(__MUSL__)
#include <libgen.h>
#endif

Expand Down
21 changes: 20 additions & 1 deletion deps/common/time/datetime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/observer/net/buffered_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ See the Mulan PSL v2 for more details. */
//

#include <algorithm>
#ifdef __MUSL__
#include <errno.h>
#else
#include <sys/errno.h>
#endif
#include <unistd.h>

#include "net/buffered_writer.h"
Expand Down
4 changes: 3 additions & 1 deletion src/observer/net/ring_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ See the Mulan PSL v2 for more details. */

#pragma once

#include <stdint.h>

#include "common/rc.h"
#include "common/lang/vector.h"

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

0 comments on commit 09ee2f4

Please sign in to comment.