2019-02-10 13:04:11 -05:00
|
|
|
def _go_command(ctx):
|
|
|
|
output = ctx.attr.output
|
|
|
|
if ctx.attr.os == "windows":
|
|
|
|
output = output + ".exe"
|
|
|
|
|
2020-07-18 22:40:33 -04:00
|
|
|
output_file = ctx.actions.declare_file(ctx.attr.os + "/" + ctx.attr.arch + "/" + ctx.attr.ver + "/" + output)
|
2019-02-10 13:04:11 -05:00
|
|
|
pkg = ctx.attr.pkg
|
|
|
|
|
2020-10-07 02:29:07 -04:00
|
|
|
ld_flags = "-s -w -buildid="
|
2019-02-10 13:04:11 -05:00
|
|
|
if ctx.attr.ld:
|
|
|
|
ld_flags = ld_flags + " " + ctx.attr.ld
|
|
|
|
|
|
|
|
options = [
|
|
|
|
"go",
|
|
|
|
"build",
|
2020-10-06 22:02:40 -04:00
|
|
|
"-trimpath",
|
|
|
|
"-o", output_file.path,
|
2019-02-10 13:04:11 -05:00
|
|
|
"-ldflags", "'%s'" % ld_flags,
|
|
|
|
"-tags", "'%s'" % ctx.attr.gotags,
|
|
|
|
pkg,
|
|
|
|
]
|
|
|
|
|
|
|
|
command = " ".join(options)
|
|
|
|
|
|
|
|
envs = [
|
|
|
|
"CGO_ENABLED=0",
|
|
|
|
"GOOS="+ctx.attr.os,
|
|
|
|
"GOARCH="+ctx.attr.arch,
|
2019-04-15 20:36:12 -04:00
|
|
|
"GO111MODULE=on",
|
2019-04-15 21:15:37 -04:00
|
|
|
"GOCACHE=${TMPDIR}/gocache"
|
2019-02-10 13:04:11 -05:00
|
|
|
]
|
2019-04-15 20:36:12 -04:00
|
|
|
|
2019-02-10 13:04:11 -05:00
|
|
|
if ctx.attr.mips: # https://github.com/golang/go/issues/27260
|
|
|
|
envs+=["GOMIPS="+ctx.attr.mips]
|
|
|
|
envs+=["GOMIPS64="+ctx.attr.mips]
|
|
|
|
envs+=["GOMIPSLE="+ctx.attr.mips]
|
|
|
|
envs+=["GOMIPS64LE="+ctx.attr.mips]
|
|
|
|
if ctx.attr.arm:
|
|
|
|
envs+=["GOARM="+ctx.attr.arm]
|
|
|
|
|
2019-04-15 20:36:12 -04:00
|
|
|
switchToPwd="cd ${SPWD} && "
|
|
|
|
|
|
|
|
command = switchToPwd + " ".join(envs) + " " + command
|
2019-02-10 13:04:11 -05:00
|
|
|
|
|
|
|
ctx.actions.run_shell(
|
|
|
|
outputs = [output_file],
|
|
|
|
command = command,
|
|
|
|
use_default_shell_env = True,
|
|
|
|
)
|
|
|
|
runfiles = ctx.runfiles(files = [output_file])
|
|
|
|
return [DefaultInfo(executable = output_file, runfiles = runfiles)]
|
|
|
|
|
|
|
|
|
|
|
|
foreign_go_binary = rule(
|
|
|
|
_go_command,
|
|
|
|
attrs = {
|
|
|
|
'pkg': attr.string(),
|
|
|
|
'output': attr.string(),
|
|
|
|
'os': attr.string(mandatory=True),
|
|
|
|
'arch': attr.string(mandatory=True),
|
2020-07-18 22:40:33 -04:00
|
|
|
'ver': attr.string(mandatory=True),
|
2019-02-10 13:04:11 -05:00
|
|
|
'mips': attr.string(),
|
|
|
|
'arm': attr.string(),
|
|
|
|
'ld': attr.string(),
|
|
|
|
'gotags': attr.string(),
|
|
|
|
},
|
|
|
|
executable = True,
|
|
|
|
)
|