Skip to content

Commit

Permalink
refactor: simplified
Browse files Browse the repository at this point in the history
  • Loading branch information
Bikappa committed Mar 13, 2023
1 parent 7a3eefd commit ff638bc
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 34 deletions.
75 changes: 42 additions & 33 deletions commands/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,17 +120,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
appendBuildProperties(r, builderCtx)
}()
r = &rpc.CompileResponse{}
if newSketchErr != nil {
if req.GetShowProperties() {
// Just get build properties and exit
compileErr := builder.RunParseHardware(builderCtx)
if compileErr != nil {
compileErr = &arduino.CompileFailedError{Message: compileErr.Error()}
}
return r, compileErr
}
return nil, &arduino.CantOpenSketchError{Cause: err}
}

builderCtx.Sketch = sk
builderCtx.ProgressCB = progressCB

Expand All @@ -142,30 +132,11 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
builderCtx.OtherLibrariesDirs = paths.NewPathList(req.GetLibraries()...)
builderCtx.OtherLibrariesDirs.Add(configuration.LibrariesDir(configuration.Settings))
builderCtx.LibraryDirs = paths.NewPathList(req.Library...)
if req.GetBuildPath() == "" {
builderCtx.BuildPath = sk.DefaultBuildPath()
} else {
builderCtx.BuildPath = paths.New(req.GetBuildPath()).Canonical()
if in, err := builderCtx.BuildPath.IsInsideDir(sk.FullPath); err != nil {
return nil, &arduino.NotFoundError{Message: tr("Cannot find build path"), Cause: err}
} else if in && builderCtx.BuildPath.IsDir() {
if sk.AdditionalFiles, err = removeBuildFromSketchFiles(sk.AdditionalFiles, builderCtx.BuildPath); err != nil {
return nil, err
}
}
}
if err = builderCtx.BuildPath.MkdirAll(); err != nil {
return nil, &arduino.PermissionDeniedError{Message: tr("Cannot create build directory"), Cause: err}
err = prepareBuildPath(sk, req.GetBuildPath(), builderCtx)
if err != nil {
return r, err
}

buildcache.New(builderCtx.BuildPath.Parent()).GetOrCreate(builderCtx.BuildPath.Base())
// cache is purged after compilation to not remove entries that might be required
defer maybePurgeBuildCache()

builderCtx.CompilationDatabase = bldr.NewCompilationDatabase(
builderCtx.BuildPath.Join("compile_commands.json"),
)

builderCtx.Verbose = req.GetVerbose()

// Optimize for debug
Expand Down Expand Up @@ -221,6 +192,15 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
return r, compileErr
}

if newSketchErr != nil {
// newSketchErr causes to exit only here since the request could have
// been to only show properties until now
return r, &arduino.CantOpenSketchError{Cause: err}
}

// cache is purged after compilation to not remove entries that might be required
defer maybePurgeBuildCache()

if req.GetPreprocess() {
// Just output preprocessed source code and exit
compileErr := builder.RunPreprocess(builderCtx)
Expand Down Expand Up @@ -300,6 +280,35 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
return r, nil
}

func prepareBuildPath(sk *sketch.Sketch, requestedBuildPath string, builderCtx *types.Context) error {
if sk == nil {
return nil
}

if requestedBuildPath == "" {
builderCtx.BuildPath = sk.DefaultBuildPath()
} else {
builderCtx.BuildPath = paths.New(requestedBuildPath).Canonical()
if in, err := builderCtx.BuildPath.IsInsideDir(sk.FullPath); err != nil {
return &arduino.NotFoundError{Message: tr("Cannot find build path"), Cause: err}
} else if in && builderCtx.BuildPath.IsDir() {
if sk.AdditionalFiles, err = removeBuildFromSketchFiles(sk.AdditionalFiles, builderCtx.BuildPath); err != nil {
return err
}
}
}

if err := builderCtx.BuildPath.MkdirAll(); err != nil {
return &arduino.PermissionDeniedError{Message: tr("Cannot create build directory"), Cause: err}
}

builderCtx.CompilationDatabase = bldr.NewCompilationDatabase(
builderCtx.BuildPath.Join("compile_commands.json"),
)
buildcache.New(builderCtx.BuildPath.Parent()).GetOrCreate(builderCtx.BuildPath.Base())
return nil
}

func appendUserLibraries(r *rpc.CompileResponse, builderCtx *types.Context, errStream io.Writer) {
importedLibs := []*rpc.Library{}
for _, lib := range builderCtx.ImportedLibraries {
Expand Down
4 changes: 3 additions & 1 deletion legacy/builder/setup_build_properties.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ func (s *SetupBuildProperties) Run(ctx *types.Context) error {
}
ctx.OptimizationFlags = buildProperties.Get("compiler.optimization_flags")

buildProperties.SetPath("build.source.path", ctx.Sketch.FullPath)
if ctx.Sketch != nil {
buildProperties.SetPath("build.source.path", ctx.Sketch.FullPath)
}

keychainProp := buildProperties.ContainsKey("build.keys.keychain")
signProp := buildProperties.ContainsKey("build.keys.sign_key")
Expand Down

0 comments on commit ff638bc

Please sign in to comment.