Skip to content

Commit f4950db

Browse files
committed
Using more paths helpers to simplify code
1 parent efdb8c4 commit f4950db

File tree

1 file changed

+14
-22
lines changed

1 file changed

+14
-22
lines changed

legacy/builder/phases/libraries_builder.go

+14-22
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package phases
1717

1818
import (
1919
"os"
20-
"path/filepath"
2120
"strings"
2221

2322
"github.com/arduino/arduino-cli/arduino/libraries"
@@ -137,25 +136,20 @@ func compileLibrary(ctx *types.Context, library *libraries.Library, buildPath *p
137136

138137
if library.Precompiled {
139138
if precompiledPath := findExpectedPrecompiledLibFolder(ctx, library); precompiledPath != nil {
140-
var staticLibExts = map[string]bool{".a": true}
141-
var dynamixLibExts = map[string]bool{".so": true}
142-
dynAndStatic := func(ext string) bool { return dynamixLibExts[ext] || staticLibExts[ext] }
143-
staticOnly := func(ext string) bool { return staticLibExts[ext] }
139+
// Find all libraries in precompiledPath
140+
libs, err := precompiledPath.ReadDir()
141+
if err != nil {
142+
return nil, errors.WithStack(err)
143+
}
144144

145145
// Add required LD flags
146-
147-
// find all library names in the folder and prepend -l
148-
dynAndStaticLibs := []string{}
149146
libsCmd := library.LDflags + " "
150-
if err := utils.FindFilesInFolder(&dynAndStaticLibs, precompiledPath.String(), dynAndStatic, false); err != nil {
151-
return nil, errors.WithStack(err)
152-
}
147+
dynAndStaticLibs := libs.Clone()
148+
dynAndStaticLibs.FilterSuffix(".a", ".so")
153149
for _, lib := range dynAndStaticLibs {
154-
name := strings.TrimSuffix(filepath.Base(lib), filepath.Ext(lib))
155-
// strip "lib" first occurrence
150+
name := strings.TrimSuffix(lib.Base(), lib.Ext())
156151
if strings.HasPrefix(name, "lib") {
157-
name = strings.Replace(name, "lib", "", 1)
158-
libsCmd += "-l" + name + " "
152+
libsCmd += "-l" + name[3:] + " "
159153
}
160154
}
161155

@@ -165,13 +159,11 @@ func compileLibrary(ctx *types.Context, library *libraries.Library, buildPath *p
165159
// TODO: This codepath is just taken for .a with unusual names that would
166160
// be ignored by -L / -l methods.
167161
// Should we force precompiled libraries to start with "lib" ?
168-
staticLibs := []string{}
169-
if err := utils.FindFilesInFolder(&staticLibs, precompiledPath.String(), staticOnly, false); err != nil {
170-
return nil, errors.WithStack(err)
171-
}
172-
for _, path := range staticLibs {
173-
if !strings.HasPrefix(filepath.Base(path), "lib") {
174-
objectFiles.Add(paths.New(path))
162+
staticLibs := libs.Clone()
163+
staticLibs.FilterSuffix(".a")
164+
for _, lib := range staticLibs {
165+
if !strings.HasPrefix(lib.Base(), "lib") {
166+
objectFiles.Add(lib)
175167
}
176168
}
177169
return objectFiles, nil

0 commit comments

Comments
 (0)