Skip to content

[breaking] Improved compile report / temporary support for profile creation #1745

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 10 commits into from
Jun 1, 2022
21 changes: 17 additions & 4 deletions arduino/cores/cores.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ import (
"encoding/json"
"fmt"
"net/url"
"path/filepath"
"sort"
"strings"

"github.com/arduino/arduino-cli/arduino/globals"
"github.com/arduino/arduino-cli/arduino/resources"
"github.com/arduino/arduino-cli/arduino/utils"
"github.com/arduino/arduino-cli/i18n"
Expand Down Expand Up @@ -359,10 +361,21 @@ func (release *PlatformRelease) String() string {
}

// ToRPCPlatformReference creates a gRPC PlatformReference message out of this PlatformRelease
func (release *PlatformRelease) ToRPCPlatformReference() *rpc.PlatformReference {
return &rpc.PlatformReference{
Id: release.Platform.String(),
Version: release.Version.String(),
func (release *PlatformRelease) ToRPCPlatformReference() *rpc.InstalledPlatformReference {
defaultURLPrefix := globals.DefaultIndexURL
// TODO: create a IndexURL object to factorize this
defaultURLPrefix = strings.TrimSuffix(defaultURLPrefix, filepath.Ext(defaultURLPrefix))
defaultURLPrefix = strings.TrimSuffix(defaultURLPrefix, filepath.Ext(defaultURLPrefix)) // removes .tar.bz2

url := release.Platform.Package.URL
if strings.HasPrefix(url, defaultURLPrefix) {
url = ""
}
return &rpc.InstalledPlatformReference{
Id: release.Platform.String(),
Version: release.Version.String(),
InstallDir: release.InstallDir.String(),
PackageUrl: url,
}
}

Expand Down
2 changes: 1 addition & 1 deletion arduino/cores/packagemanager/profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ import (

"github.com/arduino/arduino-cli/arduino"
"github.com/arduino/arduino-cli/arduino/cores"
"github.com/arduino/arduino-cli/arduino/globals"
"github.com/arduino/arduino-cli/arduino/resources"
"github.com/arduino/arduino-cli/arduino/sketch"
"github.com/arduino/arduino-cli/cli/globals"
"github.com/arduino/arduino-cli/configuration"
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
"github.com/arduino/go-paths-helper"
Expand Down
3 changes: 3 additions & 0 deletions arduino/globals/globals.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,7 @@ var (
".hpp": empty,
".hh": empty,
}

// DefaultIndexURL is the default index url
DefaultIndexURL = "https://downloads.arduino.cc/packages/package_index.tar.bz2"
)
99 changes: 97 additions & 2 deletions cli/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import (
"github.com/arduino/arduino-cli/commands"
"github.com/arduino/arduino-cli/configuration"
"github.com/arduino/arduino-cli/i18n"
"github.com/arduino/arduino-cli/table"
"github.com/fatih/color"
"github.com/sirupsen/logrus"

"github.com/arduino/arduino-cli/cli/errorcodes"
Expand Down Expand Up @@ -68,6 +70,7 @@ var (
clean bool // Cleanup the build folder and do not use any cached build
compilationDatabaseOnly bool // Only create compilation database without actually compiling
sourceOverrides string // Path to a .json file that contains a set of replacements of the sketch source code.
dumpProfile bool // Create and print a profile configuration from the build
// library and libraries sound similar but they're actually different.
// library expects a path to the root folder of one single library.
// libraries expects a path to a directory containing multiple libraries, similarly to the <directories.user>/libraries path.
Expand All @@ -93,6 +96,7 @@ func NewCommand() *cobra.Command {

fqbnArg.AddToCommand(compileCommand)
profileArg.AddToCommand(compileCommand)
compileCommand.Flags().BoolVar(&dumpProfile, "dump-profile", false, tr("Create and print a profile configuration from the build."))
compileCommand.Flags().BoolVar(&showProperties, "show-properties", false, tr("Show all build properties used instead of compiling."))
compileCommand.Flags().BoolVar(&preprocess, "preprocess", false, tr("Print preprocessed code to stdout instead of compiling."))
compileCommand.Flags().StringVar(&buildCachePath, "build-cache-path", "", tr("Builds of 'core.a' are saved into this path to be cached and reused."))
Expand Down Expand Up @@ -142,6 +146,10 @@ func NewCommand() *cobra.Command {
func runCompileCommand(cmd *cobra.Command, args []string) {
logrus.Info("Executing `arduino-cli compile`")

if dumpProfile && feedback.GetFormat() != feedback.Text {
feedback.Errorf(tr("You cannot use the %[1]s flag together with %[2]s.", "--dump-profile", "--format json"))
os.Exit(errorcodes.ErrBadArgument)
}
if profileArg.Get() != "" {
if len(libraries) > 0 {
feedback.Errorf(tr("You cannot use the %s flag while compiling with a profile.", "--libraries"))
Expand Down Expand Up @@ -268,6 +276,54 @@ func runCompileCommand(cmd *cobra.Command, args []string) {
}
}

if dumpProfile {
libs := ""
hasVendoredLibs := false
for _, lib := range compileRes.GetUsedLibraries() {
if lib.Location != rpc.LibraryLocation_LIBRARY_LOCATION_USER && lib.Location != rpc.LibraryLocation_LIBRARY_LOCATION_UNMANAGED {
continue
}
if lib.GetVersion() == "" {
hasVendoredLibs = true
continue
}
libs += fmt.Sprintln(" - " + lib.GetName() + " (" + lib.GetVersion() + ")")
}
if hasVendoredLibs {
fmt.Println()
fmt.Println(tr("WARNING: The sketch is compiled using one or more custom libraries."))
fmt.Println(tr("Currently, Build Profiles only support libraries available through Arduino Library Manager."))
}

newProfileName := "my_profile_name"
if split := strings.Split(compileRequest.GetFqbn(), ":"); len(split) > 2 {
newProfileName = split[2]
}
fmt.Println()
fmt.Println("profile:")
fmt.Println(" " + newProfileName + ":")
fmt.Println(" fqbn: " + compileRequest.GetFqbn())
fmt.Println(" platforms:")
boardPlatform := compileRes.GetBoardPlatform()
fmt.Println(" - platform: " + boardPlatform.GetId() + " (" + boardPlatform.GetVersion() + ")")
if url := boardPlatform.GetPackageUrl(); url != "" {
fmt.Println(" platform_index_url: " + url)
}

if buildPlatform := compileRes.GetBuildPlatform(); buildPlatform != nil &&
buildPlatform.Id != boardPlatform.Id &&
buildPlatform.Version != boardPlatform.Version {
fmt.Println(" - platform: " + buildPlatform.GetId() + " (" + buildPlatform.GetVersion() + ")")
if url := buildPlatform.GetPackageUrl(); url != "" {
fmt.Println(" platform_index_url: " + url)
}
}
if len(libs) > 0 {
fmt.Println(" libraries:")
fmt.Print(libs)
}
}

feedback.PrintResult(&compileResult{
CompileOut: compileStdOut.String(),
CompileErr: compileStdErr.String(),
Expand Down Expand Up @@ -316,6 +372,45 @@ func (r *compileResult) Data() interface{} {
}

func (r *compileResult) String() string {
// The output is already printed via os.Stdout/os.Stdin
return ""
titleColor := color.New(color.FgHiGreen)
nameColor := color.New(color.FgHiYellow)
pathColor := color.New(color.FgHiBlack)
build := r.BuilderResult

res := "\n"
libraries := table.New()
if len(build.GetUsedLibraries()) > 0 {
libraries.SetHeader(
table.NewCell(tr("Used library"), titleColor),
table.NewCell(tr("Version"), titleColor),
table.NewCell(tr("Path"), pathColor))
for _, l := range build.GetUsedLibraries() {
libraries.AddRow(
table.NewCell(l.GetName(), nameColor),
l.GetVersion(),
table.NewCell(l.GetInstallDir(), pathColor))
}
}
res += libraries.Render() + "\n"

platforms := table.New()
platforms.SetHeader(
table.NewCell(tr("Used platform"), titleColor),
table.NewCell(tr("Version"), titleColor),
table.NewCell(tr("Path"), pathColor))
boardPlatform := build.GetBoardPlatform()
platforms.AddRow(
table.NewCell(boardPlatform.GetId(), nameColor),
boardPlatform.GetVersion(),
table.NewCell(boardPlatform.GetInstallDir(), pathColor))
if buildPlatform := build.GetBuildPlatform(); buildPlatform != nil &&
buildPlatform.Id != boardPlatform.Id &&
buildPlatform.Version != boardPlatform.Version {
platforms.AddRow(
table.NewCell(buildPlatform.GetId(), nameColor),
buildPlatform.GetVersion(),
table.NewCell(buildPlatform.GetInstallDir(), pathColor))
}
res += platforms.Render()
return res
}
2 changes: 1 addition & 1 deletion cli/core/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ import (
"strings"
"time"

"github.com/arduino/arduino-cli/arduino/globals"
"github.com/arduino/arduino-cli/arduino/utils"
"github.com/arduino/arduino-cli/cli/errorcodes"
"github.com/arduino/arduino-cli/cli/feedback"
"github.com/arduino/arduino-cli/cli/globals"
"github.com/arduino/arduino-cli/cli/instance"
"github.com/arduino/arduino-cli/cli/output"
"github.com/arduino/arduino-cli/commands"
Expand Down
2 changes: 0 additions & 2 deletions cli/globals/globals.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,4 @@ import (
var (
// VersionInfo contains all info injected during build
VersionInfo = version.NewInfo(filepath.Base(os.Args[0]))
// DefaultIndexURL is the default index url
DefaultIndexURL = "https://downloads.arduino.cc/packages/package_index.tar.bz2"
)
23 changes: 13 additions & 10 deletions commands/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,19 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
return r, compileErr
}

defer func() {
importedLibs := []*rpc.Library{}
for _, lib := range builderCtx.ImportedLibraries {
rpcLib, err := lib.ToRPCLibrary()
if err != nil {
msg := tr("Error getting information for library %s", lib.Name) + ": " + err.Error() + "\n"
errStream.Write([]byte(msg))
}
importedLibs = append(importedLibs, rpcLib)
}
r.UsedLibraries = importedLibs
}()

// if it's a regular build, go on...
if err := builder.RunBuilder(builderCtx); err != nil {
return r, &arduino.CompileFailedError{Message: err.Error()}
Expand Down Expand Up @@ -268,16 +281,6 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
}
}

importedLibs := []*rpc.Library{}
for _, lib := range builderCtx.ImportedLibraries {
rpcLib, err := lib.ToRPCLibrary()
if err != nil {
return r, &arduino.PermissionDeniedError{Message: tr("Error getting information for library %s", lib.Name), Cause: err}
}
importedLibs = append(importedLibs, rpcLib)
}
r.UsedLibraries = importedLibs

r.ExecutableSectionsSize = builderCtx.ExecutableSectionsSize.ToRPCExecutableSectionSizeArray()

logrus.Tracef("Compile %s for %s successful", sk.Name, fqbnIn)
Expand Down
5 changes: 3 additions & 2 deletions commands/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@ import (
"github.com/arduino/arduino-cli/arduino/cores"
"github.com/arduino/arduino-cli/arduino/cores/packageindex"
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
"github.com/arduino/arduino-cli/arduino/globals"
"github.com/arduino/arduino-cli/arduino/httpclient"
"github.com/arduino/arduino-cli/arduino/libraries"
"github.com/arduino/arduino-cli/arduino/libraries/librariesindex"
"github.com/arduino/arduino-cli/arduino/libraries/librariesmanager"
"github.com/arduino/arduino-cli/arduino/resources"
"github.com/arduino/arduino-cli/arduino/sketch"
"github.com/arduino/arduino-cli/arduino/utils"
"github.com/arduino/arduino-cli/cli/globals"
cliglobals "github.com/arduino/arduino-cli/cli/globals"
"github.com/arduino/arduino-cli/configuration"
"github.com/arduino/arduino-cli/i18n"
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
Expand Down Expand Up @@ -128,7 +129,7 @@ func Create(req *rpc.CreateRequest, extraUserAgent ...string) (*rpc.CreateRespon
}

// Create package manager
userAgent := "arduino-cli/" + globals.VersionInfo.VersionString
userAgent := "arduino-cli/" + cliglobals.VersionInfo.VersionString
for _, ua := range extraUserAgent {
userAgent += " " + ua
}
Expand Down
15 changes: 15 additions & 0 deletions docs/UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,21 @@ but since it has been the default behaviour from a very long time we decided to
If a compilation fail for a missing bundled library, you can fix it just by installing the missing library from the
library manager as usual.

### gRPC: Changes in message `cc.arduino.cli.commands.v1.PlatformReference`

The gRPC message structure `cc.arduino.cli.commands.v1.PlatformReference` has been renamed to
`cc.arduino.cli.commands.v1.InstalledPlatformReference`, and some new fields have been added:

- `install_dir` is the installation directory of the platform
- `package_url` is the 3rd party platform URL of the platform

It is currently used only in `cc.arduino.cli.commands.v1.CompileResponse`, so the field type has been changed as well.
Old gRPC clients must only update gRPC bindings. They can safely ignore the new fields if not needed.

### golang API: `github.com/arduino/arduino-cli/cli/globals.DefaultIndexURL` has been moved under `github.com/arduino/arduino-cli/arduino/globals`

Legacy code should just update the import.

### golang API: PackageManager.DownloadPlatformRelease no longer need `label` parameter

```go
Expand Down
Loading