Skip to content

Commit

Permalink
Always output build path in compile json-output / Always write a vali…
Browse files Browse the repository at this point in the history
…d compilation database (#1156)

* Always report buildpath (even in case of build failed)

* Always write a correct compilation database

Even if the Contents field is empty.
  • Loading branch information
cmaglie committed Jan 29, 2021
1 parent 37e04d7 commit 1aa53c8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
8 changes: 3 additions & 5 deletions arduino/builder/compilation_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ type CompilationCommand struct {
// NewCompilationDatabase creates an empty CompilationDatabase
func NewCompilationDatabase(filename *paths.Path) *CompilationDatabase {
return &CompilationDatabase{
File: filename,
File: filename,
Contents: []CompilationCommand{},
}
}

Expand All @@ -51,10 +52,7 @@ func LoadCompilationDatabase(file *paths.Path) (*CompilationDatabase, error) {
if err != nil {
return nil, err
}
res := &CompilationDatabase{
File: file,
Contents: []CompilationCommand{},
}
res := NewCompilationDatabase(file)
return res, json.Unmarshal(f, &res.Contents)
}

Expand Down
24 changes: 15 additions & 9 deletions commands/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,16 +194,23 @@ func Compile(ctx context.Context, req *rpc.CompileReq, outStream, errStream io.W

builderCtx.SourceOverride = req.GetSourceOverride()

r = &rpc.CompileResp{}
defer func() {
if p := builderCtx.BuildPath; p != nil {
r.BuildPath = p.String()
}
}()

// if --preprocess or --show-properties were passed, we can stop here
if req.GetShowProperties() {
return &rpc.CompileResp{}, builder.RunParseHardwareAndDumpBuildProperties(builderCtx)
return r, builder.RunParseHardwareAndDumpBuildProperties(builderCtx)
} else if req.GetPreprocess() {
return &rpc.CompileResp{}, builder.RunPreprocess(builderCtx)
return r, builder.RunPreprocess(builderCtx)
}

// if it's a regular build, go on...
if err := builder.RunBuilder(builderCtx); err != nil {
return &rpc.CompileResp{}, err
return r, err
}

// If the export directory is set we assume you want to export the binaries
Expand All @@ -219,17 +226,17 @@ func Compile(ctx context.Context, req *rpc.CompileReq, outStream, errStream io.W
}
logrus.WithField("path", exportPath).Trace("Saving sketch to export path.")
if err := exportPath.MkdirAll(); err != nil {
return nil, errors.Wrap(err, "creating output dir")
return r, errors.Wrap(err, "creating output dir")
}

// Copy all "sketch.ino.*" artifacts to the export directory
baseName, ok := builderCtx.BuildProperties.GetOk("build.project_name") // == "sketch.ino"
if !ok {
return nil, errors.New("missing 'build.project_name' build property")
return r, errors.New("missing 'build.project_name' build property")
}
buildFiles, err := builderCtx.BuildPath.ReadDir()
if err != nil {
return nil, errors.Errorf("reading build directory: %s", err)
return r, errors.Errorf("reading build directory: %s", err)
}
buildFiles.FilterPrefix(baseName)
for _, buildFile := range buildFiles {
Expand All @@ -239,7 +246,7 @@ func Compile(ctx context.Context, req *rpc.CompileReq, outStream, errStream io.W
WithField("dest", exportedFile).
Trace("Copying artifact.")
if err = buildFile.CopyTo(exportedFile); err != nil {
return nil, errors.Wrapf(err, "copying output file %s", buildFile)
return r, errors.Wrapf(err, "copying output file %s", buildFile)
}
}
}
Expand All @@ -248,15 +255,14 @@ func Compile(ctx context.Context, req *rpc.CompileReq, outStream, errStream io.W
for _, lib := range builderCtx.ImportedLibraries {
rpcLib, err := lib.ToRPCLibrary()
if err != nil {
return nil, fmt.Errorf("converting library %s to rpc struct: %w", lib.Name, err)
return r, fmt.Errorf("converting library %s to rpc struct: %w", lib.Name, err)
}
importedLibs = append(importedLibs, rpcLib)
}

logrus.Tracef("Compile %s for %s successful", sketch.Name, fqbnIn)

return &rpc.CompileResp{
BuildPath: builderCtx.BuildPath.String(),
UsedLibraries: importedLibs,
ExecutableSectionsSize: builderCtx.ExecutableSectionsSize.ToRPCExecutableSectionSizeArray(),
}, nil
Expand Down

0 comments on commit 1aa53c8

Please sign in to comment.