30
30
package phases
31
31
32
32
import (
33
+ "os"
33
34
"path/filepath"
34
35
"strings"
35
36
@@ -45,6 +46,8 @@ import (
45
46
46
47
var PRECOMPILED_LIBRARIES_VALID_EXTENSIONS_STATIC = map [string ]bool {".a" : true }
47
48
var PRECOMPILED_LIBRARIES_VALID_EXTENSIONS_DYNAMIC = map [string ]bool {".so" : true }
49
+ var FLOAT_ABI_CFLAG = "float-abi"
50
+ var FPU_CFLAG = "fpu"
48
51
49
52
type LibrariesBuilder struct {}
50
53
@@ -71,28 +74,83 @@ func (s *LibrariesBuilder) Run(ctx *types.Context) error {
71
74
return nil
72
75
}
73
76
77
+ func findExpectedPrecompiledLibFolder (ctx * types.Context , library * libraries.Library ) * paths.Path {
78
+ mcu := ctx .BuildProperties .Get (constants .BUILD_PROPERTIES_BUILD_MCU )
79
+ // Add fpu specifications if they exist
80
+ // To do so, resolve recipe.cpp.o.pattern,
81
+ // search for -mfpu=xxx -mfloat-abi=yyy and add to a subfolder
82
+ command , _ := builder_utils .PrepareCommandForRecipe (ctx , ctx .BuildProperties , constants .RECIPE_CPP_PATTERN , true )
83
+ fpuSpecs := ""
84
+ for _ , el := range strings .Split (command .String (), " " ) {
85
+ if strings .Contains (el , FPU_CFLAG ) {
86
+ toAdd := strings .Split (el , "=" )
87
+ if len (toAdd ) > 1 {
88
+ fpuSpecs += strings .TrimSpace (toAdd [1 ]) + "-"
89
+ break
90
+ }
91
+ }
92
+ }
93
+ for _ , el := range strings .Split (command .String (), " " ) {
94
+ if strings .Contains (el , FLOAT_ABI_CFLAG ) {
95
+ toAdd := strings .Split (el , "=" )
96
+ if len (toAdd ) > 1 {
97
+ fpuSpecs += strings .TrimSpace (toAdd [1 ]) + "-"
98
+ break
99
+ }
100
+ }
101
+ }
102
+
103
+ logger := ctx .GetLogger ()
104
+ if len (fpuSpecs ) > 0 {
105
+ fpuSpecs = strings .TrimRight (fpuSpecs , "-" )
106
+ if library .SourceDir .Join (mcu ).Join (fpuSpecs ).Exist () {
107
+ return library .SourceDir .Join (mcu ).Join (fpuSpecs )
108
+ } else {
109
+ // we are unsure, compile from sources
110
+ logger .Fprintln (os .Stdout , constants .LOG_LEVEL_INFO ,
111
+ constants .MSG_PRECOMPILED_LIBRARY_NOT_FOUND_FOR , library .Name , library .SourceDir .Join (mcu ).Join (fpuSpecs ))
112
+ return nil
113
+ }
114
+ }
115
+
116
+ if library .SourceDir .Join (mcu ).Exist () {
117
+ return library .SourceDir .Join (mcu )
118
+ }
119
+
120
+ logger .Fprintln (os .Stdout , constants .LOG_LEVEL_INFO ,
121
+ constants .MSG_PRECOMPILED_LIBRARY_NOT_FOUND_FOR , library .Name , library .SourceDir .Join (mcu ))
122
+
123
+ return nil
124
+ }
125
+
74
126
func fixLDFLAGforPrecompiledLibraries (ctx * types.Context , libs libraries.List ) error {
75
127
76
128
for _ , library := range libs {
77
129
if library .Precompiled {
78
130
// add library src path to compiler.c.elf.extra_flags
79
131
// use library.Name as lib name and srcPath/{mcpu} as location
80
- mcu := ctx .BuildProperties .Get (constants .BUILD_PROPERTIES_BUILD_MCU )
81
- path := library .SourceDir .Join (mcu ).String ()
132
+ path := findExpectedPrecompiledLibFolder (ctx , library )
133
+ if path == nil {
134
+ break
135
+ }
82
136
// find all library names in the folder and prepend -l
83
137
filePaths := []string {}
84
138
libs_cmd := library .LDflags + " "
85
- extensions := func (ext string ) bool { return PRECOMPILED_LIBRARIES_VALID_EXTENSIONS_DYNAMIC [ext ] }
86
- utils .FindFilesInFolder (& filePaths , path , extensions , true )
139
+ extensions := func (ext string ) bool {
140
+ return PRECOMPILED_LIBRARIES_VALID_EXTENSIONS_DYNAMIC [ext ] || PRECOMPILED_LIBRARIES_VALID_EXTENSIONS_STATIC [ext ]
141
+ }
142
+ utils .FindFilesInFolder (& filePaths , path .String (), extensions , false )
87
143
for _ , lib := range filePaths {
88
144
name := strings .TrimSuffix (filepath .Base (lib ), filepath .Ext (lib ))
89
145
// strip "lib" first occurrence
90
- name = strings .Replace (name , "lib" , "" , 1 )
91
- libs_cmd += "-l" + name + " "
146
+ if strings .HasPrefix (name , "lib" ) {
147
+ name = strings .Replace (name , "lib" , "" , 1 )
148
+ libs_cmd += "-l" + name + " "
149
+ }
92
150
}
93
151
94
152
currLDFlags := ctx .BuildProperties .Get (constants .BUILD_PROPERTIES_COMPILER_LIBRARIES_LDFLAGS )
95
- ctx .BuildProperties .Set (constants .BUILD_PROPERTIES_COMPILER_LIBRARIES_LDFLAGS , currLDFlags + "\" -L" + path + "\" " + libs_cmd + " " )
153
+ ctx .BuildProperties .Set (constants .BUILD_PROPERTIES_COMPILER_LIBRARIES_LDFLAGS , currLDFlags + "\" -L" + path . String () + "\" " + libs_cmd + " " )
96
154
}
97
155
}
98
156
return nil
@@ -129,15 +187,21 @@ func compileLibrary(ctx *types.Context, library *libraries.Library, buildPath *p
129
187
extensions := func (ext string ) bool { return PRECOMPILED_LIBRARIES_VALID_EXTENSIONS_STATIC [ext ] }
130
188
131
189
filePaths := []string {}
132
- mcu := buildProperties .Get (constants .BUILD_PROPERTIES_BUILD_MCU )
133
- err := utils .FindFilesInFolder (& filePaths , library .SourceDir .Join (mcu ).String (), extensions , true )
134
- if err != nil {
135
- return nil , i18n .WrapError (err )
136
- }
137
- for _ , path := range filePaths {
138
- if strings .Contains (filepath .Base (path ), library .RealName ) {
139
- objectFiles .Add (paths .New (path ))
190
+ precompiledPath := findExpectedPrecompiledLibFolder (ctx , library )
191
+ if precompiledPath != nil {
192
+ // TODO: This codepath is just taken for .a with unusual names that would
193
+ // be ignored by -L / -l methods.
194
+ // Should we force precompiled libraries to start with "lib" ?
195
+ err := utils .FindFilesInFolder (& filePaths , precompiledPath .String (), extensions , false )
196
+ if err != nil {
197
+ return nil , i18n .WrapError (err )
198
+ }
199
+ for _ , path := range filePaths {
200
+ if ! strings .HasPrefix (filepath .Base (path ), "lib" ) {
201
+ objectFiles .Add (paths .New (path ))
202
+ }
140
203
}
204
+ return objectFiles , nil
141
205
}
142
206
}
143
207
0 commit comments