-
-
Save raphaelfff/530fec29205ebfa6c521addf4333f5d6 to your computer and use it in GitHub Desktop.
Please auto go.mod
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def _set_go_env(): | |
if CONFIG.GOROOT: | |
go_root = CONFIG.GOROOT | |
elif CONFIG.HOSTOS == 'freebsd': | |
# FreeBSD has some very strange semantics around hardlinks that lead to it finding | |
# the wrong thing (essentially os.Executable, which go uses to define GOROOT, returns | |
# the most recent hardlink to the file). We can work around this way although it's | |
# not very nice (we don't do this globally because OSX doesn't have realpath). | |
go_root = f'$(dirname $(dirname $(realpath $TOOLS_GO)))' | |
else: | |
go_root = '$("$TOOLS_GO" env GOROOT)' | |
cmd = f'export GOPATH=$TMP_DIR && export GOROOT={go_root}' | |
if CONFIG.CGO_ENABLED: | |
return f'export CGO_ENABLED={CONFIG.CGO_ENABLED} && {cmd}' | |
return cmd | |
def go_globall(test=False): | |
return glob(["**/*.go"], exclude = ["*_test.go"] if test else None) | |
def go_mod(name, visibility, deps=None): | |
aggreg_name = name | |
def module_to_target(m): | |
return m.replace('/', '_') | |
def _parse_mod_mod(line): | |
mod, version, depss = line.split(' ') | |
deps = depss.split('|') if depss else [] | |
return mod, version, deps | |
def _mod_mod(_, output): | |
for i, line in enumerate(output): | |
mod, version, deps = _parse_mod_mod(line) | |
mod_target = module_to_target(mod) | |
target_deps = [(":"+module_to_target(d)) for d in deps] | |
log.error(f'{mod} {version} {target_deps}') | |
if i == 0: | |
mod = go_library( | |
name = mod_target+"#lib", | |
srcs = ['.'], | |
deps = target_deps, | |
) | |
else: | |
moddl = go_mod_download( | |
name = mod_target+"#dl", | |
module = mod, | |
version = version, | |
) | |
mod = go_module( | |
name = mod_target, | |
module = mod, | |
download = moddl, | |
deps = target_deps, | |
exported_deps = target_deps, | |
install = None, | |
) | |
add_exported_dep(aggreg_name, mod) | |
builddeps = genrule( | |
name = name+"#builddeps", | |
srcs = {"modpy": ["//build_defs:mod.py"], "d": ["go.mod", "go.sum"]}, | |
deps = deps, | |
cmd = [ | |
_set_go_env(), | |
"$TOOLS_PYTHON $SRCS_MODPY", | |
], | |
tools = { | |
"go": CONFIG.GO_TOOL, | |
"jq": "//tools:jq|jq", | |
"python": CONFIG.DEFAULT_PYTHON_INTERPRETER, | |
}, | |
post_build = _mod_mod, | |
) | |
return filegroup( | |
name = aggreg_name, | |
deps = [builddeps], | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
import os | |
pkg_dir = os.getcwd()+"/"+os.getenv('PKG_DIR') | |
def remove_prefix(str, prefix): | |
if str.startswith(prefix): | |
return str[len(prefix):] | |
else: | |
return str | |
module = None | |
gomod = open(pkg_dir+'/go.mod') | |
for line in gomod.readlines(): | |
if line.startswith("module"): | |
module = remove_prefix(line, "module").strip() | |
break | |
if not module: | |
die("cannot guess module") | |
moddl = subprocess.check_output("$TOOLS_GO mod download -modcacherw -json | $TOOLS_JQ -r '(.Path + \"@\" + .Version)'", shell=True, cwd=pkg_dir).decode("ascii",errors="ignore").splitlines() | |
modgraph = subprocess.check_output("$TOOLS_GO mod graph", shell=True, cwd=pkg_dir).decode("ascii",errors="ignore").splitlines() | |
depsm = {} | |
depsm[module] = {'version': 'v0.0.0', 'deps': set()} | |
for line in moddl: | |
mod, version = line.split('@') | |
depsm[mod] = {'version': version, 'deps': set()} | |
for line in modgraph: | |
mod_ref, dep_ref = line.split(' ') | |
mod_parts = mod_ref.split('@') | |
mod = mod_parts[0] | |
dep_parts = dep_ref.split('@') | |
dep = dep_parts[0] | |
assert mod in depsm, "Missing entry for mod: "+mod | |
assert dep in depsm, "Missing entry for dep: "+dep | |
entry = depsm[mod] | |
entry['deps'].add(dep) | |
def print_entry(mod, e): | |
version = e['version'] | |
deps = '|'.join(e['deps']) | |
print(f'{mod} {version} {deps}') | |
print_entry(module, depsm[module]) | |
for mod, entry in depsm.items(): | |
if mod == module: | |
continue | |
print_entry(mod, entry) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment