Skip to content

Commit

Permalink
Add metadata to Windows exe files (elastic#16048)
Browse files Browse the repository at this point in the history
This adds metadata to the Windows .exe files that the build produces. This makes our binaries a little more friendly to automation on Windows because you can check the version programmatically with ease. It also makes is easy for end user to inspect some details about the file from the file properties dialog in Windows.

Closes elastic#15232

For example:

```
PS C:\vagrant\winlogbeat > (Get-Info .\winlogbeat.exe).VersionInfo | Format-List

OriginalFilename  : winlogbeat.exe
FileDescription   : Winlogbeat ships Windows event logs to Elasticsearch or Logstash.
ProductName       : Winlogbeat
Comments          : commit=8d6cf58f347579188d707421da6b70b2f66701ea
CompanyName       : Elastic
FileName          : C:\vagrant\winlogbeat\winlogbeat.exe
FileVersion       : 8.0.0
ProductVersion    : 8.0.0
IsDebug           : False
IsPatched         : False
IsPreRelease      : False
IsPrivateBuild    : False
IsSpecialBuild    : False
Language          : Language Neutral
LegalCopyright    : Copyright Elastic, License ASL 2.0
LegalTrademarks   :
PrivateBuild      :
SpecialBuild      :
FileVersionRaw    : 8.0.0.0
ProductVersionRaw : 8.0.0.0
```

(cherry picked from commit 695b167)
  • Loading branch information
andrewkroh committed Mar 16, 2020
1 parent d8cd2ff commit 288faf3
Show file tree
Hide file tree
Showing 24 changed files with 2,028 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Add `aws_ec2` provider for autodiscover. {issue}12518[12518] {pull}14823[14823]
- Add monitoring variable `libbeat.config.scans` to distinguish scans of the configuration directory from actual reloads of its contents. {pull}16440[16440]
- Add support for multiple password in redis output. {issue}16058[16058] {pull}16206[16206]
- Windows .exe files now have embedded file version info. {issue}15232[15232]t
- Remove experimental flag from `setup.template.append_fields` {pull}16576[16576]
- Add `add_cloudfoundry_metadata` processor to annotate events with Cloud Foundry application data. {pull}16621[16621]
- Add `translate_sid` processor on Windows for converting Windows security identifier (SID) values to names. {issue}7451[7451] {pull}16013[16013]
Expand Down
54 changes: 54 additions & 0 deletions NOTICE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,33 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

--------------------------------------------------------------------
Dependency: github.com/akavel/rsrc
License type (autodetected): MIT
./vendor/github.com/akavel/rsrc/LICENSE.txt:
--------------------------------------------------------------------
The MIT License (MIT)

Copyright (c) 2013-2017 The rsrc Authors.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

--------------------------------------------------------------------
Dependency: github.com/andrewkroh/sys
Revision: 287798fe3e43
Expand Down Expand Up @@ -4451,6 +4478,33 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

--------------------------------------------------------------------
Dependency: github.com/josephspurrier/goversioninfo
License type (autodetected): MIT
./vendor/github.com/josephspurrier/goversioninfo/LICENSE:
--------------------------------------------------------------------
The MIT License (MIT)

Copyright (c) 2016 Joseph Spurrier

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

--------------------------------------------------------------------
Dependency: github.com/jpillora/backoff
Version: v1.0.0
Expand Down
79 changes: 70 additions & 9 deletions dev-tools/mage/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,24 @@ import (
"path/filepath"
"strings"

"github.com/josephspurrier/goversioninfo"
"github.com/magefile/mage/sh"
"github.com/pkg/errors"
)

// BuildArgs are the arguments used for the "build" target and they define how
// "go build" is invoked.
type BuildArgs struct {
Name string // Name of binary. (On Windows '.exe' is appended.)
InputFiles []string
OutputDir string
CGO bool
Static bool
Env map[string]string
LDFlags []string
Vars map[string]string // Vars that are passed as -X key=value with the ldflags.
ExtraFlags []string
Name string // Name of binary. (On Windows '.exe' is appended.)
InputFiles []string
OutputDir string
CGO bool
Static bool
Env map[string]string
LDFlags []string
Vars map[string]string // Vars that are passed as -X key=value with the ldflags.
ExtraFlags []string
WinMetadata bool // Add resource metadata to Windows binaries (like add the version number to the .exe properties).
}

// DefaultBuildArgs returns the default BuildArgs for use in builds.
Expand All @@ -55,6 +57,7 @@ func DefaultBuildArgs() BuildArgs {
"github.com/elastic/beats/libbeat/version.buildTime": "{{ date }}",
"github.com/elastic/beats/libbeat/version.commit": "{{ commit }}",
},
WinMetadata: true,
}
if versionQualified {
args.Vars["github.com/elastic/beats/libbeat/version.qualifier"] = "{{ .Qualifier }}"
Expand Down Expand Up @@ -150,6 +153,64 @@ func Build(params BuildArgs) error {
args = append(args, params.InputFiles...)
}

if GOOS == "windows" && params.WinMetadata {
log.Println("Generating a .syso containing Windows file metadata.")
syso, err := MakeWindowsSysoFile()
if err != nil {
return errors.Wrap(err, "failed generating Windows .syso metadata file")
}
defer os.Remove(syso)
}

log.Println("Adding build environment vars:", env)
return sh.RunWith(env, "go", args...)
}

