Skip to content

Commit 2190804

Browse files
committed
My patches to ladislas' PR sudar#341 auto-lib (2nd attempt)
1 parent 0cc6faf commit 2190804

File tree

4 files changed

+133
-25
lines changed

4 files changed

+133
-25
lines changed

Arduino.mk

Lines changed: 42 additions & 24 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,17 +821,29 @@ 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

827843
ifndef ARDUINO_LIBS
828844
# automatically determine included libraries
829845
ARDUINO_LIBS += $(filter $(notdir $(wildcard $(ARDUINO_DIR)/libraries/*)), \
830846
$(shell sed -ne 's/^ *\# *include *[<\"]\(.*\)\.h[>\"]/\1/p' $(LOCAL_SRCS)))
831-
ARDUINO_LIBS += $(filter $(notdir $(wildcard $(ARDUINO_SKETCHBOOK)/libraries/*)), \
832-
$(shell sed -ne 's/^ *\# *include *[<\"]\(.*\)\.h[>\"]/\1/p' $(LOCAL_SRCS)))
833-
ARDUINO_LIBS += $(filter $(notdir $(wildcard $(USER_LIB_PATH)/*)), \
834-
$(shell sed -ne 's/^ *\# *include *[<\"]\(.*\)\.h[>\"]/\1/p' $(LOCAL_SRCS)))
835847
ARDUINO_LIBS += $(filter $(notdir $(wildcard $(ARDUINO_PLATFORM_LIB_PATH)/*)), \
836848
$(shell sed -ne 's/^ *\# *include *[<\"]\(.*\)\.h[>\"]/\1/p' $(LOCAL_SRCS)))
837849
endif
@@ -932,7 +944,10 @@ get_library_files = $(if $(and $(wildcard $(1)/src), $(wildcard $(1)/library.pr
932944
$(wildcard $(1)/*.$(2) $(1)/utility/*.$(2)))
933945

934946
# General arguments
935-
USER_LIBS := $(sort $(wildcard $(patsubst %,$(USER_LIB_PATH)/%,$(ARDUINO_LIBS))))
947+
USER_LIBS := $(wildcard $(patsubst %,$(USER_LIB_PATH)/%,$(ARDUINO_LIBS))) \
948+
$(wildcard $(patsubst %,$(USER_LIB_PATH)/%,$(SKETCH_LIBS))) \
949+
$(wildcard $(patsubst %,$(USER_LIB_PATH)/%,$(SKETCH_LIBS_DEPS)))
950+
936951
USER_LIB_NAMES := $(patsubst $(USER_LIB_PATH)/%,%,$(USER_LIBS))
937952

938953
# Let user libraries override system ones.
@@ -1107,23 +1122,6 @@ else
11071122
$(call show_config_info,Size utility: Basic (not AVR-aware),[AUTODETECTED])
11081123
endif
11091124

1110-
ifneq (,$(strip $(ARDUINO_LIBS)))
1111-
$(call arduino_output,-)
1112-
$(call show_config_info,ARDUINO_LIBS =)
1113-
endif
1114-
1115-
ifneq (,$(strip $(USER_LIB_NAMES)))
1116-
$(foreach lib,$(USER_LIB_NAMES),$(call show_config_info, $(lib),[USER]))
1117-
endif
1118-
1119-
ifneq (,$(strip $(SYS_LIB_NAMES)))
1120-
$(foreach lib,$(SYS_LIB_NAMES),$(call show_config_info, $(lib),[SYSTEM]))
1121-
endif
1122-
1123-
ifneq (,$(strip $(PLATFORM_LIB_NAMES)))
1124-
$(foreach lib,$(PLATFORM_LIB_NAMES),$(call show_config_info, $(lib),[PLATFORM]))
1125-
endif
1126-
11271125
# either calculate parent dir from arduino dir, or user-defined path
11281126
ifndef BOOTLOADER_PARENT
11291127
BOOTLOADER_PARENT = $(ARDUINO_DIR)/hardware/$(VENDOR)/$(ARCHITECTURE)/bootloaders
@@ -1132,14 +1130,34 @@ else
11321130
$(call show_config_variable,BOOTLOADER_PARENT,[USER])
11331131
endif
11341132

1135-
########################################################################
11361133
# Tools version info
11371134
ARDMK_VERSION = 1.5
11381135
$(call show_config_variable,ARDMK_VERSION,[COMPUTED])
11391136

11401137
CC_VERSION := $(shell $(CC) -dumpversion)
11411138
$(call show_config_variable,CC_VERSION,[COMPUTED],($(CC_NAME)))
11421139

1140+
# do we have any libs?
1141+
ifneq (,$(strip $(USER_LIBS) $(ARDUINO_LIBS) $(SYS_LIBS) $(PLATFORM_LIBS) $(SKETCH_LIBS) $(SKETCH_LIBS_DEPS)))
1142+
$(call show_config_info,ARDUINO_LIBS =)
1143+
endif
1144+
1145+
ifneq (,$(strip $(SKETCH_LIBS)))
1146+
$(foreach lib,$(SKETCH_LIBS),$(call show_config_info, $(lib),[USER]))
1147+
endif
1148+
1149+
ifneq (,$(strip $(SKETCH_LIBS_DEPS)))
1150+
$(foreach lib,$(SKETCH_LIBS_DEPS),$(call show_config_info, $(lib),[USER]))
1151+
endif
1152+
1153+
ifneq (,$(strip $(SYS_LIB_NAMES)))
1154+
$(foreach lib,$(SYS_LIB_NAMES),$(call show_config_info, $(lib),[SYSTEM]))
1155+
endif
1156+
1157+
ifneq (,$(strip $(PLATFORM_LIB_NAMES)))
1158+
$(foreach lib,$(PLATFORM_LIB_NAMES),$(call show_config_info, $(lib),[PLATFORM]))
1159+
endif
1160+
11431161
# end of config output
11441162
$(call show_separator)
11451163

HISTORY.md

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

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

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ The libraries will be searched in the following places in the following order.
217217

218218
The libraries inside user directories will take precedence over libraries present in Arduino core directory.
219219

220-
The makefile can autodetect the libraries that are included from your sketch and can include them automatically. But it can't detect libraries that are included from other libraries. (see [issue #93](https://github.com/sudar/Arduino-Makefile/issues/93))
220+
The makefile can autodetect the libraries that are included from your sketch and can include them automatically. It can also detect libraries that are included from other libraries.
221221

222222
## avrdude
223223

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)