@@ -129,10 +129,13 @@ func resolveLibrary(header string, headerToLibraries map[string][]*types.Library
129129 return
130130 }
131131
132- library := findLibraryOutsideAnyPlatform (header , libraries , platforms )
132+ librariesInPlatforms := librariesInSomePlatform (libraries , platforms )
133+ librariesOutsidePlatforms := filterOutLibrariesFrom (libraries , librariesInPlatforms )
134+
135+ library := findBestLibraryOutsideAnyPlatform (header , librariesOutsidePlatforms , platforms )
133136
134137 if library == nil {
135- library = findLibraryInPlatforms (header , libraries , platforms )
138+ library = findBestLibraryInPlatforms (header , librariesInPlatforms , platforms )
136139 }
137140
138141 if library == nil {
@@ -146,10 +149,10 @@ func resolveLibrary(header string, headerToLibraries map[string][]*types.Library
146149 markImportedLibrary [library ] = true
147150}
148151
149- func findLibraryInPlatforms (header string , libraries []* types.Library , platforms []* types.Platform ) * types.Library {
152+ func findBestLibraryInPlatforms (header string , librariesInPlatforms []* types.Library , platforms []* types.Platform ) * types.Library {
150153 for _ , platform := range platforms {
151154 if platform != nil {
152- librariesWithinSpecifiedPlatform := librariesWithinPlatform (libraries , platform )
155+ librariesWithinSpecifiedPlatform := librariesWithinPlatform (librariesInPlatforms , platform )
153156 library := findBestLibraryWithHeader (header , librariesWithinSpecifiedPlatform )
154157 if library != nil {
155158 return library
@@ -160,9 +163,7 @@ func findLibraryInPlatforms(header string, libraries []*types.Library, platforms
160163 return nil
161164}
162165
163- func findLibraryOutsideAnyPlatform (header string , libraries []* types.Library , platforms []* types.Platform ) * types.Library {
164- librariesOutsidePlatforms := librariesOutsideAnyPlatform (libraries , platforms )
165-
166+ func findBestLibraryOutsideAnyPlatform (header string , librariesOutsidePlatforms []* types.Library , platforms []* types.Platform ) * types.Library {
166167 for _ , platform := range platforms {
167168 if platform != nil {
168169 library := findBestLibraryWithHeader (header , librariesCompatibleWithPlatform (librariesOutsidePlatforms , platform ))
@@ -175,25 +176,15 @@ func findLibraryOutsideAnyPlatform(header string, libraries []*types.Library, pl
175176 return findBestLibraryWithHeader (header , librariesOutsidePlatforms )
176177}
177178
178- func librariesOutsideAnyPlatform (libraries []* types.Library , platforms []* types.Platform ) []* types.Library {
179- allLibsAsMap := make (map [* types.Library ]bool )
180- for _ , lib := range libraries {
181- allLibsAsMap [lib ] = true
182- }
179+ func librariesInSomePlatform (libraries []* types.Library , platforms []* types.Platform ) []* types.Library {
180+ librariesInPlatforms := []* types.Library {}
183181 for _ , platform := range platforms {
184182 if platform != nil {
185183 librariesWithinSpecifiedPlatform := librariesWithinPlatform (libraries , platform )
186- for _ , libraryWithinPlatform := range librariesWithinSpecifiedPlatform {
187- delete (allLibsAsMap , libraryWithinPlatform )
188- }
184+ librariesInPlatforms = append (librariesInPlatforms , librariesWithinSpecifiedPlatform ... )
189185 }
190186 }
191-
192- librariesOutsidePlatforms := []* types.Library {}
193- for lib , _ := range allLibsAsMap {
194- librariesOutsidePlatforms = append (librariesOutsidePlatforms , lib )
195- }
196- return librariesOutsidePlatforms
187+ return librariesInPlatforms
197188}
198189
199190func markImportedLibraryContainsOneOfCandidates (markImportedLibrary map [* types.Library ]bool , libraries []* types.Library ) bool {
@@ -216,16 +207,35 @@ func useAlreadyImportedLibraryWithSameNameIfExists(library *types.Library, markI
216207 return library
217208}
218209
219- func filterOutLibraryFrom (libraries []* types.Library , library * types.Library ) []* types.Library {
210+ func filterOutLibraryFrom (libraries []* types.Library , libraryToRemove * types.Library ) []* types.Library {
220211 filteredOutLibraries := []* types.Library {}
221212 for _ , lib := range libraries {
222- if lib != library {
213+ if lib != libraryToRemove {
223214 filteredOutLibraries = append (filteredOutLibraries , lib )
224215 }
225216 }
226217 return filteredOutLibraries
227218}
228219
220+ func filterOutLibrariesFrom (libraries []* types.Library , librariesToRemove []* types.Library ) []* types.Library {
221+ filteredOutLibraries := []* types.Library {}
222+ for _ , lib := range libraries {
223+ if findLibraryIn (librariesToRemove , lib ) == nil {
224+ filteredOutLibraries = append (filteredOutLibraries , lib )
225+ }
226+ }
227+ return filteredOutLibraries
228+ }
229+
230+ func findLibraryIn (libraries []* types.Library , library * types.Library ) * types.Library {
231+ for _ , lib := range libraries {
232+ if lib == library {
233+ return lib
234+ }
235+ }
236+ return nil
237+ }
238+
229239func libraryCompatibleWithPlatform (library * types.Library , platform * types.Platform ) bool {
230240 if len (library .Archs ) == 0 {
231241 return true
0 commit comments