Skip to content

Commit 274753d

Browse files
committed
.o file and result.json are saved in build dir, refactor some var names
1 parent ca9cf98 commit 274753d

File tree

2 files changed

+52
-19
lines changed

2 files changed

+52
-19
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
cslt-tool
2-
.vscode
2+
.vscode
3+
build/

cmd/compile.go

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,37 @@ func compileSketch(cmd *cobra.Command, args []string) {
8686
if err != nil {
8787
logrus.Fatal(err)
8888
}
89-
parseOutput(cmdOutput) // TODO save the return
89+
objFilePath, returnJson := parseOutput(cmdOutput)
90+
91+
workingDir, err := paths.Getwd()
92+
if err != nil {
93+
logrus.Fatal(err)
94+
}
95+
buildDir := workingDir.Join("build")
96+
if !buildDir.Exist() {
97+
if err = buildDir.Mkdir(); err != nil {
98+
logrus.Fatal(err)
99+
}
100+
}
101+
102+
// 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)
108+
}
109+
110+
// save the result.json in the build dir
111+
jsonFilePath := buildDir.Join("result.json")
112+
if jsonContents, err := json.MarshalIndent(returnJson, "", " "); err != nil {
113+
logrus.Errorf("error serializing json: %s", err)
114+
} else if err := jsonFilePath.WriteFile(jsonContents); err != nil {
115+
logrus.Errorf("error writing result.json: %s", err)
116+
} else {
117+
logrus.Infof("created new file in: %s", jsonFilePath)
118+
}
90119

91-
// TODO:
92-
// copy sketch.ino.o from tmp/... in current dir or in --output dir
93120
}
94121

95122
// parseOutput function takes cmdOutToParse as argument,
@@ -106,55 +133,60 @@ func parseOutput(cmdOutToParse []byte) (*paths.Path, *ReturnJson) {
106133

107134
compilerOutLines := strings.Split(compileOutput.CompilerOut, "\n")
108135
var coreLine string
109-
var objFilePath string
136+
var objFile string
110137
for _, compilerOutLine := range compilerOutLines {
111-
logrus.Info(compilerOutLine)
138+
logrus.Debug(compilerOutLine)
112139
if matched := strings.Contains(compilerOutLine, "Using core"); matched {
113140
coreLine = compilerOutLine
114141
}
115-
if objFilePath = ParseObjFilePath(compilerOutLine); objFilePath != "" {
142+
if objFile = ParseObjFilePath(compilerOutLine); objFile != "" {
116143
break // we should already have coreLine
117144
}
118145

119146
}
120147
if coreLine == "" {
121148
logrus.Fatal("cannot find core used")
122149
}
123-
if objFilePath == "" {
150+
if objFile == "" {
124151
logrus.Fatal("cannot find sketch object file")
125152
}
126153

154+
objFilePath := paths.New(objFile)
155+
if objFilePath == nil {
156+
logrus.Fatal("path cannot be created")
157+
}
158+
127159
returnJson := ReturnJson{
128160
CoreInfo: parseCoreLine(coreLine),
129161
LibsInfo: compileOutput.BuilderResult.UsedLibraries,
130162
}
131163

132-
// TODO missing calculation of <sketch>.ino.o file
133164
// 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
134165
// TODO add also the packager -> maybe ParseReference could be used from the cli
135166
// TODO core could be calculated from fqbn
136167

137-
return nil, &returnJson //TODO remove
168+
return objFilePath, &returnJson
138169
}
139170

140171
// ParseObjFilePath is a function that takes a line from the compiler output
141172
// it returns a string with the path of the object file generated during the compile process
142173
//(recognizing it by .ino.cpp.o extension) if the extension is found in the string, "" otherwise
143174
func ParseObjFilePath(compilerOutLine string) (objFilePath string) {
144175
var endChar string
145-
if index := strings.Index(compilerOutLine, ".ino.cpp.o"); index != -1 {
146-
sketchEndIndex := index + len(".ino.cpp.o")
147-
if sketchEndIndex >= len(compilerOutLine) { // this means the path terminates with the `o` and thus the end character is space
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
148180
endChar = " " // we set it this way to avoid index out of bound
149181
} else {
150-
endChar = string(compilerOutLine[sketchEndIndex])
182+
endChar = string(compilerOutLine[objFileEndIndex])
151183
}
152-
sketchStartIndex := strings.LastIndex(compilerOutLine[:sketchEndIndex], endChar)
153-
// we continue to search if the endChar is preceded by backslash (this means the endChar it's escaped)
154-
for string(compilerOutLine[sketchStartIndex-1]) == `\` {
155-
sketchStartIndex = strings.LastIndex(compilerOutLine[:sketchStartIndex], endChar)
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)
156188
}
157-
objFilePath = compilerOutLine[sketchStartIndex+1 : sketchEndIndex]
189+
objFilePath = compilerOutLine[objFileStartIndex+1 : objFileEndIndex]
158190
return objFilePath
159191
} else {
160192
return ""

0 commit comments

Comments
 (0)