go: cmd/go: cgo builds don't work with "zig cc"

Hello, I am filing this issue on behalf of an issue reported upstream here: https://github.com/ziglang/zig/issues/7342

@lu4p writes:


I would like to use “zig cc” for easily cross compiling cgo dependent go code, however the “drop-in” c compiler replacement doesn’t work.

Example cgo code

main.go

package main

//int Add(int a, int b){
//    return a+b;
//}
import "C"
import "fmt"

func main() {
	a := C.int(10)
	b := C.int(20)
	c := C.Add(a, b)
	fmt.Println(c) // 30
}

other compilers

go build main.go //build works fine (uses gcc)
CC="clang" go build main.go //build works fine (uses clang)

zig cc

$ CC="zig cc" go build main.go
# runtime/cgo
info: Usage: zig [command] [options]

Commands:

  build            Build project from build.zig
  build-exe        Create executable from source or object files
  build-lib        Create library from source or object files
  build-obj        Create object from source or assembly
  cc               Use Zig as a drop-in C compiler
  c++              Use Zig as a drop-in C++ compiler
  env              Print lib path, std path, compiler id and version
  fmt              Parse file and render in canonical zig format
  init-exe         Initialize a `zig build` application in the cwd
  init-lib         Initialize a `zig build` library in the cwd
  libc             Display native libc paths file or validate one
  run              Create executable and run immediately
  translate-c      Convert C code to Zig code
  targets          List available compilation targets
  test             Create and run a test build
  version          Print version number and exit
  zen              Print zen of zig and exit

General Options:

  --help           Print command-specific usage

error: unknown command: -E
go version go1.15.5 linux/amd64

So this looks like go is inserting flags before it passes cc. The user will have to work around this by creating a shell script like this:

#!/bin/sh
zig cc $@

And then passing that as CC. This way zig gets invoked with zig cc -E ... instead of zig -E ...

This issue is a feature request for go to honor the entire command line in CC, appending flags rather than prepending them. Or, at least adding explicit detection for zig cc and passing the cc as the first arg.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 4
  • Comments: 20 (12 by maintainers)

Commits related to this issue

Most upvoted comments

@bcmills that workaround is mentioned in the original issue description. 😃

I can confirm this fixes the issue.

Change https://golang.org/cl/341936 mentions this issue: cmd: support space and quotes in CC and CXX

oops

@seankhliao #43808 is a separate issue?

I solved the cache directory issue with https://github.com/ziglang/zig/commit/f7d600675c548b4e45b24f8e10ebdd892a6b8dba. So, as soon as https://golang.org/cl/276412 lands, I believe this issue can be considered to be solved.

Just a passing thought, but assuming that the compiler’s working directory is writeable might be flawed? At least, it seems reasonable enough to execute a C compiler from a non-writeable directory (maybe you’re compiling sources in someone else’s home directory?) as long as the output file you’ve specified is writeable. Maybe it would generally make sense for zig cc to gracefully handle cases where it can’t create a local cache in the working directory in general, and not just for cgo?