github.com/emcfarlane/larking@v0.0.0-20220605172417-1704b45ee6c3/starlib/rules/go.star (about) 1 load("rule.star", "DefaultInfo", "attr", "attrs", "rule") 2 load("thread.star", "arch", "os") 3 4 # TODO: filepath.Join() support for pathing... 5 def _go_impl(ctx): 6 print("GOT CTX", ctx) 7 args = ["build", "-o", ctx.attrs.name] 8 9 env = [] 10 goos = os or ctx.attrs.goos 11 env.append("GOOS=" + goos) 12 13 goarch = arch or ctx.attrs.goarch 14 env.append("GOARCH=" + ctx.attrs.goarch) 15 16 if ctx.attrs.cgo: 17 # TODO: better. 18 # 19 target = { 20 "amd64": "x86_64", 21 }[goarch] + "-" + { 22 "linux": "linux", 23 "darwin": "macos", 24 }[goos] 25 26 env.append("CGO_ENABLED=1") 27 env.append("CC=zig cc -target " + target) # + ctx.attrs._zcc.value.path) 28 env.append("CXX=zig c++ -target " + target) # + ctx.attrs._zxx.value.path) 29 30 # https://github.com/ziglang/zig/issues/11303 31 if goos == "darwin": 32 args.extend([ 33 "-buildmode=pie", 34 "-ldflags", 35 "-s -w -linkmode external -extldflags '--sysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -F /System/Library/Frameworks'", 36 ]) 37 else: 38 env.append("CGO_ENABLED=0") 39 print("ENV:", env) 40 41 args.append(".") 42 43 # Maybe? 44 ctx.actions.run( 45 name = "go", 46 dir = ctx.build_dir, 47 args = args, 48 env = env, 49 ) 50 51 name = ctx.actions.label(ctx.attrs.name) 52 print("name", name) 53 return [DefaultInfo( 54 executable = name, 55 files = [name], 56 )] 57 58 go = rule( 59 impl = _go_impl, 60 provides = [DefaultInfo], 61 attrs = attrs( 62 goos = attr.string(values = [ 63 "aix", 64 "android", 65 "darwin", 66 "dragonfly", 67 "freebsd", 68 "hurd", 69 "illumos", 70 "js", 71 "linux", 72 "nacl", 73 "netbsd", 74 "openbsd", 75 "plan9", 76 "solaris", 77 "windows", 78 "zos", 79 ]), 80 goarch = attr.string(values = [ 81 "386", 82 "amd64", 83 "amd64p32", 84 "arm", 85 "armbe", 86 "arm64", 87 "arm64be", 88 "ppc64", 89 "ppc64le", 90 "mips", 91 "mipsle", 92 "mips64", 93 "mips64le", 94 "mips64p32", 95 "mips64p32le", 96 "ppc", 97 "riscv", 98 "riscv64", 99 "s390", 100 "s390x", 101 "sparc", 102 "sparc64", 103 "wasm", 104 ]), 105 cgo = attr.bool(), 106 #"_zxx": attr.label(allow_files = True, default = "file://rules/go/zxx"), 107 #"_zcc": attr.label(allow_files = True, default = "file://rules/go/zcc"), 108 ), 109 #outs = { 110 # "bin": attr.label( 111 # executable = True, 112 # mandatory = True, 113 # ), 114 #}, 115 )