Arduino-Makefile/bin/auto-lib

91 lines
2.1 KiB
Text
Raw Normal View History

#!/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 = []
2014-11-09 19:04:15 +01:00
MAIN_LIBS_DEPS = []
MAIN_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))
2014-11-09 19:04:15 +01:00
# Find MAIN_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:
2014-11-09 19:04:15 +01:00
MAIN_LIBS_DEPS_STACK.append(group)
2014-11-09 19:04:15 +01:00
MAIN_LIBS_DEPS_STACK = list(set(MAIN_LIBS_DEPS_STACK))
# Recursively find all dependencies of every libraries in USER_LIB_PATH
2014-11-09 19:04:15 +01:00
while len(MAIN_LIBS_DEPS_STACK) > 0:
for lib in MAIN_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)
2014-11-09 19:04:15 +01:00
if group in USER_LIBS and group not in MAIN_LIBS_DEPS_STACK and group not in MAIN_LIBS_DEPS and group not in MAIN_LIBS:
MAIN_LIBS_DEPS_STACK.append(group)
else:
2014-11-09 19:04:15 +01:00
if lib not in MAIN_LIBS_DEPS:
MAIN_LIBS_DEPS.append(lib)
if lib in MAIN_LIBS_DEPS_STACK:
MAIN_LIBS_DEPS_STACK.remove(lib)
2014-11-09 19:04:15 +01:00
MAIN_LIBS_DEPS.sort()
# Output libraries for the Makefile
print("MAIN_LIBS"),
outputLibs(MAIN_LIBS)
2014-11-09 19:04:15 +01:00
print("MAIN_LIBS_DEPS"),
outputLibs(MAIN_LIBS_DEPS)