瀏覽代碼

use go-winres for windows build and cleanup autogen and winresources

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
CrazyMax 3 年之前
父節點
當前提交
1efda78f2b
共有 10 個文件被更改,包括 143 次插入86 次删除
  1. 0 3
      .gitignore
  2. 19 1
      Dockerfile.windows
  3. 3 10
      Makefile
  4. 0 1
      cmd/dockerd/docker_windows.go
  5. 0 4
      dockerversion/version_lib.go
  6. 0 9
      hack/ci/windows.ps1
  7. 15 7
      hack/make.ps1
  8. 106 43
      hack/make/.go-autogen.ps1
  9. 0 4
      vendor.mod
  10. 0 4
      vendor/modules.txt

+ 0 - 3
.gitignore

@@ -13,7 +13,6 @@ test.main
 .editorconfig
 .gopath/
 .go-pkg-cache/
-autogen/
 bundles/
 cli/winresources/**/winres.json
 cli/winresources/**/*.syso
@@ -26,5 +25,3 @@ junit-report.xml
 
 # top-level go.mod is not meant to be checked in
 /go.mod
-# workaround go.mod for autogen/winresources/dockerd
-/hack/make/.resources-windows/go.mod

+ 19 - 1
Dockerfile.windows

@@ -168,6 +168,7 @@ SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPref
 ARG GO_VERSION=1.18.0
 ARG CONTAINERD_VERSION=1.6.2
 ARG GOTESTSUM_VERSION=v1.7.0
+ARG GOWINRES_VERSION=v0.2.3
 
 # Environment variable notes:
 #  - GO_VERSION must be consistent with 'Dockerfile' used by Linux.
@@ -179,7 +180,8 @@ ENV GO_VERSION=${GO_VERSION} `
     GOPATH=C:\gopath `
     GO111MODULE=off `
     FROM_DOCKERFILE=1 `
