Skip to content

Commit 989ef54

Browse files
committed
Fixed charset handling in reader for dep files
1 parent 210478f commit 989ef54

File tree

1 file changed

+57
-42
lines changed
  • internal/arduino/builder/internal/utils

1 file changed

+57
-42
lines changed

Diff for: internal/arduino/builder/internal/utils/utils.go

+57-42
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package utils
1717

1818
import (
1919
"os"
20+
"runtime"
2021
"strings"
2122
"unicode"
2223

@@ -96,57 +97,71 @@ func ObjFileIsUpToDate(sourceFile, objectFile, dependencyFile *paths.Path) (bool
9697
return f.Filter(rows, f.NotEquals("")), nil
9798
}
9899

99-
rows, err := readDepFileWithEncoding(nil)
100-
if err != nil {
101-
logrus.Debugf("Could not read dependency file: %s", dependencyFile)
102-
return false, err
103-
}
104-
if len(rows) == 0 {
105-
return true, nil
106-
}
107-
108-
firstRow := rows[0]
109-
if !strings.HasSuffix(firstRow, ":") {
110-
logrus.Debugf("No colon in first line of depfile")
111-
return false, nil
112-
}
113-
objFileInDepFile := firstRow[:len(firstRow)-1]
114-
if objFileInDepFile != objectFile.String() {
115-
logrus.Debugf("Depfile is about different object file: %v", objFileInDepFile)
116-
return false, nil
117-
}
118-
119-
// The first line of the depfile contains the path to the object file to generate.
120-
// The second line of the depfile contains the path to the source file.
121-
// All subsequent lines contain the header files necessary to compile the object file.
122-
123-
// If we don't do this check it might happen that trying to compile a source file
124-
// that has the same name but a different path wouldn't recreate the object file.
125-
if sourceFile.String() != strings.Trim(rows[1], " ") {
126-
logrus.Debugf("Depfile is about different source file: %v", strings.Trim(rows[1], " "))
127-
return false, nil
128-
}
100+
checkDepFileWithEncoding := func(mapping *charmap.Charmap) (bool, error) {
101+
rows, err := readDepFileWithEncoding(mapping)
102+
if err != nil {
103+
logrus.Debugf("Could not read dependency file: %s", dependencyFile)
104+
return false, err
105+
}
106+
if len(rows) == 0 {
107+
return true, nil
108+
}
129109

130-
rows = rows[1:]
131-
for _, row := range rows {
132-
depStat, err := os.Stat(row)
133-
if err != nil && !os.IsNotExist(err) {
134-
// There is probably a parsing error of the dep file
135-
// Ignore the error and trigger a full rebuild anyway
136-
logrus.WithError(err).Debugf("Failed to read: %v", row)
110+
firstRow := rows[0]
111+
if !strings.HasSuffix(firstRow, ":") {
112+
logrus.Debugf("No colon in first line of depfile")
137113
return false, nil
138114
}
139-
if os.IsNotExist(err) {
140-
logrus.Debugf("Not found: %v", row)
115+
objFileInDepFile := firstRow[:len(firstRow)-1]
116+
if objFileInDepFile != objectFile.String() {
117+
logrus.Debugf("Depfile is about different object file: %v", objFileInDepFile)
141118
return false, nil
142119
}
143-
if depStat.ModTime().After(objectFileStat.ModTime()) {
144-
logrus.Debugf("%v newer than %v", row, objectFile)
120+
121+
// The first line of the depfile contains the path to the object file to generate.
122+
// The second line of the depfile contains the path to the source file.
123+
// All subsequent lines contain the header files necessary to compile the object file.
124+
125+
// If we don't do this check it might happen that trying to compile a source file
126+
// that has the same name but a different path wouldn't recreate the object file.
127+
if sourceFile.String() != strings.Trim(rows[1], " ") {
128+
logrus.Debugf("Depfile is about different source file: %v", strings.Trim(rows[1], " "))
145129
return false, nil
146130
}
131+
132+
rows = rows[1:]
133+
for _, row := range rows {
134+
depStat, err := os.Stat(row)
135+
if err != nil && !os.IsNotExist(err) {
136+
// There is probably a parsing error of the dep file
137+
// Ignore the error and trigger a full rebuild anyway
138+
logrus.WithError(err).Debugf("Failed to read: %v", row)
139+
return false, nil
140+
}
141+
if os.IsNotExist(err) {
142+
logrus.Debugf("Not found: %v", row)
143+
return false, nil
144+
}
145+
if depStat.ModTime().After(objectFileStat.ModTime()) {
146+
logrus.Debugf("%v newer than %v", row, objectFile)
147+
return false, nil
148+
}
149+
}
150+
151+
return true, nil
147152
}
148153

149-
return true, nil
154+
// This is required because on Windows we don't know which enconding is used
155+
// by gcc to write the dep file (it could be UTF-8 or any of the Windows
156+
// default mappings. Would ISO8859_1 be enough? Or we should differentiate the
157+
// various ISO8859_XX based on selected locale? Only time will tell...).
158+
if runtime.GOOS == "windows" {
159+
if upToDate, err := checkDepFileWithEncoding(charmap.ISO8859_1); err != nil {
160+
return upToDate, nil
161+
}
162+
// Fallback to UTF-8...
163+
}
164+
return checkDepFileWithEncoding(nil)
150165
}
151166

152167
func removeEndingBackSlash(s string) string {

0 commit comments

Comments
 (0)