vulkan-go / vulkan

Vulkan API bindings for Go programming language
MIT License
742 stars 55 forks source link

Missing an LDFLAGS directive on Linux/Unix #32

Closed jclc closed 5 years ago

jclc commented 5 years ago

First of all, love the refactoring; the repository is looking much nicer now without the different source files for each of the desktop platforms. We should strive for as little platform-specific code as possible. However, when the Linux source file was deleted, the cgo directive for linking with dl was also removed, preventing building.

go get -u -v github.com/vulkan-go/vulkan
github.com/vulkan-go/vulkan (download)
github.com/vulkan-go/vulkan
# github.com/vulkan-go/vulkan
/usr/bin/ld: $WORK/b001/_x010.o: in function `getDefaultProcAddr':
vulkan-go/vulkan/vk_default_loader.c:26: undefined reference to `dlopen'
/usr/bin/ld: vulkan-go/vulkan/vk_default_loader.c:30: undefined reference to `dlsym'
collect2: error: ld returned 1 exit status

A simple solution is to just add #cgo linux LDFLAGS: -ldl in vulkan.go, but that file is autogenerated. Could the line just be added in the generator? I'm not familiar with the bindings generator. Adding a new file just for that one directive would be a little ugly in my opinion, but maybe it would be the better choice? Otherwise you'd have to regen the bindings every time you change the build/link flags.

jclc commented 5 years ago

As a sidenote, the conventional method of having autogenerated code in Go projects is through go generate directives, eg. just a file with the contents:

// +build ignore

package vulkan

//go:generate <your command and args>

I'd make a PR for this issue, but I don't know what commands I need to run to generate vulkan.go. With go generate directives, you just run go generate in the package directory.

jclc commented 5 years ago

Another thing I noticed is that #cgo CFLAGS: -I. -DVK_NO_PROTOTYPES is duplicated in vulkan.go, vulkan_ios.go, vulkan_darwin.go and vulkan_android.go.

xlab commented 5 years ago

when the Linux source file was deleted, the cgo directive for linking with dl was also removed, preventing building.

Oops, I just got it back, only for this purpose. I could put the platform specific cflags / ldflags into the YAML manifest, but then it will copy those values across all generated files. And users sometimes contribute useful changes to these variables, I don't want to change them in 6-8 places and force them to run the generator. So as inspired by the GLFW repo https://github.com/go-gl/glfw I put these into platform-specific files.

the conventional method of having autogenerated code in Go projects is through go generate directives

Yes, but this case is different. go generate is useful when you have 1:1 mapping of source and generated files, for example models.go contains go generate directive, and it generates models.gen.go. My c-for-go generator works differently, there is a manifest and a lot of files generated.

but I don't know what commands I need to run to generate

It's https://github.com/vulkan-go/vulkan/blob/master/Makefile

I noticed is that #cgo CFLAGS: -I. -DVK_NO_PROTOTYPES is duplicated

Removed from four files (incl. vulkan_linux.go)