build.bzl 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. def _go_command(ctx):
  2. output = ctx.attr.output
  3. if ctx.attr.os == "windows":
  4. output = output + ".exe"
  5. output_file = ctx.actions.declare_file(ctx.attr.os + "/" + ctx.attr.arch + "/" + output)
  6. pkg = ctx.attr.pkg
  7. ld_flags = "-s -w"
  8. if ctx.attr.ld:
  9. ld_flags = ld_flags + " " + ctx.attr.ld
  10. options = [
  11. "go",
  12. "build",
  13. "-o", output_file.path,
  14. "-compiler", "gc",
  15. "-gcflags", '"all=-trimpath=${GOPATH}/src"',
  16. "-asmflags", '"all=-trimpath=${GOPATH}/src"',
  17. "-ldflags", "'%s'" % ld_flags,
  18. "-tags", "'%s'" % ctx.attr.gotags,
  19. pkg,
  20. ]
  21. command = " ".join(options)
  22. envs = [
  23. "CGO_ENABLED=0",
  24. "GOOS="+ctx.attr.os,
  25. "GOARCH="+ctx.attr.arch,
  26. "GOROOT_FINAL=/go",
  27. "GOCACHE=@D",
  28. ]
  29. if ctx.attr.mips: # https://github.com/golang/go/issues/27260
  30. envs+=["GOMIPS="+ctx.attr.mips]
  31. envs+=["GOMIPS64="+ctx.attr.mips]
  32. envs+=["GOMIPSLE="+ctx.attr.mips]
  33. envs+=["GOMIPS64LE="+ctx.attr.mips]
  34. if ctx.attr.arm:
  35. envs+=["GOARM="+ctx.attr.arm]
  36. command = " ".join(envs) + " " + command
  37. ctx.actions.run_shell(
  38. outputs = [output_file],
  39. command = command,
  40. use_default_shell_env = True,
  41. )
  42. runfiles = ctx.runfiles(files = [output_file])
  43. return [DefaultInfo(executable = output_file, runfiles = runfiles)]
  44. foreign_go_binary = rule(
  45. _go_command,
  46. attrs = {
  47. 'pkg': attr.string(),
  48. 'output': attr.string(),
  49. 'os': attr.string(mandatory=True),
  50. 'arch': attr.string(mandatory=True),
  51. 'mips': attr.string(),
  52. 'arm': attr.string(),
  53. 'ld': attr.string(),
  54. 'gotags': attr.string(),
  55. },
  56. executable = True,
  57. )