Skip to content

build: Introduce CMake-based build system #75

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 3 commits into from
Jul 1, 2025

Conversation

hebasto
Copy link
Member

@hebasto hebasto commented Oct 20, 2022

This PR introduces a new CMake-based build system, which can be reused in downstream projects, including Bitcoin Core.

Additionally, a GitHub Actions CI task for MSVC and clang-cl has been added, following this feedback.

Autotools vs CMake configuration option parity

Autotools CMake
--disable-ccache N/A
--enable-tests -DMINISKETCH_BUILD_TESTS
--enable-benchmark -DMINISKETCH_BUILD_BENCHMARK
--enable-fields=,-separated list -DMINISKETCH_FIELDS=;-separated list
-DMINISKETCH_INSTALL

Notes on the implementation

  1. The default CMAKE_BUILD_TYPE is left unset.

  2. CMake's per-configuration compiler flags are not modified. As a result, they may include -DNDEBUG.

  3. The target name test is reserved. Therefore, the testing binary has been renamed to test-noverify. Alternatively, following the naming used in the libsecp256k1 repo, the test binaries could be renamed as:

    • test --> noverify_tests
    • test-verify --> verify_tests

@hebasto
Copy link
Member Author

hebasto commented Oct 20, 2022

Chasing for Concept (N)ACKs...

@hebasto hebasto marked this pull request as ready for review October 23, 2022 08:02
@hebasto
Copy link
Member Author

hebasto commented Oct 23, 2022

The currently suggesting minimal CMake-based build system implementation is enough for:

add_subdirectory(minisketch EXCLUDE_FROM_ALL)
target_compile_definitions(minisketch
  PRIVATE
    DISABLE_DEFAULT_FIELDS
    ENABLE_FIELD_32
)

To became a full fledged one, the current minimal CMake-based build system requires some additional features:

  • more robustness (e.g., checking compiler/linker flags before applying them)
  • ability to install the built artifacts
  • ability to export the built artifacts and, probably, packaging of them
  • documentation

Although, those features are not required for this PR goal, i.e., providing a native Windows CI task.

@hebasto
Copy link
Member Author

hebasto commented Oct 31, 2022

Updated 5851544 -> d2408e0 (pr75.03 -> pr75.04, diff):

  • policy CMP0063 set explicitly to avoid CMake warnings
  • the default CMake build directory added to the .gitignore file

@theuni
Copy link
Contributor

theuni commented Mar 17, 2023

Imo this should be renamed "Add CMake buildsystem". I looked for this a while back and didn't find it because CMake isn't mentioned in the title.

Concept ACK. Will review next week.

@theuni
Copy link
Contributor

theuni commented Mar 31, 2023

@sipa Are you interested in CMake for this project?

@sipa
Copy link
Collaborator

sipa commented Mar 31, 2023

@theuni Yeah, will look soon.

@theuni
Copy link
Contributor

theuni commented Mar 31, 2023

@sipa No rush. I was just looking for a concept ACK.

I worked up a CMake impl as well before seeing this PR. I think we'd benefit from parts of each, so I'll get with @hebasto on creating a combined version for review.

@hebasto hebasto changed the title ci: Add "x86_64: Windows (VS 2022)" task build: Add CMake buildsystem Apr 16, 2023
@hebasto
Copy link
Member Author

hebasto commented Apr 16, 2023

Imo this should be renamed "Add CMake buildsystem".

Done.

@sipa
Copy link
Collaborator

sipa commented Apr 11, 2024

I'm happy to add a CMake build system for minisketch. I will need review from people more experienced with build systems, though.

@hebasto
Copy link
Member Author

hebasto commented Apr 11, 2024

I'm happy to add a CMake build system for minisketch. I will need review from people more experienced with build systems, though.

I'm going to update this PR shortly.

@hebasto hebasto force-pushed the 221019-ci-msvc branch 2 times, most recently from 66b4133 to b486457 Compare April 16, 2024 16:44
@hebasto
Copy link
Member Author

hebasto commented Apr 16, 2024

Reworked.

The PR description has been updated.

The approach from hebasto/bitcoin#93 was used for printing summary.

endif()

if(MINGW)
add_link_options(-static)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should port this functionality. Instead we should use the CMake shared/static selection machinery.

Copy link
Member Author

@hebasto hebasto Apr 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This affects only benchmark/test binaries and allows to run them on the build system via Wine when building either a static library or a shared one.

