Skip to content

Commit 7f1d9af

Browse files
authored
capi: split by component (#2844)
1 parent c6d70e6 commit 7f1d9af

33 files changed

+873
-969
lines changed

cmake/compiler_settings.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,5 @@ else()
123123
add_link_options(-fsanitize=safe-stack)
124124
endif()
125125
endif()
126+
127+
add_compile_definitions(SILKWORM_CAPI_COMPONENT)

silkworm/capi/CMakeLists.txt

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
include("${SILKWORM_MAIN_DIR}/cmake/common/targets.cmake")
55

6+
add_subdirectory(common)
7+
68
set(TARGET silkworm_capi)
79

810
find_package(Microsoft.GSL REQUIRED)
@@ -11,10 +13,11 @@ set(PUBLIC_LIBS "")
1113
set(PRIVATE_LIBS
1214
glaze::glaze
1315
Microsoft.GSL::GSL
16+
silkworm_capi_common
1417
silkworm_core
1518
silkworm_db
19+
silkworm_execution
1620
silkworm_sentry
17-
silkworm_node
1821
silkworm_rpcdaemon
1922
)
2023

@@ -47,4 +50,14 @@ set_target_properties(${TARGET} PROPERTIES LINK_OPTIONS "${LINK_OPTIONS}")
4750

4851
add_subdirectory(cli)
4952

50-
target_link_libraries(silkworm_capi_static_test PRIVATE silkworm_db_test_util silkworm_node)
53+
target_link_libraries(silkworm_capi_static_test PRIVATE silkworm_db_test_util)
54+
55+
# collect all public C headers into the same directory
56+
set(HEADERS_INSTALL_PATH "${CMAKE_CURRENT_BINARY_DIR}/include")
57+
file(GLOB_RECURSE HEADERS "../capi/*.h" "../*/capi/*.h")
58+
add_custom_command(
59+
TARGET ${TARGET}
60+
POST_BUILD
61+
COMMAND ${CMAKE_COMMAND} ARGS -E make_directory "${HEADERS_INSTALL_PATH}"
62+
COMMAND ${CMAKE_COMMAND} ARGS -E copy_if_different ${HEADERS} "${HEADERS_INSTALL_PATH}"
63+
)

silkworm/capi/cli/sample-go-client/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package main
33
// #cgo LDFLAGS: -lsilkworm_capi
44
// #cgo LDFLAGS: -L${SRCDIR}/../../../../build/silkworm/capi
55
// #cgo LDFLAGS: -Wl,-rpath ${SRCDIR}/../../../../build/silkworm/capi
6-
// #cgo CFLAGS: -I${SRCDIR}/../../../../silkworm/capi
6+
// #cgo CFLAGS: -I${SRCDIR}/../../../../build/silkworm/capi/include
77
/*
88
#include "silkworm.h"
99
#include <stdlib.h>

silkworm/capi/common/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Copyright 2025 The Silkworm Authors
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
include("${SILKWORM_MAIN_DIR}/cmake/common/targets.cmake")
5+
6+
silkworm_library(silkworm_capi_common)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2025 The Silkworm Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
#pragma once
5+
6+
#include <filesystem>
7+
8+
#include <silkworm/infra/common/log.hpp>
9+
#include <silkworm/infra/concurrency/context_pool_settings.hpp>
10+
11+
namespace silkworm::capi {
12+
13+
struct CommonComponent {
14+
silkworm::log::Settings log_settings;
15+
silkworm::concurrency::ContextPoolSettings context_pool_settings;
16+
std::filesystem::path data_dir_path;
17+
};
18+
19+
} // namespace silkworm::capi

silkworm/capi/common/errors.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2025 The Silkworm Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
#ifndef SILKWORM_CAPI_ERRORS_H_
5+
#define SILKWORM_CAPI_ERRORS_H_
6+
7+
// Silkworm library error codes (SILKWORM_OK indicates no error, i.e. success)
8+
9+
#define SILKWORM_OK 0
10+
#define SILKWORM_INTERNAL_ERROR 1
11+
#define SILKWORM_UNKNOWN_ERROR 2
12+
#define SILKWORM_INVALID_HANDLE 3
13+
#define SILKWORM_INVALID_PATH 4
14+
#define SILKWORM_INVALID_SNAPSHOT 5
15+
#define SILKWORM_INVALID_MDBX_ENV 6
16+
#define SILKWORM_INVALID_BLOCK_RANGE 7
17+
#define SILKWORM_BLOCK_NOT_FOUND 8
18+
#define SILKWORM_UNKNOWN_CHAIN_ID 9
19+
#define SILKWORM_MDBX_ERROR 10
20+
#define SILKWORM_INVALID_BLOCK 11
21+
#define SILKWORM_DECODING_ERROR 12
22+
#define SILKWORM_TOO_MANY_INSTANCES 13
23+
#define SILKWORM_INVALID_SETTINGS 14
24+
#define SILKWORM_TERMINATION_SIGNAL 15
25+
#define SILKWORM_SERVICE_ALREADY_STARTED 16
26+
#define SILKWORM_INCOMPATIBLE_LIBMDBX 17
27+
#define SILKWORM_INVALID_MDBX_TXN 18
28+
29+
#endif // SILKWORM_CAPI_ERRORS_H_

silkworm/capi/common/instance.hpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2025 The Silkworm Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
#pragma once
5+
6+
#include <memory>
7+
8+
#include "common_component.hpp"
9+
10+
namespace silkworm::db::capi {
11+
struct Component;
12+
}
13+
14+
namespace silkworm::rpc {
15+
class Daemon;
16+
}
17+
18+
namespace silkworm::sentry::capi {
19+
struct Component;
20+
}
21+
22+
namespace capi_todo {
23+
24+
struct SilkwormInstance {
25+
silkworm::capi::CommonComponent common;
26+
std::unique_ptr<silkworm::db::capi::Component> db;
27+
std::unique_ptr<silkworm::rpc::Daemon> rpcdaemon;
28+
std::unique_ptr<silkworm::sentry::capi::Component> sentry;
29+
30+
SilkwormInstance();
31+
~SilkwormInstance();
32+
};
33+
34+
} // namespace capi_todo

silkworm/capi/common/parse_path.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2025 The Silkworm Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
#include "parse_path.hpp"
5+
6+
#include <cstring>
7+
8+
namespace silkworm::capi {
9+
10+
std::filesystem::path parse_path(const char data_dir_path[SILKWORM_PATH_SIZE]) {
11+
// Treat as char8_t so that filesystem::path assumes UTF-8 encoding of the input path
12+
auto begin = reinterpret_cast<const char8_t*>(data_dir_path);
13+
size_t len = strnlen(data_dir_path, SILKWORM_PATH_SIZE);
14+
return std::filesystem::path{begin, begin + len};
15+
}
16+
17+
} // namespace silkworm::capi

silkworm/capi/common/parse_path.hpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2025 The Silkworm Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
#pragma once
5+
6+
#include <filesystem>
7+
8+
#include "preamble.h"
9+
10+
namespace silkworm::capi {
11+
12+
//! Build a file system path from its C null-terminated upper-bounded representation
13+
std::filesystem::path parse_path(const char path[SILKWORM_PATH_SIZE]);
14+
15+
} // namespace silkworm::capi

silkworm/capi/common/preamble.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2025 The Silkworm Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
#ifndef SILKWORM_CAPI_PREAMBLE_H_
5+
#define SILKWORM_CAPI_PREAMBLE_H_
6+
7+
#include <stdbool.h> // NOLINT(*-deprecated-headers)
8+
#include <stddef.h> // NOLINT(*-deprecated-headers)
9+
#include <stdint.h> // NOLINT(*-deprecated-headers)
10+
11+
#if defined _MSC_VER
12+
#define SILKWORM_EXPORT __declspec(dllexport)
13+
#else
14+
#define SILKWORM_EXPORT __attribute__((visibility("default")))
15+
#endif
16+
17+
#if __cplusplus
18+
#define SILKWORM_NOEXCEPT noexcept
19+
#else
20+
#define SILKWORM_NOEXCEPT
21+
#endif
22+
23+
#if __cplusplus
24+
extern "C" {
25+
#endif
26+
struct SilkwormInstance;
27+
typedef struct SilkwormInstance* SilkwormHandle;
28+
#if __cplusplus
29+
}
30+
#endif
31+
32+
#include "errors.h"
33+
34+
#define SILKWORM_PATH_SIZE 260
35+
36+
#endif // SILKWORM_CAPI_PREAMBLE_H_

0 commit comments

Comments
 (0)