Skip to content

Commit

Permalink
Fix golang ro cache
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelvigee committed May 11, 2024
1 parent 11bcf1d commit e57176c
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 51 deletions.
6 changes: 0 additions & 6 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@ load("//backend/node", "yarn_toolchain")
go_toolchain(
name = "go",
version = "1.22.2",
architectures = [
"darwin_amd64",
"darwin_arm64",
"linux_amd64",
"linux_arm64",
],
env = {
"GOEXPERIMENT": "rangefunc",
},
Expand Down
83 changes: 41 additions & 42 deletions backend/go/BUILD
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
backend_pkg = heph.pkg.addr()

cfg = CONFIG["go_backend"]

go = cfg["go"]
Expand All @@ -18,23 +20,44 @@ go_toolchain_gosh = group(
deps = ["go.sh"],
)

def go_toolchain(name, version, architectures = [], runtime_env = {}, **kwargs):
def go_std(os, arch, name = None):
if not name:
name = "_std_lib_{}_{}".format(os, arch)

xargs = "xargs " + ("-S" if get_os() == "darwin" else "-s") + " 100000"

return target(
name = name,
pkg = backend_pkg,
tools = go,
run = [
'export LGOROOT=$(pwd)/goroot',
'rm -rf $LGOROOT',
'cp -r $(go env GOROOT) $LGOROOT',
'export GOROOT=$LGOROOT',
'chmod -R 777 $GOROOT',
'go install --trimpath std',
'go list std > list',
'cat list | {} -I[] echo "packagefile []=$OUT_PKG/[].a" | sort -u > $SANDBOX/$OUT_IMPORTCFG'.format(xargs),
],
out_env = "rel_root",
env = {
'GOARCH': arch,
'GOOS': os,
'GODEBUG': 'installgoroot=all',
},
out = {
'pkg': 'goroot/pkg/{}_{}'.format(os, arch),
'importcfg': 'goroot/pkg/{}_{}.importcfg'.format(os, arch),
},
)

def go_toolchain(name, version, runtime_env = {}, **kwargs):
run = [
"./$SRC_INSTALL '{}'".format(version),
"mv $SRC_GO $OUT_GO",
"export GO_OUTDIR=$(pwd)",
"export GODEBUG='installgoroot=all'",
]

for arch in architectures:
(goos, _, goarch) = arch.partition("_")
run.append(
"(export GOOS={} && export GOARCH={} && ./$OUT_GO install --trimpath std)".format(
goos,
goarch,
),
)

return target(
name = name,
run = run,
Expand All @@ -52,7 +75,6 @@ def go_toolchain(name, version, architectures = [], runtime_env = {}, **kwargs):
},
cache = heph.cache(history = 1),
support_files = "go",
runtime_env = {"GO_SHARED": "$(shared_stage_dir)"},
transitive = heph.target_spec(
runtime_env = runtime_env | {"GO_OUTDIR": "$(outdir)", "GO_SHARED": "$(shared_stage_dir)"},
**kwargs,
Expand Down Expand Up @@ -86,10 +108,6 @@ generate_testmain = target(
},
)

backend_dir = heph.pkg.dir()

backend_pkg = heph.pkg.addr()

def go_install(name, pkg, version, bin_name):
return target(
name = name,
Expand Down Expand Up @@ -182,10 +200,8 @@ def go_mod(
env = goenv,
)

(pkg, _, _) = heph.split(go)

mod_pkgs_gen = [
"//" + pkg + ":_std_pkgs_*",
backend_pkg + ":_std_lib_*",
"go_lib",
"go_build_bin",
"test",
Expand Down Expand Up @@ -276,9 +292,7 @@ def _go_gen_importcfg():
xargs = "xargs " + ("-S" if get_os() == "darwin" else "-s") + " 100000"

return [
'cat "$SRC_STD" | {} -I[] echo "packagefile []=$GO_OUTDIR/go/pkg/${{GOOS}}_${{GOARCH}}/[].a" | sort -u > $SANDBOX/importconfig'.format(xargs),
'echo "" >> $SANDBOX/importconfig',
'find "$SANDBOX" -name "*.importcfg" | {} -I[] cat [] | sed -e "s:=:=$SANDBOX/:" | sort -u >> $SANDBOX/importconfig'.format(xargs),
'find "$SANDBOX" -name "*.importcfg" | {} -I[] cat [] | sed -e "s:=:=$SANDBOX/:" | sort -u > $SANDBOX/importconfig'.format(xargs),
]

def _go_compile_cmd(name, import_path, abi, complete, embed_cfg):
Expand Down Expand Up @@ -352,7 +366,7 @@ def go_library(
if len(s_files) > 0:
abi = target(
name = pvt_name + "#abi",
deps = src_dep if src_dep else s_files,
deps = (src_dep if src_dep else s_files) + [go_std(os = os, arch = arch)],
run = [
"eval $(go env)",
"touch $OUT_H", # the go Toolchain does this
Expand Down Expand Up @@ -391,7 +405,7 @@ def go_library(
deps = {
"lib": lib + "|a",
"hdr": lib + "|h",
"_": src_dep if src_dep else s_file,
"_": (src_dep if src_dep else s_file) + [go_std(os = os, arch = arch)],
},
run = [
"eval $(go env)",
Expand Down Expand Up @@ -473,7 +487,7 @@ def go_library(
embed_cfg = None

deps = deps | {
"std": _std_pkgs(os, arch),
"std": go_std(os = os, arch = arch),
"embed": embed_cfg,
}

Expand All @@ -497,21 +511,6 @@ def go_library(
labels = ["go_lib"],
)

def _std_pkgs(os, arch):
(pkg, _, _) = heph.split(go)

return target(
name = "_std_pkgs_{}_{}".format(os, arch),
pkg = "//" + pkg,
tools = [go],
run = "export CGO_ENABLED=1 && go list std > std_list",
out = "std_list",
env = {
"GOOS": os,
"GOARCH": arch,
},
)

def go_build_bin(
name,
main,
Expand All @@ -534,7 +533,7 @@ def go_build_bin(
_deps = {
"libs": libs,
"main": main,
"std": _std_pkgs(os, arch),
"std": go_std(os, arch),
}

return target(
Expand Down
4 changes: 2 additions & 2 deletions backend/go/go.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

export GOPATH=${GOPATH:-$GO_SHARED/path}
export GOCACHE=${GOCACHE:-$GO_SHARED/cache}
export GOROOT=$GO_OUTDIR/go
export GOROOT=${GOROOT:-$GO_OUTDIR/go}
export CGO_ENABLED=${CGO_ENABLED:-0}

set -u

exec $GO_OUTDIR/go/bin/go "$@"
exec $GOROOT/bin/go "$@"
1 change: 1 addition & 0 deletions dev_run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ set -e

export HEPH_CWD=$(pwd)
export HEPH_SRC_ROOT="<HEPH_SRC_ROOT>"
export GOEXPERIMENT=rangefunc

cd $HEPH_SRC_ROOT

Expand Down
2 changes: 2 additions & 0 deletions sandbox/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ func Make(ctx context.Context, cfg MakeConfig) error {
log.Debugf("Make %v took %v", cfg.Dir, time.Since(start))
}()

xfs.MakeDirsReadWrite(cfg.Dir)

err := os.RemoveAll(cfg.Dir)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion test/features/deps.BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ e2e_test(
name = "e2e_gen_deps",
cmd = "heph q deps '{}'".format(bin),
expect_output_contains = """
//:_std_pkgs_OS_ARCH
//:go
//go_backend:_std_lib_OS_ARCH
//test/go/mod-transitive-gen:_go_lib_
""".replace("OS", get_os()).replace("ARCH", get_arch()).strip(),
)
Expand Down

0 comments on commit e57176c

Please sign in to comment.