@@ -28,6 +28,7 @@ type CompileOutput struct {
28
28
}
29
29
30
30
type BuilderResult struct {
31
+ BuildPath string `json:"build_path"`
31
32
UsedLibraries []* LibInfo `json:"used_libraries"`
32
33
}
33
34
@@ -86,7 +87,7 @@ func compileSketch(cmd *cobra.Command, args []string) {
86
87
if err != nil {
87
88
logrus .Fatal (err )
88
89
}
89
- objFilePath , returnJson := parseOutput (cmdOutput )
90
+ objFilesPaths , returnJson := parseOutput (cmdOutput )
90
91
91
92
workingDir , err := paths .Getwd ()
92
93
if err != nil {
@@ -100,11 +101,13 @@ func compileSketch(cmd *cobra.Command, args []string) {
100
101
}
101
102
102
103
// Copy the object files from the `<tempdir>/arduino-sketch_stuff/sketch` folder
103
- destObjFilePath := buildDir .Join (objFilePath .Base ())
104
- if err = objFilePath .CopyTo (destObjFilePath ); err != nil {
105
- logrus .Errorf ("error copying object file: %s" , err )
106
- } else {
107
- logrus .Infof ("copied file to %s" , destObjFilePath )
104
+ for _ , objFilePath := range objFilesPaths {
105
+ destObjFilePath := buildDir .Join (objFilePath .Base ())
106
+ if err = objFilePath .CopyTo (destObjFilePath ); err != nil {
107
+ logrus .Errorf ("error copying object file: %s" , err )
108
+ } else {
109
+ logrus .Infof ("copied file to %s" , destObjFilePath )
110
+ }
108
111
}
109
112
110
113
// save the result.json in the build dir
@@ -116,14 +119,13 @@ func compileSketch(cmd *cobra.Command, args []string) {
116
119
} else {
117
120
logrus .Infof ("created new file in: %s" , jsonFilePath )
118
121
}
119
-
120
122
}
121
123
122
124
// parseOutput function takes cmdOutToParse as argument,
123
125
// cmdOutToParse is the output captured from the command run
124
- // the function extract the path of the sketck.ino.o and
125
- // a returnJson object
126
- func parseOutput (cmdOutToParse []byte ) (* paths.Path , * ReturnJson ) {
126
+ // the function extract the paths of the .o files and
127
+ // a ReturnJson object
128
+ func parseOutput (cmdOutToParse []byte ) ([] * paths.Path , * ReturnJson ) {
127
129
err := json .Unmarshal (cmdOutToParse , & compileOutput )
128
130
if err != nil {
129
131
logrus .Fatal (err )
@@ -133,64 +135,41 @@ func parseOutput(cmdOutToParse []byte) (*paths.Path, *ReturnJson) {
133
135
134
136
compilerOutLines := strings .Split (compileOutput .CompilerOut , "\n " )
135
137
var coreLine string
136
- var objFile string
137
138
for _ , compilerOutLine := range compilerOutLines {
138
139
logrus .Debug (compilerOutLine )
139
140
if matched := strings .Contains (compilerOutLine , "Using core" ); matched {
140
141
coreLine = compilerOutLine
141
- }
142
- if objFile = ParseObjFilePath (compilerOutLine ); objFile != "" {
143
142
break // we should already have coreLine
144
143
}
145
-
146
144
}
147
145
if coreLine == "" {
148
146
logrus .Fatal ("cannot find core used" )
149
147
}
150
- if objFile == "" {
151
- logrus .Fatal ("cannot find sketch object file" )
152
- }
153
148
154
- objFilePath := paths .New (objFile )
155
- if objFilePath == nil {
156
- logrus .Fatal ("path cannot be created" )
149
+ // this dir contains all the obj files we need (the sketch related ones and not the core or libs)
150
+ sketchDir := paths .New (compileOutput .BuilderResult .BuildPath ).Join ("sketch" )
151
+ sketchFilesPaths , err := sketchDir .ReadDir ()
152
+ if err != nil {
153
+ logrus .Fatal (err )
154
+ } else if sketchFilesPaths == nil {
155
+ logrus .Fatalf ("empty directory: %s" , sketchDir )
156
+ }
157
+ var returnObjectFilesList []* paths.Path
158
+ for _ , sketchFilePath := range sketchFilesPaths {
159
+ if sketchFilePath .Ext () == ".o" {
160
+ returnObjectFilesList = append (returnObjectFilesList , sketchFilePath )
161
+ }
157
162
}
158
163
159
164
returnJson := ReturnJson {
160
165
CoreInfo : parseCoreLine (coreLine ),
161
166
LibsInfo : compileOutput .BuilderResult .UsedLibraries ,
162
167
}
163
168
164
- // TODO there could be multiple `.o` files, see zube comment. The correct approach is to go in `<tempdir>/arduino-sketch_stuff/sketch` and copy all the `.o` files
165
169
// TODO add also the packager -> maybe ParseReference could be used from the cli
166
170
// TODO core could be calculated from fqbn
167
171
168
- return objFilePath , & returnJson
169
- }
170
-
171
- // ParseObjFilePath is a function that takes a line from the compiler output
172
- // it returns a string with the path of the object file generated during the compile process
173
- //(recognizing it by .ino.cpp.o extension) if the extension is found in the string, "" otherwise
174
- func ParseObjFilePath (compilerOutLine string ) (objFilePath string ) {
175
- var endChar string
176
- extension := ".ino.cpp.o"
177
- if extensionIndex := strings .Index (compilerOutLine , extension ); extensionIndex != - 1 {
178
- objFileEndIndex := extensionIndex + len (extension )
179
- if objFileEndIndex >= len (compilerOutLine ) { // this means the path terminates with the `o` and thus the end character is space
180
- endChar = " " // we set it this way to avoid index out of bound
181
- } else {
182
- endChar = string (compilerOutLine [objFileEndIndex ])
183
- }
184
- objFileStartIndex := strings .LastIndex (compilerOutLine [:objFileEndIndex ], endChar )
185
- // we continue to search if the endChar is preceded by backslash (this means the endChar it's escaped and thus is not an endChar :D)
186
- for string (compilerOutLine [objFileStartIndex - 1 ]) == `\` {
187
- objFileStartIndex = strings .LastIndex (compilerOutLine [:objFileStartIndex ], endChar )
188
- }
189
- objFilePath = compilerOutLine [objFileStartIndex + 1 : objFileEndIndex ]
190
- return objFilePath
191
- } else {
192
- return ""
193
- }
172
+ return returnObjectFilesList , & returnJson
194
173
}
195
174
196
175
// parseCoreLine takes the line containig info regarding the core and
0 commit comments