@@ -23,23 +23,29 @@ import (
23
23
"regexp"
24
24
"strings"
25
25
26
+ "github.com/arduino/go-paths-helper"
26
27
properties "github.com/arduino/go-properties-orderedmap"
27
28
"golang.org/x/exp/slices"
28
29
29
30
"github.com/arduino/arduino-cli/arduino/builder/utils"
30
31
"github.com/arduino/arduino-cli/arduino/globals"
32
+ "github.com/arduino/arduino-cli/arduino/libraries"
33
+ "github.com/arduino/arduino-cli/arduino/sketch"
31
34
"github.com/arduino/arduino-cli/legacy/builder/constants"
32
- "github.com/arduino/arduino-cli/legacy/builder/types"
33
35
)
34
36
35
- type ExportProjectCMake struct {
36
- // Was there an error while compiling the sketch?
37
- SketchError bool
38
- }
39
-
40
37
var lineMatcher = regexp .MustCompile (`^#line\s\d+\s"` )
41
38
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 ) {
43
49
// copies the contents of the file named src to the file named
44
50
// by dst. The file will be created if it does not already exist. If the
45
51
// 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 {
175
181
}
176
182
var validStaticLibExtensions = []string {".a" }
177
183
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
180
187
}
181
188
182
189
// Create new cmake subFolder - clean if the folder is already there
183
- cmakeFolder := ctx . BuildPath .Join ("_cmake" )
190
+ cmakeFolder := buildPath .Join ("_cmake" )
184
191
if _ , err := cmakeFolder .Stat (); err == nil {
185
192
cmakeFolder .RemoveAll ()
186
193
}
@@ -197,10 +204,10 @@ func (s *ExportProjectCMake) Run(ctx *types.Context) error {
197
204
cmakeFile := cmakeFolder .Join ("CMakeLists.txt" )
198
205
199
206
dynamicLibsFromPkgConfig := map [string ]bool {}
200
- for _ , library := range ctx . SketchLibrariesDetector . ImportedLibraries () {
207
+ for _ , library := range importedLibraries {
201
208
// Copy used libraries in the correct folder
202
209
libDir := libBaseFolder .Join (library .DirName )
203
- mcu := ctx . BuildProperties .Get ("build.mcu" )
210
+ mcu := buildProperties .Get ("build.mcu" )
204
211
copyDir (library .InstallDir .String (), libDir .String (), validExportExtensions )
205
212
206
213
// Read cmake options if available
@@ -231,20 +238,28 @@ func (s *ExportProjectCMake) Run(ctx *types.Context) error {
231
238
}
232
239
233
240
// 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 )
235
242
if err != nil {
236
243
fmt .Println (err )
237
244
}
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 )
239
246
if err != nil {
240
247
fmt .Println (err )
241
248
}
242
249
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
245
260
}
246
261
247
- err = copyDir (ctx . SketchBuildPath .String (), cmakeFolder .Join ("sketch" ).String (), validExportExtensions )
262
+ err = copyDir (sketchBuildPath .String (), cmakeFolder .Join ("sketch" ).String (), validExportExtensions )
248
263
if err != nil {
249
264
fmt .Println (err )
250
265
}
@@ -279,9 +294,9 @@ func (s *ExportProjectCMake) Run(ctx *types.Context) error {
279
294
var dynamicLibsFromGccMinusL []string
280
295
var linkDirectories []string
281
296
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 )
285
300
286
301
// Extract folders with .h in them for adding in include list
287
302
headerFiles , _ := utils .FindFilesInFolder (cmakeFolder , true , validHeaderExtensions ... )
@@ -292,7 +307,7 @@ func (s *ExportProjectCMake) Run(ctx *types.Context) error {
292
307
293
308
// Generate the CMakeLists global file
294
309
295
- projectName := ctx . Sketch .Name
310
+ projectName := sketch .Name
296
311
297
312
cmakelist := "cmake_minimum_required(VERSION 3.5.0)\n "
298
313
cmakelist += "INCLUDE(FindPkgConfig)\n "
@@ -349,14 +364,10 @@ func (s *ExportProjectCMake) Run(ctx *types.Context) error {
349
364
350
365
cmakeFile .WriteFile ([]byte (cmakelist ))
351
366
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
357
368
}
358
369
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 ) {
360
371
appendIfNotPresent := func (target []string , elements ... string ) []string {
361
372
for _ , element := range elements {
362
373
if ! slices .Contains (target , element ) {
@@ -366,7 +377,7 @@ func extractCompileFlags(ctx *types.Context, recipe string, defines, dynamicLibs
366
377
return target
367
378
}
368
379
369
- command , _ := utils .PrepareCommandForRecipe (ctx . BuildProperties , recipe , true )
380
+ command , _ := utils .PrepareCommandForRecipe (buildProperties , recipe , true )
370
381
371
382
for _ , arg := range command .GetArgs () {
372
383
if strings .HasPrefix (arg , "-D" ) {
0 commit comments