|
| 1 | +/* |
| 2 | + * This file is part of Arduino Builder. |
| 3 | + * |
| 4 | + * Arduino Builder 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 2015 Arduino LLC (http://www.arduino.cc/) |
| 28 | + */ |
| 29 | + |
| 30 | +package builder |
| 31 | + |
| 32 | +import ( |
| 33 | + "fmt" |
| 34 | + "path/filepath" |
| 35 | + |
| 36 | + "io/ioutil" |
| 37 | + |
| 38 | + "arduino.cc/builder/constants" |
| 39 | + "arduino.cc/builder/i18n" |
| 40 | + "arduino.cc/builder/types" |
| 41 | + "arduino.cc/builder/utils" |
| 42 | +) |
| 43 | + |
| 44 | +type PreprocessSketch struct{} |
| 45 | + |
| 46 | +func (s *PreprocessSketch) Run(ctx *types.Context) error { |
| 47 | + sourceFile := filepath.Join(ctx.SketchBuildPath, filepath.Base(ctx.Sketch.MainFile.Name)+".cpp") |
| 48 | + commands := []types.Command{ |
| 49 | + &GCCPreprocRunner{SourceFilePath: sourceFile, TargetFileName: constants.FILE_CTAGS_TARGET_FOR_GCC_MINUS_E, Includes: ctx.IncludeFolders}, |
| 50 | + &ArduinoPreprocessorRunner{}, |
| 51 | + &SketchSaver{}, |
| 52 | + } |
| 53 | + |
| 54 | + for _, command := range commands { |
| 55 | + PrintRingNameIfDebug(ctx, command) |
| 56 | + err := command.Run(ctx) |
| 57 | + if err != nil { |
| 58 | + return i18n.WrapError(err) |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + return nil |
| 63 | +} |
| 64 | + |
| 65 | +type ArduinoPreprocessorRunner struct{} |
| 66 | + |
| 67 | +func (s *ArduinoPreprocessorRunner) Run(ctx *types.Context) error { |
| 68 | + buildProperties := ctx.BuildProperties |
| 69 | + targetFilePath := ctx.FileToRead |
| 70 | + logger := ctx.GetLogger() |
| 71 | + |
| 72 | + properties := buildProperties.Clone() |
| 73 | + toolProps := buildProperties.SubTree("tools").SubTree("arduino-preprocessor") |
| 74 | + properties.Merge(toolProps) |
| 75 | + properties[constants.BUILD_PROPERTIES_SOURCE_FILE] = targetFilePath |
| 76 | + |
| 77 | + pattern := properties[constants.BUILD_PROPERTIES_PATTERN] |
| 78 | + if pattern == constants.EMPTY_STRING { |
| 79 | + return i18n.ErrorfWithLogger(logger, constants.MSG_PATTERN_MISSING, "arduino-preprocessor") |
| 80 | + } |
| 81 | + |
| 82 | + commandLine := properties.ExpandPropsInString(pattern) |
| 83 | + command, err := utils.PrepareCommand(commandLine, logger) |
| 84 | + if err != nil { |
| 85 | + return i18n.WrapError(err) |
| 86 | + } |
| 87 | + |
| 88 | + verbose := ctx.Verbose |
| 89 | + if verbose { |
| 90 | + fmt.Println(commandLine) |
| 91 | + } |
| 92 | + |
| 93 | + sourceBytes, err := command.Output() |
| 94 | + if err != nil { |
| 95 | + return i18n.WrapError(err) |
| 96 | + } |
| 97 | + |
| 98 | + ctx.Source = string(sourceBytes) |
| 99 | + if ctx.Source == "" { |
| 100 | + // If the output is empty the original sketch may pass through |
| 101 | + buf, err := ioutil.ReadFile(targetFilePath) |
| 102 | + if err != nil { |
| 103 | + return i18n.WrapError(err) |
| 104 | + } |
| 105 | + ctx.Source = string(buf) |
| 106 | + } |
| 107 | + //fmt.Printf("PREPROCESSOR OUTPUT:\n%s\n", ctx.Source) |
| 108 | + return nil |
| 109 | +} |
0 commit comments