Skip to content

Commit 505bb8a

Browse files
author
Federico Fissore
committed
When an included header is part of an already included library, use that
library The order in which -libraries parameters are specified matters: second one is more important than first one Signed-off-by: Federico Fissore <[email protected]>
1 parent 03fc189 commit 505bb8a

38 files changed

+8558
-17
lines changed

Diff for: src/arduino.cc/builder/includes_to_include_folders.go

+36-7
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func (s *IncludesToIncludeFolders) Run(context map[string]interface{}) error {
5353
if utils.MapHas(context, constants.CTX_IMPORTED_LIBRARIES) {
5454
importedLibraries = context[constants.CTX_IMPORTED_LIBRARIES].([]*types.Library)
5555
}
56-
newlyImportedLibraries, err := resolveLibraries(includes, headerToLibraries, []*types.Platform{platform, actualPlatform}, libraryResolutionResults)
56+
newlyImportedLibraries, err := resolveLibraries(includes, headerToLibraries, importedLibraries, []*types.Platform{platform, actualPlatform}, libraryResolutionResults)
5757
if err != nil {
5858
return utils.WrapError(err)
5959
}
@@ -92,24 +92,27 @@ func resolveIncludeFolders(importedLibraries []*types.Library, buildProperties m
9292
}
9393

9494
//FIXME it's also resolving previously resolved libraries
95-
func resolveLibraries(includes []string, headerToLibraries map[string][]*types.Library, platforms []*types.Platform, libraryResolutionResults map[string]types.LibraryResolutionResult) ([]*types.Library, error) {
95+
func resolveLibraries(includes []string, headerToLibraries map[string][]*types.Library, importedLibraries []*types.Library, platforms []*types.Platform, libraryResolutionResults map[string]types.LibraryResolutionResult) ([]*types.Library, error) {
9696
markImportedLibrary := make(map[*types.Library]bool)
97+
for _, library := range importedLibraries {
98+
markImportedLibrary[library] = true
99+
}
97100
for _, header := range includes {
98101
resolveLibrary(header, headerToLibraries, markImportedLibrary, platforms, libraryResolutionResults)
99102
}
100103

101-
var importedLibraries []*types.Library
104+
var newlyImportedLibraries []*types.Library
102105
for library, _ := range markImportedLibrary {
103-
importedLibraries = append(importedLibraries, library)
106+
newlyImportedLibraries = append(newlyImportedLibraries, library)
104107
}
105108

106-
return importedLibraries, nil
109+
return newlyImportedLibraries, nil
107110
}
108111

109112
func resolveLibrary(header string, headerToLibraries map[string][]*types.Library, markImportedLibrary map[*types.Library]bool, platforms []*types.Platform, libraryResolutionResults map[string]types.LibraryResolutionResult) {
110113
libraries := headerToLibraries[header]
111114

112-
if libraries == nil {
115+
if libraries == nil || len(libraries) == 0 {
113116
return
114117
}
115118

@@ -118,6 +121,10 @@ func resolveLibrary(header string, headerToLibraries map[string][]*types.Library
118121
return
119122
}
120123

124+
if markedLibrariesContainOneOfCandidate(markImportedLibrary, libraries) {
125+
return
126+
}
127+
121128
var library *types.Library
122129

123130
for _, platform := range platforms {
@@ -138,14 +145,36 @@ func resolveLibrary(header string, headerToLibraries map[string][]*types.Library
138145
}
139146

140147
if library == nil {
141-
library = libraries[0]
148+
library = libraries[len(libraries)-1]
142149
}
143150

151+
library = useAlreadyImportedLibraryWithSameNameIfExists(library, markImportedLibrary)
152+
144153
libraryResolutionResults[header] = types.LibraryResolutionResult{Library: library, NotUsedLibraries: filterOutLibraryFrom(libraries, library)}
145154

146155
markImportedLibrary[library] = true
147156
}
148157

158+
func markedLibrariesContainOneOfCandidate(markImportedLibrary map[*types.Library]bool, libraries []*types.Library) bool {
159+
for markedLibrary, _ := range markImportedLibrary {
160+
for _, library := range libraries {
161+
if markedLibrary == library {
162+
return true
163+
}
164+
}
165+
}
166+
return false
167+
}
168+
169+
func useAlreadyImportedLibraryWithSameNameIfExists(library *types.Library, markImportedLibrary map[*types.Library]bool) *types.Library {
170+
for lib, _ := range markImportedLibrary {
171+
if lib.Name == library.Name {
172+
return lib
173+
}
174+
}
175+
return library
176+
}
177+
149178
func filterOutLibraryFrom(libraries []*types.Library, library *types.Library) []*types.Library {
150179
filteredOutLibraries := []*types.Library{}
151180
for _, lib := range libraries {

Diff for: src/arduino.cc/builder/libraries_loader.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ import (
4343
type LibrariesLoader struct{}
4444

4545
func (s *LibrariesLoader) Run(context map[string]interface{}) error {
46-
if !utils.MapHas(context, constants.CTX_LIBRARIES_FOLDERS) {
47-
return nil
46+
librariesFolders := []string{}
47+
if utils.MapHas(context, constants.CTX_LIBRARIES_FOLDERS) {
48+
librariesFolders = context[constants.CTX_LIBRARIES_FOLDERS].([]string)
4849
}
4950

50-
librariesFolders := context[constants.CTX_LIBRARIES_FOLDERS].([]string)
5151
platform := context[constants.CTX_TARGET_PLATFORM].(*types.Platform)
5252
debugLevel := utils.DebugLevel(context)
5353
logger := context[constants.CTX_LOGGER].(i18n.Logger)
@@ -57,11 +57,11 @@ func (s *LibrariesLoader) Run(context map[string]interface{}) error {
5757
return utils.WrapError(err)
5858
}
5959

60-
librariesFolders = appendPathToLibrariesFolders(librariesFolders, filepath.Join(platform.Folder, constants.FOLDER_LIBRARIES))
60+
librariesFolders = prependPathToLibrariesFolders(librariesFolders, filepath.Join(platform.Folder, constants.FOLDER_LIBRARIES))
6161

6262
actualPlatform := context[constants.CTX_ACTUAL_PLATFORM].(*types.Platform)
6363
if actualPlatform != platform {
64-
librariesFolders = appendPathToLibrariesFolders(librariesFolders, filepath.Join(actualPlatform.Folder, constants.FOLDER_LIBRARIES))
64+
librariesFolders = prependPathToLibrariesFolders(librariesFolders, filepath.Join(actualPlatform.Folder, constants.FOLDER_LIBRARIES))
6565
}
6666

6767
librariesFolders, err = utils.AbsolutizePaths(librariesFolders)
@@ -208,7 +208,7 @@ func makeLegacyLibrary(libraryFolder string) (*types.Library, error) {
208208
return library, nil
209209
}
210210

211-
func appendPathToLibrariesFolders(librariesFolders []string, newLibrariesFolder string) []string {
211+
func prependPathToLibrariesFolders(librariesFolders []string, newLibrariesFolder string) []string {
212212
if stat, err := os.Stat(newLibrariesFolder); os.IsNotExist(err) || !stat.IsDir() {
213213
return librariesFolders
214214
}
@@ -217,5 +217,5 @@ func appendPathToLibrariesFolders(librariesFolders []string, newLibrariesFolder
217217
return librariesFolders
218218
}
219219

220-
return append(librariesFolders, newLibrariesFolder)
220+
return append([]string{newLibrariesFolder}, librariesFolders...)
221221
}

Diff for: src/arduino.cc/builder/test/includes_to_include_folders_test.go

+38-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ func TestIncludesToIncludeFoldersANewLibrary(t *testing.T) {
162162
context[constants.CTX_FQBN] = "arduino:avr:leonardo"
163163
context[constants.CTX_SKETCH_LOCATION] = filepath.Join("sketch10", "sketch.ino")
164164
context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600"
165-
context[constants.CTX_LIBRARIES_FOLDERS] = []string{"libraries", "downloaded_libraries"}
165+
context[constants.CTX_LIBRARIES_FOLDERS] = []string{"downloaded_libraries", "libraries"}
166166
context[constants.CTX_VERBOSE] = false
167167

168168
commands := []types.Command{
@@ -224,3 +224,40 @@ func TestIncludesToIncludeFoldersDuplicateLibs(t *testing.T) {
224224
require.Equal(t, "SPI", importedLibraries[0].Name)
225225
require.Equal(t, Abs(t, filepath.Join("user_hardware", "my_avr_platform", "avr", "libraries", "SPI")), importedLibraries[0].SrcFolder)
226226
}
227+
228+
func TestIncludesToIncludeFoldersDuplicateLibs2(t *testing.T) {
229+
DownloadCoresAndToolsAndLibraries(t)
230+
231+
context := make(map[string]interface{})
232+
233+
buildPath := SetupBuildPath(t, context)
234+
defer os.RemoveAll(buildPath)
235+
236+
context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware", "downloaded_board_manager_stuff"}
237+
context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools", "downloaded_board_manager_stuff"}
238+
context[constants.CTX_FQBN] = "arduino:samd:arduino_zero_native"
239+
context[constants.CTX_SKETCH_LOCATION] = filepath.Join("sketch_usbhost", "sketch_usbhost.ino")
240+
context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600"
241+
context[constants.CTX_LIBRARIES_FOLDERS] = []string{"libraries", "downloaded_libraries"}
242+
243+
commands := []types.Command{
244+
&builder.SetupHumanLoggerIfMissing{},
245+
246+
&builder.ContainerSetupHardwareToolsLibsSketchAndProps{},
247+
248+
&builder.ContainerMergeCopySketchFiles{},
249+
250+
&builder.ContainerFindIncludes{},
251+
}
252+
253+
for _, command := range commands {
254+
err := command.Run(context)
255+
NoError(t, err)
256+
}
257+
258+
importedLibraries := context[constants.CTX_IMPORTED_LIBRARIES].([]*types.Library)
259+
sort.Sort(ByLibraryName(importedLibraries))
260+
require.Equal(t, 1, len(importedLibraries))
261+
require.Equal(t, "USBHost", importedLibraries[0].Name)
262+
require.Equal(t, Abs(t, filepath.Join("libraries", "USBHost", "src")), importedLibraries[0].SrcFolder)
263+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#######################################
2+
# Syntax Coloring Map For USBHost
3+
#######################################
4+
5+
#######################################
6+
# Datatypes (KEYWORD1)
7+
#######################################
8+
9+
MouseController KEYWORD1
10+
USBHost KEYWORD1
11+
KeyboardController KEYWORD1
12+
13+
#######################################
14+
# Methods and Functions (KEYWORD2)
15+
#######################################
16+
17+
Task KEYWORD2
18+
mouseMoved KEYWORD2
19+
mouseDragged KEYWORD2
20+
mousePressed KEYWORD2
21+
mouseReleased KEYWORD2
22+
getXChange KEYWORD2
23+
getYChange KEYWORD2
24+
getButton KEYWORD2
25+
keyPressed KEYWORD2
26+
keyReleased KEYWORD2
27+
getModifiers KEYWORD2
28+
getKey KEYWORD2
29+
getOemKey KEYWORD2
30+
31+
32+
#######################################
33+
# Constants (LITERAL1)
34+
#######################################
35+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
name=USBHost
2+
version=1.0
3+
author=Arduino
4+
maintainer=Arduino <[email protected]>
5+
sentence=Allows the communication with USB peripherals like mice, keyboards, and thumbdrives. For Arduino Due and Zero.
6+
paragraph=The USBHost library allows the board to appear as a USB host, enabling it to communicate with peripherals like USB mice and keyboards. USBHost does not support devices that are connected through USB hubs. This includes some keyboards that have an internal hub.
7+
url=http://arduino.cc/en/Reference/USBHost
8+
architectures=samd
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
Copyright (c) 2012 Arduino. All right reserved.
3+
4+
This library is free software; you can redistribute it and/or
5+
modify it under the terms of the GNU Lesser General Public
6+
License as published by the Free Software Foundation; either
7+
version 2.1 of the License, or (at your option) any later version.
8+
9+
This library is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12+
See the GNU Lesser General Public License for more details.
13+
14+
You should have received a copy of the GNU Lesser General Public
15+
License along with this library; if not, write to the Free Software
16+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17+
*/
18+
19+
#include <KeyboardController.h>
20+
21+
extern "C" {
22+
void __keyboardControllerEmptyCallback() { }
23+
}
24+
25+
void keyPressed() __attribute__ ((weak, alias("__keyboardControllerEmptyCallback")));
26+
void keyReleased() __attribute__ ((weak, alias("__keyboardControllerEmptyCallback")));
27+
28+
void KeyboardController::OnKeyDown(uint8_t _mod, uint8_t _oemKey) {
29+
modifiers = _mod;
30+
keyOem = _oemKey;
31+
key = OemToAscii(_mod, _oemKey);
32+
keyPressed();
33+
}
34+
35+
void KeyboardController::OnKeyUp(uint8_t _mod, uint8_t _oemKey) {
36+
modifiers = _mod;
37+
keyOem = _oemKey;
38+
key = OemToAscii(_mod, _oemKey);
39+
keyReleased();
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
Copyright (c) 2012 Arduino. All right reserved.
3+
4+
This library is free software; you can redistribute it and/or
5+
modify it under the terms of the GNU Lesser General Public
6+
License as published by the Free Software Foundation; either
7+
version 2.1 of the License, or (at your option) any later version.
8+
9+
This library is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12+
See the GNU Lesser General Public License for more details.
13+
14+
You should have received a copy of the GNU Lesser General Public
15+
License along with this library; if not, write to the Free Software
16+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17+
*/
18+
19+
#ifndef KEYBOARD_CONTROLLER_H
20+
#define KEYBOARD_CONTROLLER_H
21+
22+
#include <hidboot.h>
23+
24+
enum KeyboardModifiers {
25+
LeftCtrl = 1,
26+
LeftShift = 2,
27+
Alt = 4,
28+
LeftCmd = 8,
29+
RightCtrl = 16,
30+
RightShift = 32,
31+
AltGr = 64,
32+
RightCmd = 128
33+
};
34+
35+
class KeyboardController : public KeyboardReportParser {
36+
public:
37+
KeyboardController(USBHost &usb) : hostKeyboard(&usb), key(0), keyOem(0), modifiers(0) {
38+
hostKeyboard.SetReportParser(0, this);
39+
};
40+
41+
uint8_t getKey() { return key; };
42+
uint8_t getModifiers() { return modifiers; };
43+
uint8_t getOemKey() { return keyOem; };
44+
45+
protected:
46+
virtual void OnKeyDown(uint8_t mod, uint8_t key);
47+
virtual void OnKeyUp(uint8_t mod, uint8_t key);
48+
49+
private:
50+
HIDBoot<HID_PROTOCOL_KEYBOARD> hostKeyboard;
51+
uint8_t key, keyOem, modifiers;
52+
};
53+
54+
#endif

0 commit comments

Comments
 (0)