Skip to content

Commit ae555b4

Browse files
make ExportCmake and PreprocessorSketch a method recevier of arduino/builder
1 parent 8da2287 commit ae555b4

File tree

3 files changed

+54
-60
lines changed

3 files changed

+54
-60
lines changed

Diff for: legacy/builder/create_cmake_rule.go renamed to arduino/builder/export_cmake.go

+16-28
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,18 @@ import (
3030
"github.com/arduino/arduino-cli/arduino/builder/utils"
3131
"github.com/arduino/arduino-cli/arduino/globals"
3232
"github.com/arduino/arduino-cli/arduino/libraries"
33-
"github.com/arduino/arduino-cli/arduino/sketch"
3433
"github.com/arduino/arduino-cli/legacy/builder/constants"
3534
)
3635

3736
var lineMatcher = regexp.MustCompile(`^#line\s\d+\s"`)
3837

39-
func ExportProjectCMake(
38+
func (b *Builder) ExportProjectCMake(
4039
sketchError bool, // Was there an error while compiling the sketch?
41-
buildPath, sketchBuildPath *paths.Path,
4240
importedLibraries libraries.List,
43-
buildProperties *properties.Map,
44-
sketch *sketch.Sketch,
4541
includeFolders paths.PathList,
4642
lineOffset int,
4743
onlyUpdateCompilationDatabase bool,
48-
) ([]byte, []byte, error) {
44+
) error {
4945
// copies the contents of the file named src to the file named
5046
// by dst. The file will be created if it does not already exist. If the
5147
// destination file exists, all it's contents will be replaced by the contents
@@ -182,12 +178,12 @@ func ExportProjectCMake(
182178
var validStaticLibExtensions = []string{".a"}
183179

184180
// If sketch error or cannot export Cmake project
185-
if sketchError || buildProperties.Get("compiler.export_cmake") == "" {
186-
return nil, nil, nil
181+
if sketchError || b.buildProperties.Get("compiler.export_cmake") == "" {
182+
return nil
187183
}
188184

189185
// Create new cmake subFolder - clean if the folder is already there
190-
cmakeFolder := buildPath.Join("_cmake")
186+
cmakeFolder := b.buildPath.Join("_cmake")
191187
if _, err := cmakeFolder.Stat(); err == nil {
192188
cmakeFolder.RemoveAll()
193189
}
@@ -207,7 +203,7 @@ func ExportProjectCMake(
207203
for _, library := range importedLibraries {
208204
// Copy used libraries in the correct folder
209205
libDir := libBaseFolder.Join(library.DirName)
210-
mcu := buildProperties.Get("build.mcu")
206+
mcu := b.buildProperties.Get("build.mcu")
211207
copyDir(library.InstallDir.String(), libDir.String(), validExportExtensions)
212208

213209
// Read cmake options if available
@@ -238,28 +234,20 @@ func ExportProjectCMake(
238234
}
239235

240236
// Copy core + variant in use + preprocessed sketch in the correct folders
241-
err := copyDir(buildProperties.Get("build.core.path"), coreFolder.String(), validExportExtensions)
237+
err := copyDir(b.buildProperties.Get("build.core.path"), coreFolder.String(), validExportExtensions)
242238
if err != nil {
243239
fmt.Println(err)
244240
}
245-
err = copyDir(buildProperties.Get("build.variant.path"), coreFolder.Join("variant").String(), validExportExtensions)
241+
err = copyDir(b.buildProperties.Get("build.variant.path"), coreFolder.Join("variant").String(), validExportExtensions)
246242
if err != nil {
247243
fmt.Println(err)
248244
}
249245

250-
normalOutput, verboseOutput, err := PreprocessSketch(
251-
sketch,
252-
buildPath,
253-
includeFolders,
254-
lineOffset,
255-
buildProperties,
256-
onlyUpdateCompilationDatabase,
257-
)
258-
if err != nil {
259-
return normalOutput, verboseOutput, err
246+
if err := b.PreprocessSketch(includeFolders, lineOffset, onlyUpdateCompilationDatabase); err != nil {
247+
return err
260248
}
261249

262-
err = copyDir(sketchBuildPath.String(), cmakeFolder.Join("sketch").String(), validExportExtensions)
250+
err = copyDir(b.sketchBuildPath.String(), cmakeFolder.Join("sketch").String(), validExportExtensions)
263251
if err != nil {
264252
fmt.Println(err)
265253
}
@@ -294,9 +282,9 @@ func ExportProjectCMake(
294282
var dynamicLibsFromGccMinusL []string
295283
var linkDirectories []string
296284

297-
extractCompileFlags(buildProperties, constants.RECIPE_C_COMBINE_PATTERN, &defines, &dynamicLibsFromGccMinusL, &linkerflags, &linkDirectories)
298-
extractCompileFlags(buildProperties, "recipe.c.o.pattern", &defines, &dynamicLibsFromGccMinusL, &linkerflags, &linkDirectories)
299-
extractCompileFlags(buildProperties, "recipe.cpp.o.pattern", &defines, &dynamicLibsFromGccMinusL, &linkerflags, &linkDirectories)
285+
extractCompileFlags(b.buildProperties, constants.RECIPE_C_COMBINE_PATTERN, &defines, &dynamicLibsFromGccMinusL, &linkerflags, &linkDirectories)
286+
extractCompileFlags(b.buildProperties, "recipe.c.o.pattern", &defines, &dynamicLibsFromGccMinusL, &linkerflags, &linkDirectories)
287+
extractCompileFlags(b.buildProperties, "recipe.cpp.o.pattern", &defines, &dynamicLibsFromGccMinusL, &linkerflags, &linkDirectories)
300288

301289
// Extract folders with .h in them for adding in include list
302290
headerFiles, _ := utils.FindFilesInFolder(cmakeFolder, true, validHeaderExtensions...)
@@ -307,7 +295,7 @@ func ExportProjectCMake(
307295

308296
// Generate the CMakeLists global file
309297

310-
projectName := sketch.Name
298+
projectName := b.sketch.Name
311299

312300
cmakelist := "cmake_minimum_required(VERSION 3.5.0)\n"
313301
cmakelist += "INCLUDE(FindPkgConfig)\n"
@@ -364,7 +352,7 @@ func ExportProjectCMake(
364352

365353
cmakeFile.WriteFile([]byte(cmakelist))
366354

367-
return normalOutput, verboseOutput, nil
355+
return nil
368356
}
369357

370358
func extractCompileFlags(buildProperties *properties.Map, recipe string, defines, dynamicLibs, linkerflags, linkDirectories *[]string) {

Diff for: arduino/builder/preprocess_sketch.go

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2023 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-cli.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to [email protected].
15+
16+
package builder
17+
18+
import (
19+
"github.com/arduino/arduino-cli/arduino/builder/preprocessor"
20+
"github.com/arduino/go-paths-helper"
21+
)
22+
23+
// PreprocessSketch fixdoc
24+
func (b *Builder) PreprocessSketch(includes paths.PathList, lineOffset int, onlyUpdateCompilationDatabase bool) error {
25+
// In the future we might change the preprocessor
26+
normalOutput, verboseOutput, err := preprocessor.PreprocessSketchWithCtags(
27+
b.sketch, b.buildPath, includes, lineOffset,
28+
b.buildProperties, onlyUpdateCompilationDatabase,
29+
)
30+
if b.logger.Verbose() {
31+
b.logger.WriteStdout(verboseOutput)
32+
}
33+
b.logger.WriteStdout(normalOutput)
34+
35+
return err
36+
}

Diff for: legacy/builder/builder.go

+2-32
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,9 @@ import (
2020
"time"
2121

2222
"github.com/arduino/arduino-cli/arduino/builder"
23-
"github.com/arduino/arduino-cli/arduino/builder/preprocessor"
2423
"github.com/arduino/arduino-cli/arduino/builder/sizer"
25-
"github.com/arduino/arduino-cli/arduino/sketch"
2624
"github.com/arduino/arduino-cli/i18n"
2725
"github.com/arduino/arduino-cli/legacy/builder/types"
28-
"github.com/arduino/go-paths-helper"
29-
properties "github.com/arduino/go-properties-orderedmap"
3026
"github.com/pkg/errors"
3127
"github.com/sirupsen/logrus"
3228
)
@@ -216,22 +212,13 @@ func (s *Builder) Run(ctx *types.Context) error {
216212
}),
217213

218214
types.BareCommand(func(ctx *types.Context) error {
219-
normalOutput, verboseOutput, err := ExportProjectCMake(
215+
return ctx.Builder.ExportProjectCMake(
220216
mainErr != nil,
221-
ctx.Builder.GetBuildPath(), ctx.Builder.GetSketchBuildPath(),
222217
ctx.SketchLibrariesDetector.ImportedLibraries(),
223-
ctx.Builder.GetBuildProperties(),
224-
ctx.Builder.Sketch(),
225218
ctx.SketchLibrariesDetector.IncludeFolders(),
226219
ctx.LineOffset,
227220
ctx.OnlyUpdateCompilationDatabase,
228221
)
229-
if ctx.BuilderLogger.Verbose() {
230-
ctx.BuilderLogger.WriteStdout(verboseOutput)
231-
} else {
232-
ctx.BuilderLogger.WriteStdout(normalOutput)
233-
}
234-
return err
235222
}),
236223

237224
types.BareCommand(func(ctx *types.Context) error {
@@ -264,27 +251,10 @@ func (s *Builder) Run(ctx *types.Context) error {
264251

265252
func preprocessSketchCommand(ctx *types.Context) types.BareCommand {
266253
return func(ctx *types.Context) error {
267-
normalOutput, verboseOutput, err := PreprocessSketch(
268-
ctx.Builder.Sketch(), ctx.Builder.GetBuildPath(), ctx.SketchLibrariesDetector.IncludeFolders(), ctx.LineOffset,
269-
ctx.Builder.GetBuildProperties(), ctx.OnlyUpdateCompilationDatabase)
270-
if ctx.BuilderLogger.Verbose() {
271-
ctx.BuilderLogger.WriteStdout(verboseOutput)
272-
} else {
273-
ctx.BuilderLogger.WriteStdout(normalOutput)
274-
}
275-
return err
254+
return ctx.Builder.PreprocessSketch(ctx.SketchLibrariesDetector.IncludeFolders(), ctx.LineOffset, ctx.OnlyUpdateCompilationDatabase)
276255
}
277256
}
278257

279-
func PreprocessSketch(
280-
sketch *sketch.Sketch, buildPath *paths.Path, includes paths.PathList, lineOffset int,
281-
buildProperties *properties.Map, onlyUpdateCompilationDatabase bool,
282-
) ([]byte, []byte, error) {
283-
// In the future we might change the preprocessor
284-
preprocessorImpl := preprocessor.PreprocessSketchWithCtags
285-
return preprocessorImpl(sketch, buildPath, includes, lineOffset, buildProperties, onlyUpdateCompilationDatabase)
286-
}
287-
288258
type Preprocess struct{}
289259

290260
func (s *Preprocess) Run(ctx *types.Context) error {

0 commit comments

Comments
 (0)