Skip to content

Commit

Permalink
Added dry-run mode in Compile command (arduino#619)
Browse files Browse the repository at this point in the history
* Added dry-run mode in Compile command

* Update cli/compile/compile.go

Co-Authored-By: Roberto Sora <[email protected]>

Co-authored-by: Roberto Sora <[email protected]>
  • Loading branch information
cmaglie and Roberto Sora authored Mar 13, 2020
1 parent f855fea commit fbd35df
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 75 deletions.
3 changes: 3 additions & 0 deletions cli/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ var (
port string // Upload port, e.g.: COM10 or /dev/ttyACM0.
verify bool // Upload, verify uploaded binary after the upload.
exportFile string // The compiled binary is written to this file
dryRun bool // Use this flag to now write the output file
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
)
Expand All @@ -67,6 +68,7 @@ func NewCommand() *cobra.Command {
command.Flags().BoolVar(&preprocess, "preprocess", false, "Print preprocessed code to stdout instead of compiling.")
command.Flags().StringVar(&buildCachePath, "build-cache-path", "", "Builds of 'core.a' are saved into this path to be cached and reused.")
command.Flags().StringVarP(&exportFile, "output", "o", "", "Filename of the compile output.")
command.Flags().BoolVarP(&dryRun, "dry-run", "n", false, "Perform the build but do not copy the compile output file.")
command.Flags().StringVar(&buildPath, "build-path", "",
"Path where to save compiled files. If omitted, a directory will be created in the default temporary path of your OS.")
command.Flags().StringSliceVar(&buildProperties, "build-properties", []string{},
Expand Down Expand Up @@ -114,6 +116,7 @@ func run(cmd *cobra.Command, args []string) {
Quiet: quiet,
VidPid: vidPid,
ExportFile: exportFile,
DryRun: dryRun,
Libraries: libraries,
OptimizeForDebug: optimizeForDebug,
}, os.Stdout, os.Stderr, viper.GetString("logging.level") == "debug")
Expand Down
98 changes: 50 additions & 48 deletions commands/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,59 +196,61 @@ func Compile(ctx context.Context, req *rpc.CompileReq, outStream, errStream io.W
return nil, err
}

// FIXME: Make a function to obtain these info...
outputPath := paths.New(
builderCtx.BuildProperties.ExpandPropsInString("{build.path}/{recipe.output.tmp_file}")) // "/build/path/sketch.ino.bin"
ext := outputPath.Ext() // ".hex" | ".bin"
base := outputPath.Base() // "sketch.ino.hex"
base = base[:len(base)-len(ext)] // "sketch.ino"

// FIXME: Make a function to produce a better name...
// Make the filename without the FQBN configs part
fqbn.Configs = properties.NewMap()
fqbnSuffix := strings.Replace(fqbn.String(), ":", ".", -1)

var exportPath *paths.Path
var exportFile string
if req.GetExportFile() == "" {
if sketch.FullPath.IsDir() {
exportPath = sketch.FullPath
if !req.GetDryRun() {
// FIXME: Make a function to obtain these info...
outputPath := paths.New(
builderCtx.BuildProperties.ExpandPropsInString("{build.path}/{recipe.output.tmp_file}")) // "/build/path/sketch.ino.bin"
ext := outputPath.Ext() // ".hex" | ".bin"
base := outputPath.Base() // "sketch.ino.hex"
base = base[:len(base)-len(ext)] // "sketch.ino"

// FIXME: Make a function to produce a better name...
// Make the filename without the FQBN configs part
fqbn.Configs = properties.NewMap()
fqbnSuffix := strings.Replace(fqbn.String(), ":", ".", -1)

var exportPath *paths.Path
var exportFile string
if req.GetExportFile() == "" {
if sketch.FullPath.IsDir() {
exportPath = sketch.FullPath
} else {
exportPath = sketch.FullPath.Parent()
}
exportFile = sketch.Name + "." + fqbnSuffix // "sketch.arduino.avr.uno"
} else {
exportPath = sketch.FullPath.Parent()
}
exportFile = sketch.Name + "." + fqbnSuffix // "sketch.arduino.avr.uno"
} else {
exportPath = paths.New(req.GetExportFile()).Parent()
exportFile = paths.New(req.GetExportFile()).Base()
if strings.HasSuffix(exportFile, ext) {
exportFile = exportFile[:len(exportFile)-len(ext)]
exportPath = paths.New(req.GetExportFile()).Parent()
exportFile = paths.New(req.GetExportFile()).Base()
if strings.HasSuffix(exportFile, ext) {
exportFile = exportFile[:len(exportFile)-len(ext)]
}
}
}

// Copy "sketch.ino.*.hex" / "sketch.ino.*.bin" artifacts to sketch directory
srcDir, err := outputPath.Parent().ReadDir() // read "/build/path/*"
if err != nil {
return nil, fmt.Errorf("reading build directory: %s", err)
}
srcDir.FilterPrefix(base + ".")
srcDir.FilterSuffix(ext)
for _, srcOutput := range srcDir {
srcFilename := srcOutput.Base() // "sketch.ino.*.bin"
srcFilename = srcFilename[len(base):] // ".*.bin"
dstOutput := exportPath.Join(exportFile + srcFilename)
logrus.WithField("from", srcOutput).WithField("to", dstOutput).Debug("copying sketch build output")
if err = srcOutput.CopyTo(dstOutput); err != nil {
return nil, fmt.Errorf("copying output file: %s", err)
// Copy "sketch.ino.*.hex" / "sketch.ino.*.bin" artifacts to sketch directory
srcDir, err := outputPath.Parent().ReadDir() // read "/build/path/*"
if err != nil {
return nil, fmt.Errorf("reading build directory: %s", err)
}
srcDir.FilterPrefix(base + ".")
srcDir.FilterSuffix(ext)
for _, srcOutput := range srcDir {
srcFilename := srcOutput.Base() // "sketch.ino.*.bin"
srcFilename = srcFilename[len(base):] // ".*.bin"
dstOutput := exportPath.Join(exportFile + srcFilename)
logrus.WithField("from", srcOutput).WithField("to", dstOutput).Debug("copying sketch build output")
if err = srcOutput.CopyTo(dstOutput); err != nil {
return nil, fmt.Errorf("copying output file: %s", err)
}
}
}

// Copy .elf file to sketch directory
srcElf := outputPath.Parent().Join(base + ".elf")
if srcElf.Exist() {
dstElf := exportPath.Join(exportFile + ".elf")
logrus.WithField("from", srcElf).WithField("to", dstElf).Debug("copying sketch build output")
if err = srcElf.CopyTo(dstElf); err != nil {
return nil, fmt.Errorf("copying elf file: %s", err)
// Copy .elf file to sketch directory
srcElf := outputPath.Parent().Join(base + ".elf")
if srcElf.Exist() {
dstElf := exportPath.Join(exportFile + ".elf")
logrus.WithField("from", srcElf).WithField("to", dstElf).Debug("copying sketch build output")
if err = srcElf.CopyTo(dstElf); err != nil {
return nil, fmt.Errorf("copying elf file: %s", err)
}
}
}

Expand Down
63 changes: 36 additions & 27 deletions rpc/commands/compile.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions rpc/commands/compile.proto
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ message CompileReq {
int32 jobs = 14; // The max number of concurrent compiler instances to run (as make -jx)
repeated string libraries = 15; // List of custom libraries paths separated by commas. Or can be used multiple times for multiple libraries paths.
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
}

message CompileResp {
Expand Down

0 comments on commit fbd35df

Please sign in to comment.