swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
66.84k stars 10.29k forks source link

Need option for specifying C header and library search paths #60890

Open JetForMe opened 1 year ago

JetForMe commented 1 year ago

I frequently work with C libraries, like libmodbus or libgdal, and it seems the only way for Swift to find the headers and libraries is if pkg-config is properly set up. But I just installed GDAL via brew install gdal, and it did not install libgdal.pc anywhere. I recall it similarly did not install libmodbus.pc anywhere, and I had to hand-craft that file.

Describe the solution you'd like

A solution that could help would be the ability to specify (multiple) search paths for C headers and libraries on the command line, e.g.

$ swift --include-searchpath=/opt/homebrew/include/gdal --include-searchpath=/opt/homebrew/include --libraries-searchpath=/opt/homebrew/lib/gdal ...

Describe alternatives you've considered

This should not be something specified in Package.swift, as it’s platform-dependent.

See this post.

finagolfin commented 1 year ago

A solution that could help would be the ability to specify (multiple) search paths for C headers and libraries on the command line

You can specify additional search paths for C headers and libraries like this, -Xcc -I/opt/homebrew/include/gdal -Xcc -I/opt/homebrew/include -L /opt/homebrew/lib/gdal.

This should not be something specified in Package.swift, as it’s platform-dependent.

You can specify platform-dependent build options using SPM.

JetForMe commented 1 year ago

You can specify additional search paths for C headers and libraries like this, -Xcc -I/opt/homebrew/include/gdal -Xcc -I/opt/homebrew/include -L /opt/homebrew/lib/gdal.

Thanks for that. It seems there’s no way to do that in Xcode; I'll file a bug with Apple.

You can specify platform-dependent build options using SPM.

I mean it's platform-independent, as in as the package author, I can't know where things live on a user's system. They need to be able to specify that for their system. It's not clear to me what default search paths are build into Swift, but /opt/homebrew is certainly not one of them.

finagolfin commented 1 year ago

They need to be able to specify that for their system. It's not clear to me what default search paths are build into Swift, but /opt/homebrew is certainly not one of them.

Oh, the pkg-config .pc files are supposed to provide those paths on linux, and it looks like SPM also expects Homebrew to provide those on macOS.

JetForMe commented 1 year ago

Yeah, that seems to be the case, but it seems to be unreliable at best with Homebrew.