Skip to content

Commit 8c2b8e7

Browse files
authored
[skip-changelog] Yet another small refactoring of legacy package (#2127)
* Removed some legacy constants * Converted PreprocessSketchArduino and ContainerAddPrototypes into functions
1 parent 6ee0c24 commit 8c2b8e7

10 files changed

+54
-70
lines changed

legacy/builder/builder.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,11 @@ func (s *Builder) Run(ctx *types.Context) error {
115115
type PreprocessSketch struct{}
116116

117117
func (s *PreprocessSketch) Run(ctx *types.Context) error {
118-
var commands []types.Command
119118
if ctx.UseArduinoPreprocessor {
120-
commands = append(commands, &PreprocessSketchArduino{})
119+
return PreprocessSketchWithArduinoPreprocessor(ctx)
121120
} else {
122-
commands = append(commands, &ContainerAddPrototypes{})
121+
return PreprocessSketchWithCtags(ctx)
123122
}
124-
return runCommands(ctx, commands)
125123
}
126124

127125
type Preprocess struct{}

legacy/builder/builder_utils/utils.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ func compileFileWithRecipe(ctx *types.Context, sourcePath *paths.Path, source *p
163163
properties := buildProperties.Clone()
164164
properties.Set(constants.BUILD_PROPERTIES_COMPILER_WARNING_FLAGS, properties.Get(constants.BUILD_PROPERTIES_COMPILER_WARNING_FLAGS+"."+ctx.WarningsLevel))
165165
properties.Set(constants.BUILD_PROPERTIES_INCLUDES, strings.Join(includes, constants.SPACE))
166-
properties.SetPath(constants.BUILD_PROPERTIES_SOURCE_FILE, source)
166+
properties.SetPath("source_file", source)
167167
relativeSource, err := sourcePath.RelTo(source)
168168
if err != nil {
169169
return nil, errors.WithStack(err)

legacy/builder/constants/constants.go

-7
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,8 @@ const BUILD_PROPERTIES_COMPILER_WARNING_FLAGS = "compiler.warning_flags"
3131
const BUILD_PROPERTIES_FQBN = "build.fqbn"
3232
const BUILD_PROPERTIES_INCLUDES = "includes"
3333
const BUILD_PROPERTIES_OBJECT_FILE = "object_file"
34-
const BUILD_PROPERTIES_PATTERN = "pattern"
35-
const BUILD_PROPERTIES_PREPROCESSED_FILE_PATH = "preprocessed_file_path"
3634
const BUILD_PROPERTIES_RUNTIME_PLATFORM_PATH = "runtime.platform.path"
37-
const BUILD_PROPERTIES_SOURCE_FILE = "source_file"
38-
const BUILD_PROPERTIES_TOOLS_KEY = "tools"
39-
const CTAGS = "ctags"
4035
const EMPTY_STRING = ""
41-
const FILE_CTAGS_TARGET_FOR_GCC_MINUS_E = "ctags_target_for_gcc_minus_e.cpp"
4236
const FILE_PLATFORM_KEYS_REWRITE_TXT = "platform.keys.rewrite.txt"
4337
const FOLDER_BOOTLOADERS = "bootloaders"
4438
const FOLDER_CORE = "core"
@@ -65,7 +59,6 @@ const PLATFORM_URL = "url"
6559
const PLATFORM_VERSION = "version"
6660
const RECIPE_AR_PATTERN = "recipe.ar.pattern"
6761
const RECIPE_C_COMBINE_PATTERN = "recipe.c.combine.pattern"
68-
const RECIPE_PREPROC_MACROS = "recipe.preproc.macros"
6962
const REWRITING_DISABLED = "disabled"
7063
const REWRITING = "rewriting"
7164
const SPACE = " "

legacy/builder/container_add_prototypes.go

+3-6
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,16 @@ import (
1919
"fmt"
2020

2121
bldr "github.com/arduino/arduino-cli/arduino/builder"
22-
"github.com/arduino/arduino-cli/legacy/builder/constants"
2322
"github.com/arduino/arduino-cli/legacy/builder/types"
2423
"github.com/pkg/errors"
2524
)
2625

27-
type ContainerAddPrototypes struct{}
28-
29-
func (s *ContainerAddPrototypes) Run(ctx *types.Context) error {
26+
func PreprocessSketchWithCtags(ctx *types.Context) error {
3027
// Generate the full pathname for the preproc output file
3128
if err := ctx.PreprocPath.MkdirAll(); err != nil {
3229
return errors.WithStack(err)
3330
}
34-
targetFilePath := ctx.PreprocPath.Join(constants.FILE_CTAGS_TARGET_FOR_GCC_MINUS_E)
31+
targetFilePath := ctx.PreprocPath.Join("ctags_target_for_gcc_minus_e.cpp")
3532

3633
// Run preprocessor
3734
sourceFile := ctx.SketchBuildPath.Join(ctx.Sketch.MainFile.Base() + ".cpp")
@@ -53,7 +50,7 @@ func (s *ContainerAddPrototypes) Run(ctx *types.Context) error {
5350
commands := []types.Command{
5451
&ReadFileAndStoreInContext{FileToRead: targetFilePath, Target: &ctx.SourceGccMinusE},
5552
&FilterSketchSource{Source: &ctx.SourceGccMinusE},
56-
&CTagsTargetFileSaver{Source: &ctx.SourceGccMinusE, TargetFileName: constants.FILE_CTAGS_TARGET_FOR_GCC_MINUS_E},
53+
&CTagsTargetFileSaver{Source: &ctx.SourceGccMinusE, TargetFileName: "ctags_target_for_gcc_minus_e.cpp"},
5754
&CTagsRunner{},
5855
&PrototypesAdder{},
5956
}

legacy/builder/create_cmake_rule.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,14 @@ func (s *ExportProjectCMake) Run(ctx *types.Context) error {
114114
fmt.Println(err)
115115
}
116116

117+
if err := PreprocessSketchWithCtags(ctx); err != nil {
118+
return err
119+
}
120+
117121
// Use old ctags method to generate export file
118122
commands := []types.Command{
119-
//&ContainerMergeCopySketchFiles{},
120-
&ContainerAddPrototypes{},
121123
&FilterSketchSource{Source: &ctx.Source, RemoveLineMarkers: true},
122124
}
123-
124125
for _, command := range commands {
125126
command.Run(ctx)
126127
}

legacy/builder/ctags_runner.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"os"
2020
"os/exec"
2121

22-
"github.com/arduino/arduino-cli/legacy/builder/constants"
2322
"github.com/arduino/arduino-cli/legacy/builder/ctags"
2423
"github.com/arduino/arduino-cli/legacy/builder/types"
2524
"github.com/arduino/arduino-cli/legacy/builder/utils"
@@ -34,12 +33,12 @@ func (s *CTagsRunner) Run(ctx *types.Context) error {
3433
ctagsTargetFilePath := ctx.CTagsTargetFile
3534

3635
ctagsProperties := buildProperties.Clone()
37-
ctagsProperties.Merge(buildProperties.SubTree(constants.BUILD_PROPERTIES_TOOLS_KEY).SubTree(constants.CTAGS))
38-
ctagsProperties.SetPath(constants.BUILD_PROPERTIES_SOURCE_FILE, ctagsTargetFilePath)
36+
ctagsProperties.Merge(buildProperties.SubTree("tools").SubTree("ctags"))
37+
ctagsProperties.SetPath("source_file", ctagsTargetFilePath)
3938

40-
pattern := ctagsProperties.Get(constants.BUILD_PROPERTIES_PATTERN)
41-
if pattern == constants.EMPTY_STRING {
42-
return errors.Errorf(tr("%s pattern is missing"), constants.CTAGS)
39+
pattern := ctagsProperties.Get("pattern")
40+
if pattern == "" {
41+
return errors.Errorf(tr("%s pattern is missing"), "ctags")
4342
}
4443

4544
commandLine := ctagsProperties.ExpandPropsInString(pattern)

legacy/builder/gcc_preproc_runner.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"strings"
2121

2222
"github.com/arduino/arduino-cli/legacy/builder/builder_utils"
23-
"github.com/arduino/arduino-cli/legacy/builder/constants"
2423
"github.com/arduino/arduino-cli/legacy/builder/types"
2524
"github.com/arduino/arduino-cli/legacy/builder/utils"
2625
"github.com/arduino/go-paths-helper"
@@ -58,18 +57,18 @@ func GCCPreprocRunnerForDiscoveringIncludes(ctx *types.Context, sourceFilePath *
5857
func prepareGCCPreprocRecipeProperties(ctx *types.Context, sourceFilePath *paths.Path, targetFilePath *paths.Path, includes paths.PathList) (*exec.Cmd, error) {
5958
properties := ctx.BuildProperties.Clone()
6059
properties.Set("build.library_discovery_phase", "1")
61-
properties.SetPath(constants.BUILD_PROPERTIES_SOURCE_FILE, sourceFilePath)
62-
properties.SetPath(constants.BUILD_PROPERTIES_PREPROCESSED_FILE_PATH, targetFilePath)
60+
properties.SetPath("source_file", sourceFilePath)
61+
properties.SetPath("preprocessed_file_path", targetFilePath)
6362

6463
includesStrings := utils.Map(includes.AsStrings(), utils.WrapWithHyphenI)
65-
properties.Set(constants.BUILD_PROPERTIES_INCLUDES, strings.Join(includesStrings, constants.SPACE))
64+
properties.Set("includes", strings.Join(includesStrings, " "))
6665

67-
if properties.Get(constants.RECIPE_PREPROC_MACROS) == "" {
66+
if properties.Get("recipe.preproc.macros") == "" {
6867
//generate PREPROC_MACROS from RECIPE_CPP_PATTERN
69-
properties.Set(constants.RECIPE_PREPROC_MACROS, GeneratePreprocPatternFromCompile(properties.Get("recipe.cpp.o.pattern")))
68+
properties.Set("recipe.preproc.macros", GeneratePreprocPatternFromCompile(properties.Get("recipe.cpp.o.pattern")))
7069
}
7170

72-
cmd, err := builder_utils.PrepareCommandForRecipe(properties, constants.RECIPE_PREPROC_MACROS, true, ctx.PackageManager.GetEnvVarsForSpawnedProcess())
71+
cmd, err := builder_utils.PrepareCommandForRecipe(properties, "recipe.preproc.macros", true, ctx.PackageManager.GetEnvVarsForSpawnedProcess())
7372
if err != nil {
7473
return nil, errors.WithStack(err)
7574
}

legacy/builder/preprocess_sketch.go

+6-9
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"runtime"
2424

2525
bldr "github.com/arduino/arduino-cli/arduino/builder"
26-
"github.com/arduino/arduino-cli/legacy/builder/constants"
2726
"github.com/arduino/arduino-cli/legacy/builder/types"
2827
"github.com/arduino/arduino-cli/legacy/builder/utils"
2928
properties "github.com/arduino/go-properties-orderedmap"
@@ -40,9 +39,7 @@ var ArduinoPreprocessorProperties = properties.NewFromHashmap(map[string]string{
4039
"preproc.macros.flags": "-w -x c++ -E -CC",
4140
})
4241

43-
type PreprocessSketchArduino struct{}
44-
45-
func (s *PreprocessSketchArduino) Run(ctx *types.Context) error {
42+
func PreprocessSketchWithArduinoPreprocessor(ctx *types.Context) error {
4643
sourceFile := ctx.SketchBuildPath.Join(ctx.Sketch.MainFile.Base() + ".cpp")
4744
commands := []types.Command{
4845
&ArduinoPreprocessorRunner{},
@@ -52,7 +49,7 @@ func (s *PreprocessSketchArduino) Run(ctx *types.Context) error {
5249
return errors.WithStack(err)
5350
}
5451

55-
GCCPreprocRunner(ctx, sourceFile, ctx.PreprocPath.Join(constants.FILE_CTAGS_TARGET_FOR_GCC_MINUS_E), ctx.IncludeFolders)
52+
GCCPreprocRunner(ctx, sourceFile, ctx.PreprocPath.Join("ctags_target_for_gcc_minus_e.cpp"), ctx.IncludeFolders)
5653

5754
for _, command := range commands {
5855
PrintRingNameIfDebug(ctx, command)
@@ -69,15 +66,15 @@ type ArduinoPreprocessorRunner struct{}
6966

7067
func (s *ArduinoPreprocessorRunner) Run(ctx *types.Context) error {
7168
buildProperties := ctx.BuildProperties
72-
targetFilePath := ctx.PreprocPath.Join(constants.FILE_CTAGS_TARGET_FOR_GCC_MINUS_E)
69+
targetFilePath := ctx.PreprocPath.Join("ctags_target_for_gcc_minus_e.cpp")
7370

7471
preprocProperties := buildProperties.Clone()
7572
toolProps := buildProperties.SubTree("tools").SubTree("arduino-preprocessor")
7673
preprocProperties.Merge(toolProps)
77-
preprocProperties.SetPath(constants.BUILD_PROPERTIES_SOURCE_FILE, targetFilePath)
74+
preprocProperties.SetPath("source_file", targetFilePath)
7875

79-
pattern := preprocProperties.Get(constants.BUILD_PROPERTIES_PATTERN)
80-
if pattern == constants.EMPTY_STRING {
76+
pattern := preprocProperties.Get("pattern")
77+
if pattern == "" {
8178
return errors.New(tr("arduino-preprocessor pattern is missing"))
8279
}
8380

legacy/builder/test/builder_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func TestBuilderEmptySketch(t *testing.T) {
6161
exist, err := buildPath.Join(constants.FOLDER_CORE, "HardwareSerial.cpp.o").ExistCheck()
6262
NoError(t, err)
6363
require.True(t, exist)
64-
exist, err = buildPath.Join(constants.FOLDER_PREPROC, constants.FILE_CTAGS_TARGET_FOR_GCC_MINUS_E).ExistCheck()
64+
exist, err = buildPath.Join(constants.FOLDER_PREPROC, "ctags_target_for_gcc_minus_e.cpp").ExistCheck()
6565
NoError(t, err)
6666
require.True(t, exist)
6767
exist, err = buildPath.Join(constants.FOLDER_SKETCH, "sketch1.ino.cpp.o").ExistCheck()
@@ -91,7 +91,7 @@ func TestBuilderBridge(t *testing.T) {
9191
exist, err := buildPath.Join(constants.FOLDER_CORE, "HardwareSerial.cpp.o").ExistCheck()
9292
NoError(t, err)
9393
require.True(t, exist)
94-
exist, err = buildPath.Join(constants.FOLDER_PREPROC, constants.FILE_CTAGS_TARGET_FOR_GCC_MINUS_E).ExistCheck()
94+
exist, err = buildPath.Join(constants.FOLDER_PREPROC, "ctags_target_for_gcc_minus_e.cpp").ExistCheck()
9595
NoError(t, err)
9696
require.True(t, exist)
9797
exist, err = buildPath.Join(constants.FOLDER_SKETCH, "Bridge.ino.cpp.o").ExistCheck()
@@ -124,7 +124,7 @@ func TestBuilderSketchWithConfig(t *testing.T) {
124124
exist, err := buildPath.Join(constants.FOLDER_CORE, "HardwareSerial.cpp.o").ExistCheck()
125125
NoError(t, err)
126126
require.True(t, exist)
127-
exist, err = buildPath.Join(constants.FOLDER_PREPROC, constants.FILE_CTAGS_TARGET_FOR_GCC_MINUS_E).ExistCheck()
127+
exist, err = buildPath.Join(constants.FOLDER_PREPROC, "ctags_target_for_gcc_minus_e.cpp").ExistCheck()
128128
NoError(t, err)
129129
require.True(t, exist)
130130
exist, err = buildPath.Join(constants.FOLDER_SKETCH, "sketch_with_config.ino.cpp.o").ExistCheck()
@@ -162,7 +162,7 @@ func TestBuilderBridgeTwice(t *testing.T) {
162162
exist, err := buildPath.Join(constants.FOLDER_CORE, "HardwareSerial.cpp.o").ExistCheck()
163163
NoError(t, err)
164164
require.True(t, exist)
165-
exist, err = buildPath.Join(constants.FOLDER_PREPROC, constants.FILE_CTAGS_TARGET_FOR_GCC_MINUS_E).ExistCheck()
165+
exist, err = buildPath.Join(constants.FOLDER_PREPROC, "ctags_target_for_gcc_minus_e.cpp").ExistCheck()
166166
NoError(t, err)
167167
require.True(t, exist)
168168
exist, err = buildPath.Join(constants.FOLDER_SKETCH, "Bridge.ino.cpp.o").ExistCheck()
@@ -202,7 +202,7 @@ func TestBuilderBridgeSAM(t *testing.T) {
202202
exist, err = buildPath.Join(constants.FOLDER_CORE, "avr", "dtostrf.c.d").ExistCheck()
203203
NoError(t, err)
204204
require.True(t, exist)
205-
exist, err = buildPath.Join(constants.FOLDER_PREPROC, constants.FILE_CTAGS_TARGET_FOR_GCC_MINUS_E).ExistCheck()
205+
exist, err = buildPath.Join(constants.FOLDER_PREPROC, "ctags_target_for_gcc_minus_e.cpp").ExistCheck()
206206
NoError(t, err)
207207
require.True(t, exist)
208208
exist, err = buildPath.Join(constants.FOLDER_SKETCH, "Bridge.ino.cpp.o").ExistCheck()
@@ -242,7 +242,7 @@ func TestBuilderBridgeRedBearLab(t *testing.T) {
242242
exist, err := buildPath.Join(constants.FOLDER_CORE, "HardwareSerial.cpp.o").ExistCheck()
243243
NoError(t, err)
244244
require.True(t, exist)
245-
exist, err = buildPath.Join(constants.FOLDER_PREPROC, constants.FILE_CTAGS_TARGET_FOR_GCC_MINUS_E).ExistCheck()
245+
exist, err = buildPath.Join(constants.FOLDER_PREPROC, "ctags_target_for_gcc_minus_e.cpp").ExistCheck()
246246
NoError(t, err)
247247
require.True(t, exist)
248248
exist, err = buildPath.Join(constants.FOLDER_SKETCH, "Bridge.ino.cpp.o").ExistCheck()

0 commit comments

Comments
 (0)