Skip to content

Commit fd77fd0

Browse files
authored
feat: support syso files in srcs. (#3763)
* feat: support syso files in srcs. This change adds support for including .syso files in srcs, which enables embedding version and icon info into windows executables. Fixes #2757 * go:generate doesnt work with empty go.mod * remove test, combine syso and o file addToArchive, update comments * update docs
1 parent 54c4515 commit fd77fd0

File tree

6 files changed

+27
-10
lines changed

6 files changed

+27
-10
lines changed

docs/go/core/rules.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ This builds an executable from a set of source files,
171171
| <a id="go_binary-pgoprofile"></a>pgoprofile | Provides a pprof file to be used for profile guided optimization when compiling go targets. A pprof file can also be provided via <code>--@io_bazel_rules_go//go/config:pgoprofile=&lt;label of a pprof file&gt;</code>. Profile guided optimization is only supported on go 1.20+. See https://go.dev/doc/pgo for more information. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | //go/config:empty |
172172
| <a id="go_binary-pure"></a>pure | Controls whether cgo source code and dependencies are compiled and linked, similar to setting <code>CGO_ENABLED</code>. May be one of <code>on</code>, <code>off</code>, or <code>auto</code>. If <code>auto</code>, pure mode is enabled when no C/C++ toolchain is configured or when cross-compiling. It's usually better to control this on the command line with <code>--@io_bazel_rules_go//go/config:pure</code>. See [mode attributes], specifically [pure]. | String | optional | "auto" |
173173
| <a id="go_binary-race"></a>race | Controls whether code is instrumented for race detection. May be one of <code>on</code>, <code>off</code>, or <code>auto</code>. Not available when cgo is disabled. In most cases, it's better to control this on the command line with <code>--@io_bazel_rules_go//go/config:race</code>. See [mode attributes], specifically [race]. | String | optional | "auto" |
174-
| <a id="go_binary-srcs"></a>srcs | The list of Go source files that are compiled to create the package. Only <code>.go</code> and <code>.s</code> files are permitted, unless the <code>cgo</code> attribute is set, in which case, <code>.c .cc .cpp .cxx .h .hh .hpp .hxx .inc .m .mm</code> files are also permitted. Files may be filtered at build time using Go [build constraints]. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | [] |
174+
| <a id="go_binary-srcs"></a>srcs | The list of Go source files that are compiled to create the package. Only <code>.go</code>, <code>.s</code>, and <code>.syso</code> files are permitted, unless the <code>cgo</code> attribute is set, in which case, <code>.c .cc .cpp .cxx .h .hh .hpp .hxx .inc .m .mm</code> files are also permitted. Files may be filtered at build time using Go [build constraints]. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | [] |
175175
| <a id="go_binary-static"></a>static | Controls whether a binary is statically linked. May be one of <code>on</code>, <code>off</code>, or <code>auto</code>. Not available on all platforms or in all modes. It's usually better to control this on the command line with <code>--@io_bazel_rules_go//go/config:static</code>. See [mode attributes], specifically [static]. | String | optional | "auto" |
176176
| <a id="go_binary-x_defs"></a>x_defs | Map of defines to add to the go link command. See [Defines and stamping] for examples of how to use these. | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | optional | {} |
177177

go/private/actions/archive.bzl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def emit_archive(go, source = None, _recompile_suffix = "", recompile_internal_d
8888
clinkopts = [f for fs in source.clinkopts for f in fs.split(" ")]
8989
cgo = cgo_configure(
9090
go,
91-
srcs = split.go + split.c + split.asm + split.cxx + split.objc + split.headers,
91+
srcs = split.go + split.c + split.asm + split.cxx + split.objc + split.headers + split.syso,
9292
cdeps = source.cdeps,
9393
cppopts = cppopts,
9494
copts = copts,
@@ -101,7 +101,7 @@ def emit_archive(go, source = None, _recompile_suffix = "", recompile_internal_d
101101
runfiles = runfiles.merge(cgo.runfiles)
102102
emit_compilepkg(
103103
go,
104-
sources = split.go + split.c + split.asm + split.cxx + split.objc + split.headers,
104+
sources = split.go + split.c + split.asm + split.cxx + split.objc + split.headers + split.syso,
105105
cover = source.cover,
106106
embedsrcs = source.embedsrcs,
107107
importpath = importpath,
@@ -127,7 +127,7 @@ def emit_archive(go, source = None, _recompile_suffix = "", recompile_internal_d
127127
cgo_deps = depset()
128128
emit_compilepkg(
129129
go,
130-
sources = split.go + split.c + split.asm + split.cxx + split.objc + split.headers,
130+
sources = split.go + split.c + split.asm + split.cxx + split.objc + split.headers + split.syso,
131131
cover = source.cover,
132132
embedsrcs = source.embedsrcs,
133133
importpath = importpath,

go/private/common.bzl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ cgo_exts = [
7272
".mm",
7373
]
7474

75+
syso_exts = [
76+
".syso",
77+
]
78+
7579
def split_srcs(srcs):
7680
"""Returns a struct of sources, divided by extension."""
7781
sources = struct(
@@ -81,6 +85,7 @@ def split_srcs(srcs):
8185
c = [],
8286
cxx = [],
8387
objc = [],
88+
syso = [],
8489
)
8590
ext_pairs = (
8691
(sources.go, go_exts),
@@ -89,6 +94,7 @@ def split_srcs(srcs):
8994
(sources.c, c_exts),
9095
(sources.cxx, cxx_exts),
9196
(sources.objc, objc_exts),
97+
(sources.syso, syso_exts),
9298
)
9399
extmap = {}
94100
for outs, exts in ext_pairs:
@@ -106,7 +112,7 @@ def split_srcs(srcs):
106112

107113
def join_srcs(source):
108114
"""Combines source from a split_srcs struct into a single list."""
109-
return source.go + source.headers + source.asm + source.c + source.cxx + source.objc
115+
return source.go + source.headers + source.asm + source.c + source.cxx + source.objc + source.syso
110116

111117
def os_path(ctx, path):
112118
path = str(path) # maybe convert from path type

go/private/rules/binary.bzl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ load(
1818
"asm_exts",
1919
"cgo_exts",
2020
"go_exts",
21+
"syso_exts",
2122
)
2223
load(
2324
"//go/private:context.bzl",
@@ -188,9 +189,9 @@ _go_binary_kwargs = {
188189
"implementation": _go_binary_impl,
189190
"attrs": {
190191
"srcs": attr.label_list(
191-
allow_files = go_exts + asm_exts + cgo_exts,
192+
allow_files = go_exts + asm_exts + cgo_exts + syso_exts,
192193
doc = """The list of Go source files that are compiled to create the package.
193-
Only `.go` and `.s` files are permitted, unless the `cgo`
194+
Only `.go`, `.s`, and `.syso` files are permitted, unless the `cgo`
194195
attribute is set, in which case,
195196
`.c .cc .cpp .cxx .h .hh .hpp .hxx .inc .m .mm`
196197
files are also permitted. Files may be filtered at build time

go/tools/builders/compilepkg.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -514,8 +514,13 @@ func compileArchive(
514514
}
515515
}
516516

517-
// Pack .o files into the archive. These may come from cgo generated code,
518-
// cgo dependencies (cdeps), or assembly.
517+
// Windows resource files (.syso) are treated the same as object files.
518+
for _, src := range srcs.sysoSrcs {
519+
objFiles = append(objFiles, src.filename)
520+
}
521+
522+
// Pack .o and .syso files into the archive. These may come from cgo generated code,
523+
// cgo dependencies (cdeps), windows resource file generation, or assembly.
519524
if len(objFiles) > 0 {
520525
if err := appendToArchive(goenv, outLinkObj, objFiles); err != nil {
521526
return err

go/tools/builders/filter.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ const (
4848
objcxxExt
4949
sExt
5050
hExt
51+
sysoExt
5152
)
5253

5354
type fileImport struct {
@@ -62,7 +63,7 @@ type fileEmbed struct {
6263
}
6364

6465
type archiveSrcs struct {
65-
goSrcs, cSrcs, cxxSrcs, objcSrcs, objcxxSrcs, sSrcs, hSrcs []fileInfo
66+
goSrcs, cSrcs, cxxSrcs, objcSrcs, objcxxSrcs, sSrcs, hSrcs, sysoSrcs []fileInfo
6667
}
6768

6869
// filterAndSplitFiles filters files using build constraints and collates
@@ -97,6 +98,8 @@ func filterAndSplitFiles(fileNames []string) (archiveSrcs, error) {
9798
srcs = &res.sSrcs
9899
case hExt:
99100
srcs = &res.hSrcs
101+
case sysoExt:
102+
srcs = &res.sysoSrcs
100103
}
101104
*srcs = append(*srcs, src)
102105
}
@@ -131,6 +134,8 @@ func readFileInfo(bctx build.Context, input string) (fileInfo, error) {
131134
fi.ext = sExt
132135
case ".h", ".hh", ".hpp", ".hxx":
133136
fi.ext = hExt
137+
case ".syso":
138+
fi.ext = sysoExt
134139
default:
135140
return fileInfo{}, fmt.Errorf("unrecognized file extension: %s", ext)
136141
}

0 commit comments

Comments
 (0)