Skip to content

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

//go:build !windows

package utils
package cpp

import (
"errors"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to [email protected].

package utils
package cpp

import (
"golang.org/x/sys/windows"
Expand Down
139 changes: 139 additions & 0 deletions internal/arduino/builder/cpp/depfile.go
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 [email protected].

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"))
if err != nil {
return nil, err
}
rows = f.Map(rows, strings.TrimSpace)
rows = f.Filter(rows, f.NotEquals(""))
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.

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 {
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
}
95 changes: 95 additions & 0 deletions internal/arduino/builder/cpp/depfile_test.go
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 [email protected].

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)
})
}
Loading
Loading