Commit c8ae01ab authored by Cristian Maglie's avatar Cristian Maglie

Added LibraryLocation field to Libraries

parent b8b3b790
......@@ -15,7 +15,7 @@
"types",
"utils"
]
revision = "fd8b7dcea92845ba26eb0252aae29ccaef635a4e"
revision = "4722d661a3ceb9adff5f9a26ea558d6c110ace6e"
source = "github.com/bcmi-labs/arduino-builder"
[[projects]]
......
......@@ -64,6 +64,7 @@ type Library struct {
Folder *paths.Path
SrcFolder *paths.Path
UtilityFolder *paths.Path
Location LibraryLocation
Layout LibraryLayout
RealName string
DotALinkage bool
......
/*
* This file is part of arduino-cli.
*
* Copyright 2018 ARDUINO AG (http://www.arduino.cc/)
*
* arduino-cli is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As a special exception, you may use this file as part of a free software
* library without restriction. Specifically, if other files instantiate
* templates or use macros or inline functions from this file, or you compile
* this file and link it with other files to produce an executable, this
* file does not by itself cause the resulting executable to be covered by
* the GNU General Public License. This exception does not however
* invalidate any other reasons why the executable file might be covered by
* the GNU General Public License.
*/
package libraries
import (
"encoding/json"
"fmt"
)
// LibraryLocation represents where the library is installed
type LibraryLocation int
// The enumeration is listed in ascending order of priority
const (
// IDEBuiltIn are libraries bundled in the IDE
IDEBuiltIn = 1 << iota
// PlatformBuiltIn are libraries bundled in a PlatformRelease
PlatformBuiltIn
// ReferencedPlatformBuiltIn are libraries bundled in a PlatformRelease referenced for build
ReferencedPlatformBuiltIn
// Sketchbook are user installed libraries
Sketchbook
)
func (d *LibraryLocation) String() string {
switch *d {
case IDEBuiltIn:
return "ide"
case PlatformBuiltIn:
return "platform"
case ReferencedPlatformBuiltIn:
return "ref-platform"
case Sketchbook:
return "sketchbook"
}
panic(fmt.Sprintf("invalid LibraryLocation value %d", *d))
}
// MarshalJSON implements the json.Marshaler interface
func (d *LibraryLocation) MarshalJSON() ([]byte, error) {
switch *d {
case IDEBuiltIn:
return json.Marshal("ide")
case PlatformBuiltIn:
return json.Marshal("platform")
case ReferencedPlatformBuiltIn:
return json.Marshal("ref-platform")
case Sketchbook:
return json.Marshal("sketchbook")
}
return nil, fmt.Errorf("invalid library location value: %d", *d)
}
// UnmarshalJSON implements the json.Unmarshaler interface
func (d *LibraryLocation) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
switch s {
case "ide":
*d = IDEBuiltIn
case "platform":
*d = PlatformBuiltIn
case "ref-platform":
*d = ReferencedPlatformBuiltIn
case "sketchbook":
*d = Sketchbook
}
return fmt.Errorf("invalid library location: %s", s)
}
......@@ -44,6 +44,14 @@ import (
type LibrariesManager struct {
Libraries map[string]*LibraryAlternatives `json:"libraries"`
Index *librariesindex.Index
librariesDir []*LibrariesDir
}
// LibrariesDir is a directory containing libraries
type LibrariesDir struct {
Path *paths.Path
Location libraries.LibraryLocation
}
// LibraryAlternatives is a list of different versions of the same library
......@@ -93,17 +101,44 @@ func (sc *LibrariesManager) LoadIndex() error {
return err
}
// AddLibrariesDir adds allPaths to the list of directories
// to scan when searching for libraries. If a path is already
// in the list it is ignored.
func (sc *LibrariesManager) AddLibrariesDir(location libraries.LibraryLocation, allPaths ...*paths.Path) {
for _, path := range allPaths {
for _, dir := range sc.librariesDir {
if dir.Path.EquivalentTo(path) {
return
}
}
sc.librariesDir = append(sc.librariesDir, &LibrariesDir{
Path: path,
Location: location,
})
}
}
// RescanLibraries reload all installed libraries in the system.
func (sc *LibrariesManager) RescanLibraries() error {
for _, dir := range sc.librariesDir {
if err := sc.LoadLibrariesFromDir(dir); err != nil {
return fmt.Errorf("loading libs from %s: %s", dir.Path, err)
}
}
return nil
}
// LoadLibrariesFromDir loads all libraries in the given folder
func (sc *LibrariesManager) LoadLibrariesFromDir(librariesDir *LibrariesDir) error {
subFolders, err := librariesDir.ReadDir()
subFolders, err := librariesDir.Path.ReadDir()
if err != nil {
return fmt.Errorf("reading dir %s: %s", librariesDir, err)
return fmt.Errorf("reading dir %s: %s", librariesDir.Path, err)
}
subFolders.FilterDirs()
subFolders.FilterOutHiddenFiles()
for _, subFolder := range subFolders {
library, err := libraries.Load(subFolder)
library, err := libraries.Load(subFolder, librariesDir.Location)
if err != nil {
return fmt.Errorf("loading library from %s: %s", subFolder, err)
}
......
......@@ -38,11 +38,11 @@ import (
)
// Load loads a library from the given folder
func Load(libDir *paths.Path) (*Library, error) {
func Load(libDir *paths.Path, location LibraryLocation) (*Library, error) {
if exist, _ := libDir.Join("library.properties").Exist(); exist {
return makeNewLibrary(libDir)
return makeNewLibrary(libDir, location)
}
return makeLegacyLibrary(libDir)
return makeLegacyLibrary(libDir, location)
}
func addUtilityFolder(library *Library) {
......@@ -52,7 +52,7 @@ func addUtilityFolder(library *Library) {
}
}
func makeNewLibrary(libraryFolder *paths.Path) (*Library, error) {
func makeNewLibrary(libraryFolder *paths.Path, location LibraryLocation) (*Library, error) {
libProperties, err := properties.Load(libraryFolder.Join("library.properties").String())
if err != nil {
return nil, fmt.Errorf("loading library.properties: %s", err)
......@@ -69,6 +69,7 @@ func makeNewLibrary(libraryFolder *paths.Path) (*Library, error) {
}
library := &Library{}
library.Location = location
library.Folder = libraryFolder
if exist, _ := libraryFolder.Join("src").Exist(); exist {
library.Layout = RecursiveLayout
......@@ -115,12 +116,13 @@ func makeNewLibrary(libraryFolder *paths.Path) (*Library, error) {
return library, nil
}
func makeLegacyLibrary(libraryFolder *paths.Path) (*Library, error) {
func makeLegacyLibrary(path *paths.Path, location LibraryLocation) (*Library, error) {
library := &Library{
Folder: libraryFolder,
SrcFolder: libraryFolder,
Folder: path,
Location: location,
SrcFolder: path,
Layout: FlatLayout,
Name: libraryFolder.Base(),
Name: path.Base(),
Architectures: []string{"*"},
IsLegacy: true,
}
......
......@@ -33,9 +33,12 @@ import (
"os"
"strings"
paths "github.com/arduino/go-paths-helper"
"github.com/bcmi-labs/arduino-cli/commands"
"github.com/bcmi-labs/arduino-cli/common/formatter"
"github.com/bcmi-labs/arduino-cli/configs"
"github.com/bcmi-labs/arduino-cli/arduino/libraries"
"github.com/bcmi-labs/arduino-cli/arduino/libraries/librariesmanager"
"github.com/bcmi-labs/arduino-cli/common/formatter/output"
"github.com/sirupsen/logrus"
......@@ -79,6 +82,12 @@ func resultFromFileName(file os.FileInfo, libs *output.LibProcessResults) {
func getLibraryManager() *librariesmanager.LibrariesManager {
logrus.Info("Starting libraries manager")
lm := librariesmanager.NewLibraryManager()
if libHome, err := configs.LibrariesFolder.Get(); err != nil {
formatter.PrintError(err, "Cannot get libraries folder.")
os.Exit(commands.ErrCoreConfig)
} else {
lm.AddLibrariesDir(libraries.Sketchbook, paths.New(libHome))
}
if err := lm.LoadIndex(); err != nil {
logrus.WithError(err).Warn("Error during libraries index loading, try to download it again")
updateIndex()
......
......@@ -32,11 +32,9 @@ package lib
import (
"os"
paths "github.com/arduino/go-paths-helper"
"github.com/bcmi-labs/arduino-cli/commands"
"github.com/bcmi-labs/arduino-cli/common/formatter"
"github.com/bcmi-labs/arduino-cli/common/formatter/output"
"github.com/bcmi-labs/arduino-cli/configs"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
......@@ -57,14 +55,8 @@ func initListCommand() *cobra.Command {
}
func runListCommand(cmd *cobra.Command, args []string) {
libHome, err := configs.LibrariesFolder.Get()
if err != nil {
formatter.PrintError(err, "Cannot get libraries folder.")
os.Exit(commands.ErrCoreConfig)
}
lm := getLibraryManager()
if err := lm.LoadLibrariesFromDir(paths.New(libHome)); err != nil {
if err := lm.RescanLibraries(); err != nil {
formatter.PrintError(err, "Error loading libraries.")
os.Exit(commands.ErrCoreConfig)
}
......
......@@ -42,41 +42,39 @@ import (
type LibrariesLoader struct{}
func (s *LibrariesLoader) Run(ctx *types.Context) error {
lm := librariesmanager.NewLibraryManager()
ctx.LibrariesManager = lm
builtInLibrariesFolders := ctx.BuiltInLibrariesFolders
if err := builtInLibrariesFolders.ToAbs(); err != nil {
return i18n.WrapError(err)
}
sortedLibrariesFolders := builtInLibrariesFolders.Clone()
lm.AddLibrariesDir(libraries.IDEBuiltIn, builtInLibrariesFolders...)
actualPlatform := ctx.ActualPlatform
platform := ctx.TargetPlatform
debugLevel := ctx.DebugLevel
logger := ctx.GetLogger()
actualPlatform := ctx.ActualPlatform
platform := ctx.TargetPlatform
if actualPlatform != platform {
if dir := actualPlatform.GetLibrariesDir(); dir != nil {
sortedLibrariesFolders.Add(dir)
lm.AddLibrariesDir(libraries.ReferencedPlatformBuiltIn, dir)
}
}
if dir := platform.GetLibrariesDir(); dir != nil {
sortedLibrariesFolders.Add(dir)
lm.AddLibrariesDir(libraries.PlatformBuiltIn, dir)
}
librariesFolders := ctx.OtherLibrariesFolders
if err := librariesFolders.ToAbs(); err != nil {
return i18n.WrapError(err)
}
sortedLibrariesFolders.AddAllMissing(librariesFolders)
lm.AddLibrariesDir(libraries.Sketchbook, librariesFolders...)
ctx.LibrariesFolders = sortedLibrariesFolders
lm := librariesmanager.NewLibraryManager()
for _, libraryFolder := range sortedLibrariesFolders {
if err := lm.LoadLibrariesFromDir(libraryFolder); err != nil {
return i18n.WrapError(err)
}
if err := lm.RescanLibraries(); err != nil {
return i18n.WrapError(err)
}
if debugLevel > 0 {
for _, lib := range lm.Libraries {
for _, libAlt := range lib.Alternatives {
......@@ -91,8 +89,6 @@ func (s *LibrariesLoader) Run(ctx *types.Context) error {
}
}
ctx.LibrariesManager = lm
headerToLibraries := make(map[string][]*libraries.Library)
for _, lib := range lm.Libraries {
for _, library := range lib.Alternatives {
......
......@@ -52,13 +52,13 @@ func (s *LibrariesBuilder) Run(ctx *types.Context) error {
librariesBuildPath := ctx.LibrariesBuildPath
buildProperties := ctx.BuildProperties
includes := utils.Map(ctx.IncludeFolders.AsStrings(), utils.WrapWithHyphenI)
libraries := ctx.ImportedLibraries
libs := ctx.ImportedLibraries
if err := librariesBuildPath.MkdirAll(); err != nil {
return i18n.WrapError(err)
}
objectFiles, err := compileLibraries(ctx, libraries, librariesBuildPath, buildProperties, includes)
objectFiles, err := compileLibraries(ctx, libs, librariesBuildPath, buildProperties, includes)
if err != nil {
return i18n.WrapError(err)
}
......@@ -66,14 +66,14 @@ func (s *LibrariesBuilder) Run(ctx *types.Context) error {
ctx.LibrariesObjectFiles = objectFiles
// Search for precompiled libraries
fixLDFLAGforPrecompiledLibraries(ctx, libraries)
fixLDFLAGforPrecompiledLibraries(ctx, libs)
return nil
}
func fixLDFLAGforPrecompiledLibraries(ctx *types.Context, libraries []*libraries.Library) error {
func fixLDFLAGforPrecompiledLibraries(ctx *types.Context, libs []*libraries.Library) error {
for _, library := range libraries {
for _, library := range libs {
if library.Precompiled {
// add library src path to compiler.c.elf.extra_flags
// use library.Name as lib name and srcPath/{mcpu} as location
......
......@@ -25,7 +25,6 @@ type Context struct {
HardwareFolders paths.PathList
ToolsFolders paths.PathList
BuiltInToolsFolders paths.PathList
LibrariesFolders paths.PathList
BuiltInLibrariesFolders paths.PathList
OtherLibrariesFolders paths.PathList
SketchLocation *paths.Path
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment