optimize auto-lib detection and remove .py

This commit is contained in:
ladislas 2014-09-01 11:05:27 +02:00
parent 9bd8815ff5
commit bf295fab24
3 changed files with 92 additions and 89 deletions

View file

@ -767,12 +767,12 @@ endif
# Automatically find the libraries needed to compile the sketch
ifndef MAIN_LIBS
MAIN_LIBS = $(shell $(ARDMK_DIR)/bin/auto-lib.py $(USER_LIB_PATH) | \
MAIN_LIBS = $(shell $(ARDMK_DIR)/bin/auto-lib $(USER_LIB_PATH) | \
sed -ne 's/MAIN_LIBS \(.*\) /\1/p')
endif
ifndef LIBS_DEPS
LIBS_DEPS = $(shell $(ARDMK_DIR)/bin/auto-lib.py $(USER_LIB_PATH) | \
LIBS_DEPS = $(shell $(ARDMK_DIR)/bin/auto-lib $(USER_LIB_PATH) | \
sed -ne 's/LIBS_DEPS \(.*\) /\1/p')
endif

90
bin/auto-lib Executable file
View file

@ -0,0 +1,90 @@
#!/usr/bin/env python
import os
import re
import sys
# Set variables
USER_LIB_PATH = sys.argv[1]
USER_LIBS = []
includeRegex = re.compile("(?<=^\#include\s\")(.*)(?=\.h\")", re.DOTALL|re.M)
MAIN_SRCS = []
MAIN_LIBS = []
LIBS_DEPS = []
LIBS_DEPS_STACK = []
# Define functions
def outputLibs(libArray):
for lib in libArray:
print(lib),
print("")
# Find local sources .ino, .c or .cpp
FILE_END = (".c", ".cpp", ".ino")
MAIN_SRCS = [f for f in os.listdir(os.curdir) if f.endswith(FILE_END)]
# Find all USER_LIBS
for path, dirs, files in os.walk(USER_LIB_PATH):
for d in dirs:
USER_LIBS.append(d)
# Find MAIN_LIBS included in MAIN_SRCS
for src in MAIN_SRCS:
currentFile = open(src)
for line in currentFile:
match = includeRegex.search(line)
if match is not None:
group = match.group(1)
if group in USER_LIBS:
MAIN_LIBS.append(group)
MAIN_LIBS = sorted(set(MAIN_LIBS))
# Find LIBS_DEPS includes in MAIN_LIBS
for lib in MAIN_LIBS:
if lib in USER_LIBS:
currentFile = open(os.path.join(USER_LIB_PATH, lib, lib + ".h"))
for line in currentFile:
match = includeRegex.search(line)
if match is not None:
group = match.group(1)
if group in USER_LIBS and group not in MAIN_LIBS:
LIBS_DEPS_STACK.append(group)
LIBS_DEPS_STACK = list(set(LIBS_DEPS_STACK))
# Recursively find all dependencies of every libraries in USER_LIB_PATH
while len(LIBS_DEPS_STACK) > 0:
for lib in LIBS_DEPS_STACK:
if lib in USER_LIBS:
currentFile = open(os.path.join(USER_LIB_PATH, lib, lib + ".h"))
for line in currentFile:
match = includeRegex.search(line)
if match is not None:
group = match.group(1)
if group in USER_LIBS and group not in LIBS_DEPS_STACK and group not in LIBS_DEPS and group not in MAIN_LIBS:
LIBS_DEPS_STACK.append(group)
else:
if lib not in LIBS_DEPS:
LIBS_DEPS.append(lib)
if lib in LIBS_DEPS_STACK:
LIBS_DEPS_STACK.remove(lib)
LIBS_DEPS.sort()
# Output libraries for the Makefile
print("MAIN_LIBS"),
outputLibs(MAIN_LIBS)
print("LIBS_DEPS"),
outputLibs(LIBS_DEPS)

View file

@ -1,87 +0,0 @@
#!/usr/bin/env python
import os
import re
import sys
# Set variables
USER_LIB_PATH = sys.argv[1]
USER_LIBS = [] ;
includeRegex = re.compile("(?<=^\#include\s[\<\"])(.*)(?=\.h[\>\"])", re.DOTALL|re.M)
MAIN_SRCS = [] ;
MAIN_LIBS = [] ;
LIBS_DEPS = [] ;
LIBS_DEPS_STACK = [] ;
# Define functions
def outputLibs(libArray):
for lib in libArray:
print(lib),
print("")
# Find local sources .ino, .c or .cpp
for file in os.listdir(os.curdir):
if file.endswith((".c", ".cpp", ".ino")):
MAIN_SRCS.append(file)
# Find all USER_LIBS
for path, dirs, files in os.walk(USER_LIB_PATH):
for d in dirs:
USER_LIBS.append(d)
# Find MAIN_LIBS included in MAIN_SRCS
for src in MAIN_SRCS:
currentFile = open(src)
includes = []
for line in currentFile:
match = includeRegex.search(line)
if match:
if match.group(1) in USER_LIBS:
MAIN_LIBS.append(match.group(1))
MAIN_LIBS = list(sorted(MAIN_LIBS))
# Find LIBS_DEPS includes in MAIN_LIBS
for lib in MAIN_LIBS:
if lib in USER_LIBS:
currentFile = open(USER_LIB_PATH + "/" + lib + "/" + lib + ".h")
for line in currentFile:
match = includeRegex.search(line)
if match:
if match.group(1) in USER_LIBS and match.group(1) not in MAIN_LIBS:
LIBS_DEPS_STACK.append(match.group(1))
LIBS_DEPS_STACK = sorted(set(LIBS_DEPS_STACK))
# Recursively find all dependencies of every libraries in USER_LIB_PATH
while LIBS_DEPS_STACK:
for lib in LIBS_DEPS_STACK:
if lib in USER_LIBS:
currentFile = open(USER_LIB_PATH + "/" + lib + "/" + lib + ".h")
for line in currentFile:
match = includeRegex.search(line)
if match:
if match.group(1) in USER_LIBS and match.group(1) not in LIBS_DEPS_STACK or match.group(1) in LIBS_DEPS and match.group(1) not in MAIN_LIBS:
LIBS_DEPS_STACK.append(match.group(1))
else:
LIBS_DEPS.append(lib)
if lib in LIBS_DEPS_STACK:
LIBS_DEPS_STACK.remove(lib)
LIBS_DEPS_STACK = sorted(set(LIBS_DEPS_STACK))
LIBS_DEPS = sorted(set(LIBS_DEPS))
# Output libraries for the Makefile
print("MAIN_LIBS"),
outputLibs(MAIN_LIBS)
print("LIBS_DEPS"),
outputLibs(LIBS_DEPS)