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

cmd/cgo: AR environment variable not used with buildmode=c-archive #68362

Open
k0574x opened this issue Jul 10, 2024 · 5 comments
Open

cmd/cgo: AR environment variable not used with buildmode=c-archive #68362

k0574x opened this issue Jul 10, 2024 · 5 comments
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@k0574x
Copy link

k0574x commented Jul 10, 2024

Go version

go version go1.22.4 windows/amd64

Output of go env in your module/workspace:

set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\wukeke\AppData\Local\go-build      
set GOENV=C:\Users\wukeke\AppData\Roaming\go\env        
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=10.88.9.190
set GOMODCACHE=C:\Users\wukeke\go\pkg\mod
set GONOPROXY=10.88.9.190
set GONOSUMDB=10.88.9.190
set GOOS=windows
set GOPATH=C:\Users\wukeke\go
set GOPRIVATE=10.88.9.190
set GOPROXY=https://goproxy.cn,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLCHAIN=auto
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.22.4
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=NUL
set GOWORK=
set CGO_CFLAGS=-O2 -g
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-O2 -g
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS=-O2 -g
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=C:\Users\wukeke\AppData\Local\Temp\go-build3614417178=/tmp/go-build -gno-record-gcc-switches

What did you do?

test code

$ cat test.go 
package main

import "C"
import "fmt"

//export test
func test() {
        fmt.Println("just test")
}

func main() {

}

cross-tools

$ aarch64-linux-gnu-ar -V
GNU ar (Linaro_Binutils-2019.12) 2.28.2.20170706
Copyright (C) 2017 Free Software Foundation, Inc.
This program is free software; you may redistribute it under the terms of
the GNU General Public License version 3 or (at your option) any later version.
This program has absolutely no warranty.

build command

$ CC=aarch64-linux-gnu-gcc CXX=aarch64-linux-gnu-g++ AR=aarch64-linux-gnu-ar CGO_ENABLED=1 GOOS=linux GOARCH=arm64 GOARM=7 go build -ldflags "-s -w" -buildmode=c-archive -o test.a test.go

What did you see happen?

command-line-arguments

C:\Program Files\Go\pkg\tool\windows_amd64\link.exe: running ar failed: exec: "ar": executable file not found in %PATH%

What did you expect to see?

The go build command should successfully use the aarch64-linux-gnu-ar tool as specified by the AR environment variable.

by the way
If I copy aarch64-linux-gnu-ar.exe to ar.exe, then the compilation can succeed.

@k0574x k0574x changed the title cmd/go: issue title Cross-compilation static library from Windows to ARM64 Linux fails to recognize AR environment variable cmd/go: Cross-compilation static library from Windows to ARM64 Linux fails to recognize AR environment variable Jul 10, 2024
@ianlancetaylor
Copy link
Contributor

The Go tools don't look at the AR environment variable. Maybe they should.

You can do what you want using -ldflags=-extar=$AR.

@seankhliao
Copy link
Member

@ianlancetaylor
Copy link
Contributor

Yes, that is how it works today. Which does seem odd. I think it happened because gccgo has needed to run ar from the start, but the gc toolchain only started using ar later on as part of supporting -buildmode=c-archive.

@seankhliao seankhliao changed the title cmd/go: Cross-compilation static library from Windows to ARM64 Linux fails to recognize AR environment variable cmd/go: AR environment variable not used with buildmode=c-archive Jul 10, 2024
@cagedmantis cagedmantis changed the title cmd/go: AR environment variable not used with buildmode=c-archive cmd/cgo: AR environment variable not used with buildmode=c-archive Jul 11, 2024
@cagedmantis cagedmantis added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jul 11, 2024
@cagedmantis cagedmantis added this to the Backlog milestone Jul 11, 2024
@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Jul 11, 2024
@cherrymui
Copy link
Member

cherrymui commented Jul 12, 2024

At tip, specifically after CL 592375, the linker now uses $CC --print-prog-name ar to find the ar command (if the -extar flag is not explicitly set). So setting CC=aarch64-linux-gnu-gcc should be enough for it to find the right ar. Could you try the tip of the master branch, or Go 1.23rc1? Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

7 participants