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):
|
|
|
|
return os.path.splitext(x)[0] + ".o"
|
|
|
|
|
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-01-08 15:33:35 +01:00
|
|
|
dst = "$(%s_SRCDIR)/%%.o"%(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-19 17:08:12 +01:00
|
|
|
f.write('\t$(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-01-08 15:33:35 +01:00
|
|
|
def write_asm_rule_pattern(f):
|
|
|
|
src = "$(%s_SRCDIR)/%%.asm"%(PREFIX)
|
|
|
|
dst = "$(%s_SRCDIR)/%%.o"%(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-19 17:08:12 +01:00
|
|
|
f.write('\t$(ASM) $(ASMFLAGS) $(ASM_INCLUDES) $(' + PREFIX + '_ASMFLAGS) $(' + PREFIX + '_ASM_INCLUDES) -o $@ $<\n')
|
2013-12-10 11:57:32 +01:00
|
|
|
f.write("\n")
|
|
|
|
|
2013-12-10 09:19:42 +01:00
|
|
|
|
|
|
|
def find_sources():
|
|
|
|
cpp_files = []
|
|
|
|
asm_files = []
|
|
|
|
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:
|
2013-12-10 09:19:42 +01:00
|
|
|
cpp_files.append(os.path.join(dir[0], file))
|
|
|
|
if os.path.splitext(file)[1] == '.asm':
|
|
|
|
asm_files.append(os.path.join(dir[0], file))
|
|
|
|
return [cpp_files, asm_files]
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
except:
|
|
|
|
sys.exit(1)
|
|
|
|
|
2013-12-10 09:19:42 +01:00
|
|
|
(cpp, asm) = find_sources()
|
|
|
|
|
|
|
|
|
|
|
|
|
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-01-17 08:55:21 +01:00
|
|
|
f.write("%s_OBJS += $(%s_CPP_SRCS:%s=.o)\n"%(PREFIX, PREFIX, CPP_SUFFIX))
|
2013-12-10 09:19:42 +01:00
|
|
|
|
2014-01-17 09:08:26 +01:00
|
|
|
if len(asm) > 0:
|
2014-01-19 17:08:12 +01:00
|
|
|
f.write("ifeq ($(USE_ASM), Yes)\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")
|
|
|
|
f.write("%s_OBJS += $(%s_ASM_SRCS:.asm=.o)\n"%(PREFIX, PREFIX))
|
|
|
|
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-01-17 09:08:26 +01:00
|
|
|
if len(asm) > 0:
|
|
|
|
write_asm_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))
|
2013-12-10 09:19:42 +01:00
|
|
|
f.write("\trm -f $(LIBPREFIX)%s.$(LIBSUFFIX)\n"%args.library)
|
2014-01-19 17:08:12 +01:00
|
|
|
f.write("\t$(AR) $(AR_OPTS) $(%s_OBJS)\n"%PREFIX)
|
|
|
|
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))
|
2013-12-17 12:57:27 +01:00
|
|
|
f.write("\t$(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()
|