Skip to content

Commit

Permalink
Adding support for sketch-specific build properties
Browse files Browse the repository at this point in the history
Tests for existence of a build_props.txt file in the sketch
directory. If present reads properties similar to those found in the
hardware and board files, allowing the user to easily customise the
build for a specific sketch. Most useful when containing lines similar
to

compiler.c.extra_flags=-D NDEBUG
compiler.cpp.extra_flags=-D NDEBUG -D MYLIBRARY_BUFSIZE=100

which enables the user to have macros defined for all compilation
units, including libraries.

Signed-off-by: Steve Marple <[email protected]>
  • Loading branch information
stevemarple committed Sep 30, 2015
1 parent f10d1b3 commit a5fc12b
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 0 deletions.
9 changes: 9 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,15 @@ func main() {
context[constants.CTX_LOGGER] = i18n.HumanLogger{}
}

sketchBuildOptions, err := builder.GetSketchBuildProperties(context)
if err != nil {
printError(err, printStackTrace)
defer os.Exit(1)
return
}
context[constants.CTX_SKETCH_BUILD_PROPERTIES] = sketchBuildOptions


if compile {
err = builder.RunBuilder(context)
} else if dumpPrefs {
Expand Down
2 changes: 2 additions & 0 deletions src/arduino.cc/builder/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ const CTX_PROTOTYPES_OF_PREPROC_SOURCE = "prototypesOfPreprocSource"
const CTX_PROTOTYPES_OF_SOURCE = "prototypesOfSource"
const CTX_PROTOTYPES = "prototypes"
const CTX_SKETCH_BUILD_PATH = "sketchBuildPath"
const CTX_SKETCH_BUILD_PROPERTIES = "sketchBuildProperties"
const CTX_SKETCH_LOCATION = "sketchLocation"
const CTX_SKETCH = "sketch"
const CTX_SOURCE = "source"
Expand Down Expand Up @@ -242,6 +243,7 @@ const RECIPE_PREPROC_MACROS = "recipe.preproc.macros"
const RECIPE_S_PATTERN = "recipe.S.o.pattern"
const REWRITING_DISABLED = "disabled"
const REWRITING = "rewriting"
const SKETCH_BUILD_OPTIONS_TXT = "build_props.txt"
const SPACE = " "
const TOOL_NAME = "name"
const TOOL_URL = "url"
Expand Down
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(context map[string]i
&SketchLoader{},
&SetupBuildProperties{},
&LoadVIDPIDSpecificProperties{},
&SetSketchBuildProperties{},
&SetCustomBuildProperties{},
&AddMissingBuildPropertiesFromParentPlatformTxtFiles{},
}
Expand Down
9 changes: 9 additions & 0 deletions src/arduino.cc/builder/create_build_options_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func (s *CreateBuildOptionsMap) Run(context map[string]interface{}) error {
constants.CTX_FQBN,
constants.CTX_SKETCH_LOCATION,
constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION,
constants.CTX_SKETCH_BUILD_PROPERTIES,
constants.CTX_CUSTOM_BUILD_PROPERTIES,
}

Expand All @@ -61,6 +62,14 @@ func (s *CreateBuildOptionsMap) Run(context map[string]interface{}) error {
value = strings.Join(originalValue.([]string), ",")
} else if kindOfValue == reflect.String {
value = originalValue.(string)
} else if kindOfValue == reflect.TypeOf(make(map[string]string)).Kind() {
data := originalValue.(map[string]string)

bytes, err := json.Marshal(data)
if err != nil {
return utils.WrapError(err)
}
value = string(bytes)
} else {
return utils.Errorf(context, constants.MSG_UNHANDLED_TYPE_IN_CONTEXT, kindOfValue.String(), key)
}
Expand Down
62 changes: 62 additions & 0 deletions src/arduino.cc/builder/get_sketch_build_properties.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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 Steve Marple
*/

package builder

import (
"arduino.cc/builder/constants"
"arduino.cc/builder/props"
"github.com/go-errors/errors"
"os"
"path/filepath"
)

func GetSketchBuildPropertiesFilename(context map[string]interface{}) (string, error) {
sketchLocation, ok := context[constants.CTX_SKETCH_LOCATION].(string)
if !ok {
return "", errors.New("Unknown sketch location")
}

filename := filepath.Join(filepath.Dir(sketchLocation), constants.SKETCH_BUILD_OPTIONS_TXT)
return filename, nil
}


func GetSketchBuildProperties(context map[string]interface{}) (map[string]string, error) {
filename, err := GetSketchBuildPropertiesFilename(context)
if err != nil {
return nil, err
}
_, err = os.Stat(filename)
if err == nil {
return props.SafeLoad(filename)
}
return make(map[string]string), nil
}

52 changes: 52 additions & 0 deletions src/arduino.cc/builder/set_sketch_build_properties.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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 Steve Marple
*/

package builder

import (
"arduino.cc/builder/constants"
"arduino.cc/builder/utils"
)

type SetSketchBuildProperties struct{}

func (s *SetSketchBuildProperties) Run(context map[string]interface{}) error {
if !utils.MapHas(context, constants.CTX_SKETCH_BUILD_PROPERTIES) {
return nil
}

buildProperties := context[constants.CTX_BUILD_PROPERTIES].(map[string]string)

sketchBuildProperties := context[constants.CTX_SKETCH_BUILD_PROPERTIES].(map[string]string)
for key, value := range sketchBuildProperties {
buildProperties[key] = value
}

return nil
}

0 comments on commit a5fc12b

Please sign in to comment.