@@ -31,6 +31,7 @@ package phases
3131
3232import (
3333 "path/filepath"
34+ "strings"
3435
3536 "arduino.cc/builder/builder_utils"
3637 "arduino.cc/builder/constants"
@@ -40,6 +41,9 @@ import (
4041 "arduino.cc/properties"
4142)
4243
44+ var PRECOMPILED_LIBRARIES_VALID_EXTENSIONS_STATIC = map [string ]bool {".a" : true }
45+ var PRECOMPILED_LIBRARIES_VALID_EXTENSIONS_DYNAMIC = map [string ]bool {".so" : true }
46+
4347type LibrariesBuilder struct {}
4448
4549func (s * LibrariesBuilder ) Run (ctx * types.Context ) error {
@@ -64,6 +68,34 @@ func (s *LibrariesBuilder) Run(ctx *types.Context) error {
6468
6569 ctx .LibrariesObjectFiles = objectFiles
6670
71+ // Search for precompiled libraries
72+ fixLDFLAGforPrecompiledLibraries (ctx , libraries )
73+
74+ return nil
75+ }
76+
77+ func fixLDFLAGforPrecompiledLibraries (ctx * types.Context , libraries []* types.Library ) error {
78+
79+ for _ , library := range libraries {
80+ if library .Precompiled {
81+ // add library src path to compiler.c.elf.extra_flags
82+ // use library.Name as lib name and srcPath/{mcpu} as location
83+ mcu := ctx .BuildProperties [constants .BUILD_PROPERTIES_BUILD_MCU ]
84+ path := filepath .Join (library .SrcFolder , mcu )
85+ // find all library names in the folder and prepend -l
86+ filePaths := []string {}
87+ libs_cmd := library .LDflags + " "
88+ extensions := func (ext string ) bool { return PRECOMPILED_LIBRARIES_VALID_EXTENSIONS_DYNAMIC [ext ] }
89+ utils .FindFilesInFolder (& filePaths , path , extensions , true )
90+ for _ , lib := range filePaths {
91+ name := strings .TrimSuffix (filepath .Base (lib ), filepath .Ext (lib ))
92+ // strip "lib" first occurrence
93+ name = strings .Replace (name , "lib" , "" , 1 )
94+ libs_cmd += "-l" + name + " "
95+ }
96+ ctx .BuildProperties [constants .BUILD_PROPERTIES_COMPILER_C_ELF_EXTRAFLAGS ] += "\" -L" + path + "\" " + libs_cmd
97+ }
98+ }
6799 return nil
68100}
69101
@@ -93,6 +125,24 @@ func compileLibrary(library *types.Library, buildPath string, buildProperties pr
93125 }
94126
95127 objectFiles := []string {}
128+
129+ if library .Precompiled {
130+ // search for files with PRECOMPILED_LIBRARIES_VALID_EXTENSIONS
131+ extensions := func (ext string ) bool { return PRECOMPILED_LIBRARIES_VALID_EXTENSIONS_STATIC [ext ] }
132+
133+ filePaths := []string {}
134+ mcu := buildProperties [constants .BUILD_PROPERTIES_BUILD_MCU ]
135+ err := utils .FindFilesInFolder (& filePaths , filepath .Join (library .SrcFolder , mcu ), extensions , true )
136+ if err != nil {
137+ return nil , i18n .WrapError (err )
138+ }
139+ for _ , path := range filePaths {
140+ if strings .Contains (filepath .Base (path ), library .RealName ) {
141+ objectFiles = append (objectFiles , path )
142+ }
143+ }
144+ }
145+
96146 if library .Layout == types .LIBRARY_RECURSIVE {
97147 objectFiles , err = builder_utils .CompileFilesRecursive (objectFiles , library .SrcFolder , libraryBuildPath , buildProperties , includes , verbose , warningsLevel , logger )
98148 if err != nil {
0 commit comments