-
Notifications
You must be signed in to change notification settings - Fork 7
/
java_compile.cpp
123 lines (111 loc) · 3.3 KB
/
java_compile.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string>
#include <string_view>
#include <vector>
#include "logging.h"
#include "util.h"
namespace {
std::string TrimExtension(std::string_view filename,
std::string_view extension) {
if (filename.size() >= extension.size() &&
filename.compare(filename.size() - extension.size(), std::string::npos,
extension) == 0) {
filename.remove_suffix(extension.size());
}
return std::string(filename);
}
[[noreturn]] void Exec(const std::vector<std::string>& args) {
std::vector<char*> argv;
argv.reserve(args.size() + 1);
for (const std::string& arg : args)
argv.emplace_back(const_cast<char*>(arg.data()));
argv.emplace_back(nullptr);
if (execve(argv[0], argv.data(), environ))
PLOG(FATAL) << "Failed to execve `" << StringJoin(args, " ") << "`";
abort();
}
int ForkExec(const std::vector<std::string>& args) {
pid_t child = vfork();
if (child == -1)
PLOG(FATAL) << "Could not fork child";
if (child == 0) {
// This is the child process.
Exec(args);
}
// Parent process.
int wstatus;
while (true) {
if (waitpid(child, &wstatus, WUNTRACED | WCONTINUED) == -1)
PLOG(FATAL) << "Failed to wait for child";
if (WIFEXITED(wstatus))
return WEXITSTATUS(wstatus);
if (WIFSIGNALED(wstatus))
return WTERMSIG(wstatus);
}
}
} // namespace
int main(int argc, char* argv[]) {
bool kotlin = false;
if (argc >= 2 && std::string_view(argv[1]).rfind("--language=") == 0) {
if (std::string_view(argv[1]) == "--language=kotlin") {
kotlin = true;
}
argc--;
argv[1] = argv[0];
argv++;
}
if (argc < 3) {
PLOG(FATAL) << argv[0]
<< " [--language=...] <target> <source> [<source> ...]";
}
std::vector<std::string> compiler_args;
std::vector<std::string> jaotc_args = {
"/usr/bin/jaotc", "-J-Xmx512M", "-J-XX:+UseSerialGC",
"-J-Xshare:on", "--output", StringPrintf("%s.so", argv[1]),
};
if (kotlin) {
compiler_args = {
"/usr/bin/java",
"-Xmx896M",
"-Xms32M",
"-Xshare:on",
"-XX:+UseSerialGC",
"-XX:+UnlockExperimentalVMOptions",
"-XX:AOTLibrary=/usr/lib/jvm/java.base.so",
"-cp",
"/usr/lib/jvm/kotlinc/lib/kotlin-preloader.jar",
"org.jetbrains.kotlin.preloading.Preloader",
"-cp",
"/usr/lib/jvm/kotlinc/lib/kotlin-compiler.jar",
"org.jetbrains.kotlin.cli.jvm.K2JVMCompiler",
};
jaotc_args.emplace_back("-J-XX:+UnlockExperimentalVMOptions");
jaotc_args.emplace_back(
"-J-XX:AOTLibrary=/usr/lib/jvm/java.base.so,/usr/lib/jvm/"
"kotlin-stdlib.jar.so");
} else {
compiler_args = {
"/usr/bin/javac",
"-J-Xmx896M",
"-J-Xms32M",
};
}
compiler_args.emplace_back("-d");
compiler_args.emplace_back(".");
for (int i = 2; i < argc; ++i) {
compiler_args.emplace_back(argv[i]);
if (kotlin) {
jaotc_args.emplace_back(
StringPrintf("%sKt.class", TrimExtension(argv[i], ".kt").data()));
} else {
jaotc_args.emplace_back(
StringPrintf("%s.class", TrimExtension(argv[i], ".java").data()));
}
}
int status = ForkExec(compiler_args);
if (status != 0)
return status;
Exec(jaotc_args);
}