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/go: cgo + assembly in same package doesn't work #6190

Closed
bradfitz opened this issue Aug 20, 2013 · 4 comments
Closed

cmd/go: cgo + assembly in same package doesn't work #6190

bradfitz opened this issue Aug 20, 2013 · 4 comments
Milestone

Comments

@bradfitz
Copy link
Contributor

If you have cgo and an assembly *.s file in the same package, the go command tries to
compile the assembly with `clang` instead of 6a.

See:

mac:~ bradfitz$ cd $GOPATH/src/foo

mac:foo bradfitz$ ls
bar.s foo.go

mac:foo bradfitz$ cat bar.s
TEXT    ·bar(SB),0,$0
        MOVQ $84, AX
        MOVQ AX, ret+0(FP)
        RET

mac:foo bradfitz$ cat foo.go
package main

/*

static int foo() {
return 42;
}

*/
import "C"

// In bar.s
func bar() int

func main() {
    println(C.foo())
    println(bar())
}
mac:foo bradfitz$ go list -json .
{
    "Dir": "/Users/bradfitz/src/foo",
    "ImportPath": "foo",
    "Name": "main",
    "Target": "/Users/bradfitz/bin/foo",
    "Stale": true,
    "Root": "/Users/bradfitz",
    "CgoFiles": [
        "foo.go"
    ],
    "SFiles": [
        "bar.s"
    ],
    "Imports": [
        "C"
    ],
    "Deps": [
        "errors",
        "runtime",
        "runtime/cgo",
        "sync",
        "sync/atomic",
        "syscall",
        "unsafe"
    ]
}
mac:foo bradfitz$ go install -x .
WORK=/var/folders/00/0g1n0000h01000cxqpysvccm001w6m/T/go-build192769161
mkdir -p $WORK/foo/_obj/
mkdir -p $WORK/foo/_obj/exe/
cd /Users/bradfitz/src/foo
/Users/bradfitz/go/pkg/tool/darwin_amd64/cgo -objdir $WORK/foo/_obj/ -- -I
$WORK/foo/_obj/ foo.go
/Users/bradfitz/go/pkg/tool/darwin_amd64/6c -F -V -w -I $WORK/foo/_obj/ -I
/Users/bradfitz/go/pkg/darwin_amd64 -o $WORK/foo/_obj/_cgo_defun.6 -D GOOS_darwin -D
GOARCH_amd64 $WORK/foo/_obj/_cgo_defun.c
clang -I . -g -O2 -fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments
-fno-common -print-libgcc-file-name
clang -I . -g -O2 -fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments
-fno-common -I $WORK/foo/_obj/ -o $WORK/foo/_obj/_cgo_main.o -c
$WORK/foo/_obj/_cgo_main.c
clang -I . -g -O2 -fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments
-fno-common -I $WORK/foo/_obj/ -o $WORK/foo/_obj/_cgo_export.o -c
$WORK/foo/_obj/_cgo_export.c
clang -I . -g -O2 -fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments
-fno-common -I $WORK/foo/_obj/ -o $WORK/foo/_obj/foo.cgo2.o -c $WORK/foo/_obj/foo.cgo2.c
clang -I . -g -O2 -fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments
-fno-common -I $WORK/foo/_obj/ -o $WORK/foo/_obj/bar.o -c ./bar.s
# foo
./bar.s:1:6: error: invalid character in input
TEXT ·bar(SB),0,$0
     ^
./bar.s:1:7: error: invalid character in input
TEXT ·bar(SB),0,$0
      ^
./bar.s:3:24: error: unexpected token in memory operand
        MOVQ AX, ret+0(FP)
                       ^
@dvyukov
Copy link
Member

dvyukov commented Aug 20, 2013

Comment 1:

What if you want to compile .s files with gcc? How it should work?

@ianlancetaylor
Copy link
Contributor

Comment 2:

I think this is working as intended.  If you're using cgo, you want to compile any .c
files with GCC, not 6c.  Otherwise you can't write your C code in a separate file.  And
extending that to .s files is a very small step.
So I would say that you are after some mechanism that lets you control, in a package
that uses cgo, which compiler/assembler should be used to compile/assemble a specific
.c/.s file.  We could abuse build tags to make this work, but otherwise I don't see how
to get anything other than what you have.

@dvyukov
Copy link
Member

dvyukov commented Aug 20, 2013

Comment 3:

Agree.
Brad, you are writing too complex package :)
You can mode .s files to a separate package, but make them implement functions in the
first package, so you don't rename and introduce wrappers.

@rsc
Copy link
Contributor

rsc commented Sep 3, 2013

Comment 4:

Status changed to WorkingAsIntended.

@rsc rsc added this to the Go1.2 milestone Apr 14, 2015
@rsc rsc removed the go1.2maybe label Apr 14, 2015
@golang golang locked and limited conversation to collaborators Jun 24, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants