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

Read additional BuildProperties from optional ./platform.sketch.txt file #282

Open
wants to merge 3 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
66 changes: 66 additions & 0 deletions add_build_properties_from_platform_sketch_txt_file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* 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 2018 Piotr Henryk Dabrowski <phd@phd.re>
*/

package builder

import (
"path/filepath"

"github.com/arduino/arduino-builder/constants"
"github.com/arduino/arduino-builder/i18n"
"github.com/arduino/arduino-builder/types"
"github.com/arduino/arduino-builder/utils"
properties "github.com/arduino/go-properties-map"
)

type AddBuildPropertiesFromPlatformSketchTxtFile struct{}

func (s *AddBuildPropertiesFromPlatformSketchTxtFile) Run(ctx *types.Context) error {
if !ctx.UsePlatformSketchTxt {
return nil
}

path := filepath.Join(filepath.Dir(ctx.Sketch.MainFile.Name), constants.FILE_PLATFORM_SKETCH_TXT)
if !utils.IsFileReadable(path) {
return nil
}
if ctx.Verbose {
ctx.GetLogger().Println(constants.LOG_LEVEL_INFO, constants.MSG_USING_SKETCH_BUILD_PROPERTIES, path)
}

newBuildProperties := ctx.BuildProperties.Clone()
sketchPlatformProperties, err := properties.SafeLoad(path)
if err != nil {
return i18n.WrapError(err)
}
newBuildProperties.Merge(sketchPlatformProperties)
ctx.BuildProperties = newBuildProperties

return nil
}
8 changes: 8 additions & 0 deletions arduino-builder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ const FLAG_VID_PID = "vid-pid"
const FLAG_JOBS = "jobs"
const FLAG_TRACE = "trace"
const FLAG_EXPERIMENTAL = "experimental"
const FLAG_USE_PLATFORM_SKETCH_TXT = "use-platform-sketch-txt"

type foldersFlag []string

Expand Down Expand Up @@ -151,6 +152,7 @@ var vidPidFlag *string
var jobsFlag *int
var traceFlag *bool
var experimentalFeatures *bool
var usePlatformSketchTxt *bool

func init() {
compileFlag = flag.Bool(FLAG_ACTION_COMPILE, false, "compiles the given sketch")
Expand Down Expand Up @@ -179,6 +181,7 @@ func init() {
jobsFlag = flag.Int(FLAG_JOBS, 0, "specify how many concurrent gcc processes should run at the same time. Defaults to the number of available cores on the running machine")
traceFlag = flag.Bool(FLAG_TRACE, false, "traces the whole process lifecycle")
experimentalFeatures = flag.Bool(FLAG_EXPERIMENTAL, false, "enables experimental features")
usePlatformSketchTxt = flag.Bool(FLAG_USE_PLATFORM_SKETCH_TXT, false, "allow reading additional platform build properies from an optional platform.sketch.txt file")
}

func main() {
Expand Down Expand Up @@ -374,6 +377,11 @@ func main() {
}
}

// FLAG_USE_PLATFORM_SKETCH_TXT
if *usePlatformSketchTxt {
ctx.UsePlatformSketchTxt = true
}

if *warningsLevelFlag != "" {
ctx.WarningsLevel = *warningsLevelFlag
}
Expand Down
2 changes: 2 additions & 0 deletions constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ const FILE_CTAGS_TARGET_FOR_GCC_MINUS_E = "ctags_target_for_gcc_minus_e.cpp"
const FILE_GCC_PREPROC_TARGET = "gcc_preproc_target.cpp"
const FILE_PLATFORM_KEYS_REWRITE_TXT = "platform.keys.rewrite.txt"
const FILE_PLATFORM_LOCAL_TXT = "platform.local.txt"
const FILE_PLATFORM_SKETCH_TXT = "platform.sketch.txt"
const FILE_PLATFORM_TXT = "platform.txt"
const FILE_PROGRAMMERS_TXT = "programmers.txt"
const FILE_INCLUDES_CACHE = "includes.cache"
Expand Down Expand Up @@ -198,6 +199,7 @@ const MSG_USING_BOARD = "Using board '{0}' from platform in folder: {1}"
const MSG_USING_CORE = "Using core '{0}' from platform in folder: {1}"
const MSG_USING_PREVIOUS_COMPILED_FILE = "Using previously compiled file: {0}"
const MSG_USING_CACHED_INCLUDES = "Using cached library dependencies for file: {0}"
const MSG_USING_SKETCH_BUILD_PROPERTIES = "Using sketch build properties from file: {0}"
const MSG_WARNING_LIB_INVALID_CATEGORY = "WARNING: Category '{0}' in library {1} is not valid. Setting to '{2}'"
const MSG_WARNING_PLATFORM_MISSING_VALUE = "Warning: platform.txt from core '{0}' misses property '{1}', using default value '{2}'. Consider upgrading this core."
const MSG_WARNING_PLATFORM_OLD_VALUES = "Warning: platform.txt from core '{0}' contains deprecated {1}, automatically converted to {2}. Consider upgrading this core."
Expand Down
1 change: 1 addition & 0 deletions 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)
&LibrariesLoader{},
&SketchLoader{},
&SetupBuildProperties{},
&AddBuildPropertiesFromPlatformSketchTxtFile{},
&LoadVIDPIDSpecificProperties{},
&SetCustomBuildProperties{},
&AddMissingBuildPropertiesFromParentPlatformTxtFiles{},
Expand Down
60 changes: 60 additions & 0 deletions test/add_build_properties_from_platform_sketch_txt_file_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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 2018 Piotr Henryk Dabrowski <phd@phd.re>
*/

