|
| 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 | + "regexp" |
| 34 | + "strings" |
| 35 | + |
| 36 | + "arduino.cc/builder/types" |
| 37 | +) |
| 38 | + |
| 39 | +type FindAndApplySketchPreprocessorDirectives struct{} |
| 40 | + |
| 41 | +func (s *FindAndApplySketchPreprocessorDirectives) Run(ctx *types.Context) error { |
| 42 | + |
| 43 | + sketch := ctx.Sketch.MainFile.Source |
| 44 | + rewritesProperties := extractPreprocessorDirectives(sketch) |
| 45 | + |
| 46 | + ctx.CustomBuildProperties = append(ctx.CustomBuildProperties, rewritesProperties...) |
| 47 | + |
| 48 | + return nil |
| 49 | +} |
| 50 | + |
| 51 | +func extractPreprocessorDirectives(sketch string) []string { |
| 52 | + /* |
| 53 | + In a very CGO-like fashion, search for strings matching |
| 54 | + // #arduino {directive} |
| 55 | + declared in the main sketch before the first line of code |
| 56 | + use the directive to populate an extra preference map |
| 57 | + */ |
| 58 | + |
| 59 | + var properties []string |
| 60 | + |
| 61 | + firstCodeChar := getFirstNonCommentNonBlankCharacter(sketch) |
| 62 | + |
| 63 | + r, _ := regexp.Compile("(?m)^//\\s*#arduino\\s*.*=.*$") |
| 64 | + results := r.FindAllString(sketch, -1) |
| 65 | + resIdx := r.FindAllStringIndex(sketch, -1) |
| 66 | + |
| 67 | + for i, result := range results { |
| 68 | + result = strings.Replace(result, "//", "", 1) |
| 69 | + result = strings.Replace(result, "#arduino", "", 1) |
| 70 | + result = strings.TrimSpace(result) |
| 71 | + if resIdx[i][0] < firstCodeChar { |
| 72 | + properties = append(properties, result) |
| 73 | + } |
| 74 | + } |
| 75 | + return properties |
| 76 | +} |
| 77 | + |
| 78 | +func getFirstNonCommentNonBlankCharacter(text string) int { |
| 79 | + |
| 80 | + lines := strings.Split(text, "\n") |
| 81 | + |
| 82 | + characters := 0 |
| 83 | + |
| 84 | + cppStyleComment, _ := regexp.Compile("(?m)^(\\s*)//") |
| 85 | + cStyleCommentOneline, _ := regexp.Compile("(?m)^(\\s*)/\\*.*\\*/(\\s*)$") |
| 86 | + cStyleStartComment, _ := regexp.Compile("(?m)^(\\s*)/\\*") |
| 87 | + cStyleEndComment, _ := regexp.Compile("(?m).*\\*/(\\s*)$") |
| 88 | + emptyLine, _ := regexp.Compile("(?m)^\\s*$") |
| 89 | + |
| 90 | + multilineComment := false |
| 91 | + |
| 92 | + for _, line := range lines { |
| 93 | + if emptyLine.MatchString(line) { |
| 94 | + characters += len(line) |
| 95 | + continue |
| 96 | + } |
| 97 | + if cppStyleComment.MatchString(line) || cStyleCommentOneline.MatchString(line) { |
| 98 | + characters += len(line) |
| 99 | + continue |
| 100 | + } |
| 101 | + if cStyleStartComment.MatchString(line) { |
| 102 | + multilineComment = true |
| 103 | + characters += len(line) |
| 104 | + continue |
| 105 | + } |
| 106 | + if cStyleEndComment.MatchString(line) && multilineComment { |
| 107 | + multilineComment = false |
| 108 | + characters += len(line) |
| 109 | + continue |
| 110 | + } |
| 111 | + if multilineComment == true { |
| 112 | + characters += len(line) |
| 113 | + continue |
| 114 | + } |
| 115 | + break |
| 116 | + } |
| 117 | + return characters |
| 118 | +} |
0 commit comments