UPD. It also links libstdc++-6 to libminisketch.dll statically when cross-compiling for Windows and -DBUILD_SHARED_LIBS=YES is provided.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider

cmake -B build \
  -DCMAKE_BUILD_TYPE=Release \
  -DCMAKE_SYSTEM_NAME=Windows \
  -DCMAKE_CXX_COMPILER=x86_64-w64-mingw32-g++-posix
cmake --build build --target test
wine ./build/src/test.exe

or

cmake -B build \
  -DCMAKE_BUILD_TYPE=Release \
  -DCMAKE_SYSTEM_NAME=Windows \
  -DCMAKE_CXX_COMPILER=x86_64-w64-mingw32-g++-posix \
  -DBUILD_SHARED_LIBS=YES
cmake --build build --target test
wine ./build/src/test.exe

)

if(DEFINED CMAKE_CXX_STANDARD)
if(CMAKE_CXX_STANDARD EQUAL 98 OR CMAKE_CXX_STANDARD LESS 11)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hah! Stupid CMake :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Y2K problem :)

@hebasto
Copy link
Member Author

hebasto commented Apr 26, 2024

Addressed @theuni's comments.

@hebasto hebasto marked this pull request as draft March 5, 2025 14:14
@sipa
Copy link
Collaborator

sipa commented May 13, 2025

What is the status here?

@hebasto
Copy link
Member Author

hebasto commented May 13, 2025

What is the status here?

I'll push an update shortly.

@hebasto hebasto marked this pull request as ready for review June 1, 2025 01:09
Comment on lines 39 to 42
# TODO: Resolve the issue and re-enable benchmark.
skip_benchmark: true
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue has been fixed in #96.


add_subdirectory(fields)

add_compile_definitions($<$<BOOL:${HAVE_CLZ}>:HAVE_CLZ>)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of attaching CLMUL to the fields interface, how about handling it here?

set_source_files_properties(minisketch.cpp PROPERTIES COMPILE_DEFINITIONS $<$<BOOL:${HAVE_CLMUL}>:HAVE_CLMUL>)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea was to keep all HAVE_CLMUL-related code local.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, it seems a good bit more straightforward to me to drop the fields/ subdir and just include that logic here:

diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index d8e4fde..cae8228 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -1,13 +1,42 @@
 include(GNUInstallDirs)

-add_subdirectory(fields)
-
 add_compile_definitions($<$<BOOL:${HAVE_CLZ}>:HAVE_CLZ>)

 add_library(minisketch minisketch.cpp)
 target_include_directories(minisketch INTERFACE
   $<BUILD_INTERFACE:$<$<NOT:$<BOOL:${PROJECT_IS_TOP_LEVEL}>>:${PROJECT_SOURCE_DIR}/include>>
 )
+
+add_library(minisketch_field_sources INTERFACE)
+target_sources(minisketch_field_sources
+  INTERFACE
+    fields/generic_1byte.cpp
+    fields/generic_2bytes.cpp
+    fields/generic_3bytes.cpp
+    fields/generic_4bytes.cpp
+    fields/generic_5bytes.cpp
+    fields/generic_6bytes.cpp
+    fields/generic_7bytes.cpp
+    fields/generic_8bytes.cpp
+)
+
+set(clmul_sources
+  fields/clmul_1byte.cpp
+  fields/clmul_2bytes.cpp
+  fields/clmul_3bytes.cpp
+  fields/clmul_4bytes.cpp
+  fields/clmul_5bytes.cpp
+  fields/clmul_6bytes.cpp
+  fields/clmul_7bytes.cpp
+  fields/clmul_8bytes.cpp
+)
+
+if(HAVE_CLMUL)
+  set_source_files_properties(minisketch.cpp PROPERTIES COMPILE_DEFINITIONS HAVE_CLMUL)
+  set_source_files_properties(${clmul_sources} PROPERTIES COMPILE_OPTIONS ${CLMUL_CXXFLAGS})
+  target_sources(minisketch_field_sources INTERFACE ${clmul_sources})
+endif()
+
 target_link_libraries(minisketch PRIVATE minisketch_field_sources)

 add_library(minisketch_verify EXCLUDE_FROM_ALL minisketch.cpp)

Copy link
Member Author

@hebasto hebasto Jun 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Your suggestion has been taken with reordering to keep code specific to each target as local as possible.

Copy link
Member Author

