forked from XRPLF/rippled
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enforce levelization in libxrpl with CMake (XRPLF#5111)
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
- Loading branch information
1 parent
6d58065
commit ea1fffe
Showing
61 changed files
with
351 additions
and
203 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
include(isolate_headers) | ||
|
||
# Create an OBJECT library target named | ||
# | ||
# ${PROJECT_NAME}.lib${parent}.${name} | ||
# | ||
# with sources in src/lib${parent}/${name} | ||
# and headers in include/${parent}/${name} | ||
# that cannot include headers from other directories in include/ | ||
# unless they come through linked libraries. | ||
# | ||
# add_module(parent a) | ||
# add_module(parent b) | ||
# target_link_libraries(project.libparent.b PUBLIC project.libparent.a) | ||
function(add_module parent name) | ||
set(target ${PROJECT_NAME}.lib${parent}.${name}) | ||
add_library(${target} OBJECT) | ||
file(GLOB_RECURSE sources CONFIGURE_DEPENDS | ||
"${CMAKE_CURRENT_SOURCE_DIR}/src/lib${parent}/${name}/*.cpp" | ||
) | ||
target_sources(${target} PRIVATE ${sources}) | ||
target_include_directories(${target} PUBLIC | ||
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>" | ||
) | ||
isolate_headers( | ||
${target} | ||
"${CMAKE_CURRENT_SOURCE_DIR}/include" | ||
"${CMAKE_CURRENT_SOURCE_DIR}/include/${parent}/${name}" | ||
PUBLIC | ||
) | ||
isolate_headers( | ||
${target} | ||
"${CMAKE_CURRENT_SOURCE_DIR}/src" | ||
"${CMAKE_CURRENT_SOURCE_DIR}/src/lib${parent}/${name}" | ||
PRIVATE | ||
) | ||
endfunction() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# file(CREATE_SYMLINK) only works on Windows with administrator privileges. | ||
# https://stackoverflow.com/a/61244115/618906 | ||
function(create_symbolic_link target link) | ||
if(WIN32) | ||
if(NOT IS_SYMLINK "${link}") | ||
if(NOT IS_ABSOLUTE "${target}") | ||
# Relative links work do not work on Windows. | ||
set(target "${link}/../${target}") | ||
endif() | ||
file(TO_NATIVE_PATH "${target}" target) | ||
file(TO_NATIVE_PATH "${link}" link) | ||
execute_process(COMMAND cmd.exe /c mklink /J "${link}" "${target}") | ||
endif() | ||
else() | ||
file(CREATE_LINK "${target}" "${link}" SYMBOLIC) | ||
endif() | ||
if(NOT IS_SYMLINK "${link}") | ||
message(ERROR "failed to create symlink: <${link}>") | ||
endif() | ||
endfunction() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
include(create_symbolic_link) | ||
|
||
# Consider include directory B nested under prefix A: | ||
# | ||
# /path/to/A/then/to/B/... | ||
# | ||
# Call C the relative path from A to B. | ||
# C is what we want to write in `#include` directives: | ||
# | ||
# #include <then/to/B/...> | ||
# | ||
# Examples, all from the `jobqueue` module: | ||
# | ||
# - Library public headers: | ||
# B = /include/xrpl/jobqueue | ||
# A = /include/ | ||
# C = xrpl/jobqueue | ||
# | ||
# - Library private headers: | ||
# B = /src/libxrpl/jobqueue | ||
# A = /src/ | ||
# C = libxrpl/jobqueue | ||
# | ||
# - Test private headers: | ||
# B = /tests/jobqueue | ||
# A = / | ||
# C = tests/jobqueue | ||
# | ||
# To isolate headers from each other, | ||
# we want to create a symlink Y that points to B, | ||
# within a subdirectory X of the `CMAKE_BINARY_DIR`, | ||
# that has the same relative path C between X and Y, | ||
# and then add X as an include directory of the target, | ||
# sometimes `PUBLIC` and sometimes `PRIVATE`. | ||
# The Cs are all guaranteed to be unique. | ||
# We can guarantee a unique X per target by using | ||
# `${CMAKE_CURRENT_BINARY_DIR}/include/${target}`. | ||
# | ||
# 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}/modules/${target}") | ||
set(Y "${X}/${C}") | ||
cmake_path(GET Y PARENT_PATH parent) | ||
file(MAKE_DIRECTORY "${parent}") | ||
create_symbolic_link("${B}" "${Y}") | ||
target_include_directories(${target} ${scope} "$<BUILD_INTERFACE:${X}>") | ||
endfunction() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Link a library to its modules (see: `add_module`) | ||
# and remove the module sources from the library's sources. | ||
# | ||
# add_module(parent a) | ||
# add_module(parent b) | ||
# target_link_libraries(project.libparent.b PUBLIC project.libparent.a) | ||
# add_library(project.libparent) | ||
# target_link_modules(parent PUBLIC a b) | ||
function(target_link_modules parent scope) | ||
set(library ${PROJECT_NAME}.lib${parent}) | ||
foreach(name ${ARGN}) | ||
set(module ${library}.${name}) | ||
get_target_property(sources ${library} SOURCES) | ||
list(LENGTH sources before) | ||
get_target_property(dupes ${module} SOURCES) | ||
list(LENGTH dupes expected) | ||
list(REMOVE_ITEM sources ${dupes}) | ||
list(LENGTH sources after) | ||
math(EXPR actual "${before} - ${after}") | ||
message(STATUS "${module} with ${expected} sources took ${actual} sources from ${library}") | ||
set_target_properties(${library} PROPERTIES SOURCES "${sources}") | ||
target_link_libraries(${library} ${scope} ${module}) | ||
endforeach() | ||
endfunction() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.