|
| 1 | +/* |
| 2 | + * This file is part of arduino-cli. |
| 3 | + * |
| 4 | + * arduino-cli is free software; you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation; either version 2 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program; if not, write to the Free Software |
| 16 | + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | + * |
| 18 | + * As a special exception, you may use this file as part of a free software |
| 19 | + * library without restriction. Specifically, if other files instantiate |
| 20 | + * templates or use macros or inline functions from this file, or you compile |
| 21 | + * this file and link it with other files to produce an executable, this |
| 22 | + * file does not by itself cause the resulting executable to be covered by |
| 23 | + * the GNU General Public License. This exception does not however |
| 24 | + * invalidate any other reasons why the executable file might be covered by |
| 25 | + * the GNU General Public License. |
| 26 | + * |
| 27 | + * Copyright 2017 ARDUINO AG (http://www.arduino.cc/) |
| 28 | + */ |
| 29 | + |
| 30 | +package cmd |
| 31 | + |
| 32 | +import ( |
| 33 | + "os" |
| 34 | + "path/filepath" |
| 35 | + "strings" |
| 36 | + |
| 37 | + builder "github.com/arduino/arduino-builder" |
| 38 | + "github.com/arduino/arduino-builder/types" |
| 39 | + "github.com/arduino/arduino-builder/utils" |
| 40 | + "github.com/bcmi-labs/arduino-cli/cmd/formatter" |
| 41 | + "github.com/bcmi-labs/arduino-cli/common" |
| 42 | + "github.com/sirupsen/logrus" |
| 43 | + "github.com/spf13/cobra" |
| 44 | +) |
| 45 | + |
| 46 | +var arduinoCompileCmd = &cobra.Command{ |
| 47 | + Use: `compile`, |
| 48 | + Short: `Compiles Arduino sketches`, |
| 49 | + Long: `Compiles Arduino sketches`, |
| 50 | + Example: `arduino compile --fqbn arduino:avr:uno --sketch mySketch`, |
| 51 | + Args: cobra.NoArgs, |
| 52 | + Run: executeCompileCommand, |
| 53 | +} |
| 54 | + |
| 55 | +// TODO: convert required flags to arguments, install ctags and core if missing |
| 56 | +func executeCompileCommand(cmd *cobra.Command, args []string) { |
| 57 | + logrus.Info("Executing `arduino compile`") |
| 58 | + isCorrectSyntax := true |
| 59 | + if arduinoCompileFlags.SketchName == "" { |
| 60 | + formatter.PrintErrorMessage("No sketch name provided") |
| 61 | + isCorrectSyntax = false |
| 62 | + } |
| 63 | + var packageName string |
| 64 | + if arduinoCompileFlags.FullyQualifiedBoardName == "" { |
| 65 | + formatter.PrintErrorMessage("No Fully Qualified Board Name provided") |
| 66 | + isCorrectSyntax = false |
| 67 | + } else { |
| 68 | + fqbnParts := strings.Split(arduinoCompileFlags.FullyQualifiedBoardName, ":") |
| 69 | + if len(fqbnParts) != 3 { |
| 70 | + formatter.PrintErrorMessage("Fully Qualified Board Name has incorrect format") |
| 71 | + isCorrectSyntax = false |
| 72 | + } else { |
| 73 | + packageName = fqbnParts[0] |
| 74 | + } |
| 75 | + } |
| 76 | + if !isCorrectSyntax { |
| 77 | + os.Exit(errBadCall) |
| 78 | + } |
| 79 | + |
| 80 | + ctx := &types.Context{} |
| 81 | + |
| 82 | + ctx.FQBN = arduinoCompileFlags.FullyQualifiedBoardName |
| 83 | + ctx.SketchLocation = filepath.Join(common.SketchbookFolder, arduinoCompileFlags.SketchName) |
| 84 | + |
| 85 | + packagesFolder, err := common.GetDefaultPkgFolder() |
| 86 | + if err != nil { |
| 87 | + formatter.PrintError(err, "Cannot get packages folder") |
| 88 | + os.Exit(errCoreConfig) |
| 89 | + } |
| 90 | + ctx.HardwareFolders = []string{packagesFolder} |
| 91 | + |
| 92 | + toolsFolder, err := common.GetDefaultToolsFolder(packageName) |
| 93 | + if err != nil { |
| 94 | + formatter.PrintError(err, "Cannot get tools folder") |
| 95 | + os.Exit(errCoreConfig) |
| 96 | + } |
| 97 | + ctx.ToolsFolders = []string{toolsFolder} |
| 98 | + |
| 99 | + librariesFolder, err := common.GetDefaultLibFolder() |
| 100 | + if err != nil { |
| 101 | + formatter.PrintError(err, "Cannot get libraries folder") |
| 102 | + os.Exit(errCoreConfig) |
| 103 | + } |
| 104 | + ctx.OtherLibrariesFolders = []string{librariesFolder} |
| 105 | + |
| 106 | + ctx.BuildPath = arduinoCompileFlags.BuildPath |
| 107 | + if ctx.BuildPath != "" { |
| 108 | + // TODO: Needed? |
| 109 | + /*_, err = os.Stat(ctx.BuildPath) |
| 110 | + if err != nil { |
| 111 | + fmt.Fprintln(os.Stderr, err) |
| 112 | + os.Exit(errBadCall) |
| 113 | + }*/ |
| 114 | + |
| 115 | + err = utils.EnsureFolderExists(ctx.BuildPath) |
| 116 | + if err != nil { |
| 117 | + formatter.PrintError(err, "Cannot create the build folder") |
| 118 | + os.Exit(errBadCall) |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + ctx.Verbose = arduinoCompileFlags.Verbose |
| 123 | + ctx.DebugLevel = arduinoCompileFlags.DebugLevel |
| 124 | + |
| 125 | + ctx.USBVidPid = arduinoCompileFlags.VidPid |
| 126 | + ctx.WarningsLevel = arduinoCompileFlags.Warnings |
| 127 | + |
| 128 | + // TODO: |
| 129 | + ctx.ArduinoAPIVersion = "10600" |
| 130 | + //ctx.BuiltInLibrariesFolders = ? |
| 131 | + //ctx.CustomBuildProperties = ? |
| 132 | + //ctx.BuildCachePath = ? |
| 133 | + |
| 134 | + if arduinoCompileFlags.DumpPreferences { |
| 135 | + err = builder.RunParseHardwareAndDumpBuildProperties(ctx) |
| 136 | + } else if arduinoCompileFlags.Preprocess { |
| 137 | + err = builder.RunPreprocess(ctx) |
| 138 | + } else { |
| 139 | + err = builder.RunBuilder(ctx) |
| 140 | + } |
| 141 | + |
| 142 | + if err != nil { |
| 143 | + formatter.PrintError(err, "Compilation failed") |
| 144 | + os.Exit(errGeneric) |
| 145 | + } |
| 146 | +} |
0 commit comments