2013-12-10 09:19:42 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
import sys
|
|
|
|
import argparse
|
|
|
|
import os
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(description="Make helper parser")
|
|
|
|
parser.add_argument("--directory", dest="directory", required=True)
|
|
|
|
parser.add_argument("--library", dest="library", help="Make a library")
|
|
|
|
parser.add_argument("--binary", dest="binary", help="Make a binary")
|
|
|
|
parser.add_argument("--exclude", dest="exclude", help="Exclude file", action="append")
|
2014-01-17 08:55:21 +01:00
|
|
|
parser.add_argument("--include", dest="include", help="Include file", action="append")
|
|
|
|
parser.add_argument("--out", dest="out", help="Output file")
|
|
|
|
parser.add_argument("--cpp-suffix", dest="cpp_suffix", help="C++ file suffix")
|
2013-12-10 09:19:42 +01:00
|
|
|
PREFIX=None
|
|
|
|
LIBRARY=None
|
|
|
|
BINARY=None
|
|
|
|
EXCLUDE=[]
|
2014-01-17 08:55:21 +01:00
|
|
|
INCLUDE=[]
|
|
|
|
OUTFILE="targets.mk"
|
|
|
|
CPP_SUFFIX=".cpp"
|
2013-12-10 09:19:42 +01:00
|
|
|
|
|
|
|
def make_o(x):
|
2014-03-18 10:21:46 +01:00
|
|
|
return os.path.splitext(x)[0] + ".$(OBJ)"
|
2013-12-10 09:19:42 +01:00
|
|
|
|
2014-01-08 15:33:35 +01:00
|
|
|
def write_cpp_rule_pattern(f):
|
2014-01-17 08:55:21 +01:00
|
|
|
src = "$(%s_SRCDIR)/%%%s"%(PREFIX, CPP_SUFFIX)
|
2014-03-18 10:21:46 +01:00
|
|
|
dst = "$(%s_SRCDIR)/%%.$(OBJ)"%(PREFIX)
|
2013-12-13 09:06:44 +01:00
|
|
|
|
2013-12-10 09:19:42 +01:00
|
|
|
f.write("%s: %s\n"%(dst, src))
|
2014-01-30 10:32:51 +01:00
|
|
|
f.write('\t$(QUIET_CXX)$(CXX) $(CFLAGS) $(CXXFLAGS) $(INCLUDES) $(' + PREFIX + '_CFLAGS) $(' + PREFIX + '_INCLUDES) -c $(CXX_O) $<\n')
|
2013-12-10 09:19:42 +01:00
|
|
|
f.write("\n")
|
2013-12-10 11:57:32 +01:00
|
|
|
|
2014-02-12 07:52:35 +01:00
|
|
|
def write_c_rule_pattern(f):
|
|
|
|
src = "$(%s_SRCDIR)/%%.c"%(PREFIX)
|
2014-03-18 10:21:46 +01:00
|
|
|
dst = "$(%s_SRCDIR)/%%.$(OBJ)"%(PREFIX)
|
2014-02-12 07:52:35 +01:00
|
|
|
|
|
|
|
f.write("%s: %s\n"%(dst, src))
|
|
|
|
f.write('\t$(QUIET_CC)$(CC) $(CFLAGS) $(INCLUDES) $(' + PREFIX + '_CFLAGS) $(' + PREFIX + '_INCLUDES) -c $(CXX_O) $<\n')
|
|
|
|
f.write("\n")
|
|
|
|
|
2014-01-08 15:33:35 +01:00
|
|
|
def write_asm_rule_pattern(f):
|
|
|
|
src = "$(%s_SRCDIR)/%%.asm"%(PREFIX)
|
2014-03-18 10:21:46 +01:00
|
|
|
dst = "$(%s_SRCDIR)/%%.$(OBJ)"%(PREFIX)
|
2013-12-13 09:06:44 +01:00
|
|
|
|
2013-12-10 11:57:32 +01:00
|
|
|
f.write("%s: %s\n"%(dst, src))
|
2014-01-30 10:32:51 +01:00
|
|
|
f.write('\t$(QUIET_ASM)$(ASM) $(ASMFLAGS) $(ASM_INCLUDES) $(' + PREFIX + '_ASMFLAGS) $(' + PREFIX + '_ASM_INCLUDES) -o $@ $<\n')
|
2013-12-10 11:57:32 +01:00
|
|
|
f.write("\n")
|
|
|
|
|
2014-02-26 13:31:52 +01:00
|
|
|
def write_asm_s_rule_pattern(f):
|
|
|
|
src = "$(%s_SRCDIR)/%%.S"%(PREFIX)
|
2014-03-18 10:21:46 +01:00
|
|
|
dst = "$(%s_SRCDIR)/%%.$(OBJ)"%(PREFIX)
|
2014-02-26 13:31:52 +01:00
|
|
|
|
|
|
|
f.write("%s: %s\n"%(dst, src))
|
|
|
|
f.write('\t$(QUIET_CCAS)$(CCAS) $(CFLAGS) $(ASMFLAGS) $(INCLUDES) $(' + PREFIX + '_CFLAGS) $(' + PREFIX + '_INCLUDES) -c -o $@ $<\n')
|
|
|
|
f.write("\n")
|
|
|
|
|
2013-12-10 09:19:42 +01:00
|
|
|
|
|
|
|
def find_sources():
|
|
|
|
cpp_files = []
|
|
|
|
asm_files = []
|
2014-02-12 07:52:35 +01:00
|
|
|
c_files = []
|
2014-02-26 13:31:52 +01:00
|
|
|
s_files = []
|
2013-12-10 09:19:42 +01:00
|
|
|
print EXCLUDE
|
|
|
|
for dir in os.walk("."):
|
|
|
|
for file in dir[2]:
|
2014-01-17 08:55:21 +01:00
|
|
|
if (len(INCLUDE) == 0 and not file in EXCLUDE) or file in INCLUDE:
|
|
|
|
if os.path.splitext(file)[1] == CPP_SUFFIX:
|
2014-01-24 13:35:38 +01:00
|
|
|
cpp_files.append(os.path.join(dir[0].strip('./'), file))
|
2013-12-10 09:19:42 +01:00
|
|
|
if os.path.splitext(file)[1] == '.asm':
|
2014-01-24 13:35:38 +01:00
|
|
|
asm_files.append(os.path.join(dir[0].strip('./'), file))
|
2014-02-12 07:52:35 +01:00
|
|
|
if os.path.splitext(file)[1] == '.c':
|
|
|
|
c_files.append(os.path.join(dir[0].strip('./'), file))
|
2014-02-26 13:31:52 +01:00
|
|
|
if os.path.splitext(file)[1] == '.S':
|
|
|
|
s_files.append(os.path.join(dir[0].strip('./'), file))
|
|
|
|
return [cpp_files, asm_files, c_files, s_files]
|
2013-12-10 09:19:42 +01:00
|
|
|
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
if args.library is not None:
|
|
|
|
PREFIX=args.library.upper()
|
|
|
|
elif args.binary is not None:
|
|
|
|
PREFIX=args.binary.upper()
|
|
|
|
else:
|
|
|
|
sys.stderr.write("Must provide either library or binary")
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
if args.exclude is not None:
|
|
|
|
EXCLUDE = args.exclude
|
2014-01-17 08:55:21 +01:00
|
|
|
if args.include is not None:
|
|
|
|
INCLUDE = args.include
|
|
|
|
if args.out is not None:
|
|
|
|
OUTFILE = args.out
|
2014-01-24 13:30:08 +01:00
|
|
|
else:
|
|
|
|
OUTFILE = os.path.join(args.directory, OUTFILE)
|
2014-01-17 08:55:21 +01:00
|
|
|
if args.cpp_suffix is not None:
|
|
|
|
CPP_SUFFIX = args.cpp_suffix
|
2014-01-24 13:30:08 +01:00
|
|
|
|
|
|
|
OUTFILE = os.path.abspath(OUTFILE)
|
|
|
|
try:
|
|
|
|
os.chdir(args.directory)
|
2014-03-24 08:26:06 +01:00
|
|
|
except OSError as e:
|
|
|
|
sys.stderr.write("Error changing directory to %s\n" % e.filename)
|
2014-01-24 13:30:08 +01:00
|
|
|
sys.exit(1)
|
|
|
|
|
2014-02-26 13:31:52 +01:00
|
|
|
(cpp, asm, cfiles, sfiles) = find_sources()
|
2013-12-10 09:19:42 +01:00
|
|
|
|
2014-03-08 00:45:22 +01:00
|
|
|
cpp = sorted(cpp, key=lambda s: s.lower())
|
|
|
|
asm = sorted(asm, key=lambda s: s.lower())
|
|
|
|
cfiles = sorted(cfiles, key=lambda s: s.lower())
|
|
|
|
sfiles = sorted(sfiles, key=lambda s: s.lower())
|
|
|
|
|
2013-12-10 09:19:42 +01:00
|
|
|
|
|
|
|
|
2014-01-17 08:55:21 +01:00
|
|
|
f = open(OUTFILE, "w")
|
2013-12-10 09:19:42 +01:00
|
|
|
f.write("%s_SRCDIR=%s\n"%(PREFIX, args.directory))
|
|
|
|
|
|
|
|
f.write("%s_CPP_SRCS=\\\n"%(PREFIX))
|
|
|
|
for c in cpp:
|
|
|
|
f.write("\t$(%s_SRCDIR)/%s\\\n"%(PREFIX, c))
|
2013-12-13 09:06:44 +01:00
|
|
|
f.write("\n")
|
2014-03-18 10:21:46 +01:00
|
|
|
f.write("%s_OBJS += $(%s_CPP_SRCS:%s=.$(OBJ))\n\n"%(PREFIX, PREFIX, CPP_SUFFIX))
|
2014-02-12 07:52:35 +01:00
|
|
|
|
|
|
|
if len(cfiles) > 0:
|
|
|
|
f.write("%s_C_SRCS=\\\n"%(PREFIX))
|
|
|
|
for cfile in cfiles:
|
|
|
|
f.write("\t$(%s_SRCDIR)/%s\\\n"%(PREFIX, cfile))
|
|
|
|
f.write("\n")
|
2014-03-18 10:21:46 +01:00
|
|
|
f.write("%s_OBJS += $(%s_C_SRCS:.c=.$(OBJ))\n\n"%(PREFIX, PREFIX))
|
2013-12-10 09:19:42 +01:00
|
|
|
|
2014-01-17 09:08:26 +01:00
|
|
|
if len(asm) > 0:
|
2014-02-25 22:52:36 +01:00
|
|
|
f.write("ifeq ($(ASM_ARCH), x86)\n")
|
2014-01-17 09:08:26 +01:00
|
|
|
f.write("%s_ASM_SRCS=\\\n"%(PREFIX))
|
|
|
|
for c in asm:
|
|
|
|
f.write("\t$(%s_SRCDIR)/%s\\\n"%(PREFIX, c))
|
|
|
|
f.write("\n")
|
2014-03-18 10:21:46 +01:00
|
|
|
f.write("%s_OBJS += $(%s_ASM_SRCS:.asm=.$(OBJ))\n"%(PREFIX, PREFIX))
|
2014-01-17 09:08:26 +01:00
|
|
|
f.write("endif\n\n")
|
2013-12-10 09:19:42 +01:00
|
|
|
|
2014-02-26 13:31:52 +01:00
|
|
|
if len(sfiles) > 0:
|
|
|
|
f.write("ifeq ($(ASM_ARCH), arm)\n")
|
|
|
|
f.write("%s_ASM_S_SRCS=\\\n"%(PREFIX))
|
|
|
|
for c in sfiles:
|
|
|
|
f.write("\t$(%s_SRCDIR)/%s\\\n"%(PREFIX, c))
|
|
|
|
f.write("\n")
|
2014-03-18 10:21:46 +01:00
|
|
|
f.write("%s_OBJS += $(%s_ASM_S_SRCS:.S=.$(OBJ))\n"%(PREFIX, PREFIX))
|
2014-02-26 13:31:52 +01:00
|
|
|
f.write("endif\n\n")
|
|
|
|
|
2013-12-10 09:19:42 +01:00
|
|
|
f.write("OBJS += $(%s_OBJS)\n"%PREFIX)
|
|
|
|
|
2014-01-08 15:33:35 +01:00
|
|
|
write_cpp_rule_pattern(f)
|
2013-12-10 09:19:42 +01:00
|
|
|
|
2014-02-12 07:52:35 +01:00
|
|
|
if len(cfiles) > 0:
|
|
|
|
write_c_rule_pattern(f)
|
|
|
|
|
2014-01-17 09:08:26 +01:00
|
|
|
if len(asm) > 0:
|
|
|
|
write_asm_rule_pattern(f)
|
2013-12-10 09:19:42 +01:00
|
|
|
|
2014-02-26 13:31:52 +01:00
|
|
|
if len(sfiles) > 0:
|
|
|
|
write_asm_s_rule_pattern(f)
|
|
|
|
|
2013-12-10 09:19:42 +01:00
|
|
|
if args.library is not None:
|
2014-01-19 17:08:12 +01:00
|
|
|
f.write("$(LIBPREFIX)%s.$(LIBSUFFIX): $(%s_OBJS)\n"%(args.library, PREFIX))
|
2014-01-30 10:32:51 +01:00
|
|
|
f.write("\t$(QUIET)rm -f $@\n")
|
|
|
|
f.write("\t$(QUIET_AR)$(AR) $(AR_OPTS) $+\n")
|
2014-01-19 17:08:12 +01:00
|
|
|
f.write("\n")
|
|
|
|
f.write("libraries: $(LIBPREFIX)%s.$(LIBSUFFIX)\n"%args.library)
|
|
|
|
f.write("LIBRARIES += $(LIBPREFIX)%s.$(LIBSUFFIX)\n"%args.library)
|
2013-12-10 09:19:42 +01:00
|
|
|
|
|
|
|
if args.binary is not None:
|
2014-01-23 15:14:09 +01:00
|
|
|
f.write("%s$(EXEEXT): $(%s_OBJS) $(LIBS) $(%s_LIBS) $(%s_DEPS)\n"%(args.binary, PREFIX, PREFIX, PREFIX))
|
2014-01-30 10:32:51 +01:00
|
|
|
f.write("\t$(QUIET_CXX)$(CXX) $(CXX_LINK_O) $(%s_OBJS) $(%s_LDFLAGS) $(%s_LIBS) $(LDFLAGS) $(LIBS)\n\n"%(PREFIX, PREFIX, PREFIX))
|
2014-01-23 15:14:09 +01:00
|
|
|
f.write("binaries: %s$(EXEEXT)\n"%args.binary)
|
|
|
|
f.write("BINARIES += %s$(EXEEXT)\n"%args.binary)
|
2014-01-19 17:08:12 +01:00
|
|
|
|
|
|
|
f.close()
|