diff options
author | Joyee Cheung <joyeec9h3@gmail.com> | 2022-03-24 16:37:08 +0300 |
---|---|---|
committer | Joyee Cheung <joyeec9h3@gmail.com> | 2022-03-31 14:29:12 +0300 |
commit | 37aee80643822c8c9ff35c906d034ecc8fc448d5 (patch) | |
tree | 65a9107f2ed3b7e4cdaf38936a1df65ccd25a587 /tools | |
parent | 8a297ba3a0aa39afb7c84fceee4accff609b7cfe (diff) |
build: add --node-snapshot-main configure option
This adds a --build-snapshot runtime option which is currently only
supported by the node_mksnapshot binary, and a --node-snapshot-main
configure option that makes use it to run a custom script when
building the embedded snapshot. The idea is to have this experimental
feature in core as a configure-time feature for now, and investigate
the renaming V8 bugs before we make it available to more users via
making it a runtime option.
PR-URL: https://github.com/nodejs/node/pull/42466
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com>
Reviewed-By: Khaidi Chu <i@2333.moe>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Diffstat (limited to 'tools')
-rw-r--r-- | tools/snapshot/node_mksnapshot.cc | 74 |
1 files changed, 56 insertions, 18 deletions
diff --git a/tools/snapshot/node_mksnapshot.cc b/tools/snapshot/node_mksnapshot.cc index e591f64a2a0..60062854327 100644 --- a/tools/snapshot/node_mksnapshot.cc +++ b/tools/snapshot/node_mksnapshot.cc @@ -11,49 +11,87 @@ #include "util-inl.h" #include "v8.h" +int BuildSnapshot(int argc, char* argv[]); + #ifdef _WIN32 #include <windows.h> -int wmain(int argc, wchar_t* argv[]) { +int wmain(int argc, wchar_t* wargv[]) { + // Windows needs conversion from wchar_t to char. + + // Convert argv to UTF8. + char** argv = new char*[argc + 1]; + for (int i = 0; i < argc; i++) { + // Compute the size of the required buffer + DWORD size = WideCharToMultiByte( + CP_UTF8, 0, wargv[i], -1, nullptr, 0, nullptr, nullptr); + if (size == 0) { + // This should never happen. + fprintf(stderr, "Could not convert arguments to utf8."); + exit(1); + } + // Do the actual conversion + argv[i] = new char[size]; + DWORD result = WideCharToMultiByte( + CP_UTF8, 0, wargv[i], -1, argv[i], size, nullptr, nullptr); + if (result == 0) { + // This should never happen. + fprintf(stderr, "Could not convert arguments to utf8."); + exit(1); + } + } + argv[argc] = nullptr; #else // UNIX int main(int argc, char* argv[]) { argv = uv_setup_args(argc, argv); + + // Disable stdio buffering, it interacts poorly with printf() + // calls elsewhere in the program (e.g., any logging from V8.) + setvbuf(stdout, nullptr, _IONBF, 0); + setvbuf(stderr, nullptr, _IONBF, 0); #endif // _WIN32 v8::V8::SetFlagsFromString("--random_seed=42"); + v8::V8::SetFlagsFromString("--harmony-import-assertions"); + return BuildSnapshot(argc, argv); +} +int BuildSnapshot(int argc, char* argv[]) { if (argc < 2) { std::cerr << "Usage: " << argv[0] << " <path/to/output.cc>\n"; + std::cerr << " " << argv[0] << " --build-snapshot " + << "<path/to/script.js> <path/to/output.cc>\n"; return 1; } - std::ofstream out; - out.open(argv[1], std::ios::out | std::ios::binary); - if (!out.is_open()) { - std::cerr << "Cannot open " << argv[1] << "\n"; - return 1; - } - -// Windows needs conversion from wchar_t to char. See node_main.cc -#ifdef _WIN32 - int node_argc = 1; - char argv0[] = "node"; - char* node_argv[] = {argv0, nullptr}; - node::InitializationResult result = - node::InitializeOncePerProcess(node_argc, node_argv); -#else node::InitializationResult result = node::InitializeOncePerProcess(argc, argv); -#endif CHECK(!result.early_return); CHECK_EQ(result.exit_code, 0); + std::string out_path; + if (node::per_process::cli_options->build_snapshot) { + out_path = result.args[2]; + } else { + out_path = result.args[1]; + } + + std::ofstream out(out_path, std::ios::out | std::ios::binary); + if (!out) { + std::cerr << "Cannot open " << out_path << "\n"; + return 1; + } + { std::string snapshot = node::SnapshotBuilder::Generate(result.args, result.exec_args); out << snapshot; - out.close(); + + if (!out) { + std::cerr << "Failed to write " << out_path << "\n"; + return 1; + } } node::TearDownOncePerProcess(); |