-
-
Notifications
You must be signed in to change notification settings - Fork 441
refactoring: implement a proper dependency (.d) file parser #2972
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,7 +15,7 @@ | |
|
|
||
| //go:build !windows | ||
|
|
||
| package utils | ||
| package cpp | ||
|
|
||
| import ( | ||
| "errors" | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| // This file is part of arduino-cli. | ||
| // | ||
| // Copyright 2025 ARDUINO SA (http://www.arduino.cc/) | ||
| // | ||
| // This software is released under the GNU General Public License version 3, | ||
| // which covers the main part of arduino-cli. | ||
| // The terms of this license can be found at: | ||
| // https://www.gnu.org/licenses/gpl-3.0.en.html | ||
| // | ||
| // You can be released from the requirements of the above licenses by purchasing | ||
| // a commercial license. Buying such a license is mandatory if you want to | ||
| // modify or otherwise use the software for commercial activities involving the | ||
| // Arduino software without disclosing the source code of your own applications. | ||
| // To purchase a commercial license, send an email to license@arduino.cc. | ||
|
|
||
| package cpp | ||
|
|
||
| import ( | ||
| "errors" | ||
| "runtime" | ||
| "strings" | ||
| "unicode" | ||
|
|
||
| "github.com/arduino/go-paths-helper" | ||
| "go.bug.st/f" | ||
| ) | ||
|
|
||
| // Dependencies represents the dependencies of a source file. | ||
| type Dependencies struct { | ||
| ObjectFile string | ||
| Dependencies []string | ||
| } | ||
|
|
||
| // ReadDepFile reads a dependency file and returns the dependencies. | ||
| // It may return nil if the dependency file is empty. | ||
| func ReadDepFile(depFilePath *paths.Path) (*Dependencies, error) { | ||
| depFileData, err := depFilePath.ReadFile() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| if runtime.GOOS == "windows" { | ||
| // This is required because on Windows we don't know which encoding is used | ||
| // by gcc to write the dep file (it could be UTF-8 or any of the Windows | ||
| // ANSI mappings). | ||
| if decoded, err := convertAnsiBytesToString(depFileData); err == nil { | ||
| if res, err := readDepFile(decoded); err == nil && res != nil { | ||
| return res, nil | ||
| } | ||
| } | ||
| // Fallback to UTF-8... | ||
| } | ||
|
|
||
| return readDepFile(string(depFileData)) | ||
| } | ||
|
|
||
| func readDepFile(depFile string) (*Dependencies, error) { | ||
| rows, err := unescapeAndSplit(strings.ReplaceAll(depFile, "\r\n", "\n")) | ||
cmaglie marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| rows = f.Map(rows, strings.TrimSpace) | ||
| rows = f.Filter(rows, f.NotEquals("")) | ||
cmaglie marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if len(rows) == 0 { | ||
| return &Dependencies{}, nil | ||
| } | ||
|
|
||
| // The first line of the depfile contains the path to the object file to generate. | ||
| // The second line of the depfile contains the path to the source file. | ||
| // All subsequent lines contain the header files necessary to compile the object file. | ||
cmaglie marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| if !strings.HasSuffix(rows[0], ":") { | ||
| return nil, errors.New("no colon in first item of depfile") | ||
| } | ||
| res := &Dependencies{ | ||
| ObjectFile: strings.TrimSuffix(rows[0], ":"), | ||
| Dependencies: rows[1:], | ||
| } | ||
| return res, nil | ||
| } | ||
|
|
||
| func unescapeAndSplit(s string) ([]string, error) { | ||
| var res []string | ||
| backslash := false | ||
| dollar := false | ||
| current := strings.Builder{} | ||
| for _, c := range s { | ||
| if backslash { | ||
cmaglie marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| switch c { | ||
| case ' ': | ||
| current.WriteRune(' ') | ||
| case '#': | ||
| current.WriteRune('#') | ||
| case '\\': | ||
| current.WriteRune('\\') | ||
| case '\n': | ||
| // ignore | ||
| default: | ||
| current.WriteRune('\\') | ||
| current.WriteRune(c) | ||
| } | ||
| backslash = false | ||
| continue | ||
| } | ||
| if dollar { | ||
| if c != '$' { | ||
| return nil, errors.New("invalid dollar sequence: $" + string(c)) | ||
| } | ||
| current.WriteByte('$') | ||
| dollar = false | ||
| continue | ||
| } | ||
|
|
||
| if c == '\\' { | ||
| backslash = true | ||
| continue | ||
| } | ||
| if c == '$' { | ||
| dollar = true | ||
| continue | ||
| } | ||
|
|
||
| if unicode.IsSpace(c) { | ||
| if current.Len() > 0 { | ||
| res = append(res, current.String()) | ||
| current.Reset() | ||
| } | ||
| continue | ||
| } | ||
| current.WriteRune(c) | ||
| } | ||
| if dollar { | ||
| return nil, errors.New("unclosed escape sequence at end of depfile") | ||
| } | ||
| if current.Len() > 0 { | ||
| res = append(res, current.String()) | ||
| } | ||
| return res, nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| // This file is part of arduino-cli. | ||
| // | ||
| // Copyright 2025 ARDUINO SA (http://www.arduino.cc/) | ||
| // | ||
| // This software is released under the GNU General Public License version 3, | ||
| // which covers the main part of arduino-cli. | ||
| // The terms of this license can be found at: | ||
| // https://www.gnu.org/licenses/gpl-3.0.en.html | ||
| // | ||
| // You can be released from the requirements of the above licenses by purchasing | ||
| // a commercial license. Buying such a license is mandatory if you want to | ||
| // modify or otherwise use the software for commercial activities involving the | ||
| // Arduino software without disclosing the source code of your own applications. | ||
| // To purchase a commercial license, send an email to license@arduino.cc. | ||
|
|
||
| package cpp | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/arduino/go-paths-helper" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestDepFileReader(t *testing.T) { | ||
| t.Run("0", func(t *testing.T) { | ||
| deps, err := ReadDepFile(paths.New("testdata", "depcheck.0.d")) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, deps) | ||
| require.Len(t, deps.Dependencies, 302) | ||
| require.Equal(t, "sketch.ino.cpp.o", deps.ObjectFile) | ||
| require.Equal(t, "/home/megabug/Arduino/sketch/build/sketch/sketch.ino.cpp.merged", deps.Dependencies[0]) | ||
| require.Equal(t, "/home/megabug/.arduino15/packages/arduino/hardware/zephyr/0.10.0-rc.10/variants/b_u585i_iot02a_stm32u585xx/llext-edk/include/zephyr/include/generated/zephyr/autoconf.h", deps.Dependencies[1]) | ||
| require.Equal(t, "/home/megabug/.arduino15/packages/arduino/hardware/zephyr/0.10.0-rc.10/variants/b_u585i_iot02a_stm32u585xx/llext-edk/include/zephyr/include/zephyr/toolchain/zephyr_stdint.h", deps.Dependencies[2]) | ||
| require.Equal(t, "/home/megabug/.arduino15/packages/arduino/hardware/zephyr/0.10.0-rc.10/libraries/Arduino_RPCLite/src/dispatcher.h", deps.Dependencies[301]) | ||
| }) | ||
| t.Run("1", func(t *testing.T) { | ||
| deps, err := ReadDepFile(paths.New("testdata", "depcheck.1.d")) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, deps) | ||
| require.Equal(t, "sketch.ino.o", deps.ObjectFile) | ||
| require.Len(t, deps.Dependencies, 302) | ||
| require.Equal(t, "/home/megabug/Arduino/sketch/build/sketch/sketch.ino.cpp", deps.Dependencies[0]) | ||
| require.Equal(t, "/home/megabug/.arduino15/packages/arduino/hardware/zephyr/0.10.0-rc.10/variants/b_u585i_iot02a_stm32u585xx/llext-edk/include/zephyr/include/generated/zephyr/autoconf.h", deps.Dependencies[1]) | ||
| require.Equal(t, "/home/megabug/.arduino15/packages/arduino/hardware/zephyr/0.10.0-rc.10/variants/b_u585i_iot02a_stm32u585xx/llext-edk/include/zephyr/include/zephyr/toolchain/zephyr_stdint.h", deps.Dependencies[2]) | ||
| require.Equal(t, "/home/megabug/.arduino15/packages/arduino/hardware/zephyr/0.10.0-rc.10/libraries/Arduino_RPCLite/src/dispatcher.h", deps.Dependencies[301]) | ||
| }) | ||
| t.Run("2", func(t *testing.T) { | ||
| deps, err := ReadDepFile(paths.New("testdata", "depcheck.2.d")) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, deps) | ||
| require.Equal(t, "ske tch.ino.cpp.o", deps.ObjectFile) | ||
| require.Len(t, deps.Dependencies, 302) | ||
| require.Equal(t, "/home/megabug/Arduino/ske tch/build/sketch/ske tch.ino.cpp.merged", deps.Dependencies[0]) | ||
| require.Equal(t, "/home/megabug/.arduino15/packages/arduino/hardware/zephyr/0.10.0-rc.10/variants/b_u585i_iot02a_stm32u585xx/llext-edk/include/zephyr/include/generated/zephyr/autoconf.h", deps.Dependencies[1]) | ||
| require.Equal(t, "/home/megabug/.arduino15/packages/arduino/hardware/zephyr/0.10.0-rc.10/variants/b_u585i_iot02a_stm32u585xx/llext-edk/include/zephyr/include/zephyr/toolchain/zephyr_stdint.h", deps.Dependencies[2]) | ||
| require.Equal(t, "/home/megabug/.arduino15/packages/arduino/hardware/zephyr/0.10.0-rc.10/libraries/Arduino_RPCLite/src/dispatcher.h", deps.Dependencies[301]) | ||
| }) | ||
| t.Run("3", func(t *testing.T) { | ||
| deps, err := ReadDepFile(paths.New("testdata", "depcheck.3.d")) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, deps) | ||
| require.Equal(t, "myfile.o", deps.ObjectFile) | ||
| require.Len(t, deps.Dependencies, 3) | ||
| require.Equal(t, "/some/path\\twith/backslash and spaces/file.cpp", deps.Dependencies[0]) | ||
| require.Equal(t, "/some/other$/path#/file.h", deps.Dependencies[1]) | ||
| require.Equal(t, "/yet/ano\\ther/path/file.h", deps.Dependencies[2]) | ||
| }) | ||
| t.Run("4", func(t *testing.T) { | ||
| deps, err := ReadDepFile(paths.New("testdata", "depcheck.4.d")) | ||
| require.EqualError(t, err, "invalid dollar sequence: $a") | ||
| require.Nil(t, deps) | ||
| }) | ||
| t.Run("6", func(t *testing.T) { | ||
| deps, err := ReadDepFile(paths.New("testdata", "depcheck.6.d")) | ||
| require.EqualError(t, err, "unclosed escape sequence at end of depfile") | ||
| require.Nil(t, deps) | ||
| }) | ||
| t.Run("7", func(t *testing.T) { | ||
| deps, err := ReadDepFile(paths.New("testdata", "depcheck.7.d")) | ||
| require.EqualError(t, err, "no colon in first item of depfile") | ||
| require.Nil(t, deps) | ||
| }) | ||
| t.Run("8", func(t *testing.T) { | ||
| deps, err := ReadDepFile(paths.New("testdata", "depcheck.8.d")) | ||
| require.NoError(t, err) | ||
| require.Nil(t, deps.Dependencies) | ||
| require.Empty(t, deps.ObjectFile) | ||
| }) | ||
| t.Run("9", func(t *testing.T) { | ||
| deps, err := ReadDepFile(paths.New("testdata", "nonexistent.d")) | ||
| require.Error(t, err) | ||
| require.Nil(t, deps) | ||
| }) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.