@hebasto hebasto Jun 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replaced set_source_files_properties back with set_property(SOURCE ...) as the latter accepts empty property value, which is the case for CLMUL_CXXFLAGS on Windows.

Otherwise, see https://github.com/hebasto/minisketch/actions/runs/15404712059.

@hebasto hebasto force-pushed the 221019-ci-msvc branch 3 times, most recently from 3170353 to 77bfe3b Compare June 5, 2025 13:50
@hebasto
Copy link
Member Author

hebasto commented Jun 18, 2025

The PR has been updated to improve integration with downstream projects and now makes use of PUBLIC_HEADER (see bitcoin-core/secp256k1#1679).

The corresponding Bitcoin Core branch that integrates the new build system is available here.

Please note that the current approach does not involve using a project-specific variable to override CMake's BUILD_SHARED_LIBS (this is done via SECP256K1_DISABLE_SHARED in the libsecp256k1 project):

function(add_minisketch subdir)
  message("")
  message("Configuring minisketch subtree...")
  set(BUILD_SHARED_LIBS OFF)
  set(CMAKE_EXPORT_COMPILE_COMMANDS OFF)
  # Hard-code subtree's options.
  set(MINISKETCH_INSTALL OFF)
  set(MINISKETCH_BUILD_TESTS OFF)
  set(MINISKETCH_BUILD_BENCHMARK OFF)
  set(MINISKETCH_FIELDS 32)
  add_subdirectory(${subdir} EXCLUDE_FROM_ALL)
  target_link_libraries(minisketch
    PRIVATE
      core_interface
  )
endfunction()

In integration scenarios similar to bitcoin-core/secp256k1#1678, the $<TARGET_OBJECTS:minisketch> generator expression expands to all associated object files.

cc @theuni

Comment on lines +31 to +32
# Prevent include directories from parent project from leaking into this one.
set_property(DIRECTORY PROPERTY INCLUDE_DIRECTORIES "")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See similar code in the libmultiprocess project:

# Prevent include directories from parent project from leaking into this one.
set_property(DIRECTORY PROPERTY INCLUDE_DIRECTORIES "")

@hebasto
Copy link
Member Author

hebasto commented Jun 27, 2025

Pushed some improvements basing on @ryanofsky's bitcoin-core/libmultiprocess@6adbb1d and offline discussions with @purpleKarrot.

The corresponding Bitcoin Core branch that integrates the new build system is available here.

@hebasto hebasto force-pushed the 221019-ci-msvc branch 2 times, most recently from 85d6f32 to 0d6e8ac Compare June 27, 2025 19:24
@hebasto
Copy link
Member Author

hebasto commented Jun 27, 2025

The feedback from @purpleKarrot has been addressed.

The corresponding Bitcoin Core branch that integrates the new build system is available here.

@purpleKarrot
Copy link

@hebasto, I think you accidentally based your latest change on an older revision. A few changes got reverted.

#=============================
# Build Options
#=============================
set(CMAKE_CXX_VISIBILITY_PRESET hidden)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feature actually requires a bit more work to get right. It is currently not working correctly in the autotools build either, so it can be done in a separate PR.

@hebasto
Copy link
Member Author

hebasto commented Jun 27, 2025

Further feedback from @purpleKarrot has been addressed.

The corresponding Bitcoin Core branch that integrates the new build system is available here.

I think you accidentally based your latest change on an older revision. A few changes got reverted.

Should be OK now.

@purpleKarrot
Copy link

ACK 850cc86

Copy link
Contributor

@theuni theuni left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 850cc86.

Once integrated into Core, I propose that we nuke the old buildsystem. I doubt sipa has much interest in supporting both here.

@sipa
Copy link
Collaborator

sipa commented Jul 1, 2025

Weakly-tested ACK 850cc86

@sipa sipa merged commit f74b7e2 into bitcoin-core:master Jul 1, 2025
21 checks passed
@hebasto hebasto deleted the 221019-ci-msvc branch July 2, 2025 08:30
hebasto added a commit to hebasto/bitcoin that referenced this pull request Jul 2, 2025
f74b7e2bc2 Merge bitcoin-core/minisketch#75: build: Introduce CMake-based build system
850cc868d5 ci: Add GHA workflow with native Windows job
40d56708c8 doc: Add "Building with CMake" section to `README.md`
a0c86c79a7 build: Add CMake-based build system

git-subtree-dir: src/minisketch
git-subtree-split: f74b7e2bc2e5e1a1360927934521216bee5f345c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants