aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorConnor Hindley <conn.hindley@gmail.com>2024-05-03 11:50:28 -0600
committerGitHub <noreply@github.com>2024-05-03 17:50:28 +0000
commitfd77fd0cddd66504eb97e4b152f72f39c183eb17 (patch)
tree9eb258d5574c11790c8af645ad3c718a1c1e5ddc
parent54c451550c520d4a26f491fa5f52a97b2dcfa0d7 (diff)
downloadbazelbuild-rules_go-upstream-master.tar.gz
feat: support syso files in srcs. (#3763)upstream-master
* 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
-rw-r--r--docs/go/core/rules.md2
-rw-r--r--go/private/actions/archive.bzl6
-rw-r--r--go/private/common.bzl8
-rw-r--r--go/private/rules/binary.bzl5
-rw-r--r--go/tools/builders/compilepkg.go9
-rw-r--r--go/tools/builders/filter.go7
6 files changed, 27 insertions, 10 deletions
diff --git a/docs/go/core/rules.md b/docs/go/core/rules.md
index 3dd32e77..1cca1c70 100644
--- a/docs/go/core/rules.md
+++ b/docs/go/core/rules.md
@@ -171,7 +171,7 @@ This builds an executable from a set of source files,
| <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 |
| <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" |
| <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" |
-| <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 | [] |
+| <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 | [] |
| <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" |
| <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 | {} |
diff --git a/go/private/actions/archive.bzl b/go/private/actions/archive.bzl
index 3bf0bffd..c518501d 100644
--- a/go/private/actions/archive.bzl
+++ b/go/private/actions/archive.bzl
@@ -88,7 +88,7 @@ def emit_archive(go, source = None, _recompile_suffix = "", recompile_internal_d
clinkopts = [f for fs in source.clinkopts for f in fs.split(" ")]
cgo = cgo_configure(
go,
- srcs = split.go + split.c + split.asm + split.cxx + split.objc + split.headers,
+ srcs = split.go + split.c + split.asm + split.cxx + split.objc + split.headers + split.syso,
cdeps = source.cdeps,
cppopts = cppopts,
copts = copts,
@@ -101,7 +101,7 @@ def emit_archive(go, source = None, _recompile_suffix = "", recompile_internal_d
runfiles = runfiles.merge(cgo.runfiles)
emit_compilepkg(
go,
- sources = split.go + split.c + split.asm + split.cxx + split.objc + split.headers,
+ sources = split.go + split.c + split.asm + split.cxx + split.objc + split.headers + split.syso,
cover = source.cover,
embedsrcs = source.embedsrcs,
importpath = importpath,
@@ -127,7 +127,7 @@ def emit_archive(go, source = None, _recompile_suffix = "", recompile_internal_d
cgo_deps = depset()
emit_compilepkg(
go,
- sources = split.go + split.c + split.asm + split.cxx + split.objc + split.headers,
+ sources = split.go + split.c + split.asm + split.cxx + split.objc + split.headers + split.syso,
cover = source.cover,
embedsrcs = source.embedsrcs,
importpath = importpath,
diff --git a/go/private/common.bzl b/go/private/common.bzl
index 51056f65..c1eedc04 100644
--- a/go/private/common.bzl
+++ b/go/private/common.bzl
@@ -72,6 +72,10 @@ cgo_exts = [
".mm",
]
+syso_exts = [
+ ".syso",
+]
+
def split_srcs(srcs):
"""Returns a struct of sources, divided by extension."""
sources = struct(
@@ -81,6 +85,7 @@ def split_srcs(srcs):
c = [],
cxx = [],
objc = [],
+ syso = [],
)
ext_pairs = (
(sources.go, go_exts),
@@ -89,6 +94,7 @@ def split_srcs(srcs):
(sources.c, c_exts),
(sources.cxx, cxx_exts),
(sources.objc, objc_exts),
+ (sources.syso, syso_exts),
)
extmap = {}
for outs, exts in ext_pairs:
@@ -106,7 +112,7 @@ def split_srcs(srcs):
def join_srcs(source):
"""Combines source from a split_srcs struct into a single list."""
- return source.go + source.headers + source.asm + source.c + source.cxx + source.objc
+ return source.go + source.headers + source.asm + source.c + source.cxx + source.objc + source.syso
def os_path(ctx, path):
path = str(path) # maybe convert from path type
diff --git a/go/private/rules/binary.bzl b/go/private/rules/binary.bzl
index 40b4011e..40a17f4d 100644
--- a/go/private/rules/binary.bzl
+++ b/go/private/rules/binary.bzl
@@ -18,6 +18,7 @@ load(
"asm_exts",
"cgo_exts",
"go_exts",
+ "syso_exts",
)
load(
"//go/private:context.bzl",
@@ -188,9 +189,9 @@ _go_binary_kwargs = {
"implementation": _go_binary_impl,
"attrs": {
"srcs": attr.label_list(
- allow_files = go_exts + asm_exts + cgo_exts,
+ allow_files = go_exts + asm_exts + cgo_exts + syso_exts,
doc = """The list of Go source files that are compiled to create the package.
- Only `.go` and `.s` files are permitted, unless the `cgo`
+ Only `.go`, `.s`, and `.syso` files are permitted, unless the `cgo`
attribute is set, in which case,
`.c .cc .cpp .cxx .h .hh .hpp .hxx .inc .m .mm`
files are also permitted. Files may be filtered at build time
diff --git a/go/tools/builders/compilepkg.go b/go/tools/builders/compilepkg.go
index 4a1fc5ba..a62192a5 100644
--- a/go/tools/builders/compilepkg.go
+++ b/go/tools/builders/compilepkg.go
@@ -514,8 +514,13 @@ func compileArchive(
}
}
- // Pack .o files into the archive. These may come from cgo generated code,
- // cgo dependencies (cdeps), or assembly.
+ // Windows resource files (.syso) are treated the same as object files.
+ for _, src := range srcs.sysoSrcs {
+ objFiles = append(objFiles, src.filename)
+ }
+
+ // Pack .o and .syso files into the archive. These may come from cgo generated code,
+ // cgo dependencies (cdeps), windows resource file generation, or assembly.
if len(objFiles) > 0 {
if err := appendToArchive(goenv, outLinkObj, objFiles); err != nil {
return err
diff --git a/go/tools/builders/filter.go b/go/tools/builders/filter.go
index 328caac9..be4c8acc 100644
--- a/go/tools/builders/filter.go
+++ b/go/tools/builders/filter.go
@@ -48,6 +48,7 @@ const (
objcxxExt
sExt
hExt
+ sysoExt
)
type fileImport struct {
@@ -62,7 +63,7 @@ type fileEmbed struct {
}
type archiveSrcs struct {
- goSrcs, cSrcs, cxxSrcs, objcSrcs, objcxxSrcs, sSrcs, hSrcs []fileInfo
+ goSrcs, cSrcs, cxxSrcs, objcSrcs, objcxxSrcs, sSrcs, hSrcs, sysoSrcs []fileInfo
}
// filterAndSplitFiles filters files using build constraints and collates
@@ -97,6 +98,8 @@ func filterAndSplitFiles(fileNames []string) (archiveSrcs, error) {
srcs = &res.sSrcs
case hExt:
srcs = &res.hSrcs
+ case sysoExt:
+ srcs = &res.sysoSrcs
}
*srcs = append(*srcs, src)
}
@@ -131,6 +134,8 @@ func readFileInfo(bctx build.Context, input string) (fileInfo, error) {
fi.ext = sExt
case ".h", ".hh", ".hpp", ".hxx":
fi.ext = hExt
+ case ".syso":
+ fi.ext = sysoExt
default:
return fileInfo{}, fmt.Errorf("unrecognized file extension: %s", ext)
}