Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 90e539b

Browse files
refactor CreateMakeRule in a function
1 parent ba0852c commit 90e539b

File tree

2 files changed

+82
-41
lines changed

2 files changed

+82
-41
lines changed

‎legacy/builder/builder.go‎

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@ import (
2020
"time"
2121

2222
"github.com/arduino/arduino-cli/arduino/builder/preprocessor"
23+
"github.com/arduino/arduino-cli/arduino/sketch"
2324
"github.com/arduino/arduino-cli/i18n"
2425
"github.com/arduino/arduino-cli/legacy/builder/phases"
2526
"github.com/arduino/arduino-cli/legacy/builder/types"
27+
"github.com/arduino/go-paths-helper"
28+
properties "github.com/arduino/go-properties-orderedmap"
2629
"github.com/pkg/errors"
2730
"github.com/sirupsen/logrus"
2831
)
@@ -58,7 +61,7 @@ func (s *Builder) Run(ctx *types.Context) error {
5861
warnAboutArchIncompatibleLibraries(ctx),
5962

6063
logIfVerbose(false, tr("Generating function prototypes...")),
61-
types.BareCommand(PreprocessSketch),
64+
preprocessSketchCommand(ctx),
6265

6366
logIfVerbose(false, tr("Compiling sketch...")),
6467

@@ -249,7 +252,24 @@ func (s *Builder) Run(ctx *types.Context) error {
249252
return nil
250253
}),
251254

252-
&ExportProjectCMake{SketchError: mainErr != nil},
255+
types.BareCommand(func(ctx *types.Context) error {
256+
normalOutput, verboseOutput, err := ExportProjectCMake(
257+
mainErr != nil,
258+
ctx.BuildPath, ctx.SketchBuildPath,
259+
ctx.SketchLibrariesDetector.ImportedLibraries(),
260+
ctx.BuildProperties,
261+
ctx.Sketch,
262+
ctx.SketchLibrariesDetector.IncludeFolders(),
263+
ctx.LineOffset,
264+
ctx.OnlyUpdateCompilationDatabase,
265+
)
266+
if ctx.Verbose {
267+
ctx.WriteStdout(verboseOutput)
268+
} else {
269+
ctx.WriteStdout(normalOutput)
270+
}
271+
return err
272+
}),
253273

