Skip to content

Commit a5fc12b

Browse files
committed
Adding support for sketch-specific build properties
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]>
1 parent f10d1b3 commit a5fc12b

File tree

6 files changed

+135
-0
lines changed

6 files changed

+135
-0
lines changed

Diff for: main.go

+9
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,15 @@ func main() {
293293
context[constants.CTX_LOGGER] = i18n.HumanLogger{}
294294
}
295295

296+
sketchBuildOptions, err := builder.GetSketchBuildProperties(context)
297+
if err != nil {
298+
printError(err, printStackTrace)
299+
defer os.Exit(1)
300+
return
301+
}
302+
context[constants.CTX_SKETCH_BUILD_PROPERTIES] = sketchBuildOptions
303+
304+
296305
if compile {
297306
err = builder.RunBuilder(context)
298307
} else if dumpPrefs {

Diff for: src/arduino.cc/builder/constants/constants.go

+2
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ const CTX_PROTOTYPES_OF_PREPROC_SOURCE = "prototypesOfPreprocSource"
111111
const CTX_PROTOTYPES_OF_SOURCE = "prototypesOfSource"
112112
const CTX_PROTOTYPES = "prototypes"
113113
const CTX_SKETCH_BUILD_PATH = "sketchBuildPath"
114+
const CTX_SKETCH_BUILD_PROPERTIES = "sketchBuildProperties"
114115
const CTX_SKETCH_LOCATION = "sketchLocation"
115116
const CTX_SKETCH = "sketch"
116117
const CTX_SOURCE = "source"
@@ -242,6 +243,7 @@ const RECIPE_PREPROC_MACROS = "recipe.preproc.macros"
242243
const RECIPE_S_PATTERN = "recipe.S.o.pattern"
243244
const REWRITING_DISABLED = "disabled"
244245
const REWRITING = "rewriting"
246+
const SKETCH_BUILD_OPTIONS_TXT = "build_props.txt"
245247
const SPACE = " "
246248
const TOOL_NAME = "name"
247249
const TOOL_URL = "url"

Diff for: src/arduino.cc/builder/container_setup.go

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ func (s *ContainerSetupHardwareToolsLibsSketchAndProps) Run(context map[string]i
5050
&SketchLoader{},
5151
&SetupBuildProperties{},
5252
&LoadVIDPIDSpecificProperties{},
53+
&SetSketchBuildProperties{},
5354
&SetCustomBuildProperties{},
5455
&AddMissingBuildPropertiesFromParentPlatformTxtFiles{},
5556
}

Diff for: src/arduino.cc/builder/create_build_options_map.go

+9
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ func (s *CreateBuildOptionsMap) Run(context map[string]interface{}) error {
4949
constants.CTX_FQBN,
5050
constants.CTX_SKETCH_LOCATION,
5151
constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION,
52+
constants.CTX_SKETCH_BUILD_PROPERTIES,
5253
constants.CTX_CUSTOM_BUILD_PROPERTIES,
5354
}
5455

@@ -61,6 +62,14 @@ func (s *CreateBuildOptionsMap) Run(context map[string]interface{}) error {
6162
value = strings.Join(originalValue.([]string), ",")
6263
} else if kindOfValue == reflect.String {
6364
value = originalValue.(string)
65+
} else if kindOfValue == reflect.TypeOf(make(map[string]string)).Kind() {
66+
data := originalValue.(map[string]string)
67+
68+
bytes, err := json.Marshal(data)
69+
if err != nil {
70+
return utils.WrapError(err)
71+
}
72+
value = string(bytes)
6473
} else {
6574
return utils.Errorf(context, constants.MSG_UNHANDLED_TYPE_IN_CONTEXT, kindOfValue.String(), key)
6675
}
+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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 Steve Marple
28+
*/
29+
30+
package builder
31+
32+
import (
33+
"arduino.cc/builder/constants"
34+
"arduino.cc/builder/props"
35+
"github.com/go-errors/errors"
36+
"os"
37+
"path/filepath"
38+
)
39+
40+
func GetSketchBuildPropertiesFilename(context map[string]interface{}) (string, error) {
41+
sketchLocation, ok := context[constants.CTX_SKETCH_LOCATION].(string)
42+
if !ok {
43+
return "", errors.New("Unknown sketch location")
44+
}
45+
46+
filename := filepath.Join(filepath.Dir(sketchLocation), constants.SKETCH_BUILD_OPTIONS_TXT)
47+
return filename, nil
48+
}
49+
50+
51+
func GetSketchBuildProperties(context map[string]interface{}) (map[string]string, error) {
52+
filename, err := GetSketchBuildPropertiesFilename(context)
53+
if err != nil {
54+
return nil, err
55+
}
56+
_, err = os.Stat(filename)
57+
if err == nil {
58+
return props.SafeLoad(filename)
59+
}
60+
return make(map[string]string), nil
61+
}
62+
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 Steve Marple
28+
*/
29+
30+
package builder
31+
32+
import (
33+
"arduino.cc/builder/constants"
34+
"arduino.cc/builder/utils"
35+
)
36+
37+
type SetSketchBuildProperties struct{}
38+
39+
func (s *SetSketchBuildProperties) Run(context map[string]interface{}) error {
40+
if !utils.MapHas(context, constants.CTX_SKETCH_BUILD_PROPERTIES) {
41+
return nil
42+
}
43+
44+
buildProperties := context[constants.CTX_BUILD_PROPERTIES].(map[string]string)
45+
46+
sketchBuildProperties := context[constants.CTX_SKETCH_BUILD_PROPERTIES].(map[string]string)
47+
for key, value := range sketchBuildProperties {
48+
buildProperties[key] = value
49+
}
50+
51+
return nil
52+
}

0 commit comments

Comments
 (0)