Skip to content

Improved lib detection: check for matching name in library.properties #1300

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 1 commit into from
May 26, 2021
Merged
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
13 changes: 8 additions & 5 deletions arduino/libraries/librariesresolver/cpp.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ func computePriority(lib *libraries.Library, header, arch string) int {
header = strings.TrimSuffix(header, filepath.Ext(header))
header = simplify(header)
name := simplify(lib.Name)
realName := simplify(lib.RealName)

priority := 0

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

if name == header {
if realName == header && name == header {
priority += 600
} else if realName == header || name == header {
priority += 500
} else if name == header+"-master" {
} else if realName == header+"-master" || name == header+"-master" {
priority += 400
} else if strings.HasPrefix(name, header) {
} else if strings.HasPrefix(realName, header) || strings.HasPrefix(name, header) {
priority += 300
} else if strings.HasSuffix(name, header) {
} else if strings.HasSuffix(realName, header) || strings.HasSuffix(name, header) {
priority += 200
} else if strings.Contains(name, header) {
} else if strings.Contains(realName, header) || strings.Contains(name, header) {
priority += 100
}

Expand Down
15 changes: 15 additions & 0 deletions arduino/libraries/librariesresolver/cpp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,18 @@ func TestCppHeaderResolver(t *testing.T) {
require.Equal(t, "Calculus Unified Lib", resolve("calculus_lib.h", l6, l7))
require.Equal(t, "Calculus Unified Lib", resolve("calculus_lib.h", l7, l6))
}

func TestCppHeaderResolverWithLibrariesInStrangeDirectoryNames(t *testing.T) {
resolver := NewCppResolver()
librarylist := libraries.List{}
librarylist.Add(&libraries.Library{Name: "onewire_2_3_4", RealName: "OneWire", Architectures: []string{"*"}})
librarylist.Add(&libraries.Library{Name: "onewireng_2_3_4", RealName: "OneWireNg", Architectures: []string{"avr"}})
resolver.headers["OneWire.h"] = librarylist
require.Equal(t, "onewire_2_3_4", resolver.ResolveFor("OneWire.h", "avr").Name)

librarylist2 := libraries.List{}
librarylist2.Add(&libraries.Library{Name: "OneWire", RealName: "OneWire", Architectures: []string{"*"}})
librarylist2.Add(&libraries.Library{Name: "onewire_2_3_4", RealName: "OneWire", Architectures: []string{"avr"}})
resolver.headers["OneWire.h"] = librarylist2
require.Equal(t, "OneWire", resolver.ResolveFor("OneWire.h", "avr").Name)
}
35 changes: 31 additions & 4 deletions test/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -998,6 +998,33 @@ def test_recompile_with_different_library(run_command, data_dir):
assert f"Using previously compiled file: {obj_path}" not in res.stdout


def test_compile_with_conflicting_libraries_include(run_command, data_dir, copy_sketch):
assert run_command("update")

assert run_command("core install arduino:[email protected]")

# Install conflicting libraries
git_url = "https://github.com/pstolarz/OneWireNg.git"
one_wire_ng_lib_path = Path(data_dir, "libraries", "onewireng_0_8_1")
assert Repo.clone_from(git_url, one_wire_ng_lib_path, multi_options=["-b 0.8.1"])

git_url = "https://github.com/PaulStoffregen/OneWire.git"
one_wire_lib_path = Path(data_dir, "libraries", "onewire_2_3_5")
assert Repo.clone_from(git_url, one_wire_lib_path, multi_options=["-b v2.3.5"])

sketch_path = copy_sketch("sketch_with_conflicting_libraries_include")
fqbn = "arduino:avr:uno"

res = run_command(f"compile -b {fqbn} {sketch_path} --verbose")
assert res.ok
expected_output = [
'Multiple libraries were found for "OneWire.h"',
f" Used: {one_wire_lib_path}",
f" Not used: {one_wire_ng_lib_path}",
]
assert "\n".join(expected_output) in res.stdout


def test_compile_with_invalid_build_options_json(run_command, data_dir):
assert run_command("update")

Expand Down Expand Up @@ -1051,7 +1078,7 @@ def test_compile_with_esp32_bundled_libraries(run_command, data_dir, copy_sketch
fqbn = "esp32:esp32:esp32"

res = run_command(f"compile -b {fqbn} {sketch_path} --verbose")
assert res.ok
assert res.failed

core_bundled_lib_path = Path(data_dir, "packages", "esp32", "hardware", "esp32", core_version, "libraries", "SD")
cli_installed_lib_path = Path(data_dir, "libraries", "SD")
Expand All @@ -1060,7 +1087,7 @@ def test_compile_with_esp32_bundled_libraries(run_command, data_dir, copy_sketch
f" Used: {core_bundled_lib_path}",
f" Not used: {cli_installed_lib_path}",
]
assert "\n".join(expected_output) in res.stdout
assert "\n".join(expected_output) not in res.stdout


def test_compile_with_esp8266_bundled_libraries(run_command, data_dir, copy_sketch):
Expand Down Expand Up @@ -1090,7 +1117,7 @@ def test_compile_with_esp8266_bundled_libraries(run_command, data_dir, copy_sket
fqbn = "esp8266:esp8266:generic"

res = run_command(f"compile -b {fqbn} {sketch_path} --verbose")
assert res.ok
assert res.failed

core_bundled_lib_path = Path(
data_dir, "packages", "esp8266", "hardware", "esp8266", core_version, "libraries", "SD"
Expand All @@ -1101,4 +1128,4 @@ def test_compile_with_esp8266_bundled_libraries(run_command, data_dir, copy_sket
f" Used: {core_bundled_lib_path}",
f" Not used: {cli_installed_lib_path}",
]
assert "\n".join(expected_output) in res.stdout
assert "\n".join(expected_output) not in res.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "OneWire.h"

void setup() {
}

void loop() {
}