Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

automatic inclusion of sketch_globals.h for sketch.ino and libraries #1524

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion legacy/builder/gcc_preproc_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ func prepareGCCPreprocRecipeProperties(ctx *types.Context, sourceFilePath *paths
properties.SetPath(constants.BUILD_PROPERTIES_SOURCE_FILE, sourceFilePath)
properties.SetPath(constants.BUILD_PROPERTIES_PREPROCESSED_FILE_PATH, targetFilePath)

includesStrings := utils.Map(includes.AsStrings(), utils.WrapWithHyphenI)
ctx.SetGlobalIncludeOption()
includesStrings := append(utils.Map(includes.AsStrings(), utils.WrapWithHyphenI), ctx.GlobalIncludeOption)
properties.Set(constants.BUILD_PROPERTIES_INCLUDES, strings.Join(includesStrings, constants.SPACE))

if properties.Get(constants.RECIPE_PREPROC_MACROS) == "" {
Expand Down
3 changes: 2 additions & 1 deletion legacy/builder/phases/libraries_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ type LibrariesBuilder struct{}
func (s *LibrariesBuilder) Run(ctx *types.Context) error {
librariesBuildPath := ctx.LibrariesBuildPath
buildProperties := ctx.BuildProperties
includes := utils.Map(ctx.IncludeFolders.AsStrings(), utils.WrapWithHyphenI)
ctx.SetGlobalIncludeOption()
includes := append(utils.Map(ctx.IncludeFolders.AsStrings(), utils.WrapWithHyphenI), ctx.GlobalIncludeOption)
libs := ctx.ImportedLibraries

if err := librariesBuildPath.MkdirAll(); err != nil {
Expand Down
3 changes: 2 additions & 1 deletion legacy/builder/phases/sketch_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ type SketchBuilder struct{}
func (s *SketchBuilder) Run(ctx *types.Context) error {
sketchBuildPath := ctx.SketchBuildPath
buildProperties := ctx.BuildProperties
includes := utils.Map(ctx.IncludeFolders.AsStrings(), utils.WrapWithHyphenI)
ctx.SetGlobalIncludeOption()
includes := append(utils.Map(ctx.IncludeFolders.AsStrings(), utils.WrapWithHyphenI), ctx.GlobalIncludeOption)

if err := sketchBuildPath.MkdirAll(); err != nil {
return errors.WithStack(err)
Expand Down
25 changes: 25 additions & 0 deletions legacy/builder/types/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import (
"github.com/arduino/arduino-cli/arduino/libraries/librariesresolver"
"github.com/arduino/arduino-cli/arduino/sketch"
"github.com/arduino/arduino-cli/commands"
"github.com/arduino/arduino-cli/legacy/builder/i18n"
"github.com/arduino/arduino-cli/legacy/builder/constants"
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
paths "github.com/arduino/go-paths-helper"
properties "github.com/arduino/go-properties-orderedmap"
Expand Down Expand Up @@ -179,6 +181,9 @@ type Context struct {
// The provided source data is used instead of reading it from disk.
// The keys of the map are paths relative to sketch folder.
SourceOverride map[string]string

// Compiler directive for transparent inclusion of global definition when present
GlobalIncludeOption string
}

// ExecutableSectionSize represents a section of the executable output file
Expand Down Expand Up @@ -271,3 +276,23 @@ func (ctx *Context) Warn(msg string) {
}
ctx.stdLock.Unlock()
}

func (ctx *Context) SetGlobalIncludeOption () {
if len(ctx.GlobalIncludeOption) == 0 {

// testing existence of path/to/sketch/sketch_globals.h

globalsHeaderName := ctx.BuildPath.Join("sketch").Join(ctx.Sketch.Name + "_globals.h").String()
_, err := os.Stat(globalsHeaderName);

if os.IsNotExist(err) {
ctx.GetLogger().Fprintln(os.Stdout, constants.LOG_LEVEL_INFO, tr("global definition file is not present") + " '" + globalsHeaderName + "'")
} else {
ctx.GlobalIncludeOption = "-include "
ctx.GlobalIncludeOption += globalsHeaderName
ctx.GetLogger().Fprintln(os.Stdout, constants.LOG_LEVEL_INFO, tr("Using global definition file") + " '" + globalsHeaderName + "'")
}

ctx.GlobalIncludeOption += " "
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

#ifndef LIB2_SOME_CONFIG
#define LIB2_SOME_CONFIG 0
#endif

#define LIB2_SOME_SIZE ((LIB2_SOME_CONFIG) * 42)
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#include "lib1.h"
#include "lib2.h"

#if LIB2_SOME_SIZE != 42
#error should be 42 per global configuration
#endif

void setup() {
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

// When it exists under the name 'sketch_globals.h' (next to 'sketch.ino'),
// this user file is always included first during libraries and sketch compilation.
// It allows global library configuration per sketch.

#pragma once

#define LIB2_SOME_CONFIG 1