diff --git a/source/backend/plugin/CarlaPluginVST2.cpp b/source/backend/plugin/CarlaPluginVST2.cpp index f0028f792..5df1f42a8 100644 --- a/source/backend/plugin/CarlaPluginVST2.cpp +++ b/source/backend/plugin/CarlaPluginVST2.cpp @@ -25,6 +25,10 @@ #include +#ifdef CARLA_OS_MAC +# import +#endif + #undef VST_FORCE_DEPRECATED #define VST_FORCE_DEPRECATED 0 @@ -62,6 +66,10 @@ public: fProcThread({nullptr, 0}), #else fProcThread(0), +#endif +#ifdef CARLA_OS_MAC + fMacBundleRef(nullptr), + fMacBundleRefNum(0), #endif fEvents(), fUI(), @@ -120,6 +128,21 @@ public: } clearBuffers(); + +#ifdef CARLA_OS_MAC + if (fMacBundleRef != nullptr) + { + CFBundleCloseBundleResourceMap(fMacBundleRef, fMacBundleRefNum); + + if (CFGetRetainCount(fMacBundleRef) == 1) + CFBundleUnloadExecutable(fMacBundleRef); + + if (CFGetRetainCount(fMacBundleRef) > 0) + CFRelease(fMacBundleRef); + + fMacBundleRef = nullptr; + } +#endif } // ------------------------------------------------------------------- @@ -2072,28 +2095,69 @@ public: } // --------------------------------------------------------------- - // open DLL - if (! pData->libOpen(filename)) + VST_Function vstFn; + +#ifdef CARLA_OS_MAC + if (CarlaString(filename).toLower().endsWith(".vst")) { - pData->engine->setLastError(pData->libError(filename)); - return false; - } + // FIXME assert returns, set engine error + const CFURLRef urlRef = CFURLCreateFromFileSystemRepresentation(0, (const UInt8*)filename, (CFIndex)strlen(filename), true); + CARLA_SAFE_ASSERT_RETURN(urlRef != nullptr, false); - // --------------------------------------------------------------- - // get DLL main entry + fMacBundleRef = CFBundleCreate(kCFAllocatorDefault, urlRef); + CFRelease(urlRef); + CARLA_SAFE_ASSERT_RETURN(fMacBundleRef != nullptr, false); + + if (! CFBundleLoadExecutable(fMacBundleRef)) + { + CFRelease(fMacBundleRef); + pData->engine->setLastError("Failed to load VST bundle executable"); + return false; + } - VST_Function vstFn = pData->libSymbol("VSTPluginMain"); + vstFn = (VST_Function)CFBundleGetFunctionPointerForName(fMacBundleRef, CFSTR("main_macho")); - if (vstFn == nullptr) - { - vstFn = pData->libSymbol("main"); + if (vstFn == nullptr) + vstFn = (VST_Function)CFBundleGetFunctionPointerForName(fMacBundleRef, CFSTR("VSTPluginMain")); if (vstFn == nullptr) { - pData->engine->setLastError("Could not find the VST main entry in the plugin library"); + CFBundleUnloadExecutable(fMacBundleRef); + CFRelease(fMacBundleRef); + pData->engine->setLastError("Not a VST plugin"); return false; } + + fMacBundleRefNum = CFBundleOpenBundleResourceMap(fMacBundleRef); + } + else +#endif + { + // ----------------------------------------------------------- + // open DLL + + if (! pData->libOpen(filename)) + { + pData->engine->setLastError(pData->libError(filename)); + return false; + } + + // ----------------------------------------------------------- + // get DLL main entry + + vstFn = pData->libSymbol("VSTPluginMain"); + + if (vstFn == nullptr) + { + vstFn = pData->libSymbol("main"); + + if (vstFn == nullptr) + { + pData->engine->setLastError("Could not find the VST main entry in the plugin library"); + return false; + } + } } // --------------------------------------------------------------- @@ -2233,6 +2297,11 @@ private: pthread_t fMainThread; pthread_t fProcThread; +#ifdef CARLA_OS_MAC + CFBundleRef fMacBundleRef; + CFBundleRefNum fMacBundleRefNum; +#endif + struct FixedVstEvents { int32_t numEvents; intptr_t reserved; diff --git a/source/backend/plugin/Makefile b/source/backend/plugin/Makefile index 43c8da095..4427f9c88 100644 --- a/source/backend/plugin/Makefile +++ b/source/backend/plugin/Makefile @@ -57,6 +57,13 @@ $(OBJDIR)/CarlaPluginLinuxSampler.cpp.o: CarlaPluginLinuxSampler.cpp @echo "Compiling $<" @$(CXX) $< $(BUILD_CXX_FLAGS) $(LINUXSAMPLER_FLAGS) -c -o $@ +ifeq ($(MACOS),true) +$(OBJDIR)/CarlaPluginVST2.cpp.o: CarlaPluginVST2.cpp + -@mkdir -p $(OBJDIR) + @echo "Compiling $<" + @$(CXX) $< $(BUILD_CXX_FLAGS) -ObjC++ -c -o $@ +endif + $(OBJDIR)/%.cpp.o: %.cpp -@mkdir -p $(OBJDIR) @echo "Compiling $<" diff --git a/source/bridges-plugin/Makefile b/source/bridges-plugin/Makefile index d16875d7b..83e8ee619 100644 --- a/source/bridges-plugin/Makefile +++ b/source/bridges-plugin/Makefile @@ -221,6 +221,13 @@ $(OBJDIR)/CarlaEng%.cpp.o: $(CWD)/backend/engine/CarlaEng%.cpp @echo "Compiling CarlaEng$*.cpp (bridge)" @$(CXX) $< $(BUILD_CXX_FLAGS) $(NATIVE_BUILD_FLAGS) -c -o $@ +ifeq ($(MACOS),true) +$(OBJDIR)/CarlaPluginVST2.cpp.o: $(CWD)/backend/plugin/CarlaPluginVST2.cpp + -@mkdir -p $(OBJDIR) + @echo "Compiling CarlaPluginVST2.cpp (bridge)" + @$(CXX) $< $(BUILD_CXX_FLAGS) $(NATIVE_BUILD_FLAGS) -ObjC++ -c -o $@ +endif + $(OBJDIR)/CarlaPlug%.cpp.o: $(CWD)/backend/plugin/CarlaPlug%.cpp -@mkdir -p $(OBJDIR) @echo "Compiling CarlaPlug$*.cpp (bridge)"