Unverified Commit 8c2b8e74 authored by Cristian Maglie's avatar Cristian Maglie Committed by GitHub

[skip-changelog] Yet another small refactoring of legacy package (#2127)

* Removed some legacy constants

* Converted PreprocessSketchArduino and ContainerAddPrototypes into functions
parent 6ee0c24c
......@@ -115,13 +115,11 @@ func (s *Builder) Run(ctx *types.Context) error {
type PreprocessSketch struct{}
func (s *PreprocessSketch) Run(ctx *types.Context) error {
var commands []types.Command
if ctx.UseArduinoPreprocessor {
commands = append(commands, &PreprocessSketchArduino{})
return PreprocessSketchWithArduinoPreprocessor(ctx)
} else {
commands = append(commands, &ContainerAddPrototypes{})
return PreprocessSketchWithCtags(ctx)
}
return runCommands(ctx, commands)
}
type Preprocess struct{}
......
......@@ -163,7 +163,7 @@ func compileFileWithRecipe(ctx *types.Context, sourcePath *paths.Path, source *p
properties := buildProperties.Clone()
properties.Set(constants.BUILD_PROPERTIES_COMPILER_WARNING_FLAGS, properties.Get(constants.BUILD_PROPERTIES_COMPILER_WARNING_FLAGS+"."+ctx.WarningsLevel))
properties.Set(constants.BUILD_PROPERTIES_INCLUDES, strings.Join(includes, constants.SPACE))
properties.SetPath(constants.BUILD_PROPERTIES_SOURCE_FILE, source)
properties.SetPath("source_file", source)
relativeSource, err := sourcePath.RelTo(source)
if err != nil {
return nil, errors.WithStack(err)
......
......@@ -31,14 +31,8 @@ const BUILD_PROPERTIES_COMPILER_WARNING_FLAGS = "compiler.warning_flags"
const BUILD_PROPERTIES_FQBN = "build.fqbn"
const BUILD_PROPERTIES_INCLUDES = "includes"
const BUILD_PROPERTIES_OBJECT_FILE = "object_file"
const BUILD_PROPERTIES_PATTERN = "pattern"
const BUILD_PROPERTIES_PREPROCESSED_FILE_PATH = "preprocessed_file_path"
const BUILD_PROPERTIES_RUNTIME_PLATFORM_PATH = "runtime.platform.path"
const BUILD_PROPERTIES_SOURCE_FILE = "source_file"
const BUILD_PROPERTIES_TOOLS_KEY = "tools"
const CTAGS = "ctags"
const EMPTY_STRING = ""
const FILE_CTAGS_TARGET_FOR_GCC_MINUS_E = "ctags_target_for_gcc_minus_e.cpp"
const FILE_PLATFORM_KEYS_REWRITE_TXT = "platform.keys.rewrite.txt"
const FOLDER_BOOTLOADERS = "bootloaders"
const FOLDER_CORE = "core"
......@@ -65,7 +59,6 @@ const PLATFORM_URL = "url"
const PLATFORM_VERSION = "version"
const RECIPE_AR_PATTERN = "recipe.ar.pattern"
const RECIPE_C_COMBINE_PATTERN = "recipe.c.combine.pattern"
const RECIPE_PREPROC_MACROS = "recipe.preproc.macros"
const REWRITING_DISABLED = "disabled"
const REWRITING = "rewriting"
const SPACE = " "
......
......@@ -19,19 +19,16 @@ import (
"fmt"
bldr "github.com/arduino/arduino-cli/arduino/builder"
"github.com/arduino/arduino-cli/legacy/builder/constants"
"github.com/arduino/arduino-cli/legacy/builder/types"
"github.com/pkg/errors"
)
type ContainerAddPrototypes struct{}
func (s *ContainerAddPrototypes) Run(ctx *types.Context) error {
func PreprocessSketchWithCtags(ctx *types.Context) error {
// Generate the full pathname for the preproc output file
if err := ctx.PreprocPath.MkdirAll(); err != nil {
return errors.WithStack(err)
}
targetFilePath := ctx.PreprocPath.Join(constants.FILE_CTAGS_TARGET_FOR_GCC_MINUS_E)
targetFilePath := ctx.PreprocPath.Join("ctags_target_for_gcc_minus_e.cpp")
// Run preprocessor
sourceFile := ctx.SketchBuildPath.Join(ctx.Sketch.MainFile.Base() + ".cpp")
......@@ -53,7 +50,7 @@ func (s *ContainerAddPrototypes) Run(ctx *types.Context) error {
commands := []types.Command{
&ReadFileAndStoreInContext{FileToRead: targetFilePath, Target: &ctx.SourceGccMinusE},
&FilterSketchSource{Source: &ctx.SourceGccMinusE},
&CTagsTargetFileSaver{Source: &ctx.SourceGccMinusE, TargetFileName: constants.FILE_CTAGS_TARGET_FOR_GCC_MINUS_E},
&CTagsTargetFileSaver{Source: &ctx.SourceGccMinusE, TargetFileName: "ctags_target_for_gcc_minus_e.cpp"},
&CTagsRunner{},
&PrototypesAdder{},
}
......
......@@ -114,13 +114,14 @@ func (s *ExportProjectCMake) Run(ctx *types.Context) error {
fmt.Println(err)
}
if err := PreprocessSketchWithCtags(ctx); err != nil {
return err
}
// Use old ctags method to generate export file
commands := []types.Command{
//&ContainerMergeCopySketchFiles{},
&ContainerAddPrototypes{},
&FilterSketchSource{Source: &ctx.Source, RemoveLineMarkers: true},
}
for _, command := range commands {
command.Run(ctx)
}
......
......@@ -19,7 +19,6 @@ import (
"os"
"os/exec"
"github.com/arduino/arduino-cli/legacy/builder/constants"
"github.com/arduino/arduino-cli/legacy/builder/ctags"
"github.com/arduino/arduino-cli/legacy/builder/types"
"github.com/arduino/arduino-cli/legacy/builder/utils"
......@@ -34,12 +33,12 @@ func (s *CTagsRunner) Run(ctx *types.Context) error {
ctagsTargetFilePath := ctx.CTagsTargetFile
ctagsProperties := buildProperties.Clone()
ctagsProperties.Merge(buildProperties.SubTree(constants.BUILD_PROPERTIES_TOOLS_KEY).SubTree(constants.CTAGS))
ctagsProperties.SetPath(constants.BUILD_PROPERTIES_SOURCE_FILE, ctagsTargetFilePath)
ctagsProperties.Merge(buildProperties.SubTree("tools").SubTree("ctags"))
ctagsProperties.SetPath("source_file", ctagsTargetFilePath)
pattern := ctagsProperties.Get(constants.BUILD_PROPERTIES_PATTERN)
if pattern == constants.EMPTY_STRING {
return errors.Errorf(tr("%s pattern is missing"), constants.CTAGS)
pattern := ctagsProperties.Get("pattern")
if pattern == "" {
return errors.Errorf(tr("%s pattern is missing"), "ctags")
}
commandLine := ctagsProperties.ExpandPropsInString(pattern)
......
......@@ -20,7 +20,6 @@ import (
"strings"
"github.com/arduino/arduino-cli/legacy/builder/builder_utils"
"github.com/arduino/arduino-cli/legacy/builder/constants"
"github.com/arduino/arduino-cli/legacy/builder/types"
"github.com/arduino/arduino-cli/legacy/builder/utils"
"github.com/arduino/go-paths-helper"
......@@ -58,18 +57,18 @@ func GCCPreprocRunnerForDiscoveringIncludes(ctx *types.Context, sourceFilePath *
func prepareGCCPreprocRecipeProperties(ctx *types.Context, sourceFilePath *paths.Path, targetFilePath *paths.Path, includes paths.PathList) (*exec.Cmd, error) {
properties := ctx.BuildProperties.Clone()
properties.Set("build.library_discovery_phase", "1")
properties.SetPath(constants.BUILD_PROPERTIES_SOURCE_FILE, sourceFilePath)
properties.SetPath(constants.BUILD_PROPERTIES_PREPROCESSED_FILE_PATH, targetFilePath)
properties.SetPath("source_file", sourceFilePath)
properties.SetPath("preprocessed_file_path", targetFilePath)
includesStrings := utils.Map(includes.AsStrings(), utils.WrapWithHyphenI)
properties.Set(constants.BUILD_PROPERTIES_INCLUDES, strings.Join(includesStrings, constants.SPACE))
properties.Set("includes", strings.Join(includesStrings, " "))
if properties.Get(constants.RECIPE_PREPROC_MACROS) == "" {
if properties.Get("recipe.preproc.macros") == "" {
//generate PREPROC_MACROS from RECIPE_CPP_PATTERN
properties.Set(constants.RECIPE_PREPROC_MACROS, GeneratePreprocPatternFromCompile(properties.Get("recipe.cpp.o.pattern")))
properties.Set("recipe.preproc.macros", GeneratePreprocPatternFromCompile(properties.Get("recipe.cpp.o.pattern")))
}
cmd, err := builder_utils.PrepareCommandForRecipe(properties, constants.RECIPE_PREPROC_MACROS, true, ctx.PackageManager.GetEnvVarsForSpawnedProcess())
cmd, err := builder_utils.PrepareCommandForRecipe(properties, "recipe.preproc.macros", true, ctx.PackageManager.GetEnvVarsForSpawnedProcess())
if err != nil {
return nil, errors.WithStack(err)
}
......
......@@ -23,7 +23,6 @@ import (
"runtime"
bldr "github.com/arduino/arduino-cli/arduino/builder"
"github.com/arduino/arduino-cli/legacy/builder/constants"
"github.com/arduino/arduino-cli/legacy/builder/types"
"github.com/arduino/arduino-cli/legacy/builder/utils"
properties "github.com/arduino/go-properties-orderedmap"
......@@ -40,9 +39,7 @@ var ArduinoPreprocessorProperties = properties.NewFromHashmap(map[string]string{
"preproc.macros.flags": "-w -x c++ -E -CC",
})
type PreprocessSketchArduino struct{}
func (s *PreprocessSketchArduino) Run(ctx *types.Context) error {
func PreprocessSketchWithArduinoPreprocessor(ctx *types.Context) error {
sourceFile := ctx.SketchBuildPath.Join(ctx.Sketch.MainFile.Base() + ".cpp")
commands := []types.Command{
&ArduinoPreprocessorRunner{},
......@@ -52,7 +49,7 @@ func (s *PreprocessSketchArduino) Run(ctx *types.Context) error {
return errors.WithStack(err)
}
GCCPreprocRunner(ctx, sourceFile, ctx.PreprocPath.Join(constants.FILE_CTAGS_TARGET_FOR_GCC_MINUS_E), ctx.IncludeFolders)
GCCPreprocRunner(ctx, sourceFile, ctx.PreprocPath.Join("ctags_target_for_gcc_minus_e.cpp"), ctx.IncludeFolders)
for _, command := range commands {
PrintRingNameIfDebug(ctx, command)
......@@ -69,15 +66,15 @@ type ArduinoPreprocessorRunner struct{}
func (s *ArduinoPreprocessorRunner) Run(ctx *types.Context) error {
buildProperties := ctx.BuildProperties
targetFilePath := ctx.PreprocPath.Join(constants.FILE_CTAGS_TARGET_FOR_GCC_MINUS_E)
targetFilePath := ctx.PreprocPath.Join("ctags_target_for_gcc_minus_e.cpp")
preprocProperties := buildProperties.Clone()
toolProps := buildProperties.SubTree("tools").SubTree("arduino-preprocessor")
preprocProperties.Merge(toolProps)
preprocProperties.SetPath(constants.BUILD_PROPERTIES_SOURCE_FILE, targetFilePath)
preprocProperties.SetPath("source_file", targetFilePath)
pattern := preprocProperties.Get(constants.BUILD_PROPERTIES_PATTERN)
if pattern == constants.EMPTY_STRING {
pattern := preprocProperties.Get("pattern")
if pattern == "" {
return errors.New(tr("arduino-preprocessor pattern is missing"))
}
......
......@@ -61,7 +61,7 @@ func TestBuilderEmptySketch(t *testing.T) {
exist, err := buildPath.Join(constants.FOLDER_CORE, "HardwareSerial.cpp.o").ExistCheck()
NoError(t, err)
require.True(t, exist)
exist, err = buildPath.Join(constants.FOLDER_PREPROC, constants.FILE_CTAGS_TARGET_FOR_GCC_MINUS_E).ExistCheck()
exist, err = buildPath.Join(constants.FOLDER_PREPROC, "ctags_target_for_gcc_minus_e.cpp").ExistCheck()
NoError(t, err)
require.True(t, exist)
exist, err = buildPath.Join(constants.FOLDER_SKETCH, "sketch1.ino.cpp.o").ExistCheck()
......@@ -91,7 +91,7 @@ func TestBuilderBridge(t *testing.T) {
exist, err := buildPath.Join(constants.FOLDER_CORE, "HardwareSerial.cpp.o").ExistCheck()
NoError(t, err)
require.True(t, exist)
exist, err = buildPath.Join(constants.FOLDER_PREPROC, constants.FILE_CTAGS_TARGET_FOR_GCC_MINUS_E).ExistCheck()
exist, err = buildPath.Join(constants.FOLDER_PREPROC, "ctags_target_for_gcc_minus_e.cpp").ExistCheck()
NoError(t, err)
require.True(t, exist)
exist, err = buildPath.Join(constants.FOLDER_SKETCH, "Bridge.ino.cpp.o").ExistCheck()
......@@ -124,7 +124,7 @@ func TestBuilderSketchWithConfig(t *testing.T) {
exist, err := buildPath.Join(constants.FOLDER_CORE, "HardwareSerial.cpp.o").ExistCheck()
NoError(t, err)
require.True(t, exist)
exist, err = buildPath.Join(constants.FOLDER_PREPROC, constants.FILE_CTAGS_TARGET_FOR_GCC_MINUS_E).ExistCheck()
exist, err = buildPath.Join(constants.FOLDER_PREPROC, "ctags_target_for_gcc_minus_e.cpp").ExistCheck()
NoError(t, err)
require.True(t, exist)
exist, err = buildPath.Join(constants.FOLDER_SKETCH, "sketch_with_config.ino.cpp.o").ExistCheck()
......@@ -162,7 +162,7 @@ func TestBuilderBridgeTwice(t *testing.T) {
exist, err := buildPath.Join(constants.FOLDER_CORE, "HardwareSerial.cpp.o").ExistCheck()
NoError(t, err)
require.True(t, exist)
exist, err = buildPath.Join(constants.FOLDER_PREPROC, constants.FILE_CTAGS_TARGET_FOR_GCC_MINUS_E).ExistCheck()
exist, err = buildPath.Join(constants.FOLDER_PREPROC, "ctags_target_for_gcc_minus_e.cpp").ExistCheck()
NoError(t, err)
require.True(t, exist)
exist, err = buildPath.Join(constants.FOLDER_SKETCH, "Bridge.ino.cpp.o").ExistCheck()
......@@ -202,7 +202,7 @@ func TestBuilderBridgeSAM(t *testing.T) {
exist, err = buildPath.Join(constants.FOLDER_CORE, "avr", "dtostrf.c.d").ExistCheck()
NoError(t, err)
require.True(t, exist)
exist, err = buildPath.Join(constants.FOLDER_PREPROC, constants.FILE_CTAGS_TARGET_FOR_GCC_MINUS_E).ExistCheck()
exist, err = buildPath.Join(constants.FOLDER_PREPROC, "ctags_target_for_gcc_minus_e.cpp").ExistCheck()
NoError(t, err)
require.True(t, exist)
exist, err = buildPath.Join(constants.FOLDER_SKETCH, "Bridge.ino.cpp.o").ExistCheck()
......@@ -242,7 +242,7 @@ func TestBuilderBridgeRedBearLab(t *testing.T) {
exist, err := buildPath.Join(constants.FOLDER_CORE, "HardwareSerial.cpp.o").ExistCheck()
NoError(t, err)
require.True(t, exist)
exist, err = buildPath.Join(constants.FOLDER_PREPROC, constants.FILE_CTAGS_TARGET_FOR_GCC_MINUS_E).ExistCheck()
exist, err = buildPath.Join(constants.FOLDER_PREPROC, "ctags_target_for_gcc_minus_e.cpp").ExistCheck()
NoError(t, err)
require.True(t, exist)
exist, err = buildPath.Join(constants.FOLDER_SKETCH, "Bridge.ino.cpp.o").ExistCheck()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment