Skip to content

Commit 1b8abb8

Browse files
committed
don't use parsing from compilerOut but json field build_path instead
the tool now supports multiple `.o`
1 parent 274753d commit 1b8abb8

File tree

2 files changed

+26
-72
lines changed

2 files changed

+26
-72
lines changed

cmd/compile.go

Lines changed: 26 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type CompileOutput struct {
2828
}
2929

3030
type BuilderResult struct {
31+
BuildPath string `json:"build_path"`
3132
UsedLibraries []*LibInfo `json:"used_libraries"`
3233
}
3334

@@ -86,7 +87,7 @@ func compileSketch(cmd *cobra.Command, args []string) {
8687
if err != nil {
8788
logrus.Fatal(err)
8889
}
89-
objFilePath, returnJson := parseOutput(cmdOutput)
90+
objFilesPaths, returnJson := parseOutput(cmdOutput)
9091

9192
workingDir, err := paths.Getwd()
9293
if err != nil {
@@ -100,11 +101,13 @@ func compileSketch(cmd *cobra.Command, args []string) {
100101
}
101102

102103
// 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+
}
108111
}
109112

110113
// save the result.json in the build dir
@@ -116,14 +119,13 @@ func compileSketch(cmd *cobra.Command, args []string) {
116119
} else {
117120
logrus.Infof("created new file in: %s", jsonFilePath)
118121
}
119-
120122
}
121123

122124
// parseOutput function takes cmdOutToParse as argument,
123125
// 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) {
127129
err := json.Unmarshal(cmdOutToParse, &compileOutput)
128130
if err != nil {
129131
logrus.Fatal(err)
@@ -133,64 +135,41 @@ func parseOutput(cmdOutToParse []byte) (*paths.Path, *ReturnJson) {
133135

134136
compilerOutLines := strings.Split(compileOutput.CompilerOut, "\n")
135137
var coreLine string
136-
var objFile string
137138
for _, compilerOutLine := range compilerOutLines {
138139
logrus.Debug(compilerOutLine)
139140
if matched := strings.Contains(compilerOutLine, "Using core"); matched {
140141
coreLine = compilerOutLine
141-
}
142-
if objFile = ParseObjFilePath(compilerOutLine); objFile != "" {
143142
break // we should already have coreLine
144143
}
145-
146144
}
147145
if coreLine == "" {
148146
logrus.Fatal("cannot find core used")
149147
}
150-
if objFile == "" {
151-
logrus.Fatal("cannot find sketch object file")
152-
}
153148

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+
}
157162
}
158163

159164
returnJson := ReturnJson{
160165
CoreInfo: parseCoreLine(coreLine),
161166
LibsInfo: compileOutput.BuilderResult.UsedLibraries,
162167
}
163168

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
165169
// TODO add also the packager -> maybe ParseReference could be used from the cli
166170
// TODO core could be calculated from fqbn
167171

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
194173
}
195174

196175
// parseCoreLine takes the line containig info regarding the core and

cmd/compile_test.go

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)