build.bzl 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. ]
  28. if ctx.attr.mips: # https://github.com/golang/go/issues/27260
  29. envs+=["GOMIPS="+ctx.attr.mips]
  30. envs+=["GOMIPS64="+ctx.attr.mips]
  31. envs+=["GOMIPSLE="+ctx.attr.mips]
  32. envs+=["GOMIPS64LE="+ctx.attr.mips]
  33. if ctx.attr.arm:
  34. envs+=["GOARM="+ctx.attr.arm]
  35. command = " ".join(envs) + " " + command
  36. ctx.actions.run_shell(
  37. outputs = [output_file],
  38. command = command,
  39. use_default_shell_env = True,
  40. )
  41. runfiles = ctx.runfiles(files = [output_file])
  42. return [DefaultInfo(executable = output_file, runfiles = runfiles)]
  43. foreign_go_binary = rule(
  44. _go_command,
  45. attrs = {
  46. 'pkg': attr.string(),
  47. 'output': attr.string(),
  48. 'os': attr.string(mandatory=True),
  49. 'arch': attr.string(mandatory=True),
  50. 'mips': attr.string(),
  51. 'arm': attr.string(),
  52. 'ld': attr.string(),
  53. 'gotags': attr.string(),
  54. },
  55. executable = True,
  56. )