@@ -40,6 +40,7 @@ import (
40
40
41
41
"github.com/arduino/arduino-cli/legacy/builder/constants"
42
42
"github.com/arduino/arduino-cli/legacy/builder/i18n"
43
+ "github.com/arduino/arduino-builder/types"
43
44
"github.com/arduino/arduino-cli/legacy/builder/types"
44
45
"github.com/arduino/arduino-cli/legacy/builder/utils"
45
46
"github.com/arduino/go-paths-helper"
@@ -59,8 +60,8 @@ func PrintProgressIfProgressEnabledAndMachineLogger(ctx *types.Context) {
59
60
}
60
61
}
61
62
62
- func CompileFilesRecursive (ctx * types.Context , sourcePath * paths.Path , buildPath * paths.Path , buildProperties * properties.Map , includes []string ) (paths.PathList , error ) {
63
- objectFiles , err := CompileFiles (ctx , sourcePath , false , buildPath , buildProperties , includes )
63
+ func CompileFilesRecursive (ctx * types.Context , sourcePath * paths.Path , buildPath * paths.Path , buildProperties * properties.Map , includes []string , libraryModel * types. CodeModelLibrary ) (paths.PathList , error ) {
64
+ objectFiles , err := CompileFiles (ctx , sourcePath , false , buildPath , buildProperties , includes , libraryModel )
64
65
if err != nil {
65
66
return nil , i18n .WrapError (err )
66
67
}
@@ -71,7 +72,7 @@ func CompileFilesRecursive(ctx *types.Context, sourcePath *paths.Path, buildPath
71
72
}
72
73
73
74
for _ , folder := range folders {
74
- subFolderObjectFiles , err := CompileFilesRecursive (ctx , sourcePath .Join (folder .Name ()), buildPath .Join (folder .Name ()), buildProperties , includes )
75
+ subFolderObjectFiles , err := CompileFilesRecursive (ctx , sourcePath .Join (folder .Name ()), buildPath .Join (folder .Name ()), buildProperties , includes , libraryModel )
75
76
if err != nil {
76
77
return nil , i18n .WrapError (err )
77
78
}
@@ -81,16 +82,16 @@ func CompileFilesRecursive(ctx *types.Context, sourcePath *paths.Path, buildPath
81
82
return objectFiles , nil
82
83
}
83
84
84
- func CompileFiles (ctx * types.Context , sourcePath * paths.Path , recurse bool , buildPath * paths.Path , buildProperties * properties.Map , includes []string ) (paths.PathList , error ) {
85
- sObjectFiles , err := compileFilesWithExtensionWithRecipe (ctx , sourcePath , recurse , buildPath , buildProperties , includes , ".S" , constants .RECIPE_S_PATTERN )
85
+ func CompileFiles (ctx * types.Context , sourcePath * paths.Path , recurse bool , buildPath * paths.Path , buildProperties * properties.Map , includes []string , libraryModel * types. CodeModelLibrary ) (paths.PathList , error ) {
86
+ sObjectFiles , err := compileFilesWithExtensionWithRecipe (ctx , sourcePath , recurse , buildPath , buildProperties , includes , ".S" , constants .RECIPE_S_PATTERN , libraryModel )
86
87
if err != nil {
87
88
return nil , i18n .WrapError (err )
88
89
}
89
- cObjectFiles , err := compileFilesWithExtensionWithRecipe (ctx , sourcePath , recurse , buildPath , buildProperties , includes , ".c" , constants .RECIPE_C_PATTERN )
90
+ cObjectFiles , err := compileFilesWithExtensionWithRecipe (ctx , sourcePath , recurse , buildPath , buildProperties , includes , ".c" , constants .RECIPE_C_PATTERN , libraryModel )
90
91
if err != nil {
91
92
return nil , i18n .WrapError (err )
92
93
}
93
- cppObjectFiles , err := compileFilesWithExtensionWithRecipe (ctx , sourcePath , recurse , buildPath , buildProperties , includes , ".cpp" , constants .RECIPE_CPP_PATTERN )
94
+ cppObjectFiles , err := compileFilesWithExtensionWithRecipe (ctx , sourcePath , recurse , buildPath , buildProperties , includes , ".cpp" , constants .RECIPE_CPP_PATTERN , libraryModel )
94
95
if err != nil {
95
96
return nil , i18n .WrapError (err )
96
97
}
@@ -101,12 +102,47 @@ func CompileFiles(ctx *types.Context, sourcePath *paths.Path, recurse bool, buil
101
102
return objectFiles , nil
102
103
}
103
104
104
- func compileFilesWithExtensionWithRecipe (ctx * types.Context , sourcePath * paths.Path , recurse bool , buildPath * paths.Path , buildProperties * properties.Map , includes []string , extension string , recipe string ) (paths.PathList , error ) {
105
+ func ReplaceOptimizationFlags (str string ) string {
106
+ var tmp = strings .Split (str , " " )
107
+ for k , v := range tmp {
108
+ if v == "-O2" || v == "-Os" || v == "-O1" || v == "-Og" || v == "-O3" {
109
+ tmp [k ] = "-O0"
110
+ } else if v == "-flto" {
111
+ tmp [k ] = ""
112
+ }
113
+ }
114
+
115
+ return strings .Join (tmp , " " )
116
+ }
117
+
118
+ func RemoveOptimizationFromBuildProperties (properties properties.Map ) properties.Map {
119
+ var result = make (map [string ]string )
120
+ for k , v := range properties {
121
+ result [k ] = v
122
+ }
123
+
124
+ result ["compiler.c.flags" ] = ReplaceOptimizationFlags (result ["compiler.c.flags" ])
125
+ result ["compiler.cpp.flags" ] = ReplaceOptimizationFlags (result ["compiler.cpp.flags" ])
126
+ return result
127
+ }
128
+
129
+ func ExpandSysprogsExtensionProperties (properties properties.Map ) properties.Map {
130
+ var result = make (map [string ]string )
131
+ for k , v := range properties {
132
+ result [k ] = v
133
+ }
134
+
135
+ result ["compiler.c.flags" ] += " " + result ["com.sysprogs.extraflags" ]
136
+ result ["compiler.cpp.flags" ] += " " + result ["com.sysprogs.extraflags" ]
137
+ return result
138
+ }
139
+
140
+ func compileFilesWithExtensionWithRecipe (ctx * types.Context , sourcePath * paths.Path , recurse bool , buildPath * paths.Path , buildProperties * properties.Map , includes []string , extension string , recipe string , libraryModel * types.CodeModelLibrary ) (paths.PathList , error ) {
105
141
sources , err := findFilesInFolder (sourcePath , extension , recurse )
106
142
if err != nil {
107
143
return nil , i18n .WrapError (err )
108
144
}
109
- return compileFilesWithRecipe (ctx , sourcePath , sources , buildPath , buildProperties , includes , recipe )
145
+ return compileFilesWithRecipe (ctx , sourcePath , sources , buildPath , buildProperties , includes , recipe , libraryModel )
110
146
}
111
147
112
148
func findFilesInFolder (sourcePath * paths.Path , extension string , recurse bool ) (paths.PathList , error ) {
@@ -168,7 +204,7 @@ func findAllFilesInFolder(sourcePath string, recurse bool) ([]string, error) {
168
204
return sources , nil
169
205
}
170
206
171
- func compileFilesWithRecipe (ctx * types.Context , sourcePath * paths.Path , sources paths.PathList , buildPath * paths.Path , buildProperties * properties.Map , includes []string , recipe string ) (paths.PathList , error ) {
207
+ func compileFilesWithRecipe (ctx * types.Context , sourcePath * paths.Path , sources paths.PathList , buildPath * paths.Path , buildProperties * properties.Map , includes []string , recipe string , libraryModel * types. CodeModelLibrary ) (paths.PathList , error ) {
172
208
objectFiles := paths .NewPathList ()
173
209
if len (sources ) == 0 {
174
210
return objectFiles , nil
@@ -182,7 +218,7 @@ func compileFilesWithRecipe(ctx *types.Context, sourcePath *paths.Path, sources
182
218
queue := make (chan * paths.Path )
183
219
job := func (source * paths.Path ) {
184
220
PrintProgressIfProgressEnabledAndMachineLogger (ctx )
185
- objectFile , err := compileFileWithRecipe (ctx , sourcePath , source , buildPath , buildProperties , includes , recipe )
221
+ objectFile , err := compileFileWithRecipe (ctx , sourcePath , source , buildPath , buildProperties , includes , recipe , libraryModel )
186
222
if err != nil {
187
223
errorsMux .Lock ()
188
224
errors = append (errors , err )
@@ -230,7 +266,7 @@ func compileFilesWithRecipe(ctx *types.Context, sourcePath *paths.Path, sources
230
266
return objectFiles , nil
231
267
}
232
268
233
- func compileFileWithRecipe (ctx * types.Context , sourcePath * paths.Path , source * paths.Path , buildPath * paths.Path , buildProperties * properties.Map , includes []string , recipe string ) (* paths.Path , error ) {
269
+ func compileFileWithRecipe (ctx * types.Context , sourcePath * paths.Path , source * paths.Path , buildPath * paths.Path , buildProperties * properties.Map , includes []string , recipe string , libraryModel * types. CodeModelLibrary ) (* paths.Path , error ) {
234
270
logger := ctx .GetLogger ()
235
271
properties := buildProperties .Clone ()
236
272
properties .Set (constants .BUILD_PROPERTIES_COMPILER_WARNING_FLAGS , properties .Get (constants .BUILD_PROPERTIES_COMPILER_WARNING_FLAGS + "." + ctx .WarningsLevel ))
@@ -249,17 +285,32 @@ func compileFileWithRecipe(ctx *types.Context, sourcePath *paths.Path, source *p
249
285
return nil , i18n .WrapError (err )
250
286
}
251
287
252
- objIsUpToDate , err := ObjFileIsUpToDate (ctx , source , objectFile , depsFile )
253
- if err != nil {
254
- return nil , i18n .WrapError (err )
255
- }
256
- if ! objIsUpToDate {
257
- _ , _ , err = ExecRecipe (ctx , properties , recipe , false /* stdout */ , utils .ShowIfVerbose /* stderr */ , utils .Show )
288
+ if libraryModel != nil {
289
+ //We are not actually building, just dumping the model
290
+ command , err := PrepareCommandForRecipe (properties , recipe , false , false , false , logger )
291
+ if err != nil {
292
+ return "" , i18n .WrapError (err )
293
+ }
294
+
295
+ var invocation = new (types.CodeModelGCCInvocation )
296
+ invocation .GCC = command .Path
297
+ invocation .InputFile = source
298
+ invocation .ObjectFile = properties [constants .BUILD_PROPERTIES_OBJECT_FILE ]
299
+ invocation .Arguments = command .Args [1 :]
300
+ libraryModel .Invocations = append (libraryModel .Invocations , invocation )
301
+ } else {
302
+ objIsUpToDate , err := ObjFileIsUpToDate (ctx , source , objectFile , depsFile )
258
303
if err != nil {
259
304
return nil , i18n .WrapError (err )
260
305
}
261
- } else if ctx .Verbose {
262
- logger .Println (constants .LOG_LEVEL_INFO , constants .MSG_USING_PREVIOUS_COMPILED_FILE , objectFile )
306
+ if ! objIsUpToDate {
307
+ _ , _ , err = ExecRecipe (ctx , properties , recipe , false /* stdout */ , utils .ShowIfVerbose /* stderr */ , utils .Show )
308
+ if err != nil {
309
+ return nil , i18n .WrapError (err )
310
+ }
311
+ } else if ctx .Verbose {
312
+ logger .Println (constants .LOG_LEVEL_INFO , constants .MSG_USING_PREVIOUS_COMPILED_FILE , objectFile )
313
+ }
263
314
}
264
315
265
316
return objectFile , nil
@@ -449,46 +500,49 @@ func TXTBuildRulesHaveChanged(corePath, targetCorePath, targetFile *paths.Path)
449
500
return true
450
501
}
451
502
452
- func ArchiveCompiledFiles (ctx * types.Context , buildPath * paths.Path , archiveFile * paths.Path , objectFilesToArchive paths.PathList , buildProperties * properties.Map ) (* paths.Path , error ) {
503
+ func ArchiveCompiledFiles (ctx * types.Context , buildPath * paths.Path , archiveFile * paths.Path , objectFilesToArchive paths.PathList , buildProperties * properties.Map , libraryModel * types. CodeModelLibrary ) (* paths.Path , error ) {
453
504
logger := ctx .GetLogger ()
454
505
archiveFilePath := buildPath .JoinPath (archiveFile )
455
506
456
507
rebuildArchive := false
508
+ if libraryModel != nil {
509
+ libraryModel .ArchiveFile = archiveFilePath
510
+ } else {
511
+ if archiveFileStat , err := archiveFilePath .Stat (); err == nil {
512
+
513
+ for _ , objectFile := range objectFilesToArchive {
514
+ objectFileStat , err := objectFile .Stat ()
515
+ if err != nil || objectFileStat .ModTime ().After (archiveFileStat .ModTime ()) {
516
+ // need to rebuild the archive
517
+ rebuildArchive = true
518
+ break
519
+ }
520
+ }
457
521
458
- if archiveFileStat , err := archiveFilePath .Stat (); err == nil {
459
-
460
- for _ , objectFile := range objectFilesToArchive {
461
- objectFileStat , err := objectFile .Stat ()
462
- if err != nil || objectFileStat .ModTime ().After (archiveFileStat .ModTime ()) {
463
- // need to rebuild the archive
464
- rebuildArchive = true
465
- break
522
+ // something changed, rebuild the core archive
523
+ if rebuildArchive {
524
+ err = archiveFilePath .Remove ()
525
+ if err != nil {
526
+ return nil , i18n .WrapError (err )
527
+ }
528
+ } else {
529
+ if ctx .Verbose {
530
+ logger .Println (constants .LOG_LEVEL_INFO , constants .MSG_USING_PREVIOUS_COMPILED_FILE , archiveFilePath )
531
+ }
532
+ return archiveFilePath , nil
466
533
}
467
534
}
468
535
469
- // something changed, rebuild the core archive
470
- if rebuildArchive {
471
- err = archiveFilePath .Remove ()
536
+ for _ , objectFile := range objectFilesToArchive {
537
+ properties := buildProperties .Clone ()
538
+ properties .Set (constants .BUILD_PROPERTIES_ARCHIVE_FILE , archiveFilePath .Base ())
539
+ properties .SetPath (constants .BUILD_PROPERTIES_ARCHIVE_FILE_PATH , archiveFilePath )
540
+ properties .SetPath (constants .BUILD_PROPERTIES_OBJECT_FILE , objectFile )
541
+
542
+ _ , _ , err := ExecRecipe (ctx , properties , constants .RECIPE_AR_PATTERN , false /* stdout */ , utils .ShowIfVerbose /* stderr */ , utils .Show )
472
543
if err != nil {
473
544
return nil , i18n .WrapError (err )
474
545
}
475
- } else {
476
- if ctx .Verbose {
477
- logger .Println (constants .LOG_LEVEL_INFO , constants .MSG_USING_PREVIOUS_COMPILED_FILE , archiveFilePath )
478
- }
479
- return archiveFilePath , nil
480
- }
481
- }
482
-
483
- for _ , objectFile := range objectFilesToArchive {
484
- properties := buildProperties .Clone ()
485
- properties .Set (constants .BUILD_PROPERTIES_ARCHIVE_FILE , archiveFilePath .Base ())
486
- properties .SetPath (constants .BUILD_PROPERTIES_ARCHIVE_FILE_PATH , archiveFilePath )
487
- properties .SetPath (constants .BUILD_PROPERTIES_OBJECT_FILE , objectFile )
488
-
489
- _ , _ , err := ExecRecipe (ctx , properties , constants .RECIPE_AR_PATTERN , false /* stdout */ , utils .ShowIfVerbose /* stderr */ , utils .Show )
490
- if err != nil {
491
- return nil , i18n .WrapError (err )
492
546
}
493
547
}
494
548
0 commit comments