Skip to content

Commit 7782bac

Browse files
committed
automatic inclusion of sketch_globals.h for sketch.ino and libraries
1 parent d458040 commit 7782bac

File tree

7 files changed

+49
-3
lines changed

7 files changed

+49
-3
lines changed

legacy/builder/gcc_preproc_runner.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ func prepareGCCPreprocRecipeProperties(ctx *types.Context, sourceFilePath *paths
6161
properties.SetPath(constants.BUILD_PROPERTIES_SOURCE_FILE, sourceFilePath)
6262
properties.SetPath(constants.BUILD_PROPERTIES_PREPROCESSED_FILE_PATH, targetFilePath)
6363

64-
includesStrings := utils.Map(includes.AsStrings(), utils.WrapWithHyphenI)
64+
ctx.SetGlobalIncludeOption()
65+
includesStrings := append(utils.Map(includes.AsStrings(), utils.WrapWithHyphenI), ctx.GlobalIncludeOption)
6566
properties.Set(constants.BUILD_PROPERTIES_INCLUDES, strings.Join(includesStrings, constants.SPACE))
6667

6768
if properties.Get(constants.RECIPE_PREPROC_MACROS) == "" {

legacy/builder/phases/libraries_builder.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ type LibrariesBuilder struct{}
3737
func (s *LibrariesBuilder) Run(ctx *types.Context) error {
3838
librariesBuildPath := ctx.LibrariesBuildPath
3939
buildProperties := ctx.BuildProperties
40-
includes := utils.Map(ctx.IncludeFolders.AsStrings(), utils.WrapWithHyphenI)
40+
ctx.SetGlobalIncludeOption()
41+
includes := append(utils.Map(ctx.IncludeFolders.AsStrings(), utils.WrapWithHyphenI), ctx.GlobalIncludeOption)
4142
libs := ctx.ImportedLibraries
4243

4344
if err := librariesBuildPath.MkdirAll(); err != nil {

legacy/builder/phases/sketch_builder.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ type SketchBuilder struct{}
2727
func (s *SketchBuilder) Run(ctx *types.Context) error {
2828
sketchBuildPath := ctx.SketchBuildPath
2929
buildProperties := ctx.BuildProperties
30-
includes := utils.Map(ctx.IncludeFolders.AsStrings(), utils.WrapWithHyphenI)
30+
ctx.SetGlobalIncludeOption()
31+
includes := append(utils.Map(ctx.IncludeFolders.AsStrings(), utils.WrapWithHyphenI), ctx.GlobalIncludeOption)
3132

3233
if err := sketchBuildPath.MkdirAll(); err != nil {
3334
return errors.WithStack(err)

legacy/builder/types/context.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package types
1717

1818
import (
19+
"os"
1920
"io"
2021
"strings"
2122

@@ -27,6 +28,7 @@ import (
2728
"github.com/arduino/arduino-cli/arduino/libraries/librariesresolver"
2829
"github.com/arduino/arduino-cli/arduino/sketch"
2930
"github.com/arduino/arduino-cli/legacy/builder/i18n"
31+
"github.com/arduino/arduino-cli/legacy/builder/constants"
3032
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
3133
paths "github.com/arduino/go-paths-helper"
3234
properties "github.com/arduino/go-properties-orderedmap"
@@ -177,6 +179,9 @@ type Context struct {
177179
// The provided source data is used instead of reading it from disk.
178180
// The keys of the map are paths relative to sketch folder.
179181
SourceOverride map[string]string
182+
183+
// Compiler directive for transparent inclusion of global definition when present
184+
GlobalIncludeOption string
180185
}
181186

182187
// ExecutableSectionSize represents a section of the executable output file
@@ -254,3 +259,23 @@ func (ctx *Context) GetLogger() i18n.Logger {
254259
func (ctx *Context) SetLogger(l i18n.Logger) {
255260
ctx.logger = l
256261
}
262+
263+
func (ctx *Context) SetGlobalIncludeOption () {
264+
if len(ctx.GlobalIncludeOption) == 0 {
265+
266+
// testing existence of path/to/sketch/sketch_globals.h
267+
268+
globalsHeaderName := ctx.BuildPath.Join("sketch").Join(ctx.Sketch.Name + "_globals.h").String()
269+
_, err := os.Stat(globalsHeaderName);
270+
271+
if os.IsNotExist(err) {
272+
ctx.GetLogger().Fprintln(os.Stdout, constants.LOG_LEVEL_INFO, tr("global definition file is not present") + " '" + globalsHeaderName + "'")
273+
} else {
274+
ctx.GlobalIncludeOption = "-include "
275+
ctx.GlobalIncludeOption += globalsHeaderName
276+
ctx.GetLogger().Fprintln(os.Stdout, constants.LOG_LEVEL_INFO, tr("Using global definition file") + " '" + globalsHeaderName + "'")
277+
}
278+
279+
ctx.GlobalIncludeOption += " "
280+
}
281+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
#ifndef LIB2_SOME_CONFIG
3+
#define LIB2_SOME_CONFIG 0
4+
#endif
5+
6+
#define LIB2_SOME_SIZE ((LIB2_SOME_CONFIG) * 42)

test/testdata/sketch_with_multiple_custom_libraries/sketch_with_multiple_custom_libraries.ino

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#include "lib1.h"
22
#include "lib2.h"
33

4+
#if LIB2_SOME_SIZE != 42
5+
#error should be 42 per global configuration
6+
#endif
7+
48
void setup() {
59
}
610

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
// When it exists under the name 'sketch_globals.h' (next to 'sketch.ino'),
3+
// this user file is always included first during libraries and sketch compilation.
4+
// It allows global library configuration per sketch.
5+
6+
#pragma once
7+
8+
#define LIB2_SOME_CONFIG 1

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy