SwiftGL / CGLFW3

Swift C bindings for GLFW v3 (Generic)
MIT License
15 stars 26 forks source link

Use pkg-config to find headers and libraries on macOS? #5

Open Feuermurmel opened 6 years ago

Feuermurmel commented 6 years ago

I tried to run the hello-window-1 example from the examples repository by naively typing swift run but got the following error:

[...]/01-hello-window-1$ swift run
[...]
<module-includes>:1:9: note: in file included from <module-includes>:1:
#import "shim.h"
        ^
[...]/01-hello-window-1/.build/checkouts/CGLFW3.git-2014552659416122263/shim.h:5:10: error: 'GLFW/glfw3.h' file not found
#include "GLFW/glfw3.h"
         ^
[...]/01-hello-window-1/main.swift:4:8: error: could not build Objective-C module 'CGLFW3'
import CGLFW3
       ^
error: terminated(1): /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-build-tool -f [...]/01-hello-window-1/.build/debug.yaml example.exe

I installed glfw using MacPorts. Adding the include and library directories on the command line solves the problem and let's the example run without modification: swift run -Xswiftc -I/opt/local/include -Xlinker -L/opt/local/lib. But this kinda sucks because I have to remember adding those options whenever I want to build or run my projects and Xcode does not automatically know about those directories either and I have to configure it there too.

I'm a complete novice when it comes to Swift package management but I noticed that adding pkgConfig: "glfw3" to CGLFW3's Package.swift allows me to run the example with just swift run and from within the generated Xcode project without any adjustments.

diff --git a/Package.swift b/Package.swift
index e1f46ee..cc1bbd3 100644
--- a/Package.swift
+++ b/Package.swift
@@ -1,5 +1,6 @@
 import PackageDescription

 let package = Package(
-    name: "CGLFW3"
+    name: "CGLFW3",
+    pkgConfig: "glfw3"
 )

Is there any downside to this? Otherwise it would IMHO greatly improve the experience of using this library on macOS.

jkolb commented 6 years ago

I don't have experience with pkgConfig and to use it would require knowing it well enough to support Linux (major distros) and macOS (brew & macports) since SwiftGL supports both platforms. Also I'm not sure how it handles stuff like "-dev" packages on Linux. Lastly the current release version of GLFW has bugs in keyboard input that I ran into easily so I've been using a manually compiled 3.3 version which fixes those and I would recommend against not using the 3.2.1 version.

So the downside ends up being the time needed to learn pkgconfig, test and support all platforms, and still manage to use the 3.3 GLFW code which isn't installed by most package managers by default and usually has to be compiled and installed manually.

I have been working on this project to upgrade it to new versions of Swift as they are released and don't have much time for the integration side of things, so any help in that regard would be welcome. The newness of Swift Package Manager and the difficulties of cross platform develop make things hard unfortunately.