Skip to content

Add support for compile with debug optimizations #593

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 17 additions & 14 deletions cli/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ var (
verify bool // Upload, verify uploaded binary after the upload.
exportFile string // The compiled binary is written to this 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
)

// NewCommand created a new `compile` command
Expand Down Expand Up @@ -80,6 +81,7 @@ func NewCommand() *cobra.Command {
command.Flags().StringVar(&vidPid, "vid-pid", "", "When specified, VID/PID specific build properties are used, if boards supports them.")
command.Flags().StringSliceVar(&libraries, "libraries", []string{},
"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 debug, not for release.")

return command
}
Expand All @@ -99,20 +101,21 @@ func run(cmd *cobra.Command, args []string) {
sketchPath := initSketchPath(path)

_, err = compile.Compile(context.Background(), &rpc.CompileReq{
Instance: inst,
Fqbn: fqbn,
SketchPath: sketchPath.String(),
ShowProperties: showProperties,
Preprocess: preprocess,
BuildCachePath: buildCachePath,
BuildPath: buildPath,
BuildProperties: buildProperties,
Warnings: warnings,
Verbose: verbose,
Quiet: quiet,
VidPid: vidPid,
ExportFile: exportFile,
Libraries: libraries,
Instance: inst,
Fqbn: fqbn,
SketchPath: sketchPath.String(),
ShowProperties: showProperties,
Preprocess: preprocess,
BuildCachePath: buildCachePath,
BuildPath: buildPath,
BuildProperties: buildProperties,
Warnings: warnings,
Verbose: verbose,
Quiet: quiet,
VidPid: vidPid,
ExportFile: exportFile,
Libraries: libraries,
OptimizeForDebug: optimizeForDebug,
}, os.Stdout, os.Stderr, viper.GetString("logging.level") == "debug")

if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions commands/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ func Compile(ctx context.Context, req *rpc.CompileReq, outStream, errStream io.W

builderCtx.Verbose = req.GetVerbose()

// Optimize for debug
builderCtx.OptimizeForDebug = req.GetOptimizeForDebug()

builderCtx.CoreBuildCachePath = paths.TempDir().Join("arduino-core-cache")

builderCtx.Jobs = int(req.GetJobs())
Expand Down
17 changes: 0 additions & 17 deletions legacy/builder/builder_utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,20 +519,3 @@ func PrepareCommandForRecipe(ctx *types.Context, buildProperties *properties.Map

return command, nil
}

// GetCachedCoreArchiveFileName returns the filename to be used to store
// the global cached core.a.
func GetCachedCoreArchiveFileName(fqbn string, coreFolder *paths.Path) string {
fqbnToUnderscore := strings.Replace(fqbn, ":", "_", -1)
fqbnToUnderscore = strings.Replace(fqbnToUnderscore, "=", "_", -1)
if absCoreFolder, err := coreFolder.Abs(); err == nil {
coreFolder = absCoreFolder
} // silently continue if absolute path can't be detected
hash := utils.MD5Sum([]byte(coreFolder.String()))
realName := "core_" + fqbnToUnderscore + "_" + hash + ".a"
if len(realName) > 100 {
// avoid really long names, simply hash the final part
realName = "core_" + utils.MD5Sum([]byte(fqbnToUnderscore+"_"+hash)) + ".a"
}
return realName
}
2 changes: 0 additions & 2 deletions legacy/builder/create_build_options_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package builder

import (
"encoding/json"

"github.com/arduino/arduino-cli/legacy/builder/i18n"
"github.com/arduino/arduino-cli/legacy/builder/types"
)
Expand All @@ -30,7 +29,6 @@ func (s *CreateBuildOptionsMap) Run(ctx *types.Context) error {
if err != nil {
return i18n.WrapError(err)
}

ctx.BuildOptionsJson = string(bytes)

return nil
Expand Down
21 changes: 20 additions & 1 deletion legacy/builder/phases/core_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package phases

import (
"os"
"strings"

"github.com/arduino/arduino-cli/legacy/builder/builder_utils"
"github.com/arduino/arduino-cli/legacy/builder/constants"
Expand Down Expand Up @@ -90,7 +91,8 @@ func compileCore(ctx *types.Context, buildPath *paths.Path, buildCachePath *path

var targetArchivedCore *paths.Path
if buildCachePath != nil {
archivedCoreName := builder_utils.GetCachedCoreArchiveFileName(buildProperties.Get(constants.BUILD_PROPERTIES_FQBN), realCoreFolder)
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)

Expand Down Expand Up @@ -129,3 +131,20 @@ func compileCore(ctx *types.Context, buildPath *paths.Path, buildCachePath *path

return archiveFile, variantObjectFiles, nil
}

// GetCachedCoreArchiveFileName returns the filename to be used to store
// the global cached core.a.
func GetCachedCoreArchiveFileName(fqbn string, optimizationFlags string, coreFolder *paths.Path) string {
fqbnToUnderscore := strings.Replace(fqbn, ":", "_", -1)
fqbnToUnderscore = strings.Replace(fqbnToUnderscore, "=", "_", -1)
if absCoreFolder, err := coreFolder.Abs(); err == nil {
coreFolder = absCoreFolder
} // silently continue if absolute path can't be detected
hash := utils.MD5Sum([]byte(coreFolder.String() + optimizationFlags))
realName := "core_" + fqbnToUnderscore + "_" + hash + ".a"
if len(realName) > 100 {
// avoid really long names, simply hash the final part
realName = "core_" + utils.MD5Sum([]byte(fqbnToUnderscore+"_"+hash)) + ".a"
}
return realName
}
11 changes: 11 additions & 0 deletions legacy/builder/setup_build_properties.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@ func (s *SetupBuildProperties) Run(ctx *types.Context) error {
buildProperties.Set("ide_version", ctx.ArduinoAPIVersion)
buildProperties.Set("runtime.os", utils.PrettyOSName())

if ctx.OptimizeForDebug {
if buildProperties.ContainsKey("compiler.optimization_flags.debug") {
buildProperties.Set("compiler.optimization_flags", buildProperties.Get("compiler.optimization_flags.debug"))
}
} else {
if buildProperties.ContainsKey("compiler.optimization_flags.release") {
buildProperties.Set("compiler.optimization_flags", buildProperties.Get("compiler.optimization_flags.release"))
}
}
ctx.OptimizationFlags = buildProperties.Get("compiler.optimization_flags")

variant := buildProperties.Get("build.variant")
if variant == "" {
buildProperties.Set("build.variant.path", "")
Expand Down
4 changes: 2 additions & 2 deletions legacy/builder/test/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import (
"github.com/arduino/go-paths-helper"

"github.com/arduino/arduino-cli/legacy/builder"
"github.com/arduino/arduino-cli/legacy/builder/builder_utils"
"github.com/arduino/arduino-cli/legacy/builder/constants"
"github.com/arduino/arduino-cli/legacy/builder/phases"
"github.com/arduino/arduino-cli/legacy/builder/types"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -379,7 +379,7 @@ func TestBuilderCacheCoreAFile(t *testing.T) {

// Pick timestamp of cached core
coreFolder := paths.New("downloaded_hardware", "arduino", "avr")
coreFileName := builder_utils.GetCachedCoreArchiveFileName(ctx.FQBN.String(), coreFolder)
coreFileName := phases.GetCachedCoreArchiveFileName(ctx.FQBN.String(), ctx.OptimizationFlags, coreFolder)
cachedCoreFile := ctx.CoreBuildCachePath.Join(coreFileName)
coreStatBefore, err := cachedCoreFile.Stat()
require.NoError(t, err)
Expand Down
2 changes: 2 additions & 0 deletions legacy/builder/test/create_build_options_map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func TestCreateBuildOptionsMap(t *testing.T) {
Verbose: true,
BuildPath: paths.New("buildPath"),
DebugLevel: 5,
OptimizationFlags: "-Os",
}

create := builder.CreateBuildOptionsMap{}
Expand All @@ -45,6 +46,7 @@ func TestCreateBuildOptionsMap(t *testing.T) {
"additionalFiles": "",
"builtInLibrariesFolders": "",
"builtInToolsFolders": "tools",
"compiler.optimization_flags": "-Os",
"customBuildProperties": "",
"fqbn": "my:nice:fqbn",
"hardwareFolders": "hardware,hardware2",
Expand Down
2 changes: 2 additions & 0 deletions legacy/builder/test/store_build_options_map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func TestStoreBuildOptionsMap(t *testing.T) {
CustomBuildProperties: []string{"custom=prop"},
Verbose: true,
DebugLevel: 5,
OptimizationFlags: "-Os",
}

buildPath := SetupBuildPath(t, ctx)
Expand All @@ -63,6 +64,7 @@ func TestStoreBuildOptionsMap(t *testing.T) {
"additionalFiles": "",
"builtInLibrariesFolders": "built-in libraries",
"builtInToolsFolders": "tools",
"compiler.optimization_flags": "-Os",
"customBuildProperties": "custom=prop",
"fqbn": "my:nice:fqbn",
"hardwareFolders": "hardware",
Expand Down
6 changes: 6 additions & 0 deletions legacy/builder/types/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ type Context struct {
Verbose bool
DebugPreprocessor bool

// Compile optimization settings
OptimizeForDebug bool
OptimizationFlags string

// Dry run, only create progress map
Progress ProgressStruct

Expand Down Expand Up @@ -140,6 +144,7 @@ func (ctx *Context) ExtractBuildOptions() *properties.Map {
opts.Set("runtime.ide.version", ctx.ArduinoAPIVersion)
opts.Set("customBuildProperties", strings.Join(ctx.CustomBuildProperties, ","))
opts.Set("additionalFiles", strings.Join(additionalFilesRelative, ","))
opts.Set("compiler.optimization_flags", ctx.OptimizationFlags)
return opts
}

Expand All @@ -156,6 +161,7 @@ func (ctx *Context) InjectBuildOptions(opts *properties.Map) {
ctx.FQBN = fqbn
ctx.ArduinoAPIVersion = opts.Get("runtime.ide.version")
ctx.CustomBuildProperties = strings.Split(opts.Get("customBuildProperties"), ",")
ctx.OptimizationFlags = opts.Get("compiler.optimization_flags")
}

func (ctx *Context) GetLogger() i18n.Logger {
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 @@ -37,6 +37,7 @@ message CompileReq {
string exportFile = 13; // The compiled binary is written to this file
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
}

message CompileResp {
Expand Down
3 changes: 1 addition & 2 deletions rpc/debug/debug.pb.go

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