Skip to content
This repository has been archived by the owner on Oct 13, 2023. It is now read-only.

Commit

Permalink
Merge pull request #3310 from crazy-max/goversioninfo
Browse files Browse the repository at this point in the history
Use goversioninfo to create Windows version info
Upstream-commit: 053056660eed351a77540ac968ac3ee5e927a460
Component: cli
  • Loading branch information
thaJeztah authored Oct 11, 2021
2 parents 203b551 + 7efa067 commit a0ef3fa
Show file tree
Hide file tree
Showing 20 changed files with 154 additions and 88 deletions.
2 changes: 2 additions & 0 deletions components/cli/.dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
.gitignore
appveyor.yml
build
/cli/winresources/versioninfo.json
/cli/winresources/*.syso
/vndr.log
3 changes: 2 additions & 1 deletion components/cli/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
Thumbs.db
.editorconfig
/build/
cli/winresources/rsrc_*.syso
/cli/winresources/versioninfo.json
/cli/winresources/*.syso
/man/man1/
/man/man5/
/man/man8/
Expand Down
12 changes: 11 additions & 1 deletion components/cli/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
ARG BASE_VARIANT=alpine
ARG GO_VERSION=1.16.8
ARG XX_VERSION=1.0.0-rc.2
ARG GOVERSIONINFO_VERSION=v1.3.0

FROM --platform=$BUILDPLATFORM golang:${GO_VERSION}-${BASE_VARIANT} AS gostable
FROM --platform=$BUILDPLATFORM golang:1.17rc1-${BASE_VARIANT} AS golatest
Expand Down Expand Up @@ -47,9 +48,18 @@ ARG GO_STRIP
ARG CGO_ENABLED
# VERSION sets the version for the produced binary
ARG VERSION
RUN --mount=ro --mount=type=cache,target=/root/.cache \
# COMPANY_NAME sets the company that produced the windows binary
ARG COMPANY_NAME
# GOVERSIONINFO_VERSION defines goversioninfo tool version
ARG GOVERSIONINFO_VERSION
RUN --mount=type=cache,target=/root/.cache \
# install goversioninfo tool
GO111MODULE=auto go install github.com/josephspurrier/goversioninfo/cmd/goversioninfo@${GOVERSIONINFO_VERSION}
RUN --mount=type=bind,target=.,ro \
--mount=type=cache,target=/root/.cache \
--mount=from=dockercore/golang-cross:xx-sdk-extras,target=/xx-sdk,src=/xx-sdk \
--mount=type=tmpfs,target=cli/winresources \
# override the default behavior of go with xx-go
xx-go --wrap && \
# export GOCACHE=$(go env GOCACHE)/$(xx-info)$([ -f /etc/alpine-release ] && echo "alpine") && \
TARGET=/out ./scripts/build/binary && \
Expand Down
4 changes: 4 additions & 0 deletions components/cli/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#
# github.com/docker/cli
#

# Sets the name of the company that produced the windows binary.
COMPANY_NAME ?=

all: binary

_:=$(shell ./scripts/warn-outside-container $(MAKECMDGOALS))
Expand Down
16 changes: 0 additions & 16 deletions components/cli/cli/winresources/res_windows.go

This file was deleted.

11 changes: 11 additions & 0 deletions components/cli/cli/winresources/winresources.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Package winresources is used to embed Windows resources into docker.exe.
//
// These resources are used to provide:
// * Version information
// * An icon
// * A Windows manifest declaring Windows version support
//
// The resource object files are generated when building with goversioninfo
// in scripts/build/binary and are located in cmd/docker/winresources.
// This occurs automatically when you build against Windows OS.
package winresources
3 changes: 0 additions & 3 deletions components/cli/cmd/docker/docker_windows.go

This file was deleted.

8 changes: 8 additions & 0 deletions components/cli/cmd/docker/docker_windows_386.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//go:build windows && 386
// +build windows,386

//go:generate goversioninfo -o=../../cli/winresources/resource.syso -icon=winresources/docker.ico -manifest=winresources/docker.exe.manifest ../../cli/winresources/versioninfo.json

package main

import _ "github.com/docker/cli/cli/winresources"
8 changes: 8 additions & 0 deletions components/cli/cmd/docker/docker_windows_amd64.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//go:build windows && amd64
// +build windows,amd64

//go:generate goversioninfo -64=true -o=../../cli/winresources/resource.syso -icon=winresources/docker.ico -manifest=winresources/docker.exe.manifest ../../cli/winresources/versioninfo.json

package main

import _ "github.com/docker/cli/cli/winresources"
8 changes: 8 additions & 0 deletions components/cli/cmd/docker/docker_windows_arm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//go:build windows && arm
// +build windows,arm

//go:generate goversioninfo -arm=true -o=../../cli/winresources/resource.syso -icon=winresources/docker.ico -manifest=winresources/docker.exe.manifest ../../cli/winresources/versioninfo.json

package main

import _ "github.com/docker/cli/cli/winresources"
8 changes: 8 additions & 0 deletions components/cli/cmd/docker/docker_windows_arm64.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//go:build windows && arm64
// +build windows,arm64

//go:generate goversioninfo -arm=true -64=true -o=../../cli/winresources/resource.syso -icon=winresources/docker.ico -manifest=winresources/docker.exe.manifest ../../cli/winresources/versioninfo.json

package main

import _ "github.com/docker/cli/cli/winresources"
6 changes: 6 additions & 0 deletions components/cli/docker-bake.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ variable "STRIP_TARGET" {
default = ""
}

# Sets the name of the company that produced the windows binary.
variable "COMPANY_NAME" {
default = ""
}

group "default" {
targets = ["binary"]
}
Expand All @@ -21,6 +26,7 @@ target "binary" {
args = {
BASE_VARIANT = USE_GLIBC != "" ? "buster" : "alpine"
VERSION = VERSION
COMPANY_NAME = COMPANY_NAME
GO_STRIP = STRIP_TARGET
}
}
Expand Down
9 changes: 6 additions & 3 deletions components/cli/docker.Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ DOCKER_CLI_MOUNTS ?= -v "$(CURDIR)":/go/src/github.com/docker/cli
DOCKER_CLI_CONTAINER_NAME ?=
DOCKER_CLI_GO_BUILD_CACHE ?= y

# Sets the name of the company that produced the windows binary.
COMPANY_NAME ?=

DEV_DOCKER_IMAGE_NAME = docker-cli-dev$(IMAGE_TAG)
BINARY_NATIVE_IMAGE_NAME = docker-cli-native$(IMAGE_TAG)
CROSS_IMAGE_NAME = docker-cli-cross$(IMAGE_TAG)
Expand Down Expand Up @@ -44,7 +47,7 @@ DOCKER_RUN := docker run --rm $(ENVVARS) $(DOCKER_CLI_MOUNTS) $(DOCKER_RUN_NAME_

.PHONY: binary
binary:
docker buildx bake binary
COMPANY_NAME=$(COMPANY_NAME) docker buildx bake binary

build: binary ## alias for binary

Expand All @@ -65,7 +68,7 @@ test: test-unit test-e2e

.PHONY: cross
cross:
docker buildx bake cross
COMPANY_NAME=$(COMPANY_NAME) docker buildx bake cross

.PHONY: plugins-windows
plugins-windows: build_cross_image ## build the example CLI plugins for Windows
Expand All @@ -77,7 +80,7 @@ plugins-osx: build_cross_image ## build the example CLI plugins for macOS

.PHONY: dynbinary
dynbinary: ## build dynamically linked binary
USE_GLIBC=1 docker buildx bake dynbinary
USE_GLIBC=1 COMPANY_NAME=$(COMPANY_NAME) docker buildx bake dynbinary

.PHONY: dev
dev: build_docker_image ## start a build container in interactive mode for in-container development
Expand Down
12 changes: 10 additions & 2 deletions components/cli/dockerfiles/Dockerfile.dev
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ RUN --mount=type=cache,target=/root/.cache/go-build \
--mount=type=tmpfs,target=/go/src/ \
GO111MODULE=on go install github.com/LK4D4/vndr@${VNDR_VERSION}

FROM golang AS goversioninfo
ARG GOVERSIONINFO_VERSION=v1.3.0
RUN --mount=type=cache,target=/root/.cache/go-build \
--mount=type=cache,target=/go/pkg/mod \
--mount=type=tmpfs,target=/go/src/ \
GO111MODULE=on go install github.com/josephspurrier/goversioninfo/cmd/goversioninfo@${GOVERSIONINFO_VERSION}

FROM golang AS dev
RUN apk add --no-cache \
bash \
Expand All @@ -32,8 +39,9 @@ CMD bash
ENV DISABLE_WARN_OUTSIDE_CONTAINER=1
ENV PATH=$PATH:/go/src/github.com/docker/cli/build

COPY --from=vndr /go/bin/* /go/bin/
COPY --from=gotestsum /go/bin/* /go/bin/
COPY --from=vndr /go/bin/* /go/bin/
COPY --from=gotestsum /go/bin/* /go/bin/
COPY --from=goversioninfo /go/bin/* /go/bin/

WORKDIR /go/src/github.com/docker/cli
ENV GO111MODULE=auto
Expand Down
31 changes: 10 additions & 21 deletions components/cli/scripts/build/binary
Original file line number Diff line number Diff line change
Expand Up @@ -54,30 +54,19 @@ if [ -n "$GO_STRIP" ]; then
LDFLAGS="$LDFLAGS -s -w"
fi

if [ "$(go env GOOS)" = "windows" ]; then
: "${WINDRES=$($(go env CC) --print-prog-name=windres)}"
if [ -z "$WINDRES" ]; then
>&2 echo "Empty WINDRES detected, skipping manifesting binary"
else
# Generate a Windows file version of the form major,minor,patch,build
VERSION_QUAD=$(printf "%s" "$VERSION" | sed -re 's/^([0-9.]*).*$/\1/' | tr . , | sed -re 's/,$//' | sed -re 's/^[0-9]+$/\0,0/' | sed -re 's/^[0-9]+,[0-9]+$/\0,0/' | sed -re 's/^[0-9]+,[0-9]+,[0-9]+$/\0,0/')

set --
[ -n "$VERSION" ] && set -- "$@" -D "DOCKER_VERSION=\"$VERSION\""
[ -n "$VERSION_QUAD" ] && set -- "$@" -D "DOCKER_VERSION_QUAD=$VERSION_QUAD"
[ -n "$GITCOMMIT" ] && set -- "$@" -D "DOCKER_COMMIT=\"$GITCOMMIT\""

target="$(dirname "$0")/../../cli/winresources/rsrc_$(go env GOARCH).syso"
mkdir -p "$(dirname "${target}")"
"$WINDRES" -i "$(dirname "$0")/../winresources/docker.rc" -o "$target" --use-temp-file "$@"
echo "package winresources" > "$(dirname "${target}")/stub_windows.go"
fi
fi

echo "Building $GO_LINKMODE $(basename "${TARGET}")"

export GO111MODULE=auto

go build -o "${TARGET}" -tags "${GO_BUILDTAGS}" --ldflags "${LDFLAGS}" ${GO_BUILDMODE} "${SOURCE}"
if [ "$(go env GOOS)" = "windows" ]; then
if [ ! -x "$(command -v goversioninfo)" ]; then
>&2 echo "goversioninfo not found, skipping manifesting binary"
else
./scripts/build/mkversioninfo
(set -x ; go generate -v "${SOURCE}")
fi
fi

(set -x ; go build -o "${TARGET}" -tags "${GO_BUILDTAGS}" --ldflags "${LDFLAGS}" ${GO_BUILDMODE} "${SOURCE}")

ln -sf "$(basename "${TARGET}")" "$(dirname "${TARGET}")/docker"
60 changes: 60 additions & 0 deletions components/cli/scripts/build/mkversioninfo
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/usr/bin/env sh
set -eu

: "${COMPANY_NAME=}"

. ./scripts/build/.variables

# Create version quad for Windows of the form major.minor.patch.build
VERSION_QUAD=$(printf "%s" "$VERSION" | sed -re 's/^([0-9.]*).*$/\1/' | sed -re 's/\.$//' | sed -re 's/^[0-9]+$/\0\.0/' | sed -re 's/^[0-9]+\.[0-9]+$/\0\.0/' | sed -re 's/^[0-9]+\.[0-9]+\.[0-9]+$/\0\.0/')

# Generate versioninfo.json to be able to create a syso file which contains
# Microsoft Windows Version Information and an icon using goversioninfo.
# https://docs.microsoft.com/en-us/windows/win32/menurc/stringfileinfo-block
# https://github.com/josephspurrier/goversioninfo/blob/master/testdata/resource/versioninfo.json
cat > ./cli/winresources/versioninfo.json <<EOL
{
"FixedFileInfo":
{
"FileVersion": {
"Major": $(echo "$VERSION_QUAD" | cut -d. -f1),
"Minor": $(echo "$VERSION_QUAD" | cut -d. -f2),
"Patch": $(echo "$VERSION_QUAD" | cut -d. -f3),
"Build": $(echo "$VERSION_QUAD" | cut -d. -f4)
},
"FileFlagsMask": "3f",
"FileFlags ": "00",
"FileOS": "040004",
"FileType": "01",
"FileSubType": "00"
},
"StringFileInfo":
{
"Comments": "",
"CompanyName": "${COMPANY_NAME}",
"FileDescription": "Docker Client",
"FileVersion": "${VERSION_QUAD}",
"InternalName": "",
"LegalCopyright": "Copyright © 2015-$(date +'%Y') Docker Inc.",
"LegalTrademarks": "",
"OriginalFilename": "$(basename "${TARGET}")",
"PrivateBuild": "",
"ProductName": "Docker Client",
"ProductVersion": "${VERSION}",
"SpecialBuild": "${GITCOMMIT}"
},
"VarFileInfo":
{
"Translation": {
"LangID": "0409",
"CharsetID": "04B0"
}
}
}
EOL
(set -x ; cat ./cli/winresources/versioninfo.json)

# Create winresources package stub if removed while using tmpfs in Dockerfile
if [ ! -f "./cli/winresources/winresources.go" ]; then
echo "package winresources" > "./cli/winresources/winresources.go"
fi
38 changes: 0 additions & 38 deletions components/cli/scripts/winresources/common.rc

This file was deleted.

3 changes: 0 additions & 3 deletions components/cli/scripts/winresources/docker.rc

This file was deleted.

0 comments on commit a0ef3fa

Please sign in to comment.