Skip to content

Commit 836cbd4

Browse files
committed
Set LD flags on CGO rule like go build odes
1 parent 6432730 commit 836cbd4

File tree

1 file changed

+27
-13
lines changed

1 file changed

+27
-13
lines changed

build_defs/cgo.build_defs

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,7 @@ def cgo_library(name:str, srcs:list=[], resources:list=None, go_srcs:list=[], c_
7474
'c': [subdir2 + src.replace('.go', '.cgo2.c') for src in file_srcs] + [subdir2 + '_cgo_export.c'],
7575
'h': [subdir2 + '_cgo_export.h'],
7676
},
77-
cmd = ' && '.join([
78-
(f'OUT_DIR="$TMP_DIR/{subdir}"') if subdir else 'OUT_DIR="$TMP_DIR"',
79-
'mkdir -p "$OUT_DIR"',
80-
'cd $PKG_DIR/' + subdir,
81-
f'$TOOL tool cgo -objdir "$OUT_DIR" -importpath {import_path} -- {compiler_flags_cmd} *.go',
82-
# Remove the .o file which BSD sed gets upset about in the next command
83-
'rm -f "$OUT_DIR"/_cgo_.o "$OUT_DIR"/_cgo_main.c',
84-
# cgo leaves absolute paths in these files which we must get rid of :(
85-
'find "$OUT_DIR" -type f -maxdepth 1 | xargs sed -i -e "s|"$TMP_DIR"/||g"',
86-
'cd "$TMP_DIR"',
87-
f'ls {subdir2}*.c {subdir2}*.go',
88-
]),
77+
cmd = _cgo_cmd(subdir, subdir2, import_path, compiler_flags_cmd, " ".join([f"'{flag}'" for flag in linker_flags])),
8978
deps = deps,
9079
tools = [CONFIG.GO.GO_TOOL],
9180
post_build = post_build if file_srcs != srcs else None,
@@ -100,7 +89,7 @@ def cgo_library(name:str, srcs:list=[], resources:list=None, go_srcs:list=[], c_
10089
compiler_flags = compiler_flags + [
10190
'-Wno-error',
10291
'-Wno-unused-parameter', # Generated code doesn't compile clean
103-
],
92+
] + linker_flags,
10493
pkg_config_libs = pkg_config,
10594
test_only = test_only,
10695
deps = deps,
@@ -144,3 +133,28 @@ def cgo_library(name:str, srcs:list=[], resources:list=None, go_srcs:list=[], c_
144133
deps = deps,
145134
exported_deps=[import_config],
146135
)
136+
137+
def _cgo_cmd(subdir, subdir2, import_path, compiler_flags_cmd, linker_flags=None):
138+
linker_flags = linker_flags or ""
139+
return ' && '.join([
140+
f'export CGO_LDFLAGS="{linker_flags}"',
141+
f'OUT_DIR="$TMP_DIR/{subdir}"' if subdir else 'OUT_DIR="$TMP_DIR"',
142+
'mkdir -p "$OUT_DIR"',
143+
'cd $PKG_DIR/' + subdir,
144+
f'$TOOL tool cgo -objdir "$OUT_DIR" -importpath {import_path} -- {compiler_flags_cmd} *.go',
145+
# Remove the .o file which BSD sed gets upset about in the next command
146+
'rm -f "$OUT_DIR"/_cgo_.o "$OUT_DIR"/_cgo_main.c',
147+
# cgo leaves absolute paths in these files which we must get rid of :(
148+
'find "$OUT_DIR" -maxdepth 1 -type f | xargs sed -i -e "s|"$TMP_DIR"/||g"',
149+
'cd "$TMP_DIR"',
150+
f'ls {subdir2}*.c {subdir2}*.go',
151+
])
152+
153+
def _collect_cgo_linker_flags(subdir, subdir2, import_path, compiler_flags_cmd):
154+
"""Returns a pre-build function to apply transitive linker flags to a go_binary rule."""
155+
def collect_linker_flags(name):
156+
ldflags, _ = _get_ldflags_and_pkgconfig(name)
157+
if ldflags:
158+
set_command(name, _cgo_cmd(subdir, subdir2, import_path, compiler_flags_cmd))
159+
return collect_linker_flags
160+

0 commit comments

Comments
 (0)