@@ -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-
4037var 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