// MakeWindowsSysoFile generates a .syso file containing metadata about the
// executable file like vendor, version, copyright. The linker automatically
// discovers the .syso file and incorporates it into the Windows exe. This
// allows users to view metadata about the exe in the Details tab of the file
// properties viewer.
func MakeWindowsSysoFile() (string, error) {
version, err := BeatQualifiedVersion()
if err != nil {
return "", err
}

commit, err := CommitHash()
if err != nil {
return "", err
}

major, minor, patch, err := ParseVersion(version)
if err != nil {
return "", err
}
fileVersion := goversioninfo.FileVersion{Major: major, Minor: minor, Patch: patch}

vi := &goversioninfo.VersionInfo{
FixedFileInfo: goversioninfo.FixedFileInfo{
FileVersion: fileVersion,
ProductVersion: fileVersion,
FileType: "01", // Application
},
StringFileInfo: goversioninfo.StringFileInfo{
CompanyName: BeatVendor,
ProductName: strings.Title(BeatName),
ProductVersion: version,
FileVersion: version,
FileDescription: BeatDescription,
OriginalFilename: BeatName + ".exe",
LegalCopyright: "Copyright " + BeatVendor + ", License " + BeatLicense,
Comments: "commit=" + commit,
},
}

vi.Build()
vi.Walk()
sysoFile := BeatName + "_windows_" + GOARCH + ".syso"
if err = vi.WriteSyso(sysoFile, GOARCH); err != nil {
return "", errors.Wrap(err, "failed to generate syso file with Windows metadata")
}
return sysoFile, nil
}
22 changes: 22 additions & 0 deletions dev-tools/mage/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -781,3 +781,25 @@ func listModuleDir(modpath string) (string, error) {
}
return gotool.ListModuleCacheDir(modpath)
}

var parseVersionRegex = regexp.MustCompile(`(?m)^[^\d]*(?P<major>\d)+\.(?P<minor>\d)+(?:\.(?P<patch>\d)+.*)?$`)

