-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile.go
87 lines (80 loc) · 1.63 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
package main
import (
"errors"
"io/ioutil"
"log"
"os/exec"
"syscall"
"time"
)
func compile(codepath, lang string) (string, error) {
cmd := new(exec.Cmd)
switch lang {
case "cpp":
cmd = exec.Command(
"g++",
codepath,
"-o", "Main",
"-static", "-w",
"-lm", "-O2", "-std=c++14",
"-DONLINE_JUDGE",
)
case "c":
cmd = exec.Command(
"gcc",
codepath,
"-o", "Main",
"-static", "-w",
"-lm", "-std=c11",
"-O2", "-DONLINE_JUDGE",
)
case "java":
cmd = exec.Command(
"javac",
codepath,
"-d", ".",
)
}
ch := make(chan string)
e := make(chan bool)
go func(cmd *exec.Cmd) {
stderr, err := cmd.StderrPipe()
if err != nil {
log.Printf("Error: %s\n", err)
e <- true
return
}
if err = cmd.Start(); err != nil {
log.Printf("Error: %s\n", err)
e <- true
return
}
bytes, _ := ioutil.ReadAll(stderr)
if err := cmd.Wait(); err != nil {
if exiterr, ok := err.(*exec.ExitError); ok {
// The program has exited with an exit code != 0
// This works on both Unix and Windows. Although package
// syscall is generally platform dependent, WaitStatus is
// defined for both Unix and Windows and in both cases has
// an ExitStatus() method with the same signature.
if _, ok := exiterr.Sys().(syscall.WaitStatus); ok {
ch <- string(bytes)
return
}
} else {
log.Fatalf("cmd.Wait: %v", err)
}
}
ch <- ""
return
}(cmd)
select {
case res := <-ch:
return res, nil
case <-e:
return "", errors.New("System Error")
case <-time.After(time.Second * 15):
return "", errors.New("Compile Time Out")
}
return "", errors.New("System Error")
}