Skip to content

Commit

Permalink
cmake: Build bitcoin_util static library
Browse files Browse the repository at this point in the history
  • Loading branch information
hebasto committed Feb 28, 2023
1 parent 318dd82 commit d028a49
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/module)
include(CMakeDependentOption)
option(ASM "Use assembly routines." ON)
cmake_dependent_option(CXX20 "Enable compilation in C++20 mode." OFF "NOT MSVC" ON)
option(THREADLOCAL "Enable features that depend on the C++ thread_local keyword (currently just thread names in debug logs)." ON)

if(CXX20)
set(CMAKE_CXX_STANDARD 20)
Expand Down Expand Up @@ -96,6 +97,9 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
add_compile_definitions(MAC_OSX)
endif()

include(AddThreadsIfNeeded)
add_threads_if_needed()

include(CheckSourceCompilesAndLinks)
include(cmake/introspection.cmake)

Expand Down
33 changes: 33 additions & 0 deletions cmake/module/AddThreadsIfNeeded.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Copyright (c) 2023 The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.

function(add_threads_if_needed)
# TODO: Not all targets, which will be added in the future,
# require Threads. Therefore, a proper check will be
# appropriate here.

set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)

set(thread_local)
if(MINGW)
#[=[
mingw32's implementation of thread_local has been shown to behave
erroneously under concurrent usage.
See:
- https://github.com/bitcoin/bitcoin/pull/15849
- https://gist.github.com/jamesob/fe9a872051a88b2025b1aa37bfa98605
]=]
elseif(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
#[=[
FreeBSD's implementation of thread_local is buggy.
See:
- https://github.com/bitcoin/bitcoin/pull/16059
- https://groups.google.com/d/msg/bsdmailinglist/22ncTZAbDp4/Dii_pII5AwAJ
]=]
elseif(THREADLOCAL)
set(thread_local "$<$<COMPILE_FEATURES:cxx_thread_local>:HAVE_THREAD_LOCAL>")
endif()
set(THREAD_LOCAL_IF_AVAILABLE "${thread_local}" PARENT_SCOPE)
endfunction()
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ include_directories(${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR})

add_subdirectory(crypto)
add_subdirectory(univalue)
add_subdirectory(util)
56 changes: 56 additions & 0 deletions src/util/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Copyright (c) 2023 The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.

add_library(bitcoin_util STATIC EXCLUDE_FROM_ALL
asmap.cpp
bip32.cpp
bytevectorhash.cpp
check.cpp
error.cpp
fees.cpp
getuniquepath.cpp
hasher.cpp
message.cpp
moneystr.cpp
rbf.cpp
readwritefile.cpp
settings.cpp
serfloat.cpp
sock.cpp
spanparsing.cpp
strencodings.cpp
string.cpp
syscall_sandbox.cpp
syserror.cpp
system.cpp
thread.cpp
threadinterrupt.cpp
threadnames.cpp
time.cpp
tokenpipe.cpp
../chainparamsbase.cpp
../clientversion.cpp
../fs.cpp
../logging.cpp
../random.cpp
../randomenv.cpp
../support/cleanse.cpp
../support/lockedpool.cpp
../sync.cpp
)

target_compile_definitions(bitcoin_util
PRIVATE
${THREAD_LOCAL_IF_AVAILABLE}
$<$<CXX_COMPILER_ID:MSVC>:_SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING>
)

target_link_libraries(bitcoin_util
PRIVATE
bitcoin_crypto
univalue
Threads::Threads
$<TARGET_NAME_IF_EXISTS:std_filesystem>
$<$<BOOL:${MINGW}>:ws2_32>
)

0 comments on commit d028a49

Please sign in to comment.