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

Cannot import packages that export C symbols #77

Open
cosmos72 opened this issue Dec 21, 2019 · 4 comments
Open

Cannot import packages that export C symbols #77

cosmos72 opened this issue Dec 21, 2019 · 4 comments
Assignees

Comments

@cosmos72
Copy link
Owner

cosmos72 commented Dec 21, 2019

The reason is:
to download and scan packages, gomacro uses golang.org/x/tools/go/packages - which is unable to compute the type of C symbols and the value of C constants (for good reasons: you need a C compiler for that).
Thus golang.org/x/tools/go/packages returns an error when trying to create the metadata of a package that exports C symbols, gomacro receives that error and bails out.

There is a possibile solution: to generate an import file, gomacro only needs the names of exported symbols, not their types or values (actually, knowing the exact values of untyped constants and the methods of interfaces would be useful too, but not absolutely required).

So, if golang.org/x/tools/go/packages successfully downloads a package, but fails to create its metadata, gomacro could locate the relevant sources inside $GOPATH/pkg/mod, parse them, collect the names of exported symbols, generate a (slightly reduced) import file and compile and load it normally as a plugin.

@Keithcat1
Copy link

Glad you're looking into this. Is this relatively easy to implement?

@cosmos72
Copy link
Owner Author

cosmos72 commented Jan 9, 2020

Probably there is a simpler solution (to be tested):

execute one of the commands
GO111MODULE=on go get PACKAGE/FULL/PATH
or
GO111MODULE=on go install PACKAGE/FULL/PATH
before using golang.org/x/tools/go/packages to load the package metadata.

This has a non-zero chance to work because go get and go install invoke the C compiler for packages that import "C"

@Keithcat1
Copy link

I tried:
set GO111MODULE=on
go install github.com/veandco/go-sdl2/sdl
gomacro
...
gomacro> import "github.com/veandco/go-sdl2/sdl"
// debug: looking for package "github.com/veandco/go-sdl2/sdl" ...
error loading package "github.com/veandco/go-sdl2/sdl" metadata: C:\py\go\pkg\mo
d\github.com\veandco\[email protected]\sdl\audio.go:29:8: could not import C (no me
tadata for C)
...

I think I mensioned this, but it works if I use :options Import.Uses.Module before importing the package that requires C.

You could always rip the code from CGO and use that to get the required info.

@mewmew
Copy link
Contributor

mewmew commented Mar 21, 2020

I'm also glad you are looking into this @cosmos72. The Gomacro REPL is already very useful and it's quite a pleasure to use. Being able to import packages that export C symbols would be a valuable addition to the use-cases for when Gomacro may be used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants