Skip to content

Commit b1fd0d9

Browse files
committed
build: support --root in js2c.cc
1 parent c40640c commit b1fd0d9

File tree

1 file changed

+42
-12
lines changed

1 file changed

+42
-12
lines changed

tools/js2c.cc

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -656,26 +656,56 @@ int JS2C(const FileList& js_files,
656656
return WriteIfChanged(out, dest);
657657
}
658658

659+
int PrintUsage(const char* argv0) {
660+
fprintf(stderr,
661+
"Usage: %s [--verbose] [--root /path/to/project/root] "
662+
"path/to/output.cc path/to/directory "
663+
"[extra-files ...]\n",
664+
argv0);
665+
return 1;
666+
}
667+
659668
int Main(int argc, char* argv[]) {
660669
if (argc < 3) {
661-
fprintf(stderr,
662-
"Usage: %s [--verbose] path/to/output.cc path/to/directory "
663-
"[extra-files ...]\n",
664-
argv[0]);
665-
return 1;
670+
return PrintUsage(argv[0]);
671+
}
672+
673+
std::vector<std::string> args;
674+
args.reserve(argc);
675+
std::string root_dir;
676+
for (int i = 1; i < argc; ++i) {
677+
std::string arg(argv[i]);
678+
if (arg == "--verbose") {
679+
is_verbose = true;
680+
} else if (arg == "--root") {
681+
if (i == argc - 1) {
682+
fprintf(stderr, "--root must be followed by a path\n");
683+
return 1;
684+
}
685+
root_dir = argv[++i];
686+
} else {
687+
args.emplace_back(argv[i]);
688+
}
689+
}
690+
691+
if (args.size() < 2) {
692+
return PrintUsage(argv[0]);
666693
}
667694

668-
int start = 1;
669-
if (strcmp(argv[start], "--verbose") == 0) {
670-
is_verbose = true;
671-
start++;
695+
if (!root_dir.empty()) {
696+
int r = uv_chdir(root_dir.c_str());
697+
if (r != 0) {
698+
fprintf(stderr, "Cannot switch to the directory specified by --root\n");
699+
PrintUvError("chdir", root_dir.c_str(), r);
700+
return 1;
701+
}
672702
}
673-
std::string output = argv[start++];
703+
std::string output = args[0];
674704

675705
FileMap file_map;
676-
for (int i = start; i < argc; ++i) {
706+
for (size_t i = 1; i < args.size(); ++i) {
677707
int error = 0;
678-
std::string file(argv[i]);
708+
const std::string& file = args[i];
679709
if (IsDirectory(file, &error)) {
680710
if (!SearchFiles(file, &file_map, std::string(kJsSuffix)) ||
681711
!SearchFiles(file, &file_map, std::string(kMjsSuffix))) {

0 commit comments

Comments
 (0)