Skip to content

Commit

Permalink
Working implementation of CGO style preproc directives
Browse files Browse the repository at this point in the history
  • Loading branch information
facchinm committed Mar 7, 2017
1 parent 2dbe042 commit 904250f
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 0 deletions.
118 changes: 118 additions & 0 deletions src/arduino.cc/builder/apply_sketch_preprocessor_directives.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* This file is part of Arduino Builder.
*
* Arduino Builder is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As a special exception, you may use this file as part of a free software
* library without restriction. Specifically, if other files instantiate
* templates or use macros or inline functions from this file, or you compile
* this file and link it with other files to produce an executable, this
* file does not by itself cause the resulting executable to be covered by
* the GNU General Public License. This exception does not however
* invalidate any other reasons why the executable file might be covered by
* the GNU General Public License.
*
* Copyright 2015 Arduino LLC (http://www.arduino.cc/)
*/

package builder

import (
"regexp"
"strings"

"arduino.cc/builder/types"
)

type FindAndApplySketchPreprocessorDirectives struct{}

func (s *FindAndApplySketchPreprocessorDirectives) Run(ctx *types.Context) error {

sketch := ctx.Sketch.MainFile.Source
rewritesProperties := extractPreprocessorDirectives(sketch)

ctx.CustomBuildProperties = append(ctx.CustomBuildProperties, rewritesProperties...)

return nil
}

func extractPreprocessorDirectives(sketch string) []string {
/*
In a very CGO-like fashion, search for strings matching
// #arduino {directive}
declared in the main sketch before the first line of code
use the directive to populate an extra preference map
*/

var properties []string

firstCodeChar := getFirstNonCommentNonBlankCharacter(sketch)

r, _ := regexp.Compile("(?m)^//\\s*#arduino\\s*.*=.*$")
results := r.FindAllString(sketch, -1)
resIdx := r.FindAllStringIndex(sketch, -1)

for i, result := range results {
result = strings.Replace(result, "//", "", 1)
result = strings.Replace(result, "#arduino", "", 1)
result = strings.TrimSpace(result)

This comment has been minimized.

Copy link
@matthijskooijman

matthijskooijman Sep 4, 2017

Doesn't regexp have some way to do this more elegantly, e.g. to return match objects that contain captures (e.g. stuff in parenthesis in the original regexp) and perhaps also the index of the match? The code right now seems fairly fragile to me (the stuff to match is defined in two places now, and I'm not quite sure that it really processes everything matched by the regex properly).

This comment has been minimized.

Copy link
@facchinm

facchinm Sep 4, 2017

Author Owner

Ah, sure, this was only a POC not intended to be merged anytime 😄
If we decide to take this approach we'll need to rewrite it entirely

if resIdx[i][0] < firstCodeChar {
properties = append(properties, result)
}
}
return properties
}

func getFirstNonCommentNonBlankCharacter(text string) int {

lines := strings.Split(text, "\n")

characters := 0

cppStyleComment, _ := regexp.Compile("(?m)^(\\s*)//")
cStyleCommentOneline, _ := regexp.Compile("(?m)^(\\s*)/\\*.*\\*/(\\s*)$")
cStyleStartComment, _ := regexp.Compile("(?m)^(\\s*)/\\*")
cStyleEndComment, _ := regexp.Compile("(?m).*\\*/(\\s*)$")
emptyLine, _ := regexp.Compile("(?m)^\\s*$")

multilineComment := false

for _, line := range lines {
if emptyLine.MatchString(line) {
characters += len(line)
continue
}
if cppStyleComment.MatchString(line) || cStyleCommentOneline.MatchString(line) {
characters += len(line)
continue
}
if cStyleStartComment.MatchString(line) {
multilineComment = true
characters += len(line)
continue
}
if cStyleEndComment.MatchString(line) && multilineComment {
multilineComment = false
characters += len(line)
continue
}
if multilineComment == true {
characters += len(line)
continue
}
break
}
return characters
}
1 change: 1 addition & 0 deletions src/arduino.cc/builder/container_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func (s *ContainerSetupHardwareToolsLibsSketchAndProps) Run(ctx *types.Context)
&SketchLoader{},
&SetupBuildProperties{},
&LoadVIDPIDSpecificProperties{},
&FindAndApplySketchPreprocessorDirectives{},
&SetCustomBuildProperties{},
&AddMissingBuildPropertiesFromParentPlatformTxtFiles{},
}
Expand Down

0 comments on commit 904250f

Please sign in to comment.