Skip to content
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

Enforce levelization in libxrpl with CMake #5111

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from

Conversation

thejohnfreeman
Copy link
Collaborator

@thejohnfreeman thejohnfreeman commented Aug 28, 2024

Adds two CMake functions:

  • add_module(library subdirectory): Declares an OBJECT "library" (a CMake abstraction for a collection of object files) with sources from the given subdirectory of the given library, representing a module. Isolates the module's headers by creating a subdirectory in the build directory, e.g. .build/tmp123, that contains just a symlink, e.g. .build/tmp123/basics, to the module's header directory, e.g. include/xrpl/basics, in the source directory, and putting .build/tmp123 (but not include/xrpl) on the include path of the module sources. This prevents the module sources from including headers not explicitly linked to the module in CMake with target_link_libraries.
  • target_link_modules(library scope modules...): Links the library target to each of the module targets, and removes their sources from its source list (so they are not compiled and linked twice).

Uses these functions to separate and explicitly link modules in libxrpl:

  • Level 01: beast
  • Level 02: basics
  • Level 03: json, crypto
  • Level 04: protocol
  • Level 05: resource, server

Define each "module" of libxrpl as a separate OBJECT library in CMake.
Link each one only to modules of a lower level.
Then link all of them into libxrpl itself.
Copy link

codecov bot commented Aug 28, 2024

Codecov Report

Attention: Patch coverage is 71.42857% with 2 lines in your changes missing coverage. Please review.

Project coverage is 75.4%. Comparing base (b6391fe) to head (4e11039).
Report is 2 commits behind head on develop.

Files with missing lines Patch % Lines
include/xrpl/basics/SHAMapHash.h 0.0% 2 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff            @@
##           develop   #5111     +/-   ##
=========================================
- Coverage     76.2%   75.4%   -0.8%     
=========================================
  Files          760     666     -94     
  Lines        61550   58420   -3130     
  Branches      8123    7982    -141     
=========================================
- Hits         46884   44061   -2823     
+ Misses       14666   14359    -307     
Files with missing lines Coverage Δ
include/xrpl/basics/partitioned_unordered_map.h 99.1% <100.0%> (+<0.1%) ⬆️
include/xrpl/protocol/AmountConversions.h 87.5% <ø> (ø)
include/xrpl/protocol/FeeUnits.h 90.1% <ø> (ø)
include/xrpl/protocol/Fees.h 100.0% <ø> (ø)
include/xrpl/protocol/LedgerHeader.h 100.0% <ø> (ø)
include/xrpl/protocol/PayChan.h 100.0% <ø> (ø)
src/libxrpl/basics/Number.cpp 99.7% <ø> (-<0.1%) ⬇️
src/xrpld/app/misc/LoadFeeTrack.h 87.8% <ø> (ø)
src/xrpld/app/misc/detail/LoadFeeTrack.cpp 89.5% <ø> (ø)
src/xrpld/app/paths/Flow.cpp 96.2% <ø> (ø)
... and 19 more

... and 99 files with indirect coverage changes

Impacted file tree graph

@ximinez
Copy link
Collaborator

ximinez commented Aug 29, 2024

Not a review yet, but this is such a cool idea.

# isolate_headers(target A B scope)
function(isolate_headers target A B scope)
file(RELATIVE_PATH C "${A}" "${B}")
set(X "${CMAKE_CURRENT_BINARY_DIR}/include/${target}")
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think /include/ is misleading here. It's source file isolation feature, not includes.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I suggest

-  set(X "${CMAKE_CURRENT_BINARY_DIR}/include/${target}")
+  set(X "${CMAKE_CURRENT_BINARY_DIR}/modules/${target}")

Comment on lines +31 to +36
isolate_headers(
${target}
"${CMAKE_CURRENT_SOURCE_DIR}/src"
"${CMAKE_CURRENT_SOURCE_DIR}/src/lib${parent}/${name}"
PRIVATE
)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this is not needed ? Or my misunderstanding of how CMake works just showed :-D

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's in case a module has private headers, not installed, but included by other translation units. I don't know if any of our modules have these (yet), but I'm copying this implementation from my more generalized project.

Comment on lines +125 to +126
PRIVATE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this is probably not needed ?

Copy link
Collaborator

Choose a reason for hiding this comment

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

actually, upon some testing, this whole target_include_directories seem to be not needed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's not needed now that all of the subdirectories in libxrpl are modules. It was necessary as I migrated them one-by-one, and it could be necessary in the future if someone adds a non-module subdirectory. Could be nice if it "just works" without tampering with the CMake. What do you think?

@Bronek
Copy link
Collaborator

Bronek commented Sep 20, 2024

I like how you moved extract to appropriate locations, but it seems that extract is function-wise the same as hash.

Given we already specialize both boost::hash (e.g. in IPEndpoint.h) and std::hash (e.g. in Book.h) I think that cleaning this up belongs in a different PR (also converting extract to hash), so maybe just take a note for future refactoring.

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.

3 participants