@@ -80,12 +80,10 @@ std::pair<MTL::Library*, NS::Error*> load_library_from_path(
8080}
8181
8282#ifdef SWIFTPM_BUNDLE
83- MTL ::Library* try_load_bundle (
83+ MTL ::Library* try_load_bundle_path (
8484 MTL ::Device* device,
85- NS :: URL * url ,
85+ const std::string& bundle_path ,
8686 const std::string& lib_name) {
87- std::string bundle_path = std::string (url->fileSystemRepresentation ()) + " /" +
88- SWIFTPM_BUNDLE + " .bundle" ;
8987 auto bundle = NS ::Bundle::alloc ()->init (
9088 NS::String::string (bundle_path.c_str(), NS::UTF8StringEncoding));
9189 if (bundle != nullptr ) {
@@ -100,6 +98,15 @@ MTL::Library* try_load_bundle(
10098 return nullptr ;
10199}
102100
101+ MTL ::Library* try_load_bundle (
102+ MTL ::Device* device,
103+ NS ::URL * url,
104+ const std::string& lib_name) {
105+ std::string bundle_path = std::string (url->fileSystemRepresentation ()) + " /" +
106+ SWIFTPM_BUNDLE + " .bundle" ;
107+ return try_load_bundle_path (device, bundle_path, lib_name);
108+ }
109+
103110MTL ::Library* try_load_framework (
104111 MTL ::Device* device,
105112 NS ::URL * url,
@@ -130,6 +137,19 @@ std::pair<MTL::Library*, NS::Error*> load_swiftpm_library(
130137 MTL ::Device* device,
131138 const std::string& lib_name) {
132139#ifdef SWIFTPM_BUNDLE
140+ const std::string swiftpm_bundle_name =
141+ std::string (SWIFTPM_BUNDLE ) + " .bundle" ;
142+
143+ auto binary_dir = current_binary_dir ();
144+ for (int i = 0 ; i < 4 && !binary_dir.empty (); ++i) {
145+ MTL ::Library* library = try_load_bundle_path (
146+ device, (binary_dir / swiftpm_bundle_name).string (), lib_name);
147+ if (library != nullptr ) {
148+ return {library, nullptr };
149+ }
150+ binary_dir = binary_dir.parent_path ();
151+ }
152+
133153 MTL ::Library* library =
134154 try_load_bundle (device, NS ::Bundle::mainBundle ()->bundleURL (), lib_name);
135155 if (library != nullptr ) {
@@ -138,6 +158,16 @@ std::pair<MTL::Library*, NS::Error*> load_swiftpm_library(
138158 auto bundles = NS ::Bundle::allBundles ();
139159 for (int i = 0 , c = (int )bundles->count (); i < c; i++) {
140160 auto bundle = reinterpret_cast <NS ::Bundle*>(bundles->object (i));
161+ const auto bundle_url = bundle->bundleURL ();
162+ const std::string bundle_path = bundle_url->fileSystemRepresentation ();
163+ if (bundle_path.size () >= swiftpm_bundle_name.size () &&
164+ bundle_path.rfind (swiftpm_bundle_name) ==
165+ bundle_path.size () - swiftpm_bundle_name.size ()) {
166+ library = try_load_framework (device, bundle->resourceURL (), lib_name);
167+ if (library != nullptr ) {
168+ return {library, nullptr };
169+ }
170+ }
141171 library = try_load_bundle (device, bundle->resourceURL (), lib_name);
142172 if (library != nullptr ) {
143173 return {library, nullptr };
0 commit comments