-
-
Notifications
You must be signed in to change notification settings - Fork 374
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
heavily refactored the ContainerBuildOptions
- Loading branch information
1 parent
900b67e
commit 41d66c6
Showing
15 changed files
with
307 additions
and
498 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
// This file is part of arduino-cli. | ||
// | ||
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/) | ||
// | ||
// This software is released under the GNU General Public License version 3, | ||
// which covers the main part of arduino-cli. | ||
// The terms of this license can be found at: | ||
// https://www.gnu.org/licenses/gpl-3.0.en.html | ||
// | ||
// You can be released from the requirements of the above licenses by purchasing | ||
// a commercial license. Buying such a license is mandatory if you want to | ||
// modify or otherwise use the software for commercial activities involving the | ||
// Arduino software without disclosing the source code of your own applications. | ||
// To purchase a commercial license, send an email to [email protected]. | ||
|
||
package builder | ||
|
||
import ( | ||
"encoding/json" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/arduino/arduino-cli/arduino/builder/logger" | ||
"github.com/arduino/arduino-cli/arduino/builder/utils" | ||
"github.com/arduino/arduino-cli/arduino/sketch" | ||
"github.com/arduino/go-paths-helper" | ||
properties "github.com/arduino/go-properties-orderedmap" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
// BuildOptionsManager fixdoc | ||
type BuildOptionsManager struct { | ||
currentOptions *properties.Map | ||
currentBuildOptionsJSON []byte | ||
|
||
hardwareDirs paths.PathList | ||
builtInToolsDirs paths.PathList | ||
otherLibrariesDirs paths.PathList | ||
builtInLibrariesDirs *paths.Path | ||
buildPath *paths.Path | ||
runtimePlatformPath *paths.Path | ||
buildCorePath *paths.Path | ||
sketch *sketch.Sketch | ||
customBuildProperties []string | ||
fqbn string | ||
compilerOptimizationFlags string | ||
clean bool | ||
builderLogger *logger.BuilderLogger | ||
} | ||
|
||
// NewBuildOptionsManager fixdoc | ||
func NewBuildOptionsManager( | ||
hardwareDirs, builtInToolsDirs, otherLibrariesDirs paths.PathList, | ||
builtInLibrariesDirs, buildPath *paths.Path, | ||
sketch *sketch.Sketch, | ||
customBuildProperties []string, | ||
fqbn string, | ||
clean bool, | ||
compilerOptimizationFlags string, | ||
runtimePlatformPath, buildCorePath *paths.Path, | ||
buildLogger *logger.BuilderLogger, | ||
) *BuildOptionsManager { | ||
opts := properties.NewMap() | ||
|
||
opts.Set("hardwareFolders", strings.Join(hardwareDirs.AsStrings(), ",")) | ||
opts.Set("builtInToolsFolders", strings.Join(builtInToolsDirs.AsStrings(), ",")) | ||
opts.Set("otherLibrariesFolders", strings.Join(otherLibrariesDirs.AsStrings(), ",")) | ||
opts.SetPath("sketchLocation", sketch.FullPath) | ||
opts.Set("fqbn", fqbn) | ||
opts.Set("customBuildProperties", strings.Join(customBuildProperties, ",")) | ||
opts.Set("compiler.optimization_flags", compilerOptimizationFlags) | ||
|
||
if builtInLibrariesDirs != nil { | ||
opts.Set("builtInLibrariesFolders", builtInLibrariesDirs.String()) | ||
} | ||
|
||
absPath := sketch.FullPath.Parent() | ||
var additionalFilesRelative []string | ||
for _, f := range sketch.AdditionalFiles { | ||
relPath, err := f.RelTo(absPath) | ||
if err != nil { | ||
continue // ignore | ||
} | ||
additionalFilesRelative = append(additionalFilesRelative, relPath.String()) | ||
} | ||
opts.Set("additionalFiles", strings.Join(additionalFilesRelative, ",")) | ||
|
||
return &BuildOptionsManager{ | ||
currentOptions: opts, | ||
hardwareDirs: hardwareDirs, | ||
builtInToolsDirs: builtInToolsDirs, | ||
otherLibrariesDirs: otherLibrariesDirs, | ||
builtInLibrariesDirs: builtInLibrariesDirs, | ||
buildPath: buildPath, | ||
runtimePlatformPath: runtimePlatformPath, | ||
buildCorePath: buildCorePath, | ||
sketch: sketch, | ||
customBuildProperties: customBuildProperties, | ||
fqbn: fqbn, | ||
compilerOptimizationFlags: compilerOptimizationFlags, | ||
clean: clean, | ||
builderLogger: buildLogger, | ||
} | ||
} | ||
|
||
// WipeBuildPath fixdoc | ||
func (m *BuildOptionsManager) WipeBuildPath() error { | ||
buildOptionsJSON, err := json.MarshalIndent(m.currentOptions, "", " ") | ||
if err != nil { | ||
return errors.WithStack(err) | ||
} | ||
m.currentBuildOptionsJSON = buildOptionsJSON | ||
|
||
if err := m.wipeBuildPath(); err != nil { | ||
return errors.WithStack(err) | ||
} | ||
return m.buildPath.Join("build.options.json").WriteFile(buildOptionsJSON) | ||
} | ||
|
||
func (m *BuildOptionsManager) wipeBuildPath() error { | ||
wipe := func() error { | ||
// FIXME: this should go outside legacy and behind a `logrus` call so users can | ||
// control when this should be printed. | ||
// logger.Println(constants.LOG_LEVEL_INFO, constants.MSG_BUILD_OPTIONS_CHANGED + constants.MSG_REBUILD_ALL) | ||
if err := m.buildPath.RemoveAll(); err != nil { | ||
return errors.WithMessage(err, tr("cleaning build path")) | ||
} | ||
if err := m.buildPath.MkdirAll(); err != nil { | ||
return errors.WithMessage(err, tr("cleaning build path")) | ||
} | ||
return nil | ||
} | ||
|
||
if m.clean { | ||
return wipe() | ||
} | ||
|
||
// Load previous build options map | ||
var buildOptionsJSONPrevious []byte | ||
var _err error | ||
if buildOptionsFile := m.buildPath.Join("build.options.json"); buildOptionsFile.Exist() { | ||
buildOptionsJSONPrevious, _err = buildOptionsFile.ReadFile() | ||
if _err != nil { | ||
return errors.WithStack(_err) | ||
} | ||
} | ||
|
||
if len(buildOptionsJSONPrevious) == 0 { | ||
return nil | ||
} | ||
|
||
var prevOpts *properties.Map | ||
if err := json.Unmarshal(buildOptionsJSONPrevious, &prevOpts); err != nil || prevOpts == nil { | ||
m.builderLogger.Info(tr("%[1]s invalid, rebuilding all", "build.options.json")) | ||
return wipe() | ||
} | ||
|
||
// If SketchLocation path is different but filename is the same, consider it equal | ||
if filepath.Base(m.currentOptions.Get("sketchLocation")) == filepath.Base(prevOpts.Get("sketchLocation")) { | ||
m.currentOptions.Remove("sketchLocation") | ||
prevOpts.Remove("sketchLocation") | ||
} | ||
|
||
// If options are not changed check if core has | ||
if m.currentOptions.Equals(prevOpts) { | ||
// check if any of the files contained in the core folders has changed | ||
// since the json was generated - like platform.txt or similar | ||
// if so, trigger a "safety" wipe | ||
targetCoreFolder := m.runtimePlatformPath | ||
coreFolder := m.buildCorePath | ||
realCoreFolder := coreFolder.Parent().Parent() | ||
jsonPath := m.buildPath.Join("build.options.json") | ||
coreUnchanged, _ := utils.DirContentIsOlderThan(realCoreFolder, jsonPath, ".txt") | ||
if coreUnchanged && targetCoreFolder != nil && !realCoreFolder.EqualsTo(targetCoreFolder) { | ||
coreUnchanged, _ = utils.DirContentIsOlderThan(targetCoreFolder, jsonPath, ".txt") | ||
} | ||
if coreUnchanged { | ||
return nil | ||
} | ||
} | ||
|
||
return wipe() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.