|
| 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 | + "encoding/json" |
| 20 | + "path/filepath" |
| 21 | + "strings" |
| 22 | + |
| 23 | + "github.com/arduino/arduino-cli/arduino/builder/logger" |
| 24 | + "github.com/arduino/arduino-cli/arduino/builder/utils" |
| 25 | + "github.com/arduino/arduino-cli/arduino/cores" |
| 26 | + "github.com/arduino/arduino-cli/arduino/sketch" |
| 27 | + "github.com/arduino/go-paths-helper" |
| 28 | + properties "github.com/arduino/go-properties-orderedmap" |
| 29 | + "github.com/pkg/errors" |
| 30 | +) |
| 31 | + |
| 32 | +// BuildOptionsManager fixdoc |
| 33 | +type BuildOptionsManager struct { |
| 34 | + currentOptions *properties.Map |
| 35 | + currentBuildOptionsJSON []byte |
| 36 | + |
| 37 | + hardwareDirs paths.PathList |
| 38 | + builtInToolsDirs paths.PathList |
| 39 | + otherLibrariesDirs paths.PathList |
| 40 | + builtInLibrariesDirs *paths.Path |
| 41 | + buildPath *paths.Path |
| 42 | + runtimePlatformPath *paths.Path |
| 43 | + buildCorePath *paths.Path |
| 44 | + sketch *sketch.Sketch |
| 45 | + customBuildProperties []string |
| 46 | + compilerOptimizationFlags string |
| 47 | + clean bool |
| 48 | + builderLogger *logger.BuilderLogger |
| 49 | +} |
| 50 | + |
| 51 | +// NewBuildOptionsManager fixdoc |
| 52 | +func NewBuildOptionsManager( |
| 53 | + hardwareDirs, builtInToolsDirs, otherLibrariesDirs paths.PathList, |
| 54 | + builtInLibrariesDirs, buildPath *paths.Path, |
| 55 | + sketch *sketch.Sketch, |
| 56 | + customBuildProperties []string, |
| 57 | + fqbn *cores.FQBN, |
| 58 | + clean bool, |
| 59 | + compilerOptimizationFlags string, |
| 60 | + runtimePlatformPath, buildCorePath *paths.Path, |
| 61 | + buildLogger *logger.BuilderLogger, |
| 62 | +) *BuildOptionsManager { |
| 63 | + opts := properties.NewMap() |
| 64 | + |
| 65 | + opts.Set("hardwareFolders", strings.Join(hardwareDirs.AsStrings(), ",")) |
| 66 | + opts.Set("builtInToolsFolders", strings.Join(builtInToolsDirs.AsStrings(), ",")) |
| 67 | + opts.Set("otherLibrariesFolders", strings.Join(otherLibrariesDirs.AsStrings(), ",")) |
| 68 | + opts.SetPath("sketchLocation", sketch.FullPath) |
| 69 | + opts.Set("fqbn", fqbn.String()) |
| 70 | + opts.Set("customBuildProperties", strings.Join(customBuildProperties, ",")) |
| 71 | + opts.Set("compiler.optimization_flags", compilerOptimizationFlags) |
| 72 | + |
| 73 | + if builtInLibrariesDirs != nil { |
| 74 | + opts.Set("builtInLibrariesFolders", builtInLibrariesDirs.String()) |
| 75 | + } |
| 76 | + |
| 77 | + absPath := sketch.FullPath.Parent() |
| 78 | + var additionalFilesRelative []string |
| 79 | + for _, f := range sketch.AdditionalFiles { |
| 80 | + relPath, err := f.RelTo(absPath) |
| 81 | + if err != nil { |
| 82 | + continue // ignore |
| 83 | + } |
| 84 | + additionalFilesRelative = append(additionalFilesRelative, relPath.String()) |
| 85 | + } |
| 86 | + opts.Set("additionalFiles", strings.Join(additionalFilesRelative, ",")) |
| 87 | + |
| 88 | + return &BuildOptionsManager{ |
| 89 | + currentOptions: opts, |
| 90 | + hardwareDirs: hardwareDirs, |
| 91 | + builtInToolsDirs: builtInToolsDirs, |
| 92 | + otherLibrariesDirs: otherLibrariesDirs, |
| 93 | + builtInLibrariesDirs: builtInLibrariesDirs, |
| 94 | + buildPath: buildPath, |
| 95 | + runtimePlatformPath: runtimePlatformPath, |
| 96 | + buildCorePath: buildCorePath, |
| 97 | + sketch: sketch, |
| 98 | + customBuildProperties: customBuildProperties, |
| 99 | + compilerOptimizationFlags: compilerOptimizationFlags, |
| 100 | + clean: clean, |
| 101 | + builderLogger: buildLogger, |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +// WipeBuildPath fixdoc |
| 106 | +func (m *BuildOptionsManager) WipeBuildPath() error { |
| 107 | + buildOptionsJSON, err := json.MarshalIndent(m.currentOptions, "", " ") |
| 108 | + if err != nil { |
| 109 | + return errors.WithStack(err) |
| 110 | + } |
| 111 | + m.currentBuildOptionsJSON = buildOptionsJSON |
| 112 | + |
| 113 | + if err := m.wipeBuildPath(); err != nil { |
| 114 | + return errors.WithStack(err) |
| 115 | + } |
| 116 | + return m.buildPath.Join("build.options.json").WriteFile(buildOptionsJSON) |
| 117 | +} |
| 118 | + |
| 119 | +func (m *BuildOptionsManager) wipeBuildPath() error { |
| 120 | + wipe := func() error { |
| 121 | + // FIXME: this should go outside legacy and behind a `logrus` call so users can |
| 122 | + // control when this should be printed. |
| 123 | + // logger.Println(constants.LOG_LEVEL_INFO, constants.MSG_BUILD_OPTIONS_CHANGED + constants.MSG_REBUILD_ALL) |
| 124 | + if err := m.buildPath.RemoveAll(); err != nil { |
| 125 | + return errors.WithMessage(err, tr("cleaning build path")) |
| 126 | + } |
| 127 | + if err := m.buildPath.MkdirAll(); err != nil { |
| 128 | + return errors.WithMessage(err, tr("cleaning build path")) |
| 129 | + } |
| 130 | + return nil |
| 131 | + } |
| 132 | + |
| 133 | + if m.clean { |
| 134 | + return wipe() |
| 135 | + } |
| 136 | + |
| 137 | + // Load previous build options map |
| 138 | + var buildOptionsJSONPrevious []byte |
| 139 | + var _err error |
| 140 | + if buildOptionsFile := m.buildPath.Join("build.options.json"); buildOptionsFile.Exist() { |
| 141 | + buildOptionsJSONPrevious, _err = buildOptionsFile.ReadFile() |
| 142 | + if _err != nil { |
| 143 | + return errors.WithStack(_err) |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + if len(buildOptionsJSONPrevious) == 0 { |
| 148 | + return nil |
| 149 | + } |
| 150 | + |
| 151 | + var prevOpts *properties.Map |
| 152 | + if err := json.Unmarshal(buildOptionsJSONPrevious, &prevOpts); err != nil || prevOpts == nil { |
| 153 | + m.builderLogger.Info(tr("%[1]s invalid, rebuilding all", "build.options.json")) |
| 154 | + return wipe() |
| 155 | + } |
| 156 | + |
| 157 | + // If SketchLocation path is different but filename is the same, consider it equal |
| 158 | + if filepath.Base(m.currentOptions.Get("sketchLocation")) == filepath.Base(prevOpts.Get("sketchLocation")) { |
| 159 | + m.currentOptions.Remove("sketchLocation") |
| 160 | + prevOpts.Remove("sketchLocation") |
| 161 | + } |
| 162 | + |
| 163 | + // If options are not changed check if core has |
| 164 | + if m.currentOptions.Equals(prevOpts) { |
| 165 | + // check if any of the files contained in the core folders has changed |
| 166 | + // since the json was generated - like platform.txt or similar |
| 167 | + // if so, trigger a "safety" wipe |
| 168 | + targetCoreFolder := m.runtimePlatformPath |
| 169 | + coreFolder := m.buildCorePath |
| 170 | + realCoreFolder := coreFolder.Parent().Parent() |
| 171 | + jsonPath := m.buildPath.Join("build.options.json") |
| 172 | + coreUnchanged, _ := utils.DirContentIsOlderThan(realCoreFolder, jsonPath, ".txt") |
| 173 | + if coreUnchanged && targetCoreFolder != nil && !realCoreFolder.EqualsTo(targetCoreFolder) { |
| 174 | + coreUnchanged, _ = utils.DirContentIsOlderThan(targetCoreFolder, jsonPath, ".txt") |
| 175 | + } |
| 176 | + if coreUnchanged { |
| 177 | + return nil |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + return wipe() |
| 182 | +} |
0 commit comments