中为Go生成syso文件

中为Go生成syso文件

本文介绍了如何在Azure DevOps的Windows Pipline中为Go生成syso文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用go build的syso文件为我的可执行文件设置图标.

I want to use a syso-file by go build to set an icon for my executable.

在win10笔记本电脑上一切正常,但是当我在ubuntu-latest或Windows-latest上使用相同的syso文件(通过git lfs检入)时,我得到以下消息:

Everything works fine on my win10 notebook, but when I use the same syso-file (checked in with git lfs) with ubuntu-latest or windows-latest I get this message:

C:\Users\VSSADM~1\AppData\Local\Temp\go-build430615882\b001\_pkg_.a(rsrc.syso): not an object file

##[error]The Go task failed with an error: Error: The process 'C:\hostedtoolcache\windows\go\1.14.2\x64\bin\go.exe' failed with exit code 2

当我尝试重新创建syso文件时,出现以下消息:bad magic number当在管道中调用可执行文件($env:GOPATH+"\bin\rsrc.exe")时.

When I try to recreate the syso-file I get this message: bad magic number when call the executable ($env:GOPATH+"\bin\rsrc.exe") in the pipeline.

对于我的问题,如何在Azure DevOps的Windows管道中为Go生成syso文件并将其与go build一起使用?

To my question, how to generate a syso-file for Go in a windows pipline on Azure DevOps and use it with go build?

pool:
  vmImage: 'windows-2019'

variables:
  GOBIN:  '$(GOPATH)/bin'
  GOPATH: '$(system.DefaultWorkingDirectory)/gopath'
  ExecutableName: tool
  GoFilePath: '$(System.DefaultWorkingDirectory)/cmd/Tool/'

steps:
- task: GoTool@0
  inputs:
    version: '1.14.2'
- task: Go@0
  inputs:
    command: 'get'
    arguments: '-d ./...'
    workingDirectory: '$(System.DefaultWorkingDirectory)'

- task: PowerShell@2
  displayName: Set last tag to variable
  inputs:
    targetType: 'inline'
    script: |
      $VERSION_TAG = git describe --tags (git rev-list --tags --max-count=1)
      Write-Host("##vso[task.setvariable variable=VERSION_TAG]$VERSION_TAG")
      Write-Host("##vso[build.addbuildtag]$VERSION_TAG")
      Write-Host($VERSION_TAG)

- task: PowerShell@2
  displayName: Set date to variable
  inputs:
    targetType: 'inline'
    script: |
      $DATE = Get-Date -Format "yyyy-MM-dd"
      Write-Host("##vso[task.setvariable variable=DATE]$DATE")

- task: Go@0
  displayName: 'Create release for windows x64'
  enabled: true
  env:
    GOOS: windows
    GOARCH: amd64
  inputs:
    command: 'build'
    arguments: '-ldflags "-s -w -X main.Version=$(VERSION_TAG) -X main.BuildTime=$(DATE)" -trimpath -o release_build/$(ExecutableName).exe $(GoFilePath)'
    workingDirectory: '$(System.DefaultWorkingDirectory)'


- task: Go@0
  displayName: 'Create release for windows x86'
  enabled: true
  env:
    GOOS: windows
    GOARCH: 386
  inputs:
    command: 'build'
    arguments: '-ldflags "-s -w -X main.Version=$(VERSION_TAG) -X main.BuildTime=$(DATE)" -trimpath -o release_build/$(ExecutableName)_x86.exe $(GoFilePath)'
    workingDirectory: '$(System.DefaultWorkingDirectory)'

- task: Go@0
  displayName: 'Create release for linux x64'
  enabled: true
  env:
    GOOS: linux
    GOARCH: amd64
  inputs:
    command: 'build'
    arguments: '-ldflags "-s -w -X main.Version=$(VERSION_TAG) -X main.BuildTime=$(DATE)" -trimpath -o release_build/$(ExecutableName).bin $(GoFilePath)'
    workingDirectory: '$(System.DefaultWorkingDirectory)'


- task: Go@0
  displayName: 'Create release for linux x86'
  enabled: true
  env:
    GOOS: linux
    GOARCH: 386
  inputs:
    command: 'build'
    arguments: '-ldflags "-s -w -X main.Version=$(VERSION_TAG) -X main.BuildTime=$(DATE)" -trimpath -o release_build/$(ExecutableName)_386.bin $(GoFilePath)'
    workingDirectory: '$(System.DefaultWorkingDirectory)'


- task: Go@0
  displayName: 'Create release for macOS x64'
  enabled: true
  env:
    GOOS: darwin
    GOARCH: amd64
  inputs:
    command: 'build'
    arguments: '-ldflags "-s -w -X main.Version=$(VERSION_TAG) -X main.BuildTime=$(DATE)" -trimpath -o release_build/$(ExecutableName).app $(GoFilePath)'
    workingDirectory: '$(System.DefaultWorkingDirectory)'

- task: CopyFiles@2
  inputs:
    SourceFolder: '$(System.DefaultWorkingDirectory)/release_build'
    TargetFolder: '$(Build.ArtifactStagingDirectory)'

- task: CopyFiles@2
  inputs:
    SourceFolder: '$(System.DefaultWorkingDirectory)/docs'
    TargetFolder: '$(Build.ArtifactStagingDirectory)'

- task: PublishBuildArtifacts@1
  inputs:
    PathtoPublish: '$(Build.ArtifactStagingDirectory)'
    ArtifactName: 'Tool Executables'

推荐答案

在写我发现的问题时,我对这些文件使用git lfs.添加此步骤后,我可以使用资源文件编译可执行文件.

While I was writing the question I figure out, that I use git lfs for theses files.After adding this step I could compile the executable with the resource file.

steps:

- checkout: self
  lfs: true

但是在定位到GOOS窗口之后,我必须删除这些文件,因为否则会出现错误:

But after the build targeting GOOS windows I must delete these files, because otherwise I got the error:

/opt/hostedtoolcache/go/1.14.2/x64/pkg/tool/linux_amd64/link: running gcc failed: exit status 1


/usr/bin/ld: i386 architecture of input file `/tmp/go-link-782633042/000000.o' is incompatible with i386:x86-64 output

collect2: error: ld returned 1 exit status

删除这些文件的步骤:

- task: PowerShell@2
  displayName: Remove syso files
  inputs:
    targetType: 'inline'
    script: |
      Remove-Item $(System.DefaultWorkingDirectory)/cmd/AnalyseTool/*.syso

说明

这是v1文本指针的示例:

Explanation

This is a example of a v1 text pointer:

version https://git-lfs.github.com/spec/v1
oid sha256:4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393
size 12345
(ending \n)

来源: https://github.com/git-lfs/git-lfs/blob/master/docs/spec.md

没有lfs支持的情况下下载源代码时,将放置此文本文件,而不是 real 文件.

When the source code is downloaded without lfs support this text file is placed instead of the real file.

很明显,该文件无法执行(错误的魔术数字)或引用的bin文件格式错误(不是目标文件).

So it is clear, that this file cannot be executed (bad magic number) or that referenced bin files have the wrong format (not an object file).

这篇关于如何在Azure DevOps的Windows Pipline中为Go生成syso文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 02:33