Skip to content

Commit 807e387

Browse files
committed
add automatic lib detection
2 parents eba20c7 + 02382b2 commit 807e387

File tree

3 files changed

+128
-6
lines changed

3 files changed

+128
-6
lines changed

Arduino.mk

Lines changed: 37 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,19 @@ 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+
ifndef SKETCH_LIBS
828+
SKETCH_LIBS := $(shell $(ARDMK_DIR)/bin/lib-detection $(USER_LIB_PATH) | \
829+
sed -ne 's/SKETCH_LIBS \(.*\) /\1/p')
830+
endif
831+
832+
ifndef SKETCH_LIBS_DEPS
833+
SKETCH_LIBS_DEPS := $(shell $(ARDMK_DIR)/bin/lib-detection $(USER_LIB_PATH) | \
834+
sed -ne 's/SKETCH_LIBS_DEPS \(.*\) /\1/p')
835+
endif
836+
824837
########################################################################
825838
# Determine ARDUINO_LIBS automatically
826839

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

934947
# General arguments
935-
USER_LIBS := $(sort $(wildcard $(patsubst %,$(USER_LIB_PATH)/%,$(ARDUINO_LIBS))))
948+
USER_LIBS := $(wildcard $(patsubst %,$(USER_LIB_PATH)/%,$(ARDUINO_LIBS))) \
949+
$(wildcard $(patsubst %,$(USER_LIB_PATH)/%,$(SKETCH_LIBS))) \
950+
$(wildcard $(patsubst %,$(USER_LIB_PATH)/%,$(SKETCH_LIBS_DEPS)))
951+
936952
USER_LIB_NAMES := $(patsubst $(USER_LIB_PATH)/%,%,$(USER_LIBS))
937953

938954
# Let user libraries override system ones.
@@ -1107,17 +1123,32 @@ else
11071123
$(call show_config_info,Size utility: Basic (not AVR-aware),[AUTODETECTED])
11081124
endif
11091125

1110-
ifneq (,$(strip $(ARDUINO_LIBS)))
1126+
ifneq (,$(strip $(SKETCH_LIBS)))
1127+
$(call arduino_output,-)
1128+
$(call show_config_info,SKETCH_LIBS =)
1129+
endif
1130+
1131+
ifneq (,$(strip $(SKETCH_LIBS)))
1132+
$(foreach lib,$(SKETCH_LIBS),$(call show_config_info, $(lib),[USER]))
1133+
endif
1134+
1135+
ifneq (,$(strip $(SKETCH_LIBS_DEPS)))
11111136
$(call arduino_output,-)
1112-
$(call show_config_info,ARDUINO_LIBS =)
1137+
$(call show_config_info,SKETCH_LIBS_DEPS =)
11131138
endif
11141139

1115-
ifneq (,$(strip $(USER_LIB_NAMES)))
1116-
$(foreach lib,$(USER_LIB_NAMES),$(call show_config_info, $(lib),[USER]))
1140+
ifneq (,$(strip $(SKETCH_LIBS_DEPS)))
1141+
$(foreach lib,$(SKETCH_LIBS_DEPS),$(call show_config_info, $(lib),[USER]))
1142+
endif
1143+
1144+
ifneq (,$(strip $(SYS_LIB_NAMES) $(PLATFORM_LIB_NAMES)))
1145+
$(call arduino_output,-)
1146+
$(call show_config_info,SYSTEM_LIBS =)
11171147
endif
11181148

11191149
ifneq (,$(strip $(SYS_LIB_NAMES)))
11201150
$(foreach lib,$(SYS_LIB_NAMES),$(call show_config_info, $(lib),[SYSTEM]))
1151+
$(call arduino_output,-)
11211152
endif
11221153

11231154
ifneq (,$(strip $(PLATFORM_LIB_NAMES)))

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/lib-detection

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

0 commit comments

Comments
 (0)