Skip to content

Commit 54b5202

Browse files
committed
My patches to ladislas' PR sudar#341 auto-lib
1 parent eba20c7 commit 54b5202

File tree

3 files changed

+131
-6
lines changed

3 files changed

+131
-6
lines changed

Arduino.mk

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -794,7 +794,7 @@ ifeq ($(strip $(CHK_SOURCES)),)
794794
$(call show_config_info,No .pde or .ino files found. If you are compiling .c or .cpp files then you need to explicitly include Arduino header files)
795795
else
796796
#TODO: Support more than one file. https://github.com/sudar/Arduino-Makefile/issues/49
797-
$(error Need exactly one .pde or .ino file. This makefile doesn't support multiple .ino/.pde files yet)
797+
$(error Need exactly one .pde or .ino file. This makefile doesn\'t support multiple .ino/.pde files yet)
798798
endif
799799
endif
800800

@@ -821,6 +821,22 @@ else
821821
$(call show_config_info,NO_CORE set so core library will not be built,[MANUAL])
822822
endif
823823

824+
########################################################################
825+
# Automatically find the libraries needed to compile the sketch
826+
827+
ARD_LIB_DETECTION := $(shell which ard-lib-detection 2> /dev/null)
828+
ifndef ARD_LIB_DETECTION
829+
ARD_LIB_DETECTION := $(ARDMK_DIR)/bin/ard-lib-detection
830+
endif
831+
832+
ifndef SKETCH_LIBS
833+
SKETCH_LIBS := $(shell $(ARD_LIB_DETECTION) $(USER_LIB_PATH) | sed -ne 's/SKETCH_LIBS \(.*\) /\1/p')
834+
endif
835+
836+
ifndef SKETCH_LIBS_DEPS
837+
SKETCH_LIBS_DEPS := $(shell $(ARD_LIB_DETECTION) $(USER_LIB_PATH) | sed -ne 's/SKETCH_LIBS_DEPS \(.*\) /\1/p')
838+
endif
839+
824840
########################################################################
825841
# Determine ARDUINO_LIBS automatically
826842

@@ -932,7 +948,10 @@ get_library_files = $(if $(and $(wildcard $(1)/src), $(wildcard $(1)/library.pr
932948
$(wildcard $(1)/*.$(2) $(1)/utility/*.$(2)))
933949

934950
# General arguments
935-
USER_LIBS := $(sort $(wildcard $(patsubst %,$(USER_LIB_PATH)/%,$(ARDUINO_LIBS))))
951+
USER_LIBS := $(wildcard $(patsubst %,$(USER_LIB_PATH)/%,$(ARDUINO_LIBS))) \
952+
$(wildcard $(patsubst %,$(USER_LIB_PATH)/%,$(SKETCH_LIBS))) \
953+
$(wildcard $(patsubst %,$(USER_LIB_PATH)/%,$(SKETCH_LIBS_DEPS)))
954+
936955
USER_LIB_NAMES := $(patsubst $(USER_LIB_PATH)/%,%,$(USER_LIBS))
937956

938957
# Let user libraries override system ones.
@@ -1107,13 +1126,27 @@ else
11071126
$(call show_config_info,Size utility: Basic (not AVR-aware),[AUTODETECTED])
11081127
endif
11091128

1110-
ifneq (,$(strip $(ARDUINO_LIBS)))
1129+
ifneq (,$(strip $(SKETCH_LIBS)))
11111130
$(call arduino_output,-)
1112-
$(call show_config_info,ARDUINO_LIBS =)
1131+
$(call show_config_info,SKETCH_LIBS =)
11131132
endif
11141133

1115-
ifneq (,$(strip $(USER_LIB_NAMES)))
1116-
$(foreach lib,$(USER_LIB_NAMES),$(call show_config_info, $(lib),[USER]))
1134+
ifneq (,$(strip $(SKETCH_LIBS)))
1135+
$(foreach lib,$(SKETCH_LIBS),$(call show_config_info, $(lib),[USER]))
1136+
endif
1137+
1138+
ifneq (,$(strip $(SKETCH_LIBS_DEPS)))
1139+
$(call arduino_output,-)
1140+
$(call show_config_info,SKETCH_LIBS_DEPS =)
1141+
endif
1142+
1143+
ifneq (,$(strip $(SKETCH_LIBS_DEPS)))
1144+
$(foreach lib,$(SKETCH_LIBS_DEPS),$(call show_config_info, $(lib),[USER]))
1145+
endif
1146+
1147+
ifneq (,$(strip $(SYS_LIB_NAMES) $(PLATFORM_LIB_NAMES)))
1148+
$(call arduino_output,-)
1149+
$(call show_config_info,SYSTEM_LIBS =)
11171150
endif
11181151

11191152
ifneq (,$(strip $(SYS_LIB_NAMES)))
@@ -1124,6 +1157,8 @@ ifneq (,$(strip $(PLATFORM_LIB_NAMES)))
11241157
$(foreach lib,$(PLATFORM_LIB_NAMES),$(call show_config_info, $(lib),[PLATFORM]))
11251158
endif
11261159

1160+
$(call arduino_output,-)
1161+
11271162
# either calculate parent dir from arduino dir, or user-defined path
11281163
ifndef BOOTLOADER_PARENT
11291164
BOOTLOADER_PARENT = $(ARDUINO_DIR)/hardware/$(VENDOR)/$(ARCHITECTURE)/bootloaders

HISTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ I tried to give credit whenever possible. If I have missed anyone, kindly add it
5454
- Fix: Added support for VARIANT being a submenu item in 1.6 cores like attiny (https://github.com/sej7278)
5555
- Fix: Replaced copyright symbol causing sed problems on OSX (Issue #335). (https://github.com/sej7278)
5656
- Fix: Fix issues with resetting Leonardo and Micro boards(Issue #340) (https://github.com/calvinli)
57+
- New: Add automatic library detection when compiling a sketch. (http://github.com/ladislas)
5758

5859
### 1.3.4 (2014-07-12)
5960
- Tweak: Allow spaces in "Serial.begin (....)". (Issue #190) (https://github.com/pdav)

bin/ard-lib-detection

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#!/usr/bin/env python
2+
3+
import os
4+
import re
5+
import sys
6+
7+
# Set variables
8+
USER_LIB_PATH = sys.argv[1]
9+
USER_LIBS = []
10+
11+
includeRegex = re.compile('^\#include\s(<|")(.*)\.h("|>)', re.DOTALL|re.M)
12+
13+
SKETCH_SRCS = []
14+
SKETCH_LIBS = []
15+
16+
SKETCH_LIBS_DEPS = []
17+
SKETCH_LIBS_DEPS_STACK = []
18+
19+
20+
# Define functions
21+
def outputLibs(libArray):
22+
for lib in libArray:
23+
print(lib),
24+
print("")
25+
26+
27+
# Find local sources .ino, .c or .cpp
28+
FILE_END = (".c", ".cpp", ".ino")
29+
SKETCH_SRCS = [f for f in os.listdir(os.curdir) if f.endswith(FILE_END)]
30+
31+
# Find all USER_LIBS
32+
for path, dirs, files in os.walk(USER_LIB_PATH):
33+
for d in dirs:
34+
USER_LIBS.append(d)
35+
36+
# Find SKETCH_LIBS included in SKETCH_SRCS
37+
for src in SKETCH_SRCS:
38+
currentFile = open(src)
39+
40+
for line in currentFile:
41+
match = includeRegex.search(line)
42+
if match is not None:
43+
group = match.group(2)
44+
if group in USER_LIBS:
45+
SKETCH_LIBS.append(group)
46+
47+
SKETCH_LIBS = sorted(set(SKETCH_LIBS))
48+
49+
# Find SKETCH_LIBS_DEPS includes in SKETCH_LIBS
50+
for lib in SKETCH_LIBS:
51+
if lib in USER_LIBS:
52+
currentFile = open(os.path.join(USER_LIB_PATH, lib, lib + ".h"))
53+
54+
for line in currentFile:
55+
match = includeRegex.search(line)
56+
if match is not None:
57+
group = match.group(2)
58+
if group in USER_LIBS and group not in SKETCH_LIBS:
59+
SKETCH_LIBS_DEPS_STACK.append(group)
60+
61+
SKETCH_LIBS_DEPS_STACK = list(set(SKETCH_LIBS_DEPS_STACK))
62+
63+
# Recursively find all dependencies of every libraries in USER_LIB_PATH
64+
while len(SKETCH_LIBS_DEPS_STACK) > 0:
65+
for lib in SKETCH_LIBS_DEPS_STACK:
66+
if lib in USER_LIBS:
67+
currentFile = open(os.path.join(USER_LIB_PATH, lib, lib + ".h"))
68+
69+
for line in currentFile:
70+
match = includeRegex.search(line)
71+
if match is not None:
72+
group = match.group(2)
73+
if group in USER_LIBS and group not in SKETCH_LIBS_DEPS_STACK and group not in SKETCH_LIBS_DEPS and group not in SKETCH_LIBS:
74+
SKETCH_LIBS_DEPS_STACK.append(group)
75+
76+
else:
77+
if lib not in SKETCH_LIBS_DEPS:
78+
SKETCH_LIBS_DEPS.append(lib)
79+
if lib in SKETCH_LIBS_DEPS_STACK:
80+
SKETCH_LIBS_DEPS_STACK.remove(lib)
81+
82+
SKETCH_LIBS_DEPS.sort()
83+
84+
# Output libraries for the Makefile
85+
print("SKETCH_LIBS"),
86+
outputLibs(SKETCH_LIBS)
87+
88+
print("SKETCH_LIBS_DEPS"),
89+
outputLibs(SKETCH_LIBS_DEPS)

0 commit comments

Comments
 (0)