From a9a1d92a1da73960162e7f351562e3110d54286e Mon Sep 17 00:00:00 2001 From: TheCharlatan Date: Wed, 4 Dec 2024 13:19:31 +0100 Subject: [PATCH 1/3] build: Add option for building for bare metal envs A bare metal build is now supported by setting CMAKE_SYSTEM_NAME=Generic Skip the platform-dependent feature checks, such as threads and atomics, which are typically not available on bare metal. Also only make the boost headers mandatory if they exist for the target. Also exclude aes from the crypto library, since that requires the secure allocator, which uses more advance features, such as mutexes. --- CMakeLists.txt | 6 ++++++ cmake/introspection.cmake | 2 ++ src/CMakeLists.txt | 2 +- src/crypto/CMakeLists.txt | 4 ++-- src/test/util/CMakeLists.txt | 2 +- 5 files changed, 12 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 69db01f2755..f09cec1b0e1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -204,22 +204,28 @@ endif() # Check and set PIC/PIE #============================= set(configure_warnings) +if(NOT CMAKE_SYSTEM_NAME STREQUAL "Generic") include(CheckLinkerSupportsPIE) # Set CMAKE_POSITION_INDEPENDENT_CODE early so it applies to all # subsequent checks, including dependency packages and tool flags. check_linker_supports_pie(configure_warnings) +endif() #============================= # Find dependencies #============================= +if(NOT CMAKE_SYSTEM_NAME STREQUAL "Generic") set(THREADS_PREFER_PTHREAD_FLAG ON) find_package(Threads REQUIRED) target_link_libraries(core_interface INTERFACE Threads::Threads ) +endif() +if(NOT CMAKE_SYSTEM_NAME STREQUAL "Generic") include(AddBoostIfNeeded) add_boost_if_needed() +endif() if(ENABLE_WALLET) if(VCPKG_TARGET_TRIPLET) diff --git a/cmake/introspection.cmake b/cmake/introspection.cmake index d2257633974..d6083f5243a 100644 --- a/cmake/introspection.cmake +++ b/cmake/introspection.cmake @@ -17,7 +17,9 @@ if(NOT WIN32) endif() include(TestAppendRequiredLibraries) +if(NOT CMAKE_SYSTEM_NAME STREQUAL "Generic") test_append_atomic_library(core_interface) +endif() # Even though ::system is part of the standard library, we still check # for it, to support building targets that don't have it, such as iOS. diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 8c0977f0882..47266dcf5cd 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -289,7 +289,7 @@ target_link_libraries(bitcoin_node leveldb minisketch univalue - Boost::headers + $ $ ) if(WITH_EMBEDDED_ASMAP) diff --git a/src/crypto/CMakeLists.txt b/src/crypto/CMakeLists.txt index 4dc84664ea7..e1993a7f98b 100644 --- a/src/crypto/CMakeLists.txt +++ b/src/crypto/CMakeLists.txt @@ -3,7 +3,7 @@ # file COPYING or https://opensource.org/license/mit/. add_library(bitcoin_crypto STATIC EXCLUDE_FROM_ALL - aes.cpp + $<$>:aes.cpp> chacha20.cpp chacha20poly1305.cpp hex_base.cpp @@ -20,7 +20,7 @@ add_library(bitcoin_crypto STATIC EXCLUDE_FROM_ALL sha512.cpp siphash.cpp ../support/cleanse.cpp - ../support/lockedpool.cpp + $<$>:../support/lockedpool.cpp> ) target_link_libraries(bitcoin_crypto diff --git a/src/test/util/CMakeLists.txt b/src/test/util/CMakeLists.txt index 5d9832fc144..2658809d098 100644 --- a/src/test/util/CMakeLists.txt +++ b/src/test/util/CMakeLists.txt @@ -23,7 +23,7 @@ add_library(test_util STATIC EXCLUDE_FROM_ALL target_link_libraries(test_util PRIVATE core_interface - Boost::headers + $ $<$:SQLite3::SQLite3> PUBLIC univalue From bfdbf513f694ce201429c09f1785d896c2af9b0f Mon Sep 17 00:00:00 2001 From: TheCharlatan Date: Wed, 4 Dec 2024 13:51:21 +0100 Subject: [PATCH 2/3] Add CI job for producing a static bare metal binary --- ci/test/00_setup_env_riscv_bare_cross.sh | 37 ++++++++++++ ci/test/01_base_install.sh | 8 +++ ci/test/03_test_script.sh | 5 ++ ci/test/link-riscv.sh | 73 ++++++++++++++++++++++++ 4 files changed, 123 insertions(+) create mode 100755 ci/test/00_setup_env_riscv_bare_cross.sh create mode 100755 ci/test/link-riscv.sh diff --git a/ci/test/00_setup_env_riscv_bare_cross.sh b/ci/test/00_setup_env_riscv_bare_cross.sh new file mode 100755 index 00000000000..b68434087dc --- /dev/null +++ b/ci/test/00_setup_env_riscv_bare_cross.sh @@ -0,0 +1,37 @@ +#!/usr/bin/env bash +# +# Copyright (c) Bitcoin Core developers +# Distributed under the MIT software license, see the accompanying +# file COPYING or http://www.opensource.org/licenses/mit-license.php. + +export LC_ALL=C.UTF-8 + +export CONTAINER_NAME=ci_native_riscv_bare + +export GOAL="bitcoin_consensus bitcoin_crypto secp256k1" +export CI_IMAGE_NAME_TAG="mirror.gcr.io/ubuntu:26.04" +export HOST="riscv32-unknown-elf-gcc" +export PACKAGES="autoconf automake autotools-dev curl python3 python3-pip libmpc-dev libmpfr-dev libgmp-dev gawk build-essential bison flex texinfo gperf libtool patchutils bc zlib1g-dev libexpat-dev ninja-build git cmake libglib2.0-dev libslirp-dev" +export BITCOIN_CONFIG="-DCMAKE_C_COMPILER=/opt/riscv-ilp32/bin/riscv32-unknown-elf-gcc \ + -DCMAKE_CXX_COMPILER=/opt/riscv-ilp32/bin/riscv32-unknown-elf-g++ \ + -DBUILD_KERNEL_LIB=OFF \ + -DBUILD_UTIL_CHAINSTATE=OFF \ + -DBUILD_TESTS=OFF \ + -DBUILD_BENCH=OFF \ + -DBUILD_FUZZ_BINARY=OFF \ + -DBUILD_DAEMON=OFF \ + -DBUILD_TX=OFF \ + -DBUILD_UTIL=OFF \ + -DBUILD_CLI=OFF \ + -DBUILD_BITCOIN_BIN=OFF \ + -DENABLE_WALLET=OFF \ + -DENABLE_EXTERNAL_SIGNER=OFF \ + -DENABLE_IPC=OFF \ + -DCMAKE_SYSTEM_NAME=Generic \ + -DIFADDR_LINKS_WITHOUT_LIBSOCKET=ON \ + " + +export BARE_METAL_RISCV="true" +export RUN_UNIT_TESTS="false" +export RUN_FUNCTIONAL_TESTS="false" +export NO_DEPENDS="true" diff --git a/ci/test/01_base_install.sh b/ci/test/01_base_install.sh index 54dafbe8fd4..3dee1b734c5 100755 --- a/ci/test/01_base_install.sh +++ b/ci/test/01_base_install.sh @@ -87,6 +87,14 @@ if [[ -n "${USE_INSTRUMENTED_LIBCPP}" ]]; then rm -rf /llvm-project fi +if [[ ${BARE_METAL_RISCV} == "true" ]]; then + ${CI_RETRY_EXE} git clone --depth=1 https://github.com/riscv-collab/riscv-gnu-toolchain -b 2026.06.06 /riscv/gcc + ( cd /riscv/gcc; + ./configure --prefix=/opt/riscv-ilp32 --with-arch=rv32gc --with-abi=ilp32 --disable-gdb; + make "$MAKEJOBS"; ) + rm -rf /riscv/gcc +fi + if [[ "${RUN_IWYU}" == true ]]; then ${CI_RETRY_EXE} git clone --depth=1 https://github.com/include-what-you-use/include-what-you-use -b clang_"${IWYU_LLVM_V}" /include-what-you-use pushd /include-what-you-use diff --git a/ci/test/03_test_script.sh b/ci/test/03_test_script.sh index c55f4430163..7650ab7d86b 100755 --- a/ci/test/03_test_script.sh +++ b/ci/test/03_test_script.sh @@ -167,6 +167,11 @@ if [ -n "${CI_LIMIT_STACK_SIZE}" ]; then ulimit -s 512 fi +if [[ ${BARE_METAL_RISCV} == "true" ]]; then + export BASE_BUILD_DIR + "${BASE_ROOT_DIR}/ci/test/link-riscv.sh" +fi + if [ -n "$USE_VALGRIND" ]; then "${BASE_ROOT_DIR}/ci/test/wrap-valgrind.py" fi diff --git a/ci/test/link-riscv.sh b/ci/test/link-riscv.sh new file mode 100755 index 00000000000..93bebfa8a97 --- /dev/null +++ b/ci/test/link-riscv.sh @@ -0,0 +1,73 @@ +#!/usr/bin/env bash +# +# Copyright (c) The Bitcoin Core developers +# Distributed under the MIT software license, see the accompanying +# file COPYING or http://www.opensource.org/licenses/mit-license.php. + +export LC_ALL=C.UTF-8 +set -o errexit -o xtrace -o pipefail + +GCC=/opt/riscv-ilp32/bin/riscv32-unknown-elf-gcc +GXX=/opt/riscv-ilp32/bin/riscv32-unknown-elf-g++ +CRTBEGIN=$("${GCC}" -print-file-name=crtbegin.o) +LIBGCC=$("${GCC}" -print-libgcc-file-name) +LIBSTDCXX=$("${GXX}" -print-file-name=libstdc++.a) +LIBC=$("${GCC}" -print-file-name=libc.a) +LIBM=$("${GCC}" -print-file-name=libm.a) + +echo -e "#include