// ParseVersion extracts the major, minor, and optional patch number from a
// version string.
func ParseVersion(version string) (major, minor, patch int, err error) {
names := parseVersionRegex.SubexpNames()
matches := parseVersionRegex.FindStringSubmatch(version)
if len(matches) == 0 {
err = errors.Errorf("failed to parse version '%v'", version)
return
}

data := map[string]string{}
for i, match := range matches {
data[names[i]] = match
}
major, _ = strconv.Atoi(data["major"])
minor, _ = strconv.Atoi(data["minor"])
patch, _ = strconv.Atoi(data["patch"])
return
}
47 changes: 47 additions & 0 deletions dev-tools/mage/common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package mage

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestParseVersion(t *testing.T) {
var tests = []struct {
Version string
Major, Minor, Patch int
}{
{"v1.2.3", 1, 2, 3},
{"1.2.3", 1, 2, 3},
{"1.2.3-SNAPSHOT", 1, 2, 3},
{"1.2.3rc1", 1, 2, 3},
{"1.2", 1, 2, 0},
}

for _, tc := range tests {
major, minor, patch, err := ParseVersion(tc.Version)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, tc.Major, major)
assert.Equal(t, tc.Minor, minor)
assert.Equal(t, tc.Patch, patch)
}
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ require (
github.com/Shopify/sarama v0.0.0-00010101000000-000000000000
github.com/StackExchange/wmi v0.0.0-20170221213301-9f32b5905fd6
github.com/aerospike/aerospike-client-go v1.27.1-0.20170612174108-0f3b54da6bdc
github.com/akavel/rsrc v0.8.0 // indirect
github.com/andrewkroh/sys v0.0.0-20151128191922-287798fe3e43
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5
github.com/aws/aws-lambda-go v1.6.0
Expand Down Expand Up @@ -93,6 +94,7 @@ require (
github.com/jcmturner/gofork v1.0.0 // indirect
github.com/jmoiron/sqlx v1.2.1-0.20190826204134-d7d95172beb5
github.com/joeshaw/multierror v0.0.0-20140124173710-69b34d4ec901
github.com/josephspurrier/goversioninfo v0.0.0-20200309025242-14b0ab84c6ca
github.com/jpillora/backoff v1.0.0 // indirect
github.com/jstemmer/go-junit-report v0.9.1
github.com/klauspost/compress v1.9.3-0.20191122130757-c099ac9f21dd // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ github.com/adriansr/fsnotify v0.0.0-20180417234312-c9bbe1f46f1d h1:g0M6kedfjDpyA
github.com/adriansr/fsnotify v0.0.0-20180417234312-c9bbe1f46f1d/go.mod h1:VykaKG/ofkKje+MSvqjrDsz1wfyHIvEVFljhq2EOZ4g=
github.com/aerospike/aerospike-client-go v1.27.1-0.20170612174108-0f3b54da6bdc h1:9iW/Fbn/R/nyUOiqo6AgwBe8uirqUIoTGF3vKG8qjoc=
github.com/aerospike/aerospike-client-go v1.27.1-0.20170612174108-0f3b54da6bdc/go.mod h1:zj8LBEnWBDOVEIJt8LvaRvDG5ARAoa5dBeHaB472NRc=
github.com/akavel/rsrc v0.8.0 h1:zjWn7ukO9Kc5Q62DOJCcxGpXC18RawVtYAGdz2aLlfw=
github.com/akavel/rsrc v0.8.0/go.mod h1:uLoCtb9J+EyAqh+26kdrTgmzRBFPGOolLWKpdxkKq+c=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
Expand Down Expand Up @@ -407,6 +409,8 @@ github.com/joeshaw/multierror v0.0.0-20140124173710-69b34d4ec901 h1:rp+c0RAYOWj8
github.com/joeshaw/multierror v0.0.0-20140124173710-69b34d4ec901/go.mod h1:Z86h9688Y0wesXCyonoVr47MasHilkuLMqGhRZ4Hpak=
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
github.com/josephspurrier/goversioninfo v0.0.0-20200309025242-14b0ab84c6ca h1:ozPUX9TKQZVek4lZWYRsQo7uS8vJ+q4OOHvRhHiCLfU=
github.com/josephspurrier/goversioninfo v0.0.0-20200309025242-14b0ab84c6ca/go.mod h1:eJTEwMjXb7kZ633hO3Ln9mBUCOjX2+FlTljvpl9SYdE=
github.com/jpillora/backoff v0.0.0-20180909062703-3050d21c67d7/go.mod h1:2iMrUgbbvHEiQClaW2NsSzMyGHqN+rDFqY705q49KG0=
github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2EA=
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
Expand Down
3 changes: 3 additions & 0 deletions vendor/github.com/akavel/rsrc/AUTHORS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions vendor/github.com/akavel/rsrc/LICENSE.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions vendor/github.com/akavel/rsrc/binutil/plain.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions vendor/github.com/akavel/rsrc/binutil/sizedfile.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 288faf3

Please sign in to comment.