Skip to content

build: Fall back to C11 for CMake <3.21 #111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
May 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 74 additions & 13 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,83 @@ jobs:
strategy:
matrix:
os:
- ubuntu-latest
- ubuntu-22.04
runs-on: ${{ matrix.os }}
- ubuntu:latest
- ubuntu:22.04
- debian:latest
- debian:stable
- debian:oldstable
- almalinux:latest
- amazonlinux:latest
- fedora:latest
- oraclelinux:9
- archlinux:latest
- clearlinux:latest
config:
- cmake_args: "-DENABLE_API=ON -DCMAKE_C_COMPILER=gcc"
- cmake_args: "-DENABLE_API=ON -DCMAKE_C_COMPILER=clang"
- cmake_args: "-DENABLE_API=OFF"
exclude:
# Clang configured for C11 rejects our C23 usage
- os: debian:oldstable
config:
cmake_args: "-DENABLE_API=ON -DCMAKE_C_COMPILER=clang"

runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Create build directory
run: mkdir -p build

- name: Install dependencies
run: sudo apt-get update && sudo apt-get install -y libcurl4-openssl-dev libjansson-dev libmicrohttpd-dev libsodium-dev

- name: Build DATUM Gateway
- name: Build inside Docker
run: |
cd build
cmake ..
make -j$(nproc)
PACKAGES_CLANG='clang'
PACKAGES_GCC='gcc'
case "${{ matrix.os }}" in
debian:*|ubuntu:*)
INSTALL_CMD="apt update && apt install -y"
PACKAGES="git libc6-dev cmake libcurl4-openssl-dev libjansson-dev libsodium-dev pkgconf"
PACKAGES_API="libmicrohttpd-dev"
;;
almalinux:*|amazonlinux:*|fedora:*|oraclelinux:*)
INSTALL_CMD="dnf install -y"
[[ "${{ matrix.os }}" =~ ^almalinux: ]] && INSTALL_CMD="dnf install -y dnf-plugins-core && dnf config-manager --set-enabled crb && $INSTALL_CMD"
[[ "${{ matrix.os }}" =~ ^oraclelinux: ]] && INSTALL_CMD="dnf install -y dnf-plugins-core && dnf config-manager --set-enabled ol9_codeready_builder && $INSTALL_CMD"
[[ "${{ matrix.os }}" =~ ^(alma|oracle)linux: ]] && INSTALL_CMD="dnf install -y epel-release && $INSTALL_CMD"
PACKAGES="git cmake libcurl-devel jansson-devel libsodium-devel pkgconf"
PACKAGES_API="libmicrohttpd-devel"
[[ "${{ matrix.config.cmake_args }}" =~ clang|gcc ]] || PACKAGES="$PACKAGES gcc"
;;
archlinux:*)
INSTALL_CMD="pacman -Syu --noconfirm"
PACKAGES="git base-devel cmake curl jansson libsodium"
PACKAGES_API="libmicrohttpd"
;;
clearlinux:*)
INSTALL_CMD="swupd bundle-add"
PACKAGES="git c-basic devpkg-curl devpkg-jansson devpkg-libsodium"
PACKAGES_API="devpkg-libmicrohttpd"
PACKAGES_CLANG="llvm"
PACKAGES_GCC='' # included in c-basic
;;
esac
PACKAGES="$PACKAGES ${{ matrix.config.extra_deps }}"
if [[ "${{ matrix.config.cmake_args }}" =~ ENABLE_API=ON ]]; then
PACKAGES="$PACKAGES $PACKAGES_API"
fi
if [[ "${{ matrix.config.cmake_args }}" =~ CMAKE_C_COMPILER=gcc ]]; then
PACKAGES="$PACKAGES $PACKAGES_GCC"
elif [[ "${{ matrix.config.cmake_args }}" =~ CMAKE_C_COMPILER=clang ]]; then
PACKAGES="$PACKAGES $PACKAGES_CLANG"
fi
CMD="set -ex
${INSTALL_CMD} ${PACKAGES}
git config --global --add safe.directory /workspace
mkdir -p build
cd build
cmake /workspace -DCMAKE_C_FLAGS='-Wall -Werror' ${{ matrix.config.cmake_args }}
make -j\$(nproc)
"
docker run \
-v "${{ github.workspace }}:/workspace":ro \
"${{ matrix.os }}" \
/bin/sh -c "${CMD}"
42 changes: 34 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
cmake_minimum_required(VERSION 3.21)
cmake_minimum_required(VERSION 3.13)

project(DATUM VERSION 0.2.3 LANGUAGES C)
set(CMAKE_C_STANDARD 23)

# Enable C23 if supported, else fall back to C11 for compatibility
if(CMAKE_VERSION VERSION_LESS "3.21")
# Older CMake: C23 not recognized; use C11
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
else()
# CMake 3.21+: C23 is available
set(CMAKE_C_STANDARD 23)
set(CMAKE_C_STANDARD_REQUIRED OFF)
endif()

option(ENABLE_API "Build API support." ON)

include(GNUInstallDirs)

