-
Notifications
You must be signed in to change notification settings - Fork 28
/
java_builder.go
66 lines (55 loc) · 1.34 KB
/
java_builder.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
package main
import (
"flag"
"fmt"
"log"
"os"
"path/filepath"
"runtime"
"v.io/x/lib/gosh"
)
type tb struct{}
func (t *tb) FailNow() {
log.Panicf("build failed")
}
func (t *tb) Logf(format string, args ...interface{}) {
log.Printf(format, args...)
}
var (
packageName string
outputDirName string
outputLibName string
)
func init() {
flag.StringVar(&packageName, "package-name", "v.io/x/jni/main", "build the specified package as a .so/.dylib for use with java")
flag.StringVar(&outputDirName, "output-dir", "", "the directory for the output file")
flag.StringVar(&outputLibName, "output-lib", "libv23", "the prefix for the output file, .so or .dylib will be appended appropriately")
}
func main() {
flag.Parse()
javaHome := os.Getenv("JAVA_HOME")
if len(javaHome) == 0 {
log.Fatalf("JAVA_HOME not specified")
}
if len(outputDirName) == 0 {
log.Fatalf("--output-dir not specified")
}
if runtime.GOOS == "darwin" {
outputLibName += ".dylib"
} else {
outputLibName += ".so"
}
sh := gosh.NewShell(&tb{})
sh.PropagateChildOutput = true
defer sh.Cleanup()
cmd := sh.Cmd(
"go",
"build",
"-buildmode=c-shared",
"-v",
"-tags", "java cgo",
"-o", filepath.Join(outputDirName, outputLibName),
packageName)
cmd.Vars["CGO_CFLAGS"] = fmt.Sprintf("-I%v/include -I%v/include/%v", javaHome, javaHome, runtime.GOOS)
cmd.Run()
}