Skip to content

Commit 8d63641

Browse files
authored
Merge pull request arduino#219 from facchinm/allow_precompiled_libs
Allow precompiled libs
2 parents 4781feb + 358be9b commit 8d63641

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

Diff for: src/arduino.cc/builder/constants/constants.go

+3
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ const BUILD_PROPERTIES_BUILD_SYSTEM_PATH = "build.system.path"
4848
const BUILD_PROPERTIES_BUILD_VARIANT = "build.variant"
4949
const BUILD_PROPERTIES_BUILD_VARIANT_PATH = "build.variant.path"
5050
const BUILD_PROPERTIES_COMPILER_C_ELF_FLAGS = "compiler.c.elf.flags"
51+
const BUILD_PROPERTIES_COMPILER_C_ELF_EXTRAFLAGS = "compiler.c.elf.extra_flags"
5152
const BUILD_PROPERTIES_COMPILER_CPP_FLAGS = "compiler.cpp.flags"
5253
const BUILD_PROPERTIES_COMPILER_PATH = "compiler.path"
5354
const BUILD_PROPERTIES_COMPILER_WARNING_FLAGS = "compiler.warning_flags"
@@ -138,6 +139,8 @@ const LIBRARY_LICENSE = "license"
138139
const LIBRARY_MAINTAINER = "maintainer"
139140
const LIBRARY_NAME = "name"
140141
const LIBRARY_PARAGRAPH = "paragraph"
142+
const LIBRARY_PRECOMPILED = "precompiled"
143+
const LIBRARY_LDFLAGS = "ldflags"
141144
const LIBRARY_PROPERTIES = "library.properties"
142145
const LIBRARY_SENTENCE = "sentence"
143146
const LIBRARY_URL = "url"

Diff for: src/arduino.cc/builder/libraries_loader.go

+3
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ func makeNewLibrary(libraryFolder string, debugLevel int, logger i18n.Logger) (*
184184
library.License = libProperties[constants.LIBRARY_LICENSE]
185185

186186
library.Name = filepath.Base(libraryFolder)
187+
library.RealName = strings.TrimSpace(libProperties[constants.LIBRARY_NAME])
187188
library.Version = strings.TrimSpace(libProperties[constants.LIBRARY_VERSION])
188189
library.Author = strings.TrimSpace(libProperties[constants.LIBRARY_AUTHOR])
189190
library.Maintainer = strings.TrimSpace(libProperties[constants.LIBRARY_MAINTAINER])
@@ -192,6 +193,8 @@ func makeNewLibrary(libraryFolder string, debugLevel int, logger i18n.Logger) (*
192193
library.URL = strings.TrimSpace(libProperties[constants.LIBRARY_URL])
193194
library.IsLegacy = false
194195
library.DotALinkage = strings.TrimSpace(libProperties[constants.LIBRARY_DOT_A_LINKAGE]) == "true"
196+
library.Precompiled = strings.TrimSpace(libProperties[constants.LIBRARY_PRECOMPILED]) == "true"
197+
library.LDflags = strings.TrimSpace(libProperties[constants.LIBRARY_LDFLAGS])
195198
library.Properties = libProperties
196199

197200
return library, nil

Diff for: src/arduino.cc/builder/phases/libraries_builder.go

+50
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ package phases
3131

3232
import (
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+
4347
type LibrariesBuilder struct{}
4448

4549
func (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 {

Diff for: src/arduino.cc/builder/types/types.go

+3
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,11 @@ type Library struct {
168168
UtilityFolder string
169169
Layout LibraryLayout
170170
Name string
171+
RealName string
171172
Archs []string
172173
DotALinkage bool
174+
Precompiled bool
175+
LDflags string
173176
IsLegacy bool
174177
Version string
175178
Author string

0 commit comments

Comments
 (0)