Skip to content

Yet another small refactoring of legacy package #2127

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

Merged
merged 2 commits into from
Mar 28, 2023
Merged
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
6 changes: 2 additions & 4 deletions legacy/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
2 changes: 1 addition & 1 deletion legacy/builder/builder_utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 0 additions & 7 deletions legacy/builder/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 = " "
Expand Down
9 changes: 3 additions & 6 deletions legacy/builder/container_add_prototypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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{},
}
Expand Down
7 changes: 4 additions & 3 deletions legacy/builder/create_cmake_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
11 changes: 5 additions & 6 deletions legacy/builder/ctags_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down
13 changes: 6 additions & 7 deletions legacy/builder/gcc_preproc_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
Expand Down
15 changes: 6 additions & 9 deletions legacy/builder/preprocess_sketch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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{},
Expand All @@ -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)
Expand All @@ -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"))
}

Expand Down
12 changes: 6 additions & 6 deletions legacy/builder/test/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
Loading