Skip to content

Commit 65915d8

Browse files
authored
[skip-changelog] legacy: refacored makeSourceFile / other minor code moved (#2303)
* Slightly de-entangled makeSourceFile * Moved some initializations directly into NewBuilder * Pruned constants * Pruned more constants * Fixed lint warnings
1 parent b8024c3 commit 65915d8

15 files changed

+113
-112
lines changed

Diff for: arduino/builder/builder.go

+38-13
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,56 @@ package builder
1818
import (
1919
"github.com/arduino/arduino-cli/arduino/sketch"
2020
"github.com/arduino/go-paths-helper"
21-
)
22-
23-
// nolint
24-
const (
25-
BuildPropertiesArchiveFile = "archive_file"
26-
BuildPropertiesArchiveFilePath = "archive_file_path"
27-
BuildPropertiesObjectFile = "object_file"
28-
RecipeARPattern = "recipe.ar.pattern"
29-
BuildPropertiesIncludes = "includes"
30-
BuildPropertiesCompilerWarningFlags = "compiler.warning_flags"
31-
Space = " "
21+
"github.com/arduino/go-properties-orderedmap"
3222
)
3323

3424
// Builder is a Sketch builder.
3525
type Builder struct {
36-
sketch *sketch.Sketch
26+
sketch *sketch.Sketch
27+
buildProperties *properties.Map
3728

3829
// core related
3930
coreBuildCachePath *paths.Path
4031
}
4132

4233
// NewBuilder creates a sketch Builder.
43-
func NewBuilder(sk *sketch.Sketch, coreBuildCachePath *paths.Path) *Builder {
34+
func NewBuilder(
35+
sk *sketch.Sketch,
36+
boardBuildProperties *properties.Map,
37+
buildPath *paths.Path,
38+
optimizeForDebug bool,
39+
coreBuildCachePath *paths.Path,
40+
) *Builder {
41+
buildProperties := properties.NewMap()
42+
if boardBuildProperties != nil {
43+
buildProperties.Merge(boardBuildProperties)
44+
}
45+
46+
if buildPath != nil {
47+
buildProperties.SetPath("build.path", buildPath)
48+
}
49+
if sk != nil {
50+
buildProperties.Set("build.project_name", sk.MainFile.Base())
51+
buildProperties.SetPath("build.source.path", sk.FullPath)
52+
}
53+
if optimizeForDebug {
54+
if debugFlags, ok := buildProperties.GetOk("compiler.optimization_flags.debug"); ok {
55+
buildProperties.Set("compiler.optimization_flags", debugFlags)
56+
}
57+
} else {
58+
if releaseFlags, ok := buildProperties.GetOk("compiler.optimization_flags.release"); ok {
59+
buildProperties.Set("compiler.optimization_flags", releaseFlags)
60+
}
61+
}
62+
4463
return &Builder{
4564
sketch: sk,
65+
buildProperties: buildProperties,
4666
coreBuildCachePath: coreBuildCachePath,
4767
}
4868
}
69+
70+
// GetBuildProperties returns the build properties for running this build
71+
func (b *Builder) GetBuildProperties() *properties.Map {
72+
return b.buildProperties
73+
}

Diff for: arduino/builder/detector/detector.go

+39-29
Original file line numberDiff line numberDiff line change
@@ -264,16 +264,16 @@ func (l *SketchLibrariesDetector) findIncludes(
264264

265265
if !l.useCachedLibrariesResolution {
266266
sketch := sketch
267-
mergedfile, err := makeSourceFile(sketchBuildPath, librariesBuildPath, sketch, paths.New(sketch.MainFile.Base()+".cpp"))
267+
mergedfile, err := makeSourceFile(sketchBuildPath, sketchBuildPath, paths.New(sketch.MainFile.Base()+".cpp"))
268268
if err != nil {
269269
return errors.WithStack(err)
270270
}
271271
sourceFileQueue.push(mergedfile)
272272

273-
l.queueSourceFilesFromFolder(sketchBuildPath, librariesBuildPath, sourceFileQueue, sketch, sketchBuildPath, false /* recurse */)
273+
l.queueSourceFilesFromFolder(sourceFileQueue, sketchBuildPath, false /* recurse */, sketchBuildPath, sketchBuildPath)
274274
srcSubfolderPath := sketchBuildPath.Join("src")
275275
if srcSubfolderPath.IsDir() {
276-
l.queueSourceFilesFromFolder(sketchBuildPath, librariesBuildPath, sourceFileQueue, sketch, srcSubfolderPath, true /* recurse */)
276+
l.queueSourceFilesFromFolder(sourceFileQueue, srcSubfolderPath, true /* recurse */, sketchBuildPath, sketchBuildPath)
277277
}
278278

279279
for !sourceFileQueue.empty() {
@@ -419,20 +419,21 @@ func (l *SketchLibrariesDetector) findIncludesUntilDone(
419419
}
420420
} else {
421421
for _, sourceDir := range library.SourceDirs() {
422-
l.queueSourceFilesFromFolder(sketchBuildPath, librariesBuildPath, sourceFileQueue, library, sourceDir.Dir, sourceDir.Recurse)
422+
l.queueSourceFilesFromFolder(sourceFileQueue, sourceDir.Dir, sourceDir.Recurse,
423+
library.SourceDir, librariesBuildPath.Join(library.DirName), library.UtilityDir)
423424
}
424425
}
425426
first = false
426427
}
427428
}
428429

429430
func (l *SketchLibrariesDetector) queueSourceFilesFromFolder(
430-
sketchBuildPath *paths.Path,
431-
librariesBuildPath *paths.Path,
432431
sourceFileQueue *uniqueSourceFileQueue,
433-
origin interface{},
434432
folder *paths.Path,
435433
recurse bool,
434+
sourceDir *paths.Path,
435+
buildDir *paths.Path,
436+
extraIncludePath ...*paths.Path,
436437
) error {
437438
sourceFileExtensions := []string{}
438439
for k := range globals.SourceFilesValidExtensions {
@@ -444,7 +445,7 @@ func (l *SketchLibrariesDetector) queueSourceFilesFromFolder(
444445
}
445446

446447
for _, filePath := range filePaths {
447-
sourceFile, err := makeSourceFile(sketchBuildPath, librariesBuildPath, origin, filePath)
448+
sourceFile, err := makeSourceFile(sourceDir, buildDir, filePath, extraIncludePath...)
448449
if err != nil {
449450
return errors.WithStack(err)
450451
}
@@ -537,33 +538,42 @@ func (f *sourceFile) Equals(g *sourceFile) bool {
537538
// given origin. The given path can be absolute, or relative within the
538539
// origin's root source folder
539540
func makeSourceFile(
540-
sketchBuildPath *paths.Path,
541-
librariesBuildPath *paths.Path,
542-
origin interface{},
543-
path *paths.Path,
541+
sourceDir *paths.Path,
542+
buildDir *paths.Path,
543+
sourceFilePath *paths.Path,
544+
extraIncludePath ...*paths.Path,
544545
) (*sourceFile, error) {
545-
res := &sourceFile{}
546-
547-
switch o := origin.(type) {
548-
case *sketch.Sketch:
549-
res.buildRoot = sketchBuildPath
550-
res.sourceRoot = sketchBuildPath
551-
case *libraries.Library:
552-
res.buildRoot = librariesBuildPath.Join(o.DirName)
553-
res.sourceRoot = o.SourceDir
554-
res.extraIncludePath = o.UtilityDir
555-
default:
556-
panic("Unexpected origin for SourceFile: " + fmt.Sprint(origin))
557-
}
558-
559-
if path.IsAbs() {
546+
res := &sourceFile{
547+
buildRoot: buildDir,
548+
sourceRoot: sourceDir,
549+
}
550+
551+
if len(extraIncludePath) > 1 {
552+
panic("only one extra include path allowed")
553+
}
554+
if len(extraIncludePath) > 0 {
555+
res.extraIncludePath = extraIncludePath[0]
556+
}
557+
// switch o := origin.(type) {
558+
// case *sketch.Sketch:
559+
// res.buildRoot = sketchBuildPath
560+
// res.sourceRoot = sketchBuildPath
561+
// case *libraries.Library:
562+
// res.buildRoot = librariesBuildPath.Join(o.DirName)
563+
// res.sourceRoot = o.SourceDir
564+
// res.extraIncludePath = o.UtilityDir
565+
// default:
566+
// panic("Unexpected origin for SourceFile: " + fmt.Sprint(origin))
567+
// }
568+
569+
if sourceFilePath.IsAbs() {
560570
var err error
561-
path, err = res.sourceRoot.RelTo(path)
571+
sourceFilePath, err = res.sourceRoot.RelTo(sourceFilePath)
562572
if err != nil {
563573
return nil, err
564574
}
565575
}
566-
res.relativePath = path
576+
res.relativePath = sourceFilePath
567577
return res, nil
568578
}
569579

Diff for: arduino/builder/sketch.go

-27
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"github.com/arduino/arduino-cli/arduino/builder/cpp"
2424
"github.com/arduino/arduino-cli/i18n"
2525
"github.com/arduino/go-paths-helper"
26-
"github.com/arduino/go-properties-orderedmap"
2726

2827
"github.com/pkg/errors"
2928
)
@@ -166,29 +165,3 @@ func writeIfDifferent(source []byte, destPath *paths.Path) error {
166165
// Source and destination are the same, don't write anything
167166
return nil
168167
}
169-
170-
// SetupBuildProperties adds the build properties related to the sketch to the
171-
// default board build properties map.
172-
func (b *Builder) SetupBuildProperties(boardBuildProperties *properties.Map, buildPath *paths.Path, optimizeForDebug bool) *properties.Map {
173-
buildProperties := properties.NewMap()
174-
buildProperties.Merge(boardBuildProperties)
175-
176-
if buildPath != nil {
177-
buildProperties.SetPath("build.path", buildPath)
178-
}
179-
if b.sketch != nil {
180-
buildProperties.Set("build.project_name", b.sketch.MainFile.Base())
181-
buildProperties.SetPath("build.source.path", b.sketch.FullPath)
182-
}
183-
if optimizeForDebug {
184-
if debugFlags, ok := buildProperties.GetOk("compiler.optimization_flags.debug"); ok {
185-
buildProperties.Set("compiler.optimization_flags", debugFlags)
186-
}
187-
} else {
188-
if releaseFlags, ok := buildProperties.GetOk("compiler.optimization_flags.release"); ok {
189-
buildProperties.Set("compiler.optimization_flags", releaseFlags)
190-
}
191-
}
192-
193-
return buildProperties
194-
}

Diff for: arduino/builder/sketch_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func TestMergeSketchSources(t *testing.T) {
4848
}
4949
mergedSources := strings.ReplaceAll(string(mergedBytes), "%s", pathToGoldenSource)
5050

51-
b := NewBuilder(sk, nil)
51+
b := NewBuilder(sk, nil, nil, false, nil)
5252
offset, source, err := b.sketchMergeSources(nil)
5353
require.Nil(t, err)
5454
require.Equal(t, 2, offset)
@@ -61,7 +61,7 @@ func TestMergeSketchSourcesArduinoIncluded(t *testing.T) {
6161
require.NotNil(t, sk)
6262

6363
// ensure not to include Arduino.h when it's already there
64-
b := NewBuilder(sk, nil)
64+
b := NewBuilder(sk, nil, nil, false, nil)
6565
_, source, err := b.sketchMergeSources(nil)
6666
require.Nil(t, err)
6767
require.Equal(t, 1, strings.Count(source, "<Arduino.h>"))
@@ -76,7 +76,7 @@ func TestCopyAdditionalFiles(t *testing.T) {
7676
sk1, err := sketch.New(paths.New("testdata", t.Name()))
7777
require.Nil(t, err)
7878
require.Equal(t, sk1.AdditionalFiles.Len(), 1)
79-
b1 := NewBuilder(sk1, nil)
79+
b1 := NewBuilder(sk1, nil, nil, false, nil)
8080

8181
// copy the sketch over, create a fake main file we don't care about it
8282
// but we need it for `SketchLoad` to succeed later

Diff for: arduino/builder/utils/utils.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -520,8 +520,8 @@ func compileFileWithRecipe(
520520
verboseStdout, verboseInfo, errOut := &bytes.Buffer{}, &bytes.Buffer{}, &bytes.Buffer{}
521521

522522
properties := buildProperties.Clone()
523-
properties.Set(builder.BuildPropertiesCompilerWarningFlags, properties.Get(builder.BuildPropertiesCompilerWarningFlags+"."+warningsLevel))
524-
properties.Set(builder.BuildPropertiesIncludes, strings.Join(includes, builder.Space))
523+
properties.Set("compiler.warning_flags", properties.Get("compiler.warning_flags."+warningsLevel))
524+
properties.Set("includes", strings.Join(includes, " "))
525525
properties.SetPath("source_file", source)
526526
relativeSource, err := sourcePath.RelTo(source)
527527
if err != nil {
@@ -530,7 +530,7 @@ func compileFileWithRecipe(
530530
depsFile := buildPath.Join(relativeSource.String() + ".d")
531531
objectFile := buildPath.Join(relativeSource.String() + ".o")
532532

533-
properties.SetPath(builder.BuildPropertiesObjectFile, objectFile)
533+
properties.SetPath("object_file", objectFile)
534534
err = objectFile.Parent().MkdirAll()
535535
if err != nil {
536536
return nil, nil, nil, nil, errors.WithStack(err)
@@ -615,11 +615,11 @@ func ArchiveCompiledFiles(
615615

616616
for _, objectFile := range objectFilesToArchive {
617617
properties := buildProperties.Clone()
618-
properties.Set(builder.BuildPropertiesArchiveFile, archiveFilePath.Base())
619-
properties.SetPath(builder.BuildPropertiesArchiveFilePath, archiveFilePath)
620-
properties.SetPath(builder.BuildPropertiesObjectFile, objectFile)
618+
properties.Set("archive_file", archiveFilePath.Base())
619+
properties.SetPath("archive_file_path", archiveFilePath)
620+
properties.SetPath("object_file", objectFile)
621621

622-
command, err := PrepareCommandForRecipe(properties, builder.RecipeARPattern, false)
622+
command, err := PrepareCommandForRecipe(properties, "recipe.ar.pattern", false)
623623
if err != nil {
624624
return nil, verboseInfobuf.Bytes(), errors.WithStack(err)
625625
}

Diff for: commands/compile/compile.go

+9-10
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
9898
if err != nil {
9999
return nil, &arduino.InvalidFQBNError{Cause: err}
100100
}
101-
targetPackage, targetPlatform, targetBoard, buildProperties, buildPlatform, err := pme.ResolveFQBN(fqbn)
101+
targetPackage, targetPlatform, targetBoard, boardBuildProperties, buildPlatform, err := pme.ResolveFQBN(fqbn)
102102
if err != nil {
103103
if targetPlatform == nil {
104104
return nil, &arduino.PlatformNotFoundError{
@@ -115,21 +115,21 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
115115

116116
// Setup sign keys if requested
117117
if req.KeysKeychain != "" {
118-
buildProperties.Set("build.keys.keychain", req.GetKeysKeychain())
118+
boardBuildProperties.Set("build.keys.keychain", req.GetKeysKeychain())
119119
}
120120
if req.SignKey != "" {
121-
buildProperties.Set("build.keys.sign_key", req.GetSignKey())
121+
boardBuildProperties.Set("build.keys.sign_key", req.GetSignKey())
122122
}
123123
if req.EncryptKey != "" {
124-
buildProperties.Set("build.keys.encrypt_key", req.GetEncryptKey())
124+
boardBuildProperties.Set("build.keys.encrypt_key", req.GetEncryptKey())
125125
}
126126
// At the current time we do not have a way of knowing if a board supports the secure boot or not,
127127
// so, if the flags to override the default keys are used, we try override the corresponding platform property nonetheless.
128128
// It's not possible to use the default name for the keys since there could be more tools to sign and encrypt.
129129
// So it's mandatory to use all three flags to sign and encrypt the binary
130-
keychainProp := buildProperties.ContainsKey("build.keys.keychain")
131-
signProp := buildProperties.ContainsKey("build.keys.sign_key")
132-
encryptProp := buildProperties.ContainsKey("build.keys.encrypt_key")
130+
keychainProp := boardBuildProperties.ContainsKey("build.keys.keychain")
131+
signProp := boardBuildProperties.ContainsKey("build.keys.sign_key")
132+
encryptProp := boardBuildProperties.ContainsKey("build.keys.encrypt_key")
133133
// we verify that all the properties for the secure boot keys are defined or none of them is defined.
134134
if !(keychainProp == signProp && signProp == encryptProp) {
135135
return nil, fmt.Errorf(tr("Firmware encryption/signing requires all the following properties to be defined: %s", "build.keys.keychain, build.keys.sign_key, build.keys.encrypt_key"))
@@ -169,10 +169,9 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
169169
coreBuildCachePath = buildCachePath.Join("core")
170170
}
171171

172-
sketchBuilder := bldr.NewBuilder(sk, coreBuildCachePath)
172+
sketchBuilder := bldr.NewBuilder(sk, boardBuildProperties, buildPath, req.GetOptimizeForDebug(), coreBuildCachePath)
173173

174-
// Add build properites related to sketch data
175-
buildProperties = sketchBuilder.SetupBuildProperties(buildProperties, buildPath, req.GetOptimizeForDebug())
174+
buildProperties := sketchBuilder.GetBuildProperties()
176175

177176
// Add user provided custom build properties
178177
customBuildPropertiesArgs := append(req.GetBuildProperties(), "build.warn_data_percentage=75")

Diff for: legacy/builder/constants/constants.go

-3
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,8 @@ const BUILD_PROPERTIES_ARCH_OVERRIDE_CHECK = "architecture.override_check"
2121
const BUILD_PROPERTIES_BOOTLOADER_FILE = "bootloader.file"
2222
const BUILD_PROPERTIES_BOOTLOADER_NOBLINK = "bootloader.noblink"
2323
const BUILD_PROPERTIES_BUILD_BOARD = "build.board"
24-
const BUILD_PROPERTIES_BUILD_MCU = "build.mcu"
2524
const BUILD_PROPERTIES_COMPILER_LDFLAGS = "compiler.ldflags"
2625
const BUILD_PROPERTIES_COMPILER_CPP_FLAGS = "compiler.cpp.flags"
27-
const BUILD_PROPERTIES_RUNTIME_PLATFORM_PATH = "runtime.platform.path"
28-
const EMPTY_STRING = ""
2926
const FOLDER_BOOTLOADERS = "bootloaders"
3027
const FOLDER_CORE = "core"
3128
const FOLDER_SKETCH = "sketch"

Diff for: legacy/builder/create_cmake_rule.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ func (s *ExportProjectCMake) Run(ctx *types.Context) error {
200200
for _, library := range ctx.SketchLibrariesDetector.ImportedLibraries() {
201201
// Copy used libraries in the correct folder
202202
libDir := libBaseFolder.Join(library.DirName)
203-
mcu := ctx.BuildProperties.Get(constants.BUILD_PROPERTIES_BUILD_MCU)
203+
mcu := ctx.BuildProperties.Get("build.mcu")
204204
copyDir(library.InstallDir.String(), libDir.String(), validExportExtensions)
205205

206206
// Read cmake options if available

Diff for: legacy/builder/merge_sketch_with_bootloader.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,15 @@ func (s *MergeSketchWithBootloader) Run(ctx *types.Context) error {
5555
return nil
5656
}
5757

58-
bootloader := constants.EMPTY_STRING
58+
bootloader := ""
5959
if bootloaderNoBlink, ok := buildProperties.GetOk(constants.BUILD_PROPERTIES_BOOTLOADER_NOBLINK); ok {
6060
bootloader = bootloaderNoBlink
6161
} else {
6262
bootloader = buildProperties.Get(constants.BUILD_PROPERTIES_BOOTLOADER_FILE)
6363
}
6464
bootloader = buildProperties.ExpandPropsInString(bootloader)
6565

66-
bootloaderPath := buildProperties.GetPath(constants.BUILD_PROPERTIES_RUNTIME_PLATFORM_PATH).Join(constants.FOLDER_BOOTLOADERS, bootloader)
66+
bootloaderPath := buildProperties.GetPath("runtime.platform.path").Join(constants.FOLDER_BOOTLOADERS, bootloader)
6767
if bootloaderPath.NotExist() {
6868
if ctx.Verbose {
6969
ctx.Warn(tr("Bootloader file specified but missing: %[1]s", bootloaderPath))

Diff for: legacy/builder/phases/core_builder.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import (
3131
"github.com/arduino/arduino-cli/buildcache"
3232
"github.com/arduino/arduino-cli/i18n"
3333
f "github.com/arduino/arduino-cli/internal/algorithms"
34-
"github.com/arduino/arduino-cli/legacy/builder/constants"
3534
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
3635
"github.com/arduino/go-paths-helper"
3736
"github.com/arduino/go-properties-orderedmap"
@@ -102,7 +101,7 @@ func compileCore(
102101
) (*paths.Path, paths.PathList, error) {
103102
coreFolder := buildProperties.GetPath("build.core.path")
104103
variantFolder := buildProperties.GetPath("build.variant.path")
105-
targetCoreFolder := buildProperties.GetPath(constants.BUILD_PROPERTIES_RUNTIME_PLATFORM_PATH)
104+
targetCoreFolder := buildProperties.GetPath("runtime.platform.path")
106105

107106
includes := []string{coreFolder.String()}
108107
if variantFolder != nil && variantFolder.IsDir() {

0 commit comments

Comments
 (0)