@@ -140,8 +140,8 @@ func resolveLibrary(header string, headerToLibraries map[string][]*types.Library
140140 var library * types.Library
141141
142142 for _ , platform := range platforms {
143- if platform != nil && library == nil {
144- library = findBestLibraryWithHeader (header , librariesCompatibleWithPlatform (libraries , platform ))
143+ if platform != nil {
144+ library = findBestLibraryWithHeader (header , librariesCompatibleWithPlatform (libraries , platform , true ))
145145 }
146146 }
147147
@@ -150,6 +150,12 @@ func resolveLibrary(header string, headerToLibraries map[string][]*types.Library
150150 }
151151
152152 if library == nil {
153+ // reorder libraries to promote fully compatible ones
154+ for _ , platform := range platforms {
155+ if platform != nil {
156+ libraries = append (librariesCompatibleWithPlatform (libraries , platform , false ), libraries ... )
157+ }
158+ }
153159 library = libraries [0 ]
154160 }
155161
@@ -218,21 +224,34 @@ func findLibraryIn(libraries []*types.Library, library *types.Library) *types.Li
218224 return nil
219225}
220226
221- func libraryCompatibleWithPlatform (library * types.Library , platform * types.Platform ) bool {
227+ func libraryCompatibleWithPlatform (library * types.Library , platform * types.Platform ) ( bool , bool ) {
222228 if len (library .Archs ) == 0 {
223- return true
229+ return true , true
230+ }
231+ if utils .SliceContains (library .Archs , constants .LIBRARY_ALL_ARCHS ) {
232+ return true , true
224233 }
234+ return utils .SliceContains (library .Archs , platform .PlatformId ), false
235+ }
236+
237+ func libraryCompatibleWithAllPlatforms (library * types.Library ) bool {
225238 if utils .SliceContains (library .Archs , constants .LIBRARY_ALL_ARCHS ) {
226239 return true
227240 }
228- return utils . SliceContains ( library . Archs , platform . PlatformId )
241+ return false
229242}
230243
231- func librariesCompatibleWithPlatform (libraries []* types.Library , platform * types.Platform ) []* types.Library {
244+ func librariesCompatibleWithPlatform (libraries []* types.Library , platform * types.Platform , reorder bool ) []* types.Library {
232245 var compatibleLibraries []* types.Library
233246 for _ , library := range libraries {
234- if libraryCompatibleWithPlatform (library , platform ) {
235- compatibleLibraries = append (compatibleLibraries , library )
247+ compatible , generic := libraryCompatibleWithPlatform (library , platform )
248+ if compatible {
249+ if ! generic && len (compatibleLibraries ) != 0 && libraryCompatibleWithAllPlatforms (compatibleLibraries [0 ]) && reorder == true {
250+ //priority inversion
251+ compatibleLibraries = append ([]* types.Library {library }, compatibleLibraries ... )
252+ } else {
253+ compatibleLibraries = append (compatibleLibraries , library )
254+ }
236255 }
237256 }
238257
0 commit comments