Skip to content

Commit

Permalink
Added core tool dependencies paths to build settings for the compile …
Browse files Browse the repository at this point in the history
…command.
  • Loading branch information
Lerg committed Jan 24, 2018
1 parent cc85b05 commit f57806a
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 4 deletions.
36 changes: 32 additions & 4 deletions commands/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ import (
func Init(rootCommand *cobra.Command) {
rootCommand.AddCommand(command)
command.Flags().StringVarP(&flags.fullyQualifiedBoardName, "fqbn", "b", "", "Fully Qualified Board Name, e.g.: arduino:avr:uno")
command.Flags().BoolVar(&flags.dumpPreferences, "dump-prefs", false, "Show all build preferences used instead of compiling.")
command.Flags().BoolVar(&flags.showProperties, "show-properties", false, "Show all build properties used instead of compiling.")
command.Flags().BoolVar(&flags.preprocess, "preprocess", false, "Print preprocessed code to stdout instead of compiling.")
command.Flags().StringVar(&flags.buildCachePath, "build-cache-path", "", "Builds of 'core.a' are saved into this folder to be cached and reused.")
command.Flags().StringVar(&flags.buildPath, "build-path", "", "Folder where to save compiled files. If omitted, a folder will be created in the temporary folder specified by your OS.")
Expand All @@ -67,7 +67,7 @@ func Init(rootCommand *cobra.Command) {

var flags struct {
fullyQualifiedBoardName string // Fully Qualified Board Name, e.g.: arduino:avr:uno.
dumpPreferences bool // Show all build preferences used instead of compiling.
showProperties bool // Show all build preferences used instead of compiling.
preprocess bool // Print preprocessed code to stdout.
buildCachePath string // Builds of 'core.a' are saved into this folder to be cached and reused.
buildPath string // Folder where to save compiled files.
Expand Down Expand Up @@ -182,7 +182,35 @@ func run(cmd *cobra.Command, args []string) {
ctx.USBVidPid = flags.vidPid
ctx.WarningsLevel = flags.warnings

ctx.CustomBuildProperties = flags.buildProperties
ctx.CustomBuildProperties = append(flags.buildProperties, "build.warn_data_percentage=75")

coreVersion, err := cores.GetLatestInstalledCoreVersion(packageName, coreName)
if err != nil {
formatter.PrintError(err, "Cannot get the core version.")
os.Exit(commands.ErrBadCall)
}
// Add dependency tools paths to build properties with versions corresponding to specific core version.
var packageIndex cores.Index
cores.LoadIndex(&packageIndex)
for _, packageFromIndex := range packageIndex.Packages {
if packageFromIndex.Name == packageName {
for _, platformFromIndex := range packageFromIndex.Platforms {
if platformFromIndex.Architecture == coreName && platformFromIndex.Version == coreVersion {
for _, toolDependencyFromIndex := range platformFromIndex.ToolDependencies {
if isInstalled, _ := cores.IsToolVersionInstalled(packageName, toolDependencyFromIndex.Name, toolDependencyFromIndex.Version); !isInstalled {
formatter.PrintError(err, fmt.Sprintf("Required tool version not found: %s - %s.", toolDependencyFromIndex.Name, toolDependencyFromIndex.Version))
os.Exit(commands.ErrBadCall)
}
property := "runtime.tools." + toolDependencyFromIndex.Name + ".path=" + filepath.Join(toolsFolder, toolDependencyFromIndex.Name, toolDependencyFromIndex.Version)
ctx.CustomBuildProperties = append(ctx.CustomBuildProperties, property)
}
break
}
}
break
}
}

if flags.buildCachePath != "" {
err = utils.EnsureFolderExists(flags.buildCachePath)
if err != nil {
Expand Down Expand Up @@ -211,7 +239,7 @@ func run(cmd *cobra.Command, args []string) {
ctx.BuiltInLibrariesFolders = []string{ideLibrariesPath}
}

if flags.dumpPreferences {
if flags.showProperties {
err = builder.RunParseHardwareAndDumpBuildProperties(ctx)
} else if flags.preprocess {
err = builder.RunPreprocess(ctx)
Expand Down
54 changes: 54 additions & 0 deletions cores/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@
package cores

import (
"errors"
"os"
"path/filepath"
"regexp"
"sort"
"strings"

"github.com/bcmi-labs/arduino-cli/common"
Expand Down Expand Up @@ -102,3 +104,55 @@ func IsToolInstalled(packageName string, name string) (bool, error) {
}
return false, nil
}

// IsToolVersionInstalled detects if a specific version of a tool has been installed.
func IsToolVersionInstalled(packageName string, name string, version string) (bool, error) {
location, err := common.GetDefaultToolsFolder(packageName)
if err != nil {
return false, err
}
_, err = os.Stat(filepath.Join(location, name, version))
if !os.IsNotExist(err) {
return true, nil
}
return false, nil
}

// GetLatestInstalledCoreVersion returns the latest version of an installed core.
func GetLatestInstalledCoreVersion(packageName string, name string) (string, error) {
location, err := common.GetDefaultCoresFolder(packageName)
if err != nil {
return "", err
}
return getLatestInstalledVersion(location, name)
}

// GetLatestInstalledToolVersion returns the latest version of an installed tool.
func GetLatestInstalledToolVersion(packageName string, name string) (string, error) {
location, err := common.GetDefaultToolsFolder(packageName)
if err != nil {
return "", err
}
return getLatestInstalledVersion(location, name)
}

func getLatestInstalledVersion(location string, name string) (string, error) {
var versions []string
root := filepath.Join(location, name)
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil || !info.IsDir() || path == root {
return nil
}
versions = append(versions, info.Name())
return filepath.SkipDir
})
if err != nil {
return "", err
}

if len(versions) > 0 {
sort.Strings(versions)
return versions[len(versions)-1], nil
}
return "", errors.New("no versions found")
}

0 comments on commit f57806a

Please sign in to comment.