254274
types.BareCommand(func(ctx *types.Context) error {
255275
executableSectionsSize, err := phases.Sizer(
@@ -282,17 +302,27 @@ func (s *Builder) Run(ctx *types.Context) error {
282302
return otherErr
283303
}
284304

285-
func PreprocessSketch(ctx *types.Context) error {
286-
preprocessorImpl := preprocessor.PreprocessSketchWithCtags
287-
normalOutput, verboseOutput, err := preprocessorImpl(
288-
ctx.Sketch, ctx.BuildPath, ctx.SketchLibrariesDetector.IncludeFolders(), ctx.LineOffset,
289-
ctx.BuildProperties, ctx.OnlyUpdateCompilationDatabase)
290-
if ctx.Verbose {
291-
ctx.WriteStdout(verboseOutput)
292-
} else {
293-
ctx.WriteStdout(normalOutput)
305+
func preprocessSketchCommand(ctx *types.Context) types.BareCommand {
306+
return func(ctx *types.Context) error {
307+
normalOutput, verboseOutput, err := PreprocessSketch(
308+
ctx.Sketch, ctx.BuildPath, ctx.SketchLibrariesDetector.IncludeFolders(), ctx.LineOffset,
309+
ctx.BuildProperties, ctx.OnlyUpdateCompilationDatabase)
310+
if ctx.Verbose {
311+
ctx.WriteStdout(verboseOutput)
312+
} else {
313+
ctx.WriteStdout(normalOutput)
314+
}
315+
return err
294316
}
295-
return err
317+
}
318+
319+
func PreprocessSketch(
320+
sketch *sketch.Sketch, buildPath *paths.Path, includes paths.PathList, lineOffset int,
321+
buildProperties *properties.Map, onlyUpdateCompilationDatabase bool,
322+
) ([]byte, []byte, error) {
323+
// In the future we might change the preprocessor
324+
preprocessorImpl := preprocessor.PreprocessSketchWithCtags
325+
return preprocessorImpl(sketch, buildPath, includes, lineOffset, buildProperties, onlyUpdateCompilationDatabase)
296326
}
297327

298328
type Preprocess struct{}
@@ -319,7 +349,7 @@ func (s *Preprocess) Run(ctx *types.Context) error {
319349

320350
warnAboutArchIncompatibleLibraries(ctx),
321351

322-
types.BareCommand(PreprocessSketch),
352+
preprocessSketchCommand(ctx),
323353
}
324354

325355
if err := runCommands(ctx, commands); err != nil {

‎legacy/builder/create_cmake_rule.go‎

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,29 @@ import (
2323
"regexp"
2424
"strings"
2525

26+
"github.com/arduino/go-paths-helper"
2627
properties "github.com/arduino/go-properties-orderedmap"
2728
"golang.org/x/exp/slices"
2829

2930
"github.com/arduino/arduino-cli/arduino/builder/utils"
3031
"github.com/arduino/arduino-cli/arduino/globals"
32+
"github.com/arduino/arduino-cli/arduino/libraries"
33+
"github.com/arduino/arduino-cli/arduino/sketch"
3134
"github.com/arduino/arduino-cli/legacy/builder/constants"
32-
"github.com/arduino/arduino-cli/legacy/builder/types"
3335
)
3436

35-
type ExportProjectCMake struct {
36-
// Was there an error while compiling the sketch?
37-
SketchError bool
38-
}
39-
4037
var lineMatcher = regexp.MustCompile(`^#line\s\d+\s"`)
4138

42-
func (s *ExportProjectCMake) Run(ctx *types.Context) error {
39+
func ExportProjectCMake(
40+
sketchError bool, // Was there an error while compiling the sketch?
41+
buildPath, sketchBuildPath *paths.Path,
42+
importedLibraries libraries.List,
43+
buildProperties *properties.Map,
44+
sketch *sketch.Sketch,
45+
includeFolders paths.PathList,
46+
lineOffset int,
47+
onlyUpdateCompilationDatabase bool,
48+
) ([]byte, []byte, error) {
4349
// copies the contents of the file named src to the file named
4450
// by dst. The file will be created if it does not already exist. If the
4551
// destination file exists, all it's contents will be replaced by the contents
@@ -175,12 +181,13 @@ func (s *ExportProjectCMake) Run(ctx *types.Context) error {
175181
}
176182
var validStaticLibExtensions = []string{".a"}
177183

178-
if s.SketchError || !canExportCmakeProject(ctx) {
179-
return nil
184+
// If sketch error or cannot export Cmake project
185+
if sketchError || buildProperties.Get("compiler.export_cmake") == "" {
186+
return nil, nil, nil
180187
}
181188

182189
// Create new cmake subFolder - clean if the folder is already there
183-
cmakeFolder := ctx.BuildPath.Join("_cmake")
190+
cmakeFolder := buildPath.Join("_cmake")
184191
if _, err := cmakeFolder.Stat(); err == nil {
185192
cmakeFolder.RemoveAll()
186193
}
@@ -197,10 +204,10 @@ func (s *ExportProjectCMake) Run(ctx *types.Context) error {
197204
cmakeFile := cmakeFolder.Join("CMakeLists.txt")
198205

199206
dynamicLibsFromPkgConfig := map[string]bool{}
200-
for _, library := range ctx.SketchLibrariesDetector.ImportedLibraries() {
207+
for _, library := range importedLibraries {
201208
// Copy used libraries in the correct folder
202209
libDir := libBaseFolder.Join(library.DirName)
203-
mcu := ctx.BuildProperties.Get("build.mcu")
210+
mcu := buildProperties.Get("build.mcu")
204211
copyDir(library.InstallDir.String(), libDir.String(), validExportExtensions)
205212

206213
// Read cmake options if available
@@ -231,20 +238,28 @@ func (s *ExportProjectCMake) Run(ctx *types.Context) error {
231238
}
232239

233240
// Copy core + variant in use + preprocessed sketch in the correct folders
234-
err := copyDir(ctx.BuildProperties.Get("build.core.path"), coreFolder.String(), validExportExtensions)
241+
err := copyDir(buildProperties.Get("build.core.path"), coreFolder.String(), validExportExtensions)
235242
if err != nil {
236243
fmt.Println(err)
237244
}
238-
err = copyDir(ctx.BuildProperties.Get("build.variant.path"), coreFolder.Join("variant").String(), validExportExtensions)
245+
err = copyDir(buildProperties.Get("build.variant.path"), coreFolder.Join("variant").String(), validExportExtensions)
239246
if err != nil {
240247
fmt.Println(err)
241248
}
242249

243-
if err := PreprocessSketch(ctx); err != nil {
244-
return err
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
245260
}
246261

247-
err = copyDir(ctx.SketchBuildPath.String(), cmakeFolder.Join("sketch").String(), validExportExtensions)
262+
err = copyDir(sketchBuildPath.String(), cmakeFolder.Join("sketch").String(), validExportExtensions)
248263
if err != nil {
249264
fmt.Println(err)
250265
}
@@ -279,9 +294,9 @@ func (s *ExportProjectCMake) Run(ctx *types.Context) error {
279294
var dynamicLibsFromGccMinusL []string
280295
var linkDirectories []string
281296

282-
extractCompileFlags(ctx, constants.RECIPE_C_COMBINE_PATTERN, &defines, &dynamicLibsFromGccMinusL, &linkerflags, &linkDirectories)
283-
extractCompileFlags(ctx, "recipe.c.o.pattern", &defines, &dynamicLibsFromGccMinusL, &linkerflags, &linkDirectories)
284-
extractCompileFlags(ctx, "recipe.cpp.o.pattern", &defines, &dynamicLibsFromGccMinusL, &linkerflags, &linkDirectories)
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)
285300

286301
// Extract folders with .h in them for adding in include list
287302
headerFiles, _ := utils.FindFilesInFolder(cmakeFolder, true, validHeaderExtensions...)
@@ -292,7 +307,7 @@ func (s *ExportProjectCMake) Run(ctx *types.Context) error {
292307

293308
// Generate the CMakeLists global file
294309

295-
projectName := ctx.Sketch.Name
310+
projectName := sketch.Name
296311

297312
cmakelist := "cmake_minimum_required(VERSION 3.5.0)\n"
298313
cmakelist += "INCLUDE(FindPkgConfig)\n"
@@ -349,14 +364,10 @@ func (s *ExportProjectCMake) Run(ctx *types.Context) error {
349364

350365
cmakeFile.WriteFile([]byte(cmakelist))
351366

352-
return nil
353-
}
354-
355-
func canExportCmakeProject(ctx *types.Context) bool {
356-
return ctx.BuildProperties.Get("compiler.export_cmake") != ""
367+
return normalOutput, verboseOutput, nil
357368
}
358369

359-
func extractCompileFlags(ctx*types.Context, recipe string, defines, dynamicLibs, linkerflags, linkDirectories *[]string) {
370+
func extractCompileFlags(buildProperties*properties.Map, recipe string, defines, dynamicLibs, linkerflags, linkDirectories *[]string) {
360371
appendIfNotPresent := func(target []string, elements ...string) []string {
361372
for _, element := range elements {
362373
if !slices.Contains(target, element) {
@@ -366,7 +377,7 @@ func extractCompileFlags(ctx *types.Context, recipe string, defines, dynamicLibs
366377
return target
367378
}
368379

369-
command, _ := utils.PrepareCommandForRecipe(ctx.BuildProperties, recipe, true)
380+
command, _ := utils.PrepareCommandForRecipe(buildProperties, recipe, true)
370381

371382
for _, arg := range command.GetArgs() {
372383
if strings.HasPrefix(arg, "-D") {

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /