@@ -41,12 +41,20 @@ func IsWorkerCompatibleBinary() (string, bool) {
41
41
42
42
var unique int32
43
43
44
+ // CompileOpts are additional options for dynamic compiles of the local code
45
+ // for development purposes. Production runs should build the worker binary
46
+ // separately for the target environment.
47
+ // See https://beam.apache.org/documentation/sdks/go-cross-compilation/ for details.
48
+ type CompileOpts struct {
49
+ OS , Arch string
50
+ }
51
+
44
52
// BuildTempWorkerBinary creates a local worker binary in the tmp directory
45
53
// for linux/amd64. Caller responsible for deleting the binary.
46
- func BuildTempWorkerBinary (ctx context.Context ) (string , error ) {
54
+ func BuildTempWorkerBinary (ctx context.Context , opts CompileOpts ) (string , error ) {
47
55
id := atomic .AddInt32 (& unique , 1 )
48
56
filename := filepath .Join (os .TempDir (), fmt .Sprintf ("worker-%v-%v" , id , time .Now ().UnixNano ()))
49
- if err := buildWorkerBinary (ctx , filename ); err != nil {
57
+ if err := buildWorkerBinary (ctx , filename , opts ); err != nil {
50
58
return "" , err
51
59
}
52
60
return filename , nil
@@ -59,7 +67,7 @@ func BuildTempWorkerBinary(ctx context.Context) (string, error) {
59
67
// * /Users/herohde/go/src/github.com/apache/beam/sdks/go/examples/wordcount/wordcount.go (skip: 3)
60
68
// /usr/local/go/src/runtime/proc.go (skip: 4) // not always present
61
69
// /usr/local/go/src/runtime/asm_amd64.s (skip: 4 or 5)
62
- func buildWorkerBinary (ctx context.Context , filename string ) error {
70
+ func buildWorkerBinary (ctx context.Context , filename string , opts CompileOpts ) error {
63
71
program := ""
64
72
var isTest bool
65
73
for i := 3 ; ; i ++ {
@@ -77,9 +85,17 @@ func buildWorkerBinary(ctx context.Context, filename string) error {
77
85
}
78
86
goos := "linux"
79
87
goarch := "amd64"
88
+
89
+ if opts .OS != "" {
90
+ goos = opts .OS
91
+ }
92
+ if opts .Arch != "" {
93
+ goarch = opts .Arch
94
+ }
95
+
80
96
cgo := "0"
81
97
82
- log .Infof (ctx , "Cross-compiling %v with GOOS=%s GOARCH=%s CGO_ENABLED=%s as %v" , goos , goarch , cgo , program , filename )
98
+ log .Infof (ctx , "Cross-compiling %v with GOOS=%s GOARCH=%s CGO_ENABLED=%s as %v" , program , goos , goarch , cgo , filename )
83
99
84
100
// Cross-compile given go program. Not awesome.
85
101
program = program [:strings .LastIndex (program , "/" )+ 1 ]
0 commit comments