Skip to content

Commit 31db0b8

Browse files
joyeecheungruyadorno
authored andcommitted
build: add --write-snapshot-as-array-literals to configure.py
This makes it easier to locate indeterminism in the snapshot, with the following command: $ ./configure --write-snapshot-as-array-literals $ make V= $ mv out/Release/obj/gen/node_snapshot.cc ./node_snapshot.cc $ make V= $ diff out/Release/obj/gen/node_snapshot.cc ./node_snapshot.cc PR-URL: #49312 Refs: nodejs/build#3043 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
1 parent 8480280 commit 31db0b8

File tree

5 files changed

+28
-14
lines changed

5 files changed

+28
-14
lines changed

configure.py

+13
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,14 @@
609609
default=None,
610610
help='Use Link Time Code Generation. This feature is only available on Windows.')
611611

612+
parser.add_argument('--write-snapshot-as-array-literals',
613+
action='store_true',
614+
dest='write_snapshot_as_array_literals',
615+
default=None,
616+
help='Write the snapshot data as array literals for readability.'
617+
'By default the snapshot data may be written as string literals on some '
618+
'platforms to speed up compilation.')
619+
612620
parser.add_argument('--without-node-snapshot',
613621
action='store_true',
614622
dest='without_node_snapshot',
@@ -1290,6 +1298,11 @@ def configure_node(o):
12901298
o['variables']['node_use_node_code_cache'] = b(
12911299
not cross_compiling and not options.shared)
12921300

1301+
if options.write_snapshot_as_array_literals is not None:
1302+
o['variables']['node_write_snapshot_as_array_literals'] = b(options.write_snapshot_as_array_literals)
1303+
else:
1304+
o['variables']['node_write_snapshot_as_array_literals'] = b(flavor != 'mac' and flavor != 'linux')
1305+
12931306
if target_arch == 'arm':
12941307
configure_arm(o)
12951308
elif target_arch in ('mips', 'mipsel', 'mips64el'):

node.gyp

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
'node_use_v8_platform%': 'true',
1111
'node_use_bundled_v8%': 'true',
1212
'node_shared%': 'false',
13+
'node_write_snapshot_as_string_literals': 'true',
1314
'force_dynamic_crt%': 0,
1415
'ossfuzz' : 'false',
1516
'node_module_version%': '',
@@ -1247,8 +1248,8 @@
12471248
],
12481249

12491250
'conditions': [
1250-
['OS in "linux mac"', {
1251-
'defines': [ 'NODE_MKSNAPSHOT_USE_STRING_LITERALS=1' ],
1251+
['node_write_snapshot_as_array_literals=="true"', {
1252+
'defines': [ 'NODE_MKSNAPSHOT_USE_ARRAY_LITERALS=1' ],
12521253
}],
12531254
[ 'node_use_openssl=="true"', {
12541255
'defines': [

src/node_snapshot_builder.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class NODE_EXTERN_PRIVATE SnapshotBuilder {
2323
const std::vector<std::string>& args,
2424
const std::vector<std::string>& exec_args,
2525
std::optional<std::string_view> main_script_path = std::nullopt,
26-
bool use_string_literals = true);
26+
bool use_array_literals = false);
2727

2828
// Generate the snapshot into out.
2929
static ExitCode Generate(SnapshotData* out,

src/node_snapshotable.cc

+7-7
Original file line numberDiff line numberDiff line change
@@ -773,11 +773,11 @@ void WriteByteVectorLiteral(std::ostream* ss,
773773
const T* vec,
774774
size_t size,
775775
const char* var_name,
776-
bool use_string_literals) {
776+
bool use_array_literals) {
777777
constexpr bool is_uint8_t = std::is_same_v<T, uint8_t>;
778778
static_assert(is_uint8_t || std::is_same_v<T, char>);
779779
constexpr const char* type_name = is_uint8_t ? "uint8_t" : "char";
780-
if (use_string_literals) {
780+
if (!use_array_literals) {
781781
const uint8_t* data = reinterpret_cast<const uint8_t*>(vec);
782782
*ss << "static const " << type_name << " *" << var_name << " = ";
783783
*ss << (is_uint8_t ? R"(reinterpret_cast<const uint8_t *>(")" : "\"");
@@ -818,7 +818,7 @@ static void WriteCodeCacheInitializer(std::ostream* ss,
818818

819819
void FormatBlob(std::ostream& ss,
820820
const SnapshotData* data,
821-
bool use_string_literals) {
821+
bool use_array_literals) {
822822
ss << R"(#include <cstddef>
823823
#include "env.h"
824824
#include "node_snapshot_builder.h"
@@ -833,7 +833,7 @@ namespace node {
833833
data->v8_snapshot_blob_data.data,
834834
data->v8_snapshot_blob_data.raw_size,
835835
"v8_snapshot_blob_data",
836-
use_string_literals);
836+
use_array_literals);
837837

838838
ss << R"(static const int v8_snapshot_blob_size = )"
839839
<< data->v8_snapshot_blob_data.raw_size << ";\n";
@@ -846,7 +846,7 @@ namespace node {
846846
item.data.data,
847847
item.data.length,
848848
var_name.c_str(),
849-
use_string_literals);
849+
use_array_literals);
850850
}
851851

852852
ss << R"(const SnapshotData snapshot_data {
@@ -1132,7 +1132,7 @@ ExitCode SnapshotBuilder::GenerateAsSource(
11321132
const std::vector<std::string>& args,
11331133
const std::vector<std::string>& exec_args,
11341134
std::optional<std::string_view> main_script_path,
1135-
bool use_string_literals) {
1135+
bool use_array_literals) {
11361136
std::string main_script_content;
11371137
std::optional<std::string_view> main_script_optional;
11381138
if (main_script_path.has_value()) {
@@ -1159,7 +1159,7 @@ ExitCode SnapshotBuilder::GenerateAsSource(
11591159
if (exit_code != ExitCode::kNoFailure) {
11601160
return exit_code;
11611161
}
1162-
FormatBlob(out, &data, use_string_literals);
1162+
FormatBlob(out, &data, use_array_literals);
11631163

11641164
if (!out) {
11651165
std::cerr << "Failed to write to " << out_path << "\n";

tools/snapshot/node_mksnapshot.cc

+4-4
Original file line numberDiff line numberDiff line change
@@ -79,18 +79,18 @@ int BuildSnapshot(int argc, char* argv[]) {
7979
out_path = result->args()[1];
8080
}
8181

82-
#ifdef NODE_MKSNAPSHOT_USE_STRING_LITERALS
83-
bool use_string_literals = true;
82+
#ifdef NODE_MKSNAPSHOT_USE_ARRAY_LITERALS
83+
bool use_array_literals = true;
8484
#else
85-
bool use_string_literals = false;
85+
bool use_array_literals = false;
8686
#endif
8787

8888
node::ExitCode exit_code =
8989
node::SnapshotBuilder::GenerateAsSource(out_path.c_str(),
9090
result->args(),
9191
result->exec_args(),
9292
main_script_path,
93-
use_string_literals);
93+
use_array_literals);
9494

9595
node::TearDownOncePerProcess();
9696
return static_cast<int>(exit_code);

0 commit comments

Comments
 (0)