add_executable(datum_gateway
src/datum_api.c
src/datum_blocktemplates.c
src/datum_coinbaser.c
src/datum_conf.c
Expand Down Expand Up @@ -42,17 +53,25 @@ set(WEB_RESOURCES
find_package(PkgConfig REQUIRED)
pkg_check_modules(CURL REQUIRED libcurl)
pkg_check_modules(JANSSON REQUIRED jansson)
if(ENABLE_API)
pkg_check_modules(MICROHTTPD REQUIRED libmicrohttpd)
endif()
pkg_check_modules(SODIUM REQUIRED libsodium)
find_package(Threads REQUIRED)

set(POW_LIBS "")
include(CheckLibraryExists)
include(CMakePushCheckState)
cmake_push_check_state(RESET)
string(APPEND CMAKE_REQUIRED_FLAGS -Wno-error)

set(POW_LIBS "")
check_library_exists(m pow "" LIBM)
if(LIBM)
list(APPEND POW_LIBS "m")
endif()

cmake_pop_check_state()

add_custom_target(generate_git_version
BYPRODUCTS ${PROJECT_BINARY_DIR}/git_version.h
COMMAND ${CMAKE_COMMAND} -DBUILD_INFO_HEADER_PATH=${PROJECT_BINARY_DIR}/git_version.h -DSOURCE_DIR=${PROJECT_SOURCE_DIR} -P ${PROJECT_SOURCE_DIR}/cmake/script/GenerateBuildInfo.cmake
Expand All @@ -73,14 +92,12 @@ target_include_directories(datum_gateway
PRIVATE
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}>
${CURL_INCLUDE_DIRS}
${MICROHTTPD_INCLUDE_DIRS}
${JANSSON_INCLUDE_DIRS}
${SODIUM_INCLUDE_DIRS}
)
target_link_directories(datum_gateway
PUBLIC
${CURL_LIBRARY_DIRS}
${MICROHTTPD_LIBRARY_DIRS}
${JANSSON_LIBRARY_DIRS}
${SODIUM_LIBRARY_DIRS}
)
Expand All @@ -89,17 +106,26 @@ target_link_libraries(datum_gateway
${POW_LIBS}
Threads::Threads
${CURL_LIBRARIES} ${CURL_LDFLAGS} ${CURL_LDFLAGS_OTHER}
${MICROHTTPD_LIBRARIES} ${MICROHTTPD_LDFLAGS} ${MICROHTTPD_LDFLAGS_OTHER}
${JANSSON_LIBRARIES} ${JANSSON_LDFLAGS} ${JANSSON_LDFLAGS_OTHER}
${SODIUM_LIBRARIES} ${SODIUM_LDFLAGS} ${SODIUM_LDFLAGS_OTHER}
)
target_compile_options(datum_gateway
PUBLIC
${CURL_CFLAGS} ${CURL_CFLAGS_OTHER}
${MICROHTTPD_CFLAGS} ${MICROHTTPD_CFLAGS_OTHER}
${JANSSON_CFLAGS} ${JANSSON_CFLAGS_OTHER}
${SODIUM_CFLAGS} ${SODIUM_CFLAGS_OTHER}
)

if(ENABLE_API)
target_sources(datum_gateway PRIVATE src/datum_api.c)
target_include_directories(datum_gateway PRIVATE ${MICROHTTPD_INCLUDE_DIRS})
target_link_directories(datum_gateway PUBLIC ${MICROHTTPD_LIBRARY_DIRS})
target_link_libraries(datum_gateway PUBLIC ${MICROHTTPD_LIBRARIES} ${MICROHTTPD_LDFLAGS} ${MICROHTTPD_LDFLAGS_OTHER})
target_compile_options(datum_gateway PUBLIC
-DENABLE_API
${MICROHTTPD_CFLAGS} ${MICROHTTPD_CFLAGS_OTHER}
)
endif()

install(FILES README.md DESTINATION ${CMAKE_INSTALL_DOCDIR})
install(FILES doc/DATUM_recommended_setup-network_diagram.svg DESTINATION ${CMAKE_INSTALL_DOCDIR}/doc)
6 changes: 6 additions & 0 deletions src/datum_conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,11 @@ int datum_read_config(const char *conffile) {
datum_config.bitcoind_work_update_seconds = 120;
}

#ifndef ENABLE_API
if (datum_config.api_listen_port) {
DLOG_WARN("API is enabled in configuration, but this build was compiled without API support");
}
#else
datum_config.api_admin_password_len = strlen(datum_config.api_admin_password);
if (datum_config.api_admin_password_len) {
static const char hash_tag[] = "DATUM Anti-CSRF Token";
Expand All @@ -326,6 +331,7 @@ int datum_read_config(const char *conffile) {
my_sha256(hash, data, data_sz);
hash2hex(hash, datum_config.api_csrf_token);
}
#endif

if (datum_config.stratum_v1_max_threads > MAX_THREADS) {
DLOG_FATAL("Maximum threads must be less than %d.", MAX_THREADS);
Expand Down
2 changes: 2 additions & 0 deletions src/datum_gateway.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,13 @@ int main(int argc, char **argv) {
}
last_datum_protocol_connect_tsms = current_time_millis();

#ifdef ENABLE_API
if (datum_api_init()) {
DLOG_FATAL("Error initializing API interface");
usleep(100000);
exit(1);
}
#endif

if (datum_coinbaser_init()) {
DLOG_FATAL("Error initializing coinbaser thread");
Expand Down