Skip to content

Commit

Permalink
build: support --root in js2c.cc
Browse files Browse the repository at this point in the history
  • Loading branch information
joyeecheung committed May 13, 2023
1 parent c40640c commit b1fd0d9
Showing 1 changed file with 42 additions and 12 deletions.
54 changes: 42 additions & 12 deletions tools/js2c.cc
Original file line number Diff line number Diff line change
Expand Up @@ -656,26 +656,56 @@ int JS2C(const FileList& js_files,
return WriteIfChanged(out, dest);
}

int PrintUsage(const char* argv0) {
fprintf(stderr,
"Usage: %s [--verbose] [--root /path/to/project/root] "
"path/to/output.cc path/to/directory "
"[extra-files ...]\n",
argv0);
return 1;
}

int Main(int argc, char* argv[]) {
if (argc < 3) {
fprintf(stderr,
"Usage: %s [--verbose] path/to/output.cc path/to/directory "
"[extra-files ...]\n",
argv[0]);
return 1;
return PrintUsage(argv[0]);
}

std::vector<std::string> args;
args.reserve(argc);
std::string root_dir;
for (int i = 1; i < argc; ++i) {
std::string arg(argv[i]);
if (arg == "--verbose") {
is_verbose = true;
} else if (arg == "--root") {
if (i == argc - 1) {
fprintf(stderr, "--root must be followed by a path\n");
return 1;
}
root_dir = argv[++i];
} else {
args.emplace_back(argv[i]);
}
}

if (args.size() < 2) {
return PrintUsage(argv[0]);
}

int start = 1;
if (strcmp(argv[start], "--verbose") == 0) {
is_verbose = true;
start++;
if (!root_dir.empty()) {
int r = uv_chdir(root_dir.c_str());
if (r != 0) {
fprintf(stderr, "Cannot switch to the directory specified by --root\n");
PrintUvError("chdir", root_dir.c_str(), r);
return 1;
}
}
std::string output = argv[start++];
std::string output = args[0];

FileMap file_map;
for (int i = start; i < argc; ++i) {
for (size_t i = 1; i < args.size(); ++i) {
int error = 0;
std::string file(argv[i]);
const std::string& file = args[i];
if (IsDirectory(file, &error)) {
if (!SearchFiles(file, &file_map, std::string(kJsSuffix)) ||
!SearchFiles(file, &file_map, std::string(kMjsSuffix))) {
Expand Down

0 comments on commit b1fd0d9

Please sign in to comment.