@@ -125,27 +125,14 @@ func resolveLibrary(header string, headerToLibraries map[string][]*types.Library
125125 return
126126 }
127127
128- if markedLibrariesContainOneOfCandidate (markImportedLibrary , libraries ) {
128+ if markImportedLibraryContainsOneOfCandidates (markImportedLibrary , libraries ) {
129129 return
130130 }
131131
132- var library * types.Library
133-
134- for _ , platform := range platforms {
135- if platform != nil && library == nil {
136- librariesWithinSpecifiedPlatform := librariesWithinPlatform (libraries , platform )
137- library = findBestLibraryWithHeader (header , librariesWithinSpecifiedPlatform )
138- }
139- }
140-
141- for _ , platform := range platforms {
142- if platform != nil && library == nil {
143- library = findBestLibraryWithHeader (header , librariesCompatibleWithPlatform (libraries , platform ))
144- }
145- }
132+ library := findLibraryOutsideAnyPlatform (header , libraries , platforms )
146133
147134 if library == nil {
148- library = findBestLibraryWithHeader (header , libraries )
135+ library = findLibraryInPlatforms (header , libraries , platforms )
149136 }
150137
151138 if library == nil {
@@ -159,7 +146,57 @@ func resolveLibrary(header string, headerToLibraries map[string][]*types.Library
159146 markImportedLibrary [library ] = true
160147}
161148
162- func markedLibrariesContainOneOfCandidate (markImportedLibrary map [* types.Library ]bool , libraries []* types.Library ) bool {
149+ func findLibraryInPlatforms (header string , libraries []* types.Library , platforms []* types.Platform ) * types.Library {
150+ for _ , platform := range platforms {
151+ if platform != nil {
152+ librariesWithinSpecifiedPlatform := librariesWithinPlatform (libraries , platform )
153+ library := findBestLibraryWithHeader (header , librariesWithinSpecifiedPlatform )
154+ if library != nil {
155+ return library
156+ }
157+ }
158+ }
159+
160+ return nil
161+ }
162+
163+ func findLibraryOutsideAnyPlatform (header string , libraries []* types.Library , platforms []* types.Platform ) * types.Library {
164+ librariesOutsidePlatforms := librariesOutsideAnyPlatform (libraries , platforms )
165+
166+ for _ , platform := range platforms {
167+ if platform != nil {
168+ library := findBestLibraryWithHeader (header , librariesCompatibleWithPlatform (librariesOutsidePlatforms , platform ))
169+ if library != nil {
170+ return library
171+ }
172+ }
173+ }
174+
175+ return findBestLibraryWithHeader (header , librariesOutsidePlatforms )
176+ }
177+
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+ }
183+ for _ , platform := range platforms {
184+ if platform != nil {
185+ librariesWithinSpecifiedPlatform := librariesWithinPlatform (libraries , platform )
186+ for _ , libraryWithinPlatform := range librariesWithinSpecifiedPlatform {
187+ delete (allLibsAsMap , libraryWithinPlatform )
188+ }
189+ }
190+ }
191+
192+ librariesOutsidePlatforms := []* types.Library {}
193+ for lib , _ := range allLibsAsMap {
194+ librariesOutsidePlatforms = append (librariesOutsidePlatforms , lib )
195+ }
196+ return librariesOutsidePlatforms
197+ }
198+
199+ func markImportedLibraryContainsOneOfCandidates (markImportedLibrary map [* types.Library ]bool , libraries []* types.Library ) bool {
163200 for markedLibrary , _ := range markImportedLibrary {
164201 for _ , library := range libraries {
165202 if markedLibrary == library {
@@ -211,7 +248,7 @@ func librariesCompatibleWithPlatform(libraries []*types.Library, platform *types
211248}
212249
213250func librariesWithinPlatform (libraries []* types.Library , platform * types.Platform ) []* types.Library {
214- var librariesWithinSpecifiedPlatform []* types.Library
251+ librariesWithinSpecifiedPlatform := []* types.Library {}
215252 for _ , library := range libraries {
216253 cleanPlatformFolder := filepath .Clean (platform .Folder )
217254 cleanLibraryFolder := filepath .Clean (library .SrcFolder )
0 commit comments