Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cherry-pick #16048 to 7.x: Add metadata to Windows exe files #16944

Merged
merged 2 commits into from
Mar 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
56 changes: 56 additions & 0 deletions NOTICE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,34 @@ 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
Version: v0.8.0
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 +4479,34 @@ 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
Revision: 14b0ab84c6ca
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