diff --git a/add_build_properties_from_platform_sketch_txt_file.go b/add_build_properties_from_platform_sketch_txt_file.go new file mode 100644 index 00000000..9f244e24 --- /dev/null +++ b/add_build_properties_from_platform_sketch_txt_file.go @@ -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 + */ + +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 +} diff --git a/arduino-builder/main.go b/arduino-builder/main.go index 18a46856..d695de6c 100644 --- a/arduino-builder/main.go +++ b/arduino-builder/main.go @@ -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 @@ -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") @@ -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() { @@ -374,6 +377,11 @@ func main() { } } + // FLAG_USE_PLATFORM_SKETCH_TXT + if *usePlatformSketchTxt { + ctx.UsePlatformSketchTxt = true + } + if *warningsLevelFlag != "" { ctx.WarningsLevel = *warningsLevelFlag } diff --git a/constants/constants.go b/constants/constants.go index 7254c2df..9743585d 100644 --- a/constants/constants.go +++ b/constants/constants.go @@ -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" @@ -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." diff --git a/container_setup.go b/container_setup.go index 57749aa2..fe7f4caa 100644 --- a/container_setup.go +++ b/container_setup.go @@ -50,6 +50,7 @@ func (s *ContainerSetupHardwareToolsLibsSketchAndProps) Run(ctx *types.Context) &LibrariesLoader{}, &SketchLoader{}, &SetupBuildProperties{}, + &AddBuildPropertiesFromPlatformSketchTxtFile{}, &LoadVIDPIDSpecificProperties{}, &SetCustomBuildProperties{}, &AddMissingBuildPropertiesFromParentPlatformTxtFiles{}, diff --git a/test/add_build_properties_from_platform_sketch_txt_file_test.go b/test/add_build_properties_from_platform_sketch_txt_file_test.go new file mode 100644 index 00000000..513beb0c --- /dev/null +++ b/test/add_build_properties_from_platform_sketch_txt_file_test.go @@ -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 + */ + +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) +} diff --git a/test/sketch_with_platform_sketch_txt/platform.sketch.txt b/test/sketch_with_platform_sketch_txt/platform.sketch.txt new file mode 100644 index 00000000..17ca5122 --- /dev/null +++ b/test/sketch_with_platform_sketch_txt/platform.sketch.txt @@ -0,0 +1,2 @@ +compiler.c.extra_flags = -DSKETCH_DEFINED_VALUE=1 +compiler.cpp.extra_flags = -DSKETCH_DEFINED_VALUE=2 diff --git a/test/sketch_with_platform_sketch_txt/sketch_with_platform_sketch_txt.ino b/test/sketch_with_platform_sketch_txt/sketch_with_platform_sketch_txt.ino new file mode 100644 index 00000000..59606dae --- /dev/null +++ b/test/sketch_with_platform_sketch_txt/sketch_with_platform_sketch_txt.ino @@ -0,0 +1,6 @@ +#ifndef SKETCH_DEFINED_VALUE + #error SKETCH_DEFINED_VALUE was not defined in ./platform.sketch.txt +#endif + +void setup() {} +void loop() {} diff --git a/types/context.go b/types/context.go index e9368c08..4f5d4158 100644 --- a/types/context.go +++ b/types/context.go @@ -64,6 +64,8 @@ type Context struct { SourceGccMinusE string CodeCompletions string + UsePlatformSketchTxt bool + WarningsLevel string // Libraries handling diff --git a/utils/utils.go b/utils/utils.go index 0f9ae060..08a36ab8 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -448,12 +448,9 @@ 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 @@ -461,6 +458,16 @@ func FindFilesInFolder(files *[]string, folder string, extensions CheckExtension 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