How to Wrap libraries (.dylib) built for macOS (Brew,MacPorts,etc.)

LiveCode Builder is a language for extending LiveCode's capabilities, creating new object types as Widgets, and libraries that access lower-level APIs in OSes, applications, and DLLs.

Moderators: LCMark, LCfraser

Post Reply
PaulDaMacMan
Posts: 616
Joined: Wed Apr 24, 2013 4:53 pm
Contact:

How to Wrap libraries (.dylib) built for macOS (Brew,MacPorts,etc.)

Post by PaulDaMacMan » Sat Nov 14, 2020 10:24 am

For once I thought I would actually post an answer instead of a question!

First the backstory:
WAY BACK in 2015 at the dawn of LiveCode Builder's FFI when I was thinking of trying to wrap the cross-platform FluidSynth library, @n.allan managed to get a basic wrapper going for the Windows version of libFluidSynth and posted it here in the forums
I was never able to get it to work on macOS. Fast forward 5 years, I'm now getting fairly proficient with LiveCode Builder, I made a Piano Widget that some people seem to like and then a Windows user @prometheus starts making another wrapper for FluidSynth! This time it works on my macOS (10.12.6) laptop, which I had already installed libFluidSynth on via the "Home Brew" package manager . It works great, I'm thinking "Awesome"! I'll work on this wrapper for a bit since FluidSynth has been ported to pretty much all the platforms! I go to test it on my work mac (running 10.14, no Brew, non-admin account). The LCB compiles fine but it won't load the library when called to, WTH?!?!? The same lib files are in the appropriate folders in my LCB project directory, it should work? No!
So I tried again the things discussed in that 2015 thread, copy the libs to ~/lib (the only standard search location I have access to as non-admin), no dice! Unexceptable, I want this FluidSynth wrapper to be self contained / portable!

The Fix:
Back on my laptop I used the Activity Monitor to look at what files LiveCode is actually opening when the LCB wrapper/FluidSynth is working. It is loading the libefluidsynth.dylib from my LCB Project folder as expected BUT the other libraries in the folder, which are dependencies for libefluidsynth.dylib, (such as Glib stuff like libgthread-2.0.0.dylib) are actually loading from the Brew install path and NOT from the copies in my LCB Project folder! It's back to scouring StackOverflow once again!
The FluidSynth library has these search paths embedded into it when the library binary was built. You can use the command line otool to get a list of these embedded paths by passing -L option and the path to the library file. Apple has another command line utility for altering these paths without recompiling the libraries from source code (without messing around altering make files). It's called "install_name_tool" and you can use it to change the path that points to your Brew (or MacPorts, Fink, or whatever packager) path to @loader_path/libglib-2.0.0.dylib".

The format is this (repeated for each dependency used by the original lib that aren't built into the macOS):
install_name_tool -change "/usr/local/opt/oldpath/lib/libFooDependency.dylib" "@loader_path/libFooDependency.dylib" /path/to/dylibToChange/libOriginalFoo.dylib
Now the LCB extension loads the libFluidSynth and it's dependent glib libraries from my LCB project folder. YEAH! Should be self contained and portable! Maybe I'll Mac a LC Stack that uses shell() to automated
Mystery solved (only took 5 years!). Brew and MacPorts have about 4000 packages waiting to be wrapped with LCB !
My GitHub Repos: https://github.com/PaulMcClernan/
Related YouTube Videos: PlayList

PaulDaMacMan
Posts: 616
Joined: Wed Apr 24, 2013 4:53 pm
Contact:

Re: How to Wrap libraries (.dylib) built for macOS (Brew,MacPorts,etc.)

Post by PaulDaMacMan » Mon Nov 16, 2020 9:51 pm

This is the page that helped me figure it out: https://medium.com/@donblas/fun-with-r ... e41ae86172
And then later I noticed that Trevor Devore already had this figured out for his LCB pocketsphinx wrapper.
My GitHub Repos: https://github.com/PaulMcClernan/
Related YouTube Videos: PlayList

Post Reply

Return to “LiveCode Builder”