-
Notifications
You must be signed in to change notification settings - Fork 11
/
compile.go
276 lines (256 loc) · 7.75 KB
/
compile.go
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package worker
// Compiling anything that's more compicated than single file:
//
// - Prepare a "compile_%s.sh" file, with %s being the language (cc, go, rs, java, py2, py3, pas).
// - Prepare any more files as needed. They will all be put into the CWD of the script.
// - The CWD also contains "code.%s" (%s being the language's respective extension) file, which is the contestant's source code.
// - The script should do whatever it wants (unsandboxed, because it's not my job to do so) within 20 seconds.
// - It should produce a single binary called "code" in the CWD.
import (
"bytes"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
"github.com/jmoiron/sqlx"
"github.com/natsukagami/kjudge/models"
"github.com/pkg/errors"
)
// CompileContext is the information needed to perform compilation.
type CompileContext struct {
DB *sqlx.Tx
Sub *models.Submission
Problem *models.Problem
}
// Compile performs compilation.
// Returns whether the compilation succeeds.
func Compile(c *CompileContext) (bool, error) {
// First we gotta know which compilation scheme we will be taking.
files, err := models.GetProblemFiles(c.DB, c.Problem.ID)
if err != nil {
return false, err
}
action, batchFile, err := CompileBatch(c.Sub.Language)
if err != nil {
return false, err
}
hasFile := false
hasBatch := false
for _, file := range files {
hasBatch = hasBatch || isBatchFile(file.Filename)
if file.Filename == batchFile {
hasFile = true
break
}
}
if !hasBatch {
// No batch file, compiling as a single file.
action, err = CompileSingle(c.Sub.Language)
if err != nil {
return false, err
}
} else if !hasFile {
// Batch compile mode enabled, but this language is not supported.
c.Sub.CompiledSource = nil
c.Sub.Verdict = VerdictCompileError
c.Sub.CompilerOutput = []byte("Custom Compilers are not enabled for this language.")
return false, c.Sub.Write(c.DB)
}
log.Printf("[WORKER] Compiling submission %v\n", c.Sub.ID)
// Now, create a temporary directory.
dir, err := os.MkdirTemp("", "*")
if err != nil {
return false, errors.WithStack(err)
}
defer action.Cleanup(dir)
// Prepare source and files
action.Source.Content = c.Sub.Source
action.Files = files
if err := action.Prepare(dir); err != nil {
return false, err
}
// Perform compilation
result, messages := action.Perform(dir)
c.Sub.CompilerOutput = messages
if result {
// Success!
output, err := os.ReadFile(filepath.Join(dir, action.Output))
if err != nil {
return false, errors.WithStack(err)
}
c.Sub.CompiledSource = output
} else {
c.Sub.CompiledSource = nil
c.Sub.Verdict = VerdictCompileError
}
log.Printf("[WORKER] Compiling submission %v succeeded (result = %v).", c.Sub.ID, result)
return result, c.Sub.Write(c.DB)
}
// CompileAction is an action revolving writing the source into a file in "Source",
// compile it with "Command" and taking the "Output" as the result.
type CompileAction struct {
Source *models.File
Files []*models.File
Commands [][]string
Output string
}
// Prepare prepares a temporary folder and copies all the content there.
func (c *CompileAction) Prepare(dir string) error {
// Copy over all files and the source code.
if err := os.WriteFile(filepath.Join(dir, c.Source.Filename), c.Source.Content, 0666); err != nil {
return errors.WithStack(err)
}
for _, file := range c.Files {
if err := os.WriteFile(filepath.Join(dir, file.Filename), file.Content, 0666); err != nil {
return errors.Wrapf(err, "copying file %s", file.Filename)
}
}
return nil
}
// Cleanup performs clean-up on the prepared directory.
func (c *CompileAction) Cleanup(dir string) {
_ = os.RemoveAll(dir)
}
// Perform performs the compile action on the given directory.
// The directory MUST contain all files given by the Problem, PLUS the written "Source" file.
func (c *CompileAction) Perform(cwd string) (succeeded bool, messages []byte) {
allOutputs := bytes.Buffer{}
for _, command := range c.Commands {
allOutputs.WriteString(fmt.Sprintf("%s:\n", strings.Join(command, " ")))
cmd := exec.Command(command[0], command[1:]...)
// Set the cwd
cmd.Dir = cwd
// Run the command and collect outputs
var (
output []byte
err error
done = make(chan struct{}, 1)
)
go func() {
output, err = cmd.CombinedOutput()
done <- struct{}{}
}()
select {
case <-time.After(20 * time.Second):
allOutputs.WriteString("Command has timed out\n")
return false, allOutputs.Bytes()
case <-done:
}
allOutputs.Write(output)
allOutputs.WriteString("\n")
if err != nil {
log.Println(err)
return false, allOutputs.Bytes()
}
}
return true, allOutputs.Bytes()
}
var batchFilenames = []string{
"compile_cc.sh", "compile_go.sh", "compile_java.sh", "compile_rs.sh", "compile_pas.sh", "compile_py2.sh", "compile_py3.sh",
}
func isBatchFile(filename string) bool {
for _, f := range batchFilenames {
if f == filename {
return true
}
}
return false
}
// CompileBatch returns a compile action, along with the required batch filename
// in order to successfully compile.
func CompileBatch(l models.Language) (*CompileAction, string, error) {
var source, batch string
switch l {
case models.LanguageCpp:
source = "code.cc"
batch = "compile_cc.sh"
case models.LanguageGo:
source = "code.go"
batch = "compile_go.sh"
case models.LanguageJava:
source = "code.java"
batch = "compile_java.sh"
case models.LanguageRust:
source = "code.rs"
batch = "compile_rs.sh"
case models.LanguagePas:
source = "code.pas"
batch = "compile_pas.sh"
case models.LanguagePy2:
source = "code.py"
batch = "compile_py2.sh"
case models.LanguagePy3:
source = "code.py"
batch = "compile_py3.sh"
default:
return nil, "", errors.New("unknown language")
}
return &CompileAction{
Source: &models.File{Filename: source},
Commands: [][]string{{"sh", batch}},
Output: "code",
}, batch, nil
}
// CompileSingle creates a compilation command for a single source code file.
// Sometimes this is as simple as "copy".
func CompileSingle(l models.Language) (*CompileAction, error) {
switch l {
case models.LanguageCpp:
return &CompileAction{
Source: &models.File{Filename: "code.cc"},
Commands: [][]string{{"g++", "-std=c++17", "-O2", "-s", "-lm", "-DONLINE_JUDGE", "-DKJUDGE", "-o", "code", "code.cc"}},
Output: "code",
}, nil
case models.LanguageGo:
return &CompileAction{
Source: &models.File{Filename: "code.go"},
Commands: [][]string{{"go", "build", "-buildmode=exe", "-tags", "online_judge,kjudge", "-o", "code", "code.go"}},
Output: "code",
}, nil
case models.LanguageJava:
return &CompileAction{
Source: &models.File{Filename: "code.java"},
Commands: [][]string{
{"javac", "-d", ".", "code.java"},
{"sh", "-c", "jar cf code *.class"},
{"sh", "-c", "rm *.class"},
},
Output: "code",
}, nil
case models.LanguagePas:
return &CompileAction{
Source: &models.File{Filename: "code.pas"},
Commands: [][]string{
{"fpc", "-O3", "-dONLINE_JUDGE", "-dKJUDGE", "-ocode", "code.pas"},
},
Output: "code",
}, nil
case models.LanguageRust:
return &CompileAction{
Source: &models.File{Filename: "code.rs"},
Commands: [][]string{
{"rustc", "-O", "--cfg", "online_judge", "--cfg", "kjudge", "-o", "code", "code.rs"},
},
Output: "code",
}, nil
case models.LanguagePy2:
return &CompileAction{
Source: &models.File{Filename: "code.py"},
Commands: [][]string{
{"python2", "-m", "py_compile", "code.py"},
},
Output: "code.pyc",
}, nil
case models.LanguagePy3:
return &CompileAction{
Source: &models.File{Filename: "code.py"},
Commands: [][]string{{"python3", "-c", `import py_compile as m; m.compile("code.py", "code.pyc", doraise=True)`}},
Output: "code.pyc",
}, nil
default:
return nil, errors.Errorf("Unknown language: %v", l)
}
}