Skip to content

Commit f8c0dc9

Browse files
cmagliesilvanocerza
andcommitted
Improved lib detection: check for matching name in library.properties (#1276)
* Improved lib detection: check for matching name in library.properties The library may be stored in a directory that doesn't match the library name, for example we had a case in the wild where the directories: libraries/onewire_2_3_4/... libraries/onewireng_1_2_3/... were used instead of: libraries/OneWire/... libraries/OneWireNg/... this lead to incorrect selection of onewireng_1_2_3 when using OneWire.h (because the OneWireNg had an architecture=avr that had priority over the architecture=* of onewire_2_3_4). This commit will restore priority straight. * Added test for lib resolve improvement * Lib discovery: always prefer libraries with the correct directory name * [skip changelog] Add integration test Co-authored-by: Silvano Cerza <[email protected]>
1 parent 73e5d36 commit f8c0dc9

File tree

4 files changed

+55
-5
lines changed

4 files changed

+55
-5
lines changed

Diff for: arduino/libraries/librariesresolver/cpp.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ func computePriority(lib *libraries.Library, header, arch string) int {
121121
header = strings.TrimSuffix(header, filepath.Ext(header))
122122
header = simplify(header)
123123
name := simplify(lib.Name)
124+
realName := simplify(lib.RealName)
124125

125126
priority := 0
126127

@@ -137,15 +138,17 @@ func computePriority(lib *libraries.Library, header, arch string) int {
137138
priority += 0
138139
}
139140

140-
if name == header {
141+
if realName == header && name == header {
142+
priority += 600
143+
} else if realName == header || name == header {
141144
priority += 500
142-
} else if name == header+"-master" {
145+
} else if realName == header+"-master" || name == header+"-master" {
143146
priority += 400
144-
} else if strings.HasPrefix(name, header) {
147+
} else if strings.HasPrefix(realName, header) || strings.HasPrefix(name, header) {
145148
priority += 300
146-
} else if strings.HasSuffix(name, header) {
149+
} else if strings.HasSuffix(realName, header) || strings.HasSuffix(name, header) {
147150
priority += 200
148-
} else if strings.Contains(name, header) {
151+
} else if strings.Contains(realName, header) || strings.Contains(name, header) {
149152
priority += 100
150153
}
151154

Diff for: arduino/libraries/librariesresolver/cpp_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,18 @@ func TestCppHeaderResolver(t *testing.T) {
143143
require.Equal(t, "Calculus Unified Lib", resolve("calculus_lib.h", l6, l7))
144144
require.Equal(t, "Calculus Unified Lib", resolve("calculus_lib.h", l7, l6))
145145
}
146+
147+
func TestCppHeaderResolverWithLibrariesInStrangeDirectoryNames(t *testing.T) {
148+
resolver := NewCppResolver()
149+
librarylist := libraries.List{}
150+
librarylist.Add(&libraries.Library{Name: "onewire_2_3_4", RealName: "OneWire", Architectures: []string{"*"}})
151+
librarylist.Add(&libraries.Library{Name: "onewireng_2_3_4", RealName: "OneWireNg", Architectures: []string{"avr"}})
152+
resolver.headers["OneWire.h"] = librarylist
153+
require.Equal(t, "onewire_2_3_4", resolver.ResolveFor("OneWire.h", "avr").Name)
154+
155+
librarylist2 := libraries.List{}
156+
librarylist2.Add(&libraries.Library{Name: "OneWire", RealName: "OneWire", Architectures: []string{"*"}})
157+
librarylist2.Add(&libraries.Library{Name: "onewire_2_3_4", RealName: "OneWire", Architectures: []string{"avr"}})
158+
resolver.headers["OneWire.h"] = librarylist2
159+
require.Equal(t, "OneWire", resolver.ResolveFor("OneWire.h", "avr").Name)
160+
}

Diff for: test/test_compile.py

+25
Original file line numberDiff line numberDiff line change
@@ -996,3 +996,28 @@ def test_recompile_with_different_library(run_command, data_dir):
996996
assert res.ok
997997
obj_path = build_dir / "libraries" / "WiFi101" / "WiFi.cpp.o"
998998
assert f"Using previously compiled file: {obj_path}" not in res.stdout
999+
1000+
1001+
def test_compile_with_conflicting_libraries_include(run_command, data_dir, copy_sketch):
1002+
assert run_command("update")
1003+
1004+
assert run_command("core install arduino:[email protected]")
1005+
1006+
# Install conflicting libraries
1007+
git_url = "https://github.com/pstolarz/OneWireNg.git"
1008+
one_wire_ng_lib_path = Path(data_dir, "libraries", "onewireng_0_8_1")
1009+
assert Repo.clone_from(git_url, one_wire_ng_lib_path, multi_options=["-b 0.8.1"])
1010+
1011+
git_url = "https://github.com/PaulStoffregen/OneWire.git"
1012+
one_wire_lib_path = Path(data_dir, "libraries", "onewire_2_3_5")
1013+
assert Repo.clone_from(git_url, one_wire_lib_path, multi_options=["-b v2.3.5"])
1014+
1015+
sketch_path = copy_sketch("sketch_with_conflicting_libraries_include")
1016+
fqbn = "arduino:avr:uno"
1017+
1018+
res = run_command(f"compile -b {fqbn} {sketch_path} --verbose")
1019+
assert res.ok
1020+
lines = [l.strip() for l in res.stdout.splitlines()]
1021+
assert 'Multiple libraries were found for "OneWire.h"' in lines
1022+
assert f"Used: {one_wire_lib_path}" in lines
1023+
assert f"Not used: {one_wire_ng_lib_path}" in lines
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "OneWire.h"
2+
3+
void setup() {
4+
}
5+
6+
void loop() {
7+
}

0 commit comments

Comments
 (0)