x/tools/cmd/gomvpkg: cgo包处理不正确

jljoyd4f  于 5个月前  发布在  Go
关注(0)|答案(2)|浏览(56)

你使用的Go版本是什么(go version)?

go version devel +438a757d73 Wed Feb 21 18:10:00 2018 +0000 linux/amd64

这个问题在最新版本的gomvpkg(rev golang/tools@77106db)上是否重现?

是的,在最新版本的gomvpkg(rev golang/tools@77106db)上。

你正在使用什么操作系统和处理器架构(go env)?

GOARCH="amd64"
GOBIN=""
GOCACHE="/home/u/.cache/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/u/Desktop/foo"
GORACE=""
GOROOT="/home/u/go"
GOTMPDIR=""
GOTOOLDIR="/home/u/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build382385690=/tmp/go-build -gno-record-gcc-switches"

你做了什么?

尝试移动一个包含cgo代码的包。在这个例子中,我们尝试将包a移动到b

gomvpkg -from a -to b

a的源代码。文件:

a.go

package a

import "C"

var _ = C.CString("foo")

b的输出源代码。文件:

a.go:

// Created by cgo - DO NOT EDIT

//line a.go:1
package b

import _ "unsafe"

var _ = (_Cfunc_CString)("foo")

C:

// Created by cgo - DO NOT EDIT

package b

import "unsafe"

import _ "runtime/cgo"

import "syscall"

var _ syscall.Errno

func _Cgo_ptr(ptr unsafe.Pointer) unsafe.Pointer { return ptr }

//go:linkname _Cgo_always_false runtime.cgoAlwaysFalse
var _Cgo_always_false bool

//go:linkname _Cgo_use runtime.cgoUse
func _Cgo_use(interface{})

type _Ctype_char int8

type _Ctype_void [0]byte

//go:linkname _cgo_runtime_cgocall runtime.cgocall
func _cgo_runtime_cgocall(unsafe.Pointer, uintptr) int32

//go:linkname _cgo_runtime_cgocallback runtime.cgocallback
func _cgo_runtime_cgocallback(unsafe.Pointer, unsafe.Pointer, uintptr, uintptr)

//go:linkname _cgoCheckPointer runtime.cgoCheckPointer
func _cgoCheckPointer(interface{}, ...interface{})

//go:linkname _cgoCheckResult runtime.cgoCheckResult
func _cgoCheckResult(interface{})

func _Cfunc_CString(s string) *_Ctype_char {
	p := _cgo_cmalloc(uint64(len(s) + 1))
	pp := (*[1 << 30]byte)(p)
	copy(pp[:], s)
	pp[len(s)] = 0
	return (*_Ctype_char)(p)
}

//go:cgo_import_static _cgo_dbcbf9d0883b_Cfunc__Cmalloc
//go:linkname __cgofn__cgo_dbcbf9d0883b_Cfunc__Cmalloc _cgo_dbcbf9d0883b_Cfunc__Cmalloc
var __cgofn__cgo_dbcbf9d0883b_Cfunc__Cmalloc byte
var _cgo_dbcbf9d0883b_Cfunc__Cmalloc = unsafe.Pointer(&__cgofn__cgo_dbcbf9d0883b_Cfunc__Cmalloc)

//go:linkname runtime_throw runtime.throw
func runtime_throw(string)

//go:cgo_unsafe_args
func _cgo_cmalloc(p0 uint64) (r1 unsafe.Pointer) {
	_cgo_runtime_cgocall(_cgo_dbcbf9d0883b_Cfunc__Cmalloc, uintptr(unsafe.Pointer(&p0)))
	if r1 == nil {
		runtime_throw("runtime: C malloc failed")
	}
	return
}

你期望看到什么?

我期望看到包从$GOPATH/src/a移动到$GOPATH/src/b,而不影响cgo代码。输出应该是b/b.go:

package b

import "C"

var _ = C.CString("foo")

你看到了什么?

一个神秘的C文件和使用cgo重写的代码。请注意,输出(即包b)无法编译,而输入可以。

相关问题