|
| 1 | +// This file is part of arduino-cli. |
| 2 | +// |
| 3 | +// Copyright 2020 ARDUINO SA (http://www.arduino.cc/) |
| 4 | +// |
| 5 | +// This software is released under the GNU General Public License version 3, |
| 6 | +// which covers the main part of arduino-cli. |
| 7 | +// The terms of this license can be found at: |
| 8 | +// https://www.gnu.org/licenses/gpl-3.0.en.html |
| 9 | +// |
| 10 | +// You can be released from the requirements of the above licenses by purchasing |
| 11 | +// a commercial license. Buying such a license is mandatory if you want to |
| 12 | +// modify or otherwise use the software for commercial activities involving the |
| 13 | +// Arduino software without disclosing the source code of your own applications. |
| 14 | +// To purchase a commercial license, send an email to [email protected]. |
| 15 | + |
| 16 | +package builder |
| 17 | + |
| 18 | +import ( |
| 19 | + "bytes" |
| 20 | + |
| 21 | + "github.com/arduino/arduino-cli/arduino/cores" |
| 22 | + "github.com/arduino/arduino-cli/arduino/libraries" |
| 23 | + "github.com/arduino/arduino-cli/arduino/libraries/librariesmanager" |
| 24 | + "github.com/arduino/arduino-cli/arduino/libraries/librariesresolver" |
| 25 | + "github.com/arduino/go-paths-helper" |
| 26 | + "github.com/pkg/errors" |
| 27 | +) |
| 28 | + |
| 29 | +func LibrariesLoader( |
| 30 | + useCachedLibrariesResolution bool, |
| 31 | + librariesManager *librariesmanager.LibrariesManager, |
| 32 | + builtInLibrariesDirs *paths.Path, libraryDirs, otherLibrariesDirs paths.PathList, |
| 33 | + actualPlatform, targetPlatform *cores.PlatformRelease, |
| 34 | +) (*librariesmanager.LibrariesManager, *librariesresolver.Cpp, []byte, error) { |
| 35 | + verboseOut := &bytes.Buffer{} |
| 36 | + lm := librariesManager |
| 37 | + if useCachedLibrariesResolution { |
| 38 | + // Since we are using the cached libraries resolution |
| 39 | + // the library manager is not needed. |
| 40 | + lm = librariesmanager.NewLibraryManager(nil, nil) |
| 41 | + } |
| 42 | + if librariesManager == nil { |
| 43 | + lm = librariesmanager.NewLibraryManager(nil, nil) |
| 44 | + |
| 45 | + builtInLibrariesFolders := builtInLibrariesDirs |
| 46 | + if builtInLibrariesFolders != nil { |
| 47 | + if err := builtInLibrariesFolders.ToAbs(); err != nil { |
| 48 | + return nil, nil, nil, errors.WithStack(err) |
| 49 | + } |
| 50 | + lm.AddLibrariesDir(builtInLibrariesFolders, libraries.IDEBuiltIn) |
| 51 | + } |
| 52 | + |
| 53 | + if actualPlatform != targetPlatform { |
| 54 | + lm.AddPlatformReleaseLibrariesDir(actualPlatform, libraries.ReferencedPlatformBuiltIn) |
| 55 | + } |
| 56 | + lm.AddPlatformReleaseLibrariesDir(targetPlatform, libraries.PlatformBuiltIn) |
| 57 | + |
| 58 | + librariesFolders := otherLibrariesDirs |
| 59 | + if err := librariesFolders.ToAbs(); err != nil { |
| 60 | + return nil, nil, nil, errors.WithStack(err) |
| 61 | + } |
| 62 | + for _, folder := range librariesFolders { |
| 63 | + lm.AddLibrariesDir(folder, libraries.User) |
| 64 | + } |
| 65 | + |
| 66 | + for _, status := range lm.RescanLibraries() { |
| 67 | + // With the refactoring of the initialization step of the CLI we changed how |
| 68 | + // errors are returned when loading platforms and libraries, that meant returning a list of |
| 69 | + // errors instead of a single one to enhance the experience for the user. |
| 70 | + // I have no intention right now to start a refactoring of the legacy package too, so |
| 71 | + // here's this shitty solution for now. |
| 72 | + // When we're gonna refactor the legacy package this will be gone. |
| 73 | + verboseOut.Write([]byte(status.Message())) |
| 74 | + } |
| 75 | + |
| 76 | + for _, dir := range libraryDirs { |
| 77 | + // Libraries specified this way have top priority |
| 78 | + if err := lm.LoadLibraryFromDir(dir, libraries.Unmanaged); err != nil { |
| 79 | + return nil, nil, nil, errors.WithStack(err) |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + resolver := librariesresolver.NewCppResolver() |
| 85 | + if err := resolver.ScanIDEBuiltinLibraries(lm); err != nil { |
| 86 | + return nil, nil, nil, errors.WithStack(err) |
| 87 | + } |
| 88 | + if err := resolver.ScanUserAndUnmanagedLibraries(lm); err != nil { |
| 89 | + return nil, nil, nil, errors.WithStack(err) |
| 90 | + } |
| 91 | + if err := resolver.ScanPlatformLibraries(lm, targetPlatform); err != nil { |
| 92 | + return nil, nil, nil, errors.WithStack(err) |
| 93 | + } |
| 94 | + if actualPlatform != targetPlatform { |
| 95 | + if err := resolver.ScanPlatformLibraries(lm, actualPlatform); err != nil { |
| 96 | + return nil, nil, nil, errors.WithStack(err) |
| 97 | + } |
| 98 | + } |
| 99 | + return lm, resolver, verboseOut.Bytes(), nil |
| 100 | +} |
0 commit comments