How to generate a syso-file for Go in a windows pipline on Azure DevOps

大兔子大兔子 提交于 2020-08-10 13:03:40

问题


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

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

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.

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'

回答1:


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

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 

Step to delete these files:

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

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)

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

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

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).



来源:https://stackoverflow.com/questions/62230694/how-to-generate-a-syso-file-for-go-in-a-windows-pipline-on-azure-devops

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!