Skip to content

Commit d68c52d

Browse files
l0rincfjahr
andcommitted
cmake: compact generated data headers
Change the raw and json data generators to emit concatenated byte string literals instead of per-element initializer lists. Add MakeByteArray in span.h so raw headers stay compact while still exposing the same compile-time byte spans. The hash checks added in the previous commit keep the embedded fixture contents pinned while this commit only changes their C++ representation. Co-authored-by: Fabian Jahr <fjahr@protonmail.com>
1 parent cc56620 commit d68c52d

File tree

3 files changed

+29
-12
lines changed

3 files changed

+29
-12
lines changed

cmake/script/GenerateHeaderFromJson.cmake

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,23 @@
55
cmake_path(GET JSON_SOURCE_PATH STEM json_source_basename)
66

77
file(READ ${JSON_SOURCE_PATH} hex_content HEX)
8-
string(REGEX REPLACE "................" "\\0\n" formatted_bytes "${hex_content}")
9-
string(REGEX REPLACE "[^\n][^\n]" "'\\\\x\\0'," formatted_bytes "${formatted_bytes}")
8+
string(REGEX REPLACE "................................................................" "\\0\n" formatted_bytes "${hex_content}")
9+
string(REGEX REPLACE "[^\n][^\n]" "\\\\x\\0" formatted_bytes "${formatted_bytes}")
10+
if(formatted_bytes STREQUAL "")
11+
set(formatted_bytes "\"\"")
12+
else()
13+
string(REGEX REPLACE "([^\n]+)" "\"\\1\"" formatted_bytes "${formatted_bytes}")
14+
endif()
1015

1116
set(header_content
1217
"#include <string_view>
1318
1419
namespace json_tests {
15-
inline constexpr char detail_${json_source_basename}_bytes[] {
20+
inline constexpr char detail_${json_source_basename}_bytes[] =
1621
${formatted_bytes}
17-
};
22+
;
1823
19-
inline constexpr std::string_view ${json_source_basename}{std::begin(detail_${json_source_basename}_bytes), std::end(detail_${json_source_basename}_bytes)};
24+
inline constexpr std::string_view ${json_source_basename}{detail_${json_source_basename}_bytes, sizeof(detail_${json_source_basename}_bytes) - 1};
2025
}
2126
")
2227
file(WRITE ${HEADER_PATH} "${header_content}")

cmake/script/GenerateHeaderFromRaw.cmake

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,22 @@
55
cmake_path(GET RAW_SOURCE_PATH STEM raw_source_basename)
66

77
file(READ ${RAW_SOURCE_PATH} hex_content HEX)
8-
string(REGEX REPLACE "................" "\\0\n" formatted_bytes "${hex_content}")
9-
string(REGEX REPLACE "[^\n][^\n]" "std::byte{0x\\0}," formatted_bytes "${formatted_bytes}")
8+
string(REGEX REPLACE "................................................................" "\\0\n" formatted_bytes "${hex_content}")
9+
string(REGEX REPLACE "[^\n][^\n]" "\\\\x\\0" formatted_bytes "${formatted_bytes}")
10+
if(formatted_bytes STREQUAL "")
11+
set(formatted_bytes "\"\"")
12+
else()
13+
string(REGEX REPLACE "([^\n]+)" "\"\\1\"" formatted_bytes "${formatted_bytes}")
14+
endif()
1015

1116
set(header_content
12-
"#include <cstddef>
13-
#include <span>
17+
"#include <span.h>
1418
1519
namespace ${RAW_NAMESPACE} {
16-
inline constexpr std::byte detail_${raw_source_basename}_raw[] {
20+
inline constexpr auto detail_${raw_source_basename}_raw{MakeByteArray(
1721
${formatted_bytes}
18-
};
22+
)};
1923
20-
inline constexpr std::span ${raw_source_basename}{detail_${raw_source_basename}_raw};
24+
inline constexpr auto ${raw_source_basename}{std::span{detail_${raw_source_basename}_raw}.first<detail_${raw_source_basename}_raw.size() - 1>()};
2125
}")
2226
file(WRITE ${HEADER_PATH} "${header_content}")

src/span.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#ifndef BITCOIN_SPAN_H
66
#define BITCOIN_SPAN_H
77

8+
#include <array>
9+
#include <bit>
810
#include <cassert>
911
#include <cstddef>
1012
#include <span>
@@ -80,6 +82,12 @@ T& SpanPopBack(std::span<T>& span)
8082
return back;
8183
}
8284

85+
template <size_t N>
86+
consteval auto MakeByteArray(const char (&bytes)[N])
87+
{
88+
return std::bit_cast<std::array<std::byte, N>>(std::to_array(bytes));
89+
}
90+
8391
template <typename V>
8492
auto MakeByteSpan(const V& v) noexcept
8593
{

0 commit comments

Comments
 (0)