-    GOTESTSUM_VERSION=${GOTESTSUM_VERSION}
+    GOTESTSUM_VERSION=${GOTESTSUM_VERSION} `
+    GOWINRES_VERSION=${GOWINRES_VERSION}
 
 RUN `
   Function Test-Nano() { `
@@ -289,6 +291,22 @@ RUN `
   `
   Install-GoTestSum
 
+RUN `
+  Function Install-GoWinres() { `
+    $Env:GO111MODULE = 'on'; `
+    $tmpGobin = "${Env:GOBIN_TMP}"; `
+    $Env:GOBIN = """${Env:GOPATH}`\bin"""; `
+    Write-Host "INFO: Installing go-winres version $Env:GOWINRES_VERSION in $Env:GOBIN"; `
+    &go install "github.com/tc-hib/go-winres@${Env:GOWINRES_VERSION}"; `
+    $Env:GOBIN = "${tmpGobin}"; `
+    $Env:GO111MODULE = 'off'; `
+    if ($LASTEXITCODE -ne 0) {  `
+      Throw '"go-winres install failed..."'; `
+    } `
+  } `
+  `
+  Install-GoWinres
+
 # Make PowerShell the default entrypoint
 ENTRYPOINT ["powershell.exe"]
 

+ 3 - 10
Makefile

@@ -166,21 +166,14 @@ default: binary
 all: build ## validate all checks, build linux binaries, run all tests,\ncross build non-linux binaries, and generate archives
 	$(DOCKER_RUN_DOCKER) bash -c 'hack/validate/default && hack/make.sh'
 
-# This is only used to work around read-only bind mounts of the source code into
-# binary build targets. We end up mounting a tmpfs over autogen which allows us
-# to write build-time generated assets even though the source is mounted read-only
-# ...But in order to do so, this dir needs to already exist.
-autogen:
-	mkdir -p autogen
-
-binary: buildx autogen ## build statically linked linux binaries
+binary: buildx ## build statically linked linux binaries
 	$(BUILD_CMD) $(BUILD_OPTS) --output=bundles/ --target=$@ $(VERSION_AUTOGEN_ARGS) .
 
-dynbinary: buildx autogen ## build dynamically linked linux binaries
+dynbinary: buildx ## build dynamically linked linux binaries
 	$(BUILD_CMD) $(BUILD_OPTS) --output=bundles/ --target=$@ $(VERSION_AUTOGEN_ARGS) .
 
 cross: BUILD_OPTS += --build-arg CROSS=true --build-arg DOCKER_CROSSPLATFORMS
-cross: buildx autogen ## cross build the binaries for darwin, freebsd and\nwindows
+cross: buildx ## cross build the binaries for darwin, freebsd and\nwindows
 	$(BUILD_CMD) $(BUILD_OPTS) --output=bundles/ --target=$@ $(VERSION_AUTOGEN_ARGS) .
 
 bundles:

+ 0 - 1
cmd/dockerd/docker_windows.go

@@ -5,7 +5,6 @@ import (
 	"path/filepath"
 
 	"github.com/Microsoft/go-winio/pkg/etwlogrus"
-	_ "github.com/docker/docker/autogen/winresources/dockerd"
 	"github.com/sirupsen/logrus"
 )
 

+ 0 - 4
dockerversion/version_lib.go

@@ -1,7 +1,3 @@
-//go:build !autogen
-// +build !autogen
-
-// Package dockerversion is auto-generated at build-time
 package dockerversion // import "github.com/docker/docker/dockerversion"
 
 // Default build-time variable for library-import.

+ 0 - 9
hack/ci/windows.ps1

@@ -551,14 +551,6 @@ Try {
         Write-Host -ForegroundColor Magenta "WARN: Skipping building the binaries"
     }
 
-    Write-Host -ForegroundColor Green "INFO: Copying dockerversion from the container..."
-    $ErrorActionPreference = "SilentlyContinue"
-    docker cp "$contPath\..\dockerversion\version_autogen.go" "$env:SOURCES_DRIVE`:\$env:SOURCES_SUBDIR\src\github.com\docker\docker\dockerversion"
-    if (-not($LastExitCode -eq 0)) {
-         Throw "ERROR: Failed to docker cp the generated version_autogen.go to $env:SOURCES_DRIVE`:\$env:SOURCES_SUBDIR\src\github.com\docker\docker\dockerversion"
-    }
-    $ErrorActionPreference = "Stop"
-
     # Grab the golang installer out of the built image. That way, we know we are consistent once extracted and paths set,
     # so there's no need to re-deploy on account of an upgrade to the version of GO being used in docker.
     if ($null -eq $env:SKIP_COPY_GO) {
@@ -854,7 +846,6 @@ Try {
             $c += "`"$env:INTEGRATION_TEST_NAME`" "
             Write-Host -ForegroundColor Magenta "WARN: Only running integration tests matching $env:INTEGRATION_TEST_NAME"
         }
-        $c += "`"-tags`" " + "`"autogen`" "
         $c += "`"-test.timeout`" " + "`"200m`" "
 
         if ($null -ne $env:INTEGRATION_IN_CONTAINER) {

+ 15 - 7
hack/make.ps1

@@ -168,9 +168,10 @@ Function Get-UpstreamCommit() {
 }
 
 # Build a binary (client or daemon)
-Function Execute-Build($type, $additionalBuildTags, $directory) {
+Function Execute-Build($type, $additionalBuildTags, $directory, $ldflags) {
     # Generate the build flags
-    $buildTags = "autogen"
+    $buildTags = ""
+    $ldflags = "-linkmode=internal " + $ldflags
     if ($Noisy)                     { $verboseParm=" -v" }
     if ($Race)                      { Write-Warning "Using race detector"; $raceParm=" -race"}
     if ($ForceBuildAll)             { $allParm=" -a" }
@@ -188,7 +189,7 @@ Function Execute-Build($type, $additionalBuildTags, $directory) {
                     $allParm + `
                     $optParm + `
                     " -tags """ + $buildTags + """" + `
-                    " -ldflags """ + "-linkmode=internal" + """" + `
+                    " -ldflags """ + $ldflags + """" + `
                     " -o $root\bundles\"+$directory+".exe"
     Invoke-Expression $buildCommand
     if ($LASTEXITCODE -ne 0) { Throw "Failed to compile $type" }
@@ -427,13 +428,20 @@ Try {
     # Verify GOPATH is set
     if ($env:GOPATH.Length -eq 0) { Throw "Missing GOPATH environment variable. See https://golang.org/doc/code.html#GOPATH" }
 
-    # Run autogen if building binaries or running unit tests.
-    if ($Client -or $Daemon -or $TestUnit) {
+    # Run autogen if building daemon.
+    if ($Daemon) {
         Write-Host "INFO: Invoking autogen..."
-        Try { .\hack\make\.go-autogen.ps1 -CommitString $gitCommit -DockerVersion $dockerVersion -Platform "$env:PLATFORM" -Product "$env:PRODUCT" }
+        Try { .\hack\make\.go-autogen.ps1 -CommitString $gitCommit -DockerVersion $dockerVersion -Platform "$env:PLATFORM" -Product "$env:PRODUCT" -PackagerName "$env:PACKAGER_NAME" }
         Catch [Exception] { Throw $_ }
     }
 
+    $ldflags = "-X 'github.com/docker/docker/dockerversion.Version="+$dockerVersion+"'"
+    $ldflags += " -X 'github.com/docker/docker/dockerversion.GitCommit="+$gitCommit+"'"
+    $ldflags += " -X 'github.com/docker/docker/dockerversion.BuildTime="+$env:BUILDTIME+"'"
+    $ldflags += " -X 'github.com/docker/docker/dockerversion.PlatformName="+$env:PLATFORM+"'"
+    $ldflags += " -X 'github.com/docker/docker/dockerversion.ProductName="+$env:PRODUCT+"'"
+    $ldflags += " -X 'github.com/docker/docker/dockerversion.DefaultProductLicense="+$env:DEFAULT_PRODUCT_LICENSE+"'"
+
     # DCO, Package import and Go formatting tests.
     if ($DCO -or $PkgImports -or $GoFormat) {
         # We need the head and upstream commits for these
@@ -454,7 +462,7 @@ Try {
     if ($Client -or $Daemon) {
 
         # Perform the actual build
-        if ($Daemon) { Execute-Build "daemon" "daemon" "dockerd" }
+        if ($Daemon) { Execute-Build "daemon" "daemon" "dockerd" $ldflags }
         if ($Client) {
             # Get the Docker channel and version from the environment, or use the defaults.
             if (-not ($channel = $env:DOCKERCLI_CHANNEL)) { $channel = "stable" }

+ 106 - 43
hack/make/.go-autogen.ps1

@@ -10,6 +10,22 @@
 
 .PARAMETER DockerVersion
      The version such as 17.04.0-dev. This is calculated externally to this script.
+
+.PARAMETER Platform
+     The platform name, such as "Docker Engine - Community".
+
+.PARAMETER Product
+     The product name, used to set version.ProductName, which is used to set BuildKit's
+     ExportedProduct variable in order to show useful error messages to users when a
+     certain version of the product doesn't support a BuildKit feature.
+
+.PARAMETER DefaultProductLicense
+     Sets the version.DefaultProductLicense string, such as "Community Engine". This field
+     can contain a summary of the product license of the daemon if a commercial license has
+     been applied to the daemon.
+
+.PARAMETER PackagerName
+     The name of the packager (e.g. "Docker, Inc."). This used to set CompanyName in the manifest.
 #>
 
 param(
@@ -17,7 +33,8 @@ param(
     [Parameter(Mandatory=$true)][string]$DockerVersion,
     [Parameter(Mandatory=$false)][string]$Platform,
     [Parameter(Mandatory=$false)][string]$Product,
-    [Parameter(Mandatory=$false)][string]$DefaultProductLicense
+    [Parameter(Mandatory=$false)][string]$DefaultProductLicense,
+    [Parameter(Mandatory=$false)][string]$PackagerName
 )
 
 $ErrorActionPreference = "Stop"
@@ -27,63 +44,109 @@ Function Get-BuildDateTime() {
     return $(Get-Date).ToUniversalTime()
 }
 
-try {
-    $buildDateTime=Get-BuildDateTime
+Function Get-Year() {
+    return $(Get-Date).year
+}
 
-    if (Test-Path ".\autogen") {
-        Remove-Item ".\autogen" -Recurse -Force | Out-Null
+Function Get-FixQuadVersionNumber($number) {
+    if ($number -eq 0) {
+        return $number
     }
+    return $number.TrimStart("0")
+}
 
-    $fileContents = '
-// +build autogen
-
-// Package dockerversion is auto-generated at build-time
-package dockerversion
-
-// Default build-time variable for library-import.
-// This file is overridden on build with build-time information.
-const (
-	GitCommit             string = "'+$CommitString+'"
-	Version               string = "'+$DockerVersion+'"
-	BuildTime             string = "'+$buildDateTime+'"
-	PlatformName          string = "'+$Platform+'"
-	ProductName           string = "'+$Product+'"
-	DefaultProductLicense string = "'+$DefaultProductLicense+'"
-)
-
-// AUTOGENERATED FILE; see hack\make\.go-autogen.ps1
-'
-
-    # Write the file without BOM
-    $outputFile="$(Get-Location)\dockerversion\version_autogen.go"
-    if (Test-Path $outputFile) { Remove-Item $outputFile }
-    [System.IO.File]::WriteAllText($outputFile, $fileContents, (New-Object System.Text.UTF8Encoding($False)))
+try {
+    $buildDateTime=Get-BuildDateTime
+    $currentYear=Get-Year
 
-    New-Item -ItemType Directory -Path "autogen\winresources\tmp" | Out-Null
-    New-Item -ItemType Directory -Path "autogen\winresources\dockerd" | Out-Null
-    Copy-Item "hack\make\.resources-windows\resources.go" "autogen\winresources\dockerd"
+    # Update PATH
+    $env:PATH="$env:GOPATH\bin;$env:PATH"
 
     # Generate a version in the form major,minor,patch,build
-    $versionQuad=$DockerVersion -replace "[^0-9.]*" -replace "\.", ","
+    $versionQuad=($DockerVersion -replace "[^0-9.]*")
+    if ($versionQuad -Match "^\d+`.\d+`.\d+$"){
+        $versionQuad = $versionQuad + ".0"
+    }
+    $versionMatches = $($versionQuad | Select-String -AllMatches -Pattern "(\d+)`.(\d+)`.(\d+)`.(\d+)").Matches
 
-    # Compile the messages
-    windmc hack\make\.resources-windows\event_messages.mc -h autogen\winresources\tmp -r autogen\winresources\tmp
-    if ($LASTEXITCODE -ne 0) { Throw "Failed to compile event message resources" }
+    $mkwinresContents = '{
+  "RT_GROUP_ICON": {
+    "#1": {
+      "0409": "../../winresources/docker.ico"
+    }
+  },
+  "RT_MANIFEST": {
+    "#1": {
+      "0409": {
+        "identity": {},
+        "description": "Docker Engine",
+        "minimum-os": "vista",
+        "execution-level": "",
+        "ui-access": false,
+        "auto-elevate": false,
+        "dpi-awareness": "unaware",
+        "disable-theming": false,
+        "disable-window-filtering": false,
+        "high-resolution-scrolling-aware": false,
+        "ultra-high-resolution-scrolling-aware": false,
+        "long-path-aware": false,
+        "printer-driver-isolation": false,
+        "gdi-scaling": false,
+        "segment-heap": false,
+        "use-common-controls-v6": false
+      }
+    }
+  },
+  "RT_MESSAGETABLE": {
+    "#1": {
+      "0409": "../../winresources/event_messages.bin"
+    }
+  },
+  "RT_VERSION": {
+    "#1": {
+      "0409": {
+        "fixed": {
+          "file_version": "'+(Get-FixQuadVersionNumber($versionMatches.Groups[1].Value))+'.'+(Get-FixQuadVersionNumber($versionMatches.Groups[2].Value))+'.'+(Get-FixQuadVersionNumber($versionMatches.Groups[3].Value))+'.'+(Get-FixQuadVersionNumber($versionMatches.Groups[4].Value))+'",
+          "product_version": "'+(Get-FixQuadVersionNumber($versionMatches.Groups[1].Value))+'.'+(Get-FixQuadVersionNumber($versionMatches.Groups[2].Value))+'.'+(Get-FixQuadVersionNumber($versionMatches.Groups[3].Value))+'.'+(Get-FixQuadVersionNumber($versionMatches.Groups[4].Value))+'",
+          "type": "Unknown"
+        },
+        "info": {
+          "0000": {
+            "CompanyName": "'+$PackagerName+'",
+            "FileVersion": "'+$DockerVersion+'",
+            "LegalCopyright": "Copyright (C) 2015-'+$currentYear+' Docker Inc.",
+            "OriginalFileName": "dockerd.exe",
+            "ProductName": "'+$Product+'",
+            "ProductVersion": "'+$DockerVersion+'",
+            "SpecialBuild": "'+$CommitString+'"
+          }
+        }
+      }
+    }
+  }
+}'
 
-    # If you really want to understand this madness below, search the Internet for powershell variables after verbatim arguments... Needed to get double-quotes passed through to the compiler options.
-    # Generate the .syso files containing all the resources and manifest needed to compile the final docker binaries. Both 32 and 64-bit clients.
-    $env:_ag_dockerVersion=$DockerVersion
-    $env:_ag_gitCommit=$CommitString
+    # Write the file
+    $outputFile="$(Get-Location)\cli\winresources\dockerd\winres.json"
+    if (Test-Path $outputFile) { Remove-Item $outputFile }
+    [System.IO.File]::WriteAllText($outputFile, $mkwinresContents)
+    Get-Content $outputFile | Out-Host
+
+    # Create winresources package stub if removed while using tmpfs in Dockerfile
+    $stubPackage="$(Get-Location)\cli\winresources\dockerd\winresources.go"
+    if(![System.IO.File]::Exists($stubPackage)){
+        Set-Content -NoNewline -Path $stubPackage -Value 'package winresources'
+    }
 
-    windres -i hack/make/.resources-windows/dockerd.rc -o autogen/winresources/dockerd/rsrc_amd64.syso -F pe-x86-64 --use-temp-file -I autogen/winresources/tmp -D DOCKER_VERSION_QUAD=$versionQuad --% -D DOCKER_VERSION=\"%_ag_dockerVersion%\" -D DOCKER_COMMIT=\"%_ag_gitCommit%\"
-    if ($LASTEXITCODE -ne 0) { Throw "Failed to compile daemon resources" }
+    # Generate
+    go generate -v "github.com/docker/docker/cmd/dockerd"
+    if ($LASTEXITCODE -ne 0) { Throw "Failed to generate version info" }
 }
 Catch [Exception] {
     # Throw the error onto the caller to display errors. We don't expect this script to be called directly 
     Throw ".go-autogen.ps1 failed with error $_"
 }
 Finally {
-    Remove-Item .\autogen\winresources\tmp -Recurse -Force -ErrorAction SilentlyContinue | Out-Null
     $env:_ag_dockerVersion=""
     $env:_ag_gitCommit=""
 }

+ 0 - 4
vendor.mod

@@ -26,7 +26,6 @@ require (
 	github.com/creack/pty v1.1.11
 	github.com/deckarep/golang-set v0.0.0-20141123011944-ef32fa3046d9
 	github.com/docker/distribution v2.8.1+incompatible
-	github.com/docker/docker/autogen/winresources/dockerd v0.0.0-00010101000000-000000000000
 	github.com/docker/go-connections v0.4.0
 	github.com/docker/go-events v0.0.0-20190806004212-e31b211e4f1c
 	github.com/docker/go-metrics v0.0.1
@@ -185,6 +184,3 @@ replace (
 
 // Removes etcd dependency
 replace github.com/rexray/gocsi => github.com/dperny/gocsi v1.2.3-pre
-
-// autogen/winresources/dockerd is generated a build time, this replacement is only for the purpose of `go mod vendor`
-replace github.com/docker/docker/autogen/winresources/dockerd => ./hack/make/.resources-windows

+ 0 - 4
vendor/modules.txt

@@ -284,9 +284,6 @@ github.com/docker/distribution/registry/client/auth/challenge
 github.com/docker/distribution/registry/client/transport
 github.com/docker/distribution/registry/storage/cache
 github.com/docker/distribution/registry/storage/cache/memory
-# github.com/docker/docker/autogen/winresources/dockerd v0.0.0-00010101000000-000000000000 => ./hack/make/.resources-windows
-## explicit; go 1.17
-github.com/docker/docker/autogen/winresources/dockerd
 # github.com/docker/go-connections v0.4.0
 ## explicit
 github.com/docker/go-connections/nat
@@ -1132,4 +1129,3 @@ gotest.tools/v3/skip
 # github.com/vishvananda/netlink => github.com/vishvananda/netlink v1.1.0
 # go.opencensus.io => go.opencensus.io v0.22.3
 # github.com/rexray/gocsi => github.com/dperny/gocsi v1.2.3-pre
-# github.com/docker/docker/autogen/winresources/dockerd => ./hack/make/.resources-windows