@@ -17,8 +17,6 @@ package builder
1717
1818import (
1919 "fmt"
20- "io/ioutil"
21- "os"
2220 "path/filepath"
2321 "regexp"
2422 "strings"
@@ -32,8 +30,10 @@ import (
3230)
3331
3432var VALID_EXPORT_EXTENSIONS = map [string ]bool {".h" : true , ".c" : true , ".hpp" : true , ".hh" : true , ".cpp" : true , ".S" : true , ".a" : true , ".properties" : true }
35- var DOTHEXTENSION = map [string ]bool {".h" : true , ".hh" : true , ".hpp" : true }
36- var DOTAEXTENSION = map [string ]bool {".a" : true }
33+
34+ var ValidExportExtensions = []string {".h" , ".c" , ".hpp" , ".hh" , ".cpp" , ".S" , ".a" , ".properties" }
35+ var DotHExtension = []string {".h" , ".hh" , ".hpp" }
36+ var DotAExtension = []string {".a" }
3737
3838type ExportProjectCMake struct {
3939 // Was there an error while compiling the sketch?
@@ -66,7 +66,6 @@ func (s *ExportProjectCMake) Run(ctx *types.Context) error {
6666
6767 dynamicLibsFromPkgConfig := map [string ]bool {}
6868 extensions := func (ext string ) bool { return VALID_EXPORT_EXTENSIONS [ext ] }
69- staticLibsExtensions := func (ext string ) bool { return DOTAEXTENSION [ext ] }
7069 for _ , library := range ctx .ImportedLibraries {
7170 // Copy used libraries in the correct folder
7271 libDir := libBaseFolder .Join (library .Name )
@@ -91,12 +90,11 @@ func (s *ExportProjectCMake) Run(ctx *types.Context) error {
9190 }
9291
9392 // Remove stray folders contining incompatible or not needed libraries archives
94- var files []string
95- utils .FindFilesInFolder (& files , libDir .Join ("src" ).String (), staticLibsExtensions , true )
93+ files , _ := utils .FindFilesInFolder (libDir .Join ("src" ), true , DotAExtension )
9694 for _ , file := range files {
97- staticLibDir := filepath . Dir ( file )
98- if ! isStaticLib || ! strings .Contains (staticLibDir , mcu ) {
99- os .RemoveAll (staticLibDir )
95+ staticLibDir := file . Parent ( )
96+ if ! isStaticLib || ! strings .Contains (staticLibDir . String () , mcu ) {
97+ staticLibDir .RemoveAll ()
10098 }
10199 }
102100 }
@@ -128,11 +126,10 @@ func (s *ExportProjectCMake) Run(ctx *types.Context) error {
128126 }
129127
130128 // remove "#line 1 ..." from exported c_make folder sketch
131- var sketchFiles []string
132- utils .FindFilesInFolder (& sketchFiles , cmakeFolder .Join ("sketch" ).String (), extensions , false )
129+ sketchFiles , _ := utils .FindFilesInFolder (cmakeFolder .Join ("sketch" ), false , ValidExportExtensions )
133130
134131 for _ , file := range sketchFiles {
135- input , err := ioutil .ReadFile (file )
132+ input , err := file .ReadFile ()
136133 if err != nil {
137134 fmt .Println (err )
138135 continue
@@ -146,7 +143,7 @@ func (s *ExportProjectCMake) Run(ctx *types.Context) error {
146143 }
147144 }
148145 output := strings .Join (lines , "\n " )
149- err = ioutil .WriteFile (file , []byte (output ), 0644 )
146+ err = file .WriteFile ([]byte (output ))
150147 if err != nil {
151148 fmt .Println (err )
152149 }
@@ -163,14 +160,11 @@ func (s *ExportProjectCMake) Run(ctx *types.Context) error {
163160 extractCompileFlags (ctx , "recipe.cpp.o.pattern" , & defines , & dynamicLibsFromGccMinusL , & linkerflags , & linkDirectories )
164161
165162 // Extract folders with .h in them for adding in include list
166- var headerFiles []string
167- isHeader := func (ext string ) bool { return DOTHEXTENSION [ext ] }
168- utils .FindFilesInFolder (& headerFiles , cmakeFolder .String (), isHeader , true )
169- foldersContainingDotH := findUniqueFoldersRelative (headerFiles , cmakeFolder .String ())
163+ headerFiles , _ := utils .FindFilesInFolder (cmakeFolder , true , DotHExtension )
164+ foldersContainingDotH := findUniqueFoldersRelative (headerFiles .AsStrings (), cmakeFolder .String ())
170165
171166 // Extract folders with .a in them for adding in static libs paths list
172- var staticLibs []string
173- utils .FindFilesInFolder (& staticLibs , cmakeFolder .String (), staticLibsExtensions , true )
167+ staticLibs , _ := utils .FindFilesInFolder (cmakeFolder , true , DotAExtension )
174168
175169 // Generate the CMakeLists global file
176170
@@ -210,13 +204,13 @@ func (s *ExportProjectCMake) Run(ctx *types.Context) error {
210204 cmakelist += "link_directories (" + strings .Join (relLinkDirectories , " " ) + " ${EXTRA_LIBS_DIRS})\n "
211205 for _ , staticLib := range staticLibs {
212206 // Static libraries are fully configured
213- lib := filepath .Base (staticLib )
207+ lib := staticLib .Base ()
214208 lib = strings .TrimPrefix (lib , "lib" )
215209 lib = strings .TrimSuffix (lib , ".a" )
216210 if ! utils .SliceContains (dynamicLibsFromGccMinusL , lib ) {
217211 linkGroup += " " + lib
218212 cmakelist += "add_library (" + lib + " STATIC IMPORTED)\n "
219- location := strings .TrimPrefix (staticLib , cmakeFolder .String ())
213+ location := strings .TrimPrefix (staticLib . String () , cmakeFolder .String ())
220214 cmakelist += "set_property(TARGET " + lib + " PROPERTY IMPORTED_LOCATION " + "${PROJECT_SOURCE_DIR}" + location + " )\n "
221215 }
222216 }
0 commit comments