diff --git a/cli/compile/compile.go b/cli/compile/compile.go index 273d64dd9e9..a5c8a78acd5 100644 --- a/cli/compile/compile.go +++ b/cli/compile/compile.go @@ -51,6 +51,7 @@ var ( libraries []string // List of custom libraries paths separated by commas. Or can be used multiple times for multiple libraries paths. optimizeForDebug bool // Optimize compile output for debug, not for release programmer string // Use the specified programmer to upload + clean bool // Cleanup the build folder and do not use any cached build ) // NewCommand created a new `compile` command @@ -86,6 +87,7 @@ func NewCommand() *cobra.Command { "List of custom libraries paths separated by commas. Or can be used multiple times for multiple libraries paths.") command.Flags().BoolVar(&optimizeForDebug, "optimize-for-debug", false, "Optional, optimize compile output for debugging, rather than for release.") command.Flags().StringVarP(&programmer, "programmer", "P", "", "Optional, use the specified programmer to upload.") + command.Flags().BoolVar(&clean, "clean", false, "Optional, cleanup the build folder and do not use any cached build.") return command } @@ -121,6 +123,7 @@ func run(cmd *cobra.Command, args []string) { DryRun: dryRun, Libraries: libraries, OptimizeForDebug: optimizeForDebug, + Clean: clean, }, os.Stdout, os.Stderr, viper.GetString("logging.level") == "debug") if err != nil { diff --git a/commands/compile/compile.go b/commands/compile/compile.go index 2d220e1b84c..128aa9412ec 100644 --- a/commands/compile/compile.go +++ b/commands/compile/compile.go @@ -59,6 +59,7 @@ func Compile(ctx context.Context, req *rpc.CompileReq, outStream, errStream io.W "exportDir": telemetry.Sanitize(req.GetExportDir()), "jobs": strconv.FormatInt(int64(req.Jobs), 10), "libraries": strings.Join(req.Libraries, ","), + "clean": strconv.FormatBool(req.GetClean()), } if req.GetExportFile() != "" { @@ -188,6 +189,7 @@ func Compile(ctx context.Context, req *rpc.CompileReq, outStream, errStream io.W builderCtx.ExecStdout = outStream builderCtx.ExecStderr = errStream builderCtx.SetLogger(i18n.LoggerToCustomStreams{Stdout: outStream, Stderr: errStream}) + builderCtx.Clean = req.GetClean() // if --preprocess or --show-properties were passed, we can stop here if req.GetShowProperties() { diff --git a/legacy/builder/phases/core_builder.go b/legacy/builder/phases/core_builder.go index 92e017acb72..9fc85fc90cc 100644 --- a/legacy/builder/phases/core_builder.go +++ b/legacy/builder/phases/core_builder.go @@ -94,7 +94,7 @@ func compileCore(ctx *types.Context, buildPath *paths.Path, buildCachePath *path archivedCoreName := GetCachedCoreArchiveFileName(buildProperties.Get(constants.BUILD_PROPERTIES_FQBN), buildProperties.Get("compiler.optimization_flags"), realCoreFolder) targetArchivedCore = buildCachePath.Join(archivedCoreName) - canUseArchivedCore := !builder_utils.CoreOrReferencedCoreHasChanged(realCoreFolder, targetCoreFolder, targetArchivedCore) + canUseArchivedCore := !ctx.Clean && !builder_utils.CoreOrReferencedCoreHasChanged(realCoreFolder, targetCoreFolder, targetArchivedCore) if canUseArchivedCore { // use archived core diff --git a/legacy/builder/types/context.go b/legacy/builder/types/context.go index bcd24047550..cd5510e1897 100644 --- a/legacy/builder/types/context.go +++ b/legacy/builder/types/context.go @@ -70,6 +70,7 @@ type Context struct { ArduinoAPIVersion string FQBN *cores.FQBN CodeCompleteAt string + Clean bool // Build options are serialized here BuildOptionsJson string diff --git a/legacy/builder/wipeout_build_path_if_build_options_changed.go b/legacy/builder/wipeout_build_path_if_build_options_changed.go index 72237b875fb..57be59838d6 100644 --- a/legacy/builder/wipeout_build_path_if_build_options_changed.go +++ b/legacy/builder/wipeout_build_path_if_build_options_changed.go @@ -21,8 +21,8 @@ import ( "github.com/arduino/arduino-cli/legacy/builder/builder_utils" "github.com/arduino/arduino-cli/legacy/builder/constants" - "github.com/arduino/arduino-cli/legacy/builder/gohasissues" "github.com/arduino/arduino-cli/legacy/builder/types" + "github.com/arduino/go-paths-helper" properties "github.com/arduino/go-properties-orderedmap" "github.com/pkg/errors" ) @@ -30,6 +30,9 @@ import ( type WipeoutBuildPathIfBuildOptionsChanged struct{} func (s *WipeoutBuildPathIfBuildOptionsChanged) Run(ctx *types.Context) error { + if ctx.Clean { + return doCleanup(ctx.BuildPath) + } if ctx.BuildOptionsJsonPrevious == "" { return nil } @@ -64,18 +67,22 @@ func (s *WipeoutBuildPathIfBuildOptionsChanged) Run(ctx *types.Context) error { } } + return doCleanup(ctx.BuildPath) +} + +func doCleanup(buildPath *paths.Path) 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) - buildPath := ctx.BuildPath - files, err := gohasissues.ReadDir(buildPath.String()) - if err != nil { - return errors.WithStack(err) - } - for _, file := range files { - buildPath.Join(file.Name()).RemoveAll() + if files, err := buildPath.ReadDir(); err != nil { + return errors.WithMessage(err, "cleaning build path") + } else { + for _, file := range files { + if err := file.RemoveAll(); err != nil { + return errors.WithMessage(err, "cleaning build path") + } + } } - return nil } diff --git a/rpc/commands/compile.pb.go b/rpc/commands/compile.pb.go index ed4efd99a3e..0edd056793a 100644 --- a/rpc/commands/compile.pb.go +++ b/rpc/commands/compile.pb.go @@ -64,6 +64,7 @@ type CompileReq struct { OptimizeForDebug bool `protobuf:"varint,16,opt,name=optimizeForDebug,proto3" json:"optimizeForDebug,omitempty"` // Optimize compile output for debug, not for release. DryRun bool `protobuf:"varint,17,opt,name=dryRun,proto3" json:"dryRun,omitempty"` // When set to `true` the compiled binary will not be copied to the export directory. ExportDir string `protobuf:"bytes,18,opt,name=export_dir,json=exportDir,proto3" json:"export_dir,omitempty"` // Optional: save the build artifacts in this directory, the directory must exist. + Clean bool `protobuf:"varint,19,opt,name=clean,proto3" json:"clean,omitempty"` // Optional: cleanup the build folder and do not use any previously cached build } func (x *CompileReq) Reset() { @@ -225,6 +226,13 @@ func (x *CompileReq) GetExportDir() string { return "" } +func (x *CompileReq) GetClean() bool { + if x != nil { + return x.Clean + } + return false +} + type CompileResp struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache @@ -287,7 +295,7 @@ var file_commands_compile_proto_rawDesc = []byte{ 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x17, 0x63, 0x63, 0x2e, 0x61, 0x72, 0x64, 0x75, 0x69, 0x6e, 0x6f, 0x2e, 0x63, 0x6c, 0x69, 0x2e, 0x63, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x73, 0x1a, 0x15, 0x63, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x73, 0x2f, 0x63, 0x6f, 0x6d, 0x6d, - 0x6f, 0x6e, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0xd4, 0x04, 0x0a, 0x0a, 0x43, 0x6f, 0x6d, + 0x6f, 0x6e, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0xea, 0x04, 0x0a, 0x0a, 0x43, 0x6f, 0x6d, 0x70, 0x69, 0x6c, 0x65, 0x52, 0x65, 0x71, 0x12, 0x3d, 0x0a, 0x08, 0x69, 0x6e, 0x73, 0x74, 0x61, 0x6e, 0x63, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x21, 0x2e, 0x63, 0x63, 0x2e, 0x61, 0x72, 0x64, 0x75, 0x69, 0x6e, 0x6f, 0x2e, 0x63, 0x6c, 0x69, 0x2e, 0x63, 0x6f, 0x6d, 0x6d, 0x61, @@ -324,16 +332,17 @@ var file_commands_compile_proto_rawDesc = []byte{ 0x46, 0x6f, 0x72, 0x44, 0x65, 0x62, 0x75, 0x67, 0x12, 0x16, 0x0a, 0x06, 0x64, 0x72, 0x79, 0x52, 0x75, 0x6e, 0x18, 0x11, 0x20, 0x01, 0x28, 0x08, 0x52, 0x06, 0x64, 0x72, 0x79, 0x52, 0x75, 0x6e, 0x12, 0x1d, 0x0a, 0x0a, 0x65, 0x78, 0x70, 0x6f, 0x72, 0x74, 0x5f, 0x64, 0x69, 0x72, 0x18, 0x12, - 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x65, 0x78, 0x70, 0x6f, 0x72, 0x74, 0x44, 0x69, 0x72, 0x22, - 0x4b, 0x0a, 0x0b, 0x43, 0x6f, 0x6d, 0x70, 0x69, 0x6c, 0x65, 0x52, 0x65, 0x73, 0x70, 0x12, 0x1d, - 0x0a, 0x0a, 0x6f, 0x75, 0x74, 0x5f, 0x73, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x18, 0x01, 0x20, 0x01, - 0x28, 0x0c, 0x52, 0x09, 0x6f, 0x75, 0x74, 0x53, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x12, 0x1d, 0x0a, - 0x0a, 0x65, 0x72, 0x72, 0x5f, 0x73, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x18, 0x02, 0x20, 0x01, 0x28, - 0x0c, 0x52, 0x09, 0x65, 0x72, 0x72, 0x53, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x42, 0x2d, 0x5a, 0x2b, - 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x61, 0x72, 0x64, 0x75, 0x69, - 0x6e, 0x6f, 0x2f, 0x61, 0x72, 0x64, 0x75, 0x69, 0x6e, 0x6f, 0x2d, 0x63, 0x6c, 0x69, 0x2f, 0x72, - 0x70, 0x63, 0x2f, 0x63, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x73, 0x62, 0x06, 0x70, 0x72, 0x6f, - 0x74, 0x6f, 0x33, + 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x65, 0x78, 0x70, 0x6f, 0x72, 0x74, 0x44, 0x69, 0x72, 0x12, + 0x14, 0x0a, 0x05, 0x63, 0x6c, 0x65, 0x61, 0x6e, 0x18, 0x13, 0x20, 0x01, 0x28, 0x08, 0x52, 0x05, + 0x63, 0x6c, 0x65, 0x61, 0x6e, 0x22, 0x4b, 0x0a, 0x0b, 0x43, 0x6f, 0x6d, 0x70, 0x69, 0x6c, 0x65, + 0x52, 0x65, 0x73, 0x70, 0x12, 0x1d, 0x0a, 0x0a, 0x6f, 0x75, 0x74, 0x5f, 0x73, 0x74, 0x72, 0x65, + 0x61, 0x6d, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x09, 0x6f, 0x75, 0x74, 0x53, 0x74, 0x72, + 0x65, 0x61, 0x6d, 0x12, 0x1d, 0x0a, 0x0a, 0x65, 0x72, 0x72, 0x5f, 0x73, 0x74, 0x72, 0x65, 0x61, + 0x6d, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x09, 0x65, 0x72, 0x72, 0x53, 0x74, 0x72, 0x65, + 0x61, 0x6d, 0x42, 0x2d, 0x5a, 0x2b, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, + 0x2f, 0x61, 0x72, 0x64, 0x75, 0x69, 0x6e, 0x6f, 0x2f, 0x61, 0x72, 0x64, 0x75, 0x69, 0x6e, 0x6f, + 0x2d, 0x63, 0x6c, 0x69, 0x2f, 0x72, 0x70, 0x63, 0x2f, 0x63, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, + 0x73, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( diff --git a/rpc/commands/compile.proto b/rpc/commands/compile.proto index 050c1d8bb9a..6b547fd448a 100644 --- a/rpc/commands/compile.proto +++ b/rpc/commands/compile.proto @@ -40,6 +40,7 @@ message CompileReq { bool optimizeForDebug = 16; // Optimize compile output for debug, not for release. bool dryRun = 17; // When set to `true` the compiled binary will not be copied to the export directory. string export_dir = 18; // Optional: save the build artifacts in this directory, the directory must exist. + bool clean = 19; // Optional: cleanup the build folder and do not use any previously cached build } message CompileResp {