package test

import (
"path/filepath"
"testing"

"github.com/arduino/arduino-builder"
"github.com/stretchr/testify/require"
)

func TestAddBuildBoardPropertiesFromPlatformSketchTxtFile(t *testing.T) {
ctx := makeDefaultContext(t)
ctx.UsePlatformSketchTxt = true

sketchLocation := filepath.Join("sketch_with_platform_sketch_txt", "sketch_with_platform_sketch_txt.ino")
ctx.SketchLocation = sketchLocation

err := builder.RunBuilder(ctx)
require.NoError(t, err, "Unexpected build error for "+sketchLocation)
}

func TestAddBuildBoardPropertiesFromPlatformSketchTxtFileDisabled(t *testing.T) {
ctx := makeDefaultContext(t)
ctx.UsePlatformSketchTxt = false

sketchLocation := filepath.Join("sketch_with_platform_sketch_txt", "sketch_with_platform_sketch_txt.ino")
ctx.SketchLocation = sketchLocation

err := builder.RunBuilder(ctx)
require.Error(t, err, "No expected build error for "+sketchLocation)
}
2 changes: 2 additions & 0 deletions test/sketch_with_platform_sketch_txt/platform.sketch.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
compiler.c.extra_flags = -DSKETCH_DEFINED_VALUE=1
compiler.cpp.extra_flags = -DSKETCH_DEFINED_VALUE=2
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef SKETCH_DEFINED_VALUE
#error SKETCH_DEFINED_VALUE was not defined in ./platform.sketch.txt
#endif

void setup() {}
void loop() {}
2 changes: 2 additions & 0 deletions types/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ type Context struct {
SourceGccMinusE string
CodeCompletions string

UsePlatformSketchTxt bool

WarningsLevel string

// Libraries handling
Expand Down
15 changes: 11 additions & 4 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,19 +448,26 @@ func FindFilesInFolder(files *[]string, folder string, extensions CheckExtension
return nil
}

// See if the file is readable by opening it
currentFile, err := os.Open(path)
if err != nil {
if !IsFileReadable(path) {
return nil
}
currentFile.Close()

*files = append(*files, path)
return nil
}
return gohasissues.Walk(folder, walkFunc)
}

func IsFileReadable(path string) bool {
// See if the file is readable by opening it
file, err := os.Open(path)
if err != nil {
return false
}
file.Close()
return true
}

func GetParentFolder(basefolder string, n int) string {
tempFolder := basefolder
i := 0
Expand Down