• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/python
2import sys
3import argparse
4import os
5
6parser = argparse.ArgumentParser(description="Make helper parser")
7parser.add_argument("--directory", dest="directory", required=True)
8parser.add_argument("--library", dest="library", help="Make a library")
9parser.add_argument("--binary", dest="binary", help="Make a binary")
10parser.add_argument("--prefix", dest="prefix", help="Make a set of objs")
11parser.add_argument("--exclude", dest="exclude", help="Exclude file", action="append")
12parser.add_argument("--include", dest="include", help="Include file", action="append")
13parser.add_argument("--out", dest="out", help="Output file")
14parser.add_argument("--cpp-suffix", dest="cpp_suffix", help="C++ file suffix")
15PREFIX=None
16LIBRARY=None
17BINARY=None
18EXCLUDE=[]
19INCLUDE=[]
20OUTFILE="targets.mk"
21CPP_SUFFIX=".cpp"
22
23def write_cpp_rule_pattern(f):
24    src = "$(%s_SRCDIR)/%%%s"%(PREFIX, CPP_SUFFIX)
25    dst = "$(%s_SRCDIR)/%%.$(OBJ)"%(PREFIX)
26
27    f.write("%s: %s\n"%(dst, src))
28    f.write('\t$(QUIET_CXX)$(CXX) $(CFLAGS) $(CXXFLAGS) $(INCLUDES) $(' + PREFIX + '_CFLAGS) $(' + PREFIX + '_INCLUDES) -c $(CXX_O) $<\n')
29    f.write("\n")
30
31def write_c_rule_pattern(f):
32    src = "$(%s_SRCDIR)/%%.c"%(PREFIX)
33    dst = "$(%s_SRCDIR)/%%.$(OBJ)"%(PREFIX)
34
35    f.write("%s: %s\n"%(dst, src))
36    f.write('\t$(QUIET_CC)$(CC) $(CFLAGS) $(INCLUDES) $(' + PREFIX + '_CFLAGS) $(' + PREFIX + '_INCLUDES) -c $(CXX_O) $<\n')
37    f.write("\n")
38
39def write_asm_rule_pattern(f):
40    src = "$(%s_SRCDIR)/%%.asm"%(PREFIX)
41    dst = "$(%s_SRCDIR)/%%.$(OBJ)"%(PREFIX)
42
43    f.write("%s: %s\n"%(dst, src))
44    f.write('\t$(QUIET_ASM)$(ASM) $(ASMFLAGS) $(ASM_INCLUDES) $(' + PREFIX + '_ASMFLAGS) $(' + PREFIX + '_ASM_INCLUDES) -o $@ $<\n')
45    f.write("\n")
46
47def write_asm_s_rule_pattern(f):
48    src = "$(%s_SRCDIR)/%%.S"%(PREFIX)
49    dst = "$(%s_SRCDIR)/%%.$(OBJ)"%(PREFIX)
50
51    f.write("%s: %s\n"%(dst, src))
52    f.write('\t$(QUIET_CCAS)$(CCAS) $(CCASFLAGS) $(ASMFLAGS) $(INCLUDES) $(' + PREFIX + '_CFLAGS) $(' + PREFIX + '_INCLUDES) -c -o $@ $<\n')
53    f.write("\n")
54
55
56def find_sources():
57    cpp_files = []
58    asm_files = []
59    c_files = []
60    s_files = []
61    print EXCLUDE
62    for dir in os.walk("."):
63        for file in dir[2]:
64            if (len(INCLUDE) == 0 and not file in EXCLUDE) or file in INCLUDE:
65                if os.path.splitext(file)[1] == CPP_SUFFIX:
66                    cpp_files.append(os.path.join(dir[0].strip('./'), file))
67                if os.path.splitext(file)[1] == '.asm':
68                    asm_files.append(os.path.join(dir[0].strip('./'), file))
69                if os.path.splitext(file)[1] == '.c':
70                    c_files.append(os.path.join(dir[0].strip('./'), file))
71                if os.path.splitext(file)[1] == '.S':
72                    s_files.append(os.path.join(dir[0].strip('./'), file))
73    return [cpp_files, asm_files, c_files, s_files]
74
75
76args = parser.parse_args()
77
78if args.library is not None:
79    PREFIX=args.library.upper()
80elif args.binary is not None:
81    PREFIX=args.binary.upper()
82elif args.prefix is not None:
83    PREFIX=args.prefix.upper()
84else:
85    sys.stderr.write("Must provide either library, binary or prefix")
86    sys.exit(1)
87
88if args.exclude is not None:
89    EXCLUDE = args.exclude
90if args.include is not None:
91    INCLUDE = args.include
92if args.out is not None:
93    OUTFILE = args.out
94else:
95    OUTFILE = os.path.join(args.directory, OUTFILE)
96if args.cpp_suffix is not None:
97    CPP_SUFFIX = args.cpp_suffix
98
99OUTFILE = os.path.abspath(OUTFILE)
100try:
101    os.chdir(args.directory)
102except OSError as e:
103    sys.stderr.write("Error changing directory to %s\n" % e.filename)
104    sys.exit(1)
105
106(cpp, asm, cfiles, sfiles) = find_sources()
107
108cpp = sorted(cpp, key=lambda s: s.lower())
109asm = sorted(asm, key=lambda s: s.lower())
110cfiles = sorted(cfiles, key=lambda s: s.lower())
111sfiles = sorted(sfiles, key=lambda s: s.lower())
112armfiles = []
113arm64files = []
114for file in sfiles:
115    c = file.split('/')
116    if 'arm64' in c:
117        arm64files.append(file)
118    elif 'arm' in c:
119        armfiles.append(file)
120mipsfiles = []
121for file in cfiles:
122    c = file.split('/')
123    if 'mips' in c:
124        mipsfiles.append(file)
125cfiles = [x for x in cfiles if x not in mipsfiles]
126loongarchfiles = []
127for file in cfiles:
128    c = file.split('/')
129    if 'loongarch' in c:
130        loongarchfiles.append(file)
131cfiles = [x for x in cfiles if x not in loongarchfiles]
132
133
134f = open(OUTFILE, "w")
135f.write("# This file is autogenerated, do not edit it directly, edit build/mktargets.py\n")
136f.write("# instead. To regenerate files, run build/mktargets.sh.\n")
137f.write("\n")
138
139f.write("%s_SRCDIR=%s\n"%(PREFIX, args.directory))
140
141f.write("%s_CPP_SRCS=\\\n"%(PREFIX))
142for c in cpp:
143    f.write("\t$(%s_SRCDIR)/%s\\\n"%(PREFIX, c))
144f.write("\n")
145f.write("%s_OBJS += $(%s_CPP_SRCS:%s=.$(OBJ))\n\n"%(PREFIX, PREFIX, CPP_SUFFIX))
146
147if len(cfiles) > 0:
148    f.write("%s_C_SRCS=\\\n"%(PREFIX))
149    for cfile in cfiles:
150        f.write("\t$(%s_SRCDIR)/%s\\\n"%(PREFIX, cfile))
151    f.write("\n")
152    f.write("%s_OBJS += $(%s_C_SRCS:.c=.$(OBJ))\n"%(PREFIX, PREFIX))
153
154if len(asm) > 0:
155    f.write("%s_ASM_SRCS=\\\n"%(PREFIX))
156    for c in asm:
157        f.write("\t$(%s_SRCDIR)/%s\\\n"%(PREFIX, c))
158    f.write("\n")
159    f.write("%s_OBJSASM += $(%s_ASM_SRCS:.asm=.$(OBJ))\n"%(PREFIX, PREFIX))
160    f.write("ifeq ($(ASM_ARCH), x86)\n")
161    f.write("%s_OBJS += $(%s_OBJSASM)\n"%(PREFIX,PREFIX))
162    f.write("endif\n")
163    f.write("OBJS += $(%s_OBJSASM)\n\n"%(PREFIX))
164
165if len(armfiles) > 0:
166    f.write("%s_ASM_ARM_SRCS=\\\n"%(PREFIX))
167    for c in armfiles:
168        f.write("\t$(%s_SRCDIR)/%s\\\n"%(PREFIX, c))
169    f.write("\n")
170    f.write("%s_OBJSARM += $(%s_ASM_ARM_SRCS:.S=.$(OBJ))\n"%(PREFIX, PREFIX))
171    f.write("ifeq ($(ASM_ARCH), arm)\n")
172    f.write("%s_OBJS += $(%s_OBJSARM)\n"%(PREFIX,PREFIX))
173    f.write("endif\n")
174    f.write("OBJS += $(%s_OBJSARM)\n\n"%(PREFIX))
175
176if len(arm64files) > 0:
177    f.write("%s_ASM_ARM64_SRCS=\\\n"%(PREFIX))
178    for c in arm64files:
179        f.write("\t$(%s_SRCDIR)/%s\\\n"%(PREFIX, c))
180    f.write("\n")
181    f.write("%s_OBJSARM64 += $(%s_ASM_ARM64_SRCS:.S=.$(OBJ))\n"%(PREFIX, PREFIX))
182    f.write("ifeq ($(ASM_ARCH), arm64)\n")
183    f.write("%s_OBJS += $(%s_OBJSARM64)\n"%(PREFIX,PREFIX))
184    f.write("endif\n")
185    f.write("OBJS += $(%s_OBJSARM64)\n\n"%(PREFIX))
186
187if len(mipsfiles) > 0:
188    mmifiles = []
189    for file in mipsfiles:
190        if '_mmi' in file:
191            mmifiles.append(file)
192    f.write("%s_ASM_MIPS_MMI_SRCS=\\\n"%(PREFIX))
193    for c in mmifiles:
194        f.write("\t$(%s_SRCDIR)/%s\\\n"%(PREFIX, c))
195    f.write("\n")
196    f.write("%s_OBJSMIPS_MMI += $(%s_ASM_MIPS_MMI_SRCS:.c=.$(OBJ))\n\n"%(PREFIX, PREFIX))
197    msafiles = []
198    for file in mipsfiles:
199        if '_msa' in file:
200            msafiles.append(file)
201    f.write("%s_ASM_MIPS_MSA_SRCS=\\\n"%(PREFIX))
202    for c in msafiles:
203        f.write("\t$(%s_SRCDIR)/%s\\\n"%(PREFIX, c))
204    f.write("\n")
205    f.write("%s_OBJSMIPS_MSA += $(%s_ASM_MIPS_MSA_SRCS:.c=.$(OBJ))\n"%(PREFIX, PREFIX))
206    f.write("ifeq ($(ASM_ARCH), mips)\n")
207    f.write("ifeq ($(ENABLE_MMI), Yes)\n")
208    f.write("%s_OBJS += $(%s_OBJSMIPS_MMI)\n"%(PREFIX,PREFIX))
209    f.write("endif\n")
210    f.write("ifeq ($(ENABLE_MSA), Yes)\n")
211    f.write("%s_OBJS += $(%s_OBJSMIPS_MSA)\n"%(PREFIX,PREFIX))
212    f.write("endif\n")
213    f.write("endif\n")
214    f.write("OBJS += $(%s_OBJSMIPS_MMI)\n"%(PREFIX))
215    f.write("OBJS += $(%s_OBJSMIPS_MSA)\n\n"%(PREFIX))
216
217if len(loongarchfiles) > 0:
218    lsxfiles = []
219    for file in loongarchfiles:
220        if '_lsx' in file:
221            lsxfiles.append(file)
222    f.write("%s_ASM_LOONGARCH_LSX_SRCS=\\\n"%(PREFIX))
223    for c in lsxfiles:
224        f.write("\t$(%s_SRCDIR)/%s\\\n"%(PREFIX, c))
225    f.write("\n")
226    f.write("%s_OBJSLOONGARCH_LSX += $(%s_ASM_LOONGARCH_LSX_SRCS:.c=.$(OBJ))\n\n"%(PREFIX, PREFIX))
227    lasxfiles = []
228    for file in loongarchfiles:
229        if '_lasx' in file:
230            lasxfiles.append(file)
231    f.write("%s_ASM_LOONGARCH_LASX_SRCS=\\\n"%(PREFIX))
232    for c in lasxfiles:
233        f.write("\t$(%s_SRCDIR)/%s\\\n"%(PREFIX, c))
234    f.write("\n")
235    f.write("%s_OBJSLOONGARCH_LASX += $(%s_ASM_LOONGARCH_LASX_SRCS:.c=.$(OBJ))\n"%(PREFIX, PREFIX))
236
237    f.write("ifeq ($(ASM_ARCH), loongarch)\n")
238    f.write("ifeq ($(ENABLE_LSX), Yes)\n")
239    f.write("%s_OBJS += $(%s_OBJSLOONGARCH_LSX)\n"%(PREFIX,PREFIX))
240    f.write("endif\n")
241    f.write("ifeq ($(ENABLE_LASX), Yes)\n")
242    f.write("%s_OBJS += $(%s_OBJSLOONGARCH_LASX)\n"%(PREFIX,PREFIX))
243    f.write("endif\n")
244    f.write("endif\n")
245    f.write("OBJS += $(%s_OBJSLOONGARCH_LSX)\n"%(PREFIX))
246    f.write("OBJS += $(%s_OBJSLOONGARCH_LASX)\n\n"%(PREFIX))
247
248f.write("OBJS += $(%s_OBJS)\n\n"%(PREFIX))
249write_cpp_rule_pattern(f)
250
251if len(cfiles) > 0 or len(mipsfiles) > 0:
252    write_c_rule_pattern(f)
253
254if len(asm) > 0:
255    write_asm_rule_pattern(f)
256
257if len(sfiles) > 0:
258    write_asm_s_rule_pattern(f)
259
260if args.library is not None:
261    f.write("$(LIBPREFIX)%s.$(LIBSUFFIX): $(%s_OBJS)\n"%(args.library, PREFIX))
262    f.write("\t$(QUIET)rm -f $@\n")
263    f.write("\t$(QUIET_AR)$(AR) $(AR_OPTS) $+\n")
264    f.write("\n")
265    f.write("libraries: $(LIBPREFIX)%s.$(LIBSUFFIX)\n"%args.library)
266    f.write("LIBRARIES += $(LIBPREFIX)%s.$(LIBSUFFIX)\n"%args.library)
267
268if args.binary is not None:
269    f.write("%s$(EXEEXT): $(%s_OBJS) $(%s_DEPS)\n"%(args.binary, PREFIX, PREFIX))
270    f.write("\t$(QUIET_CXX)$(CXX) $(CXX_LINK_O) $(%s_OBJS) $(%s_LDFLAGS) $(LDFLAGS)\n\n"%(PREFIX, PREFIX))
271    f.write("binaries: %s$(EXEEXT)\n"%args.binary)
272    f.write("BINARIES += %s$(EXEEXT)\n"%args.binary)
273
274f.close()
275