]> git.proxmox.com Git - mirror_frr.git/blob - python/makefile.py
Merge pull request #8124 from pguibert6WIND/ipsec_iptable_dplane
[mirror_frr.git] / python / makefile.py
1 #!/usr/bin/python3
2 #
3 # FRR extended automake/Makefile functionality helper
4 #
5 # This script is executed on/after generating Makefile to add some pieces for
6 # clippy.
7
8 import sys
9 import os
10 import subprocess
11 import re
12 import argparse
13 from string import Template
14 from makevars import MakeReVars
15
16 argp = argparse.ArgumentParser(description="FRR Makefile extensions")
17 argp.add_argument(
18 "--dev-build",
19 action="store_const",
20 const=True,
21 help="run additional developer checks",
22 )
23 args = argp.parse_args()
24
25 with open("Makefile", "r") as fd:
26 before = fd.read()
27
28 mv = MakeReVars(before)
29
30 clippy_scan = mv["clippy_scan"].strip().split()
31 for clippy_file in clippy_scan:
32 assert clippy_file.endswith(".c")
33
34 xref_targets = []
35 for varname in ["bin_PROGRAMS", "sbin_PROGRAMS", "lib_LTLIBRARIES", "module_LTLIBRARIES"]:
36 xref_targets.extend(mv[varname].strip().split())
37
38 # check for files using clippy but not listed in clippy_scan
39 if args.dev_build:
40 basepath = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
41 if os.path.exists(os.path.join(basepath, ".git")):
42 clippy_ref = subprocess.check_output(
43 [
44 "git",
45 "-C",
46 basepath,
47 "grep",
48 "-l",
49 "-P",
50 "^#\s*include.*_clippy.c",
51 "--",
52 "**.c",
53 ]
54 ).decode("US-ASCII")
55
56 clippy_ref = set(clippy_ref.splitlines())
57 missing = clippy_ref - set(clippy_scan)
58
59 if len(missing) > 0:
60 sys.stderr.write(
61 'error: files seem to be using clippy, but not listed in "clippy_scan" in subdir.am:\n\t%s\n'
62 % ("\n\t".join(sorted(missing)))
63 )
64 sys.exit(1)
65
66 clippydep = Template(
67 """
68 ${clippybase}.$$(OBJEXT): ${clippybase}_clippy.c
69 ${clippybase}.lo: ${clippybase}_clippy.c
70 ${clippybase}_clippy.c: $$(CLIPPY_DEPS)"""
71 )
72
73 clippyauxdep = Template(
74 """# clippy{
75 # auxiliary clippy target
76 ${target}: ${clippybase}_clippy.c
77 # }clippy"""
78 )
79
80 lines = before.splitlines()
81 autoderp = "#AUTODERP# "
82 out_lines = []
83 bcdeps = []
84 make_rule_re = re.compile("^([^:\s]+):\s*([^:\s]+)\s*($|\n)")
85
86 while lines:
87 line = lines.pop(0)
88 if line.startswith(autoderp):
89 line = line[len(autoderp) :]
90
91 if line == "# clippy{":
92 while lines:
93 line = lines.pop(0)
94 if line == "# }clippy":
95 break
96 continue
97
98 if line.startswith("#"):
99 out_lines.append(line)
100 continue
101
102 full_line = line
103 full_lines = lines[:]
104 while full_line.endswith("\\"):
105 full_line = full_line[:-1] + full_lines.pop(0)
106
107 m = make_rule_re.match(full_line)
108 if m is None:
109 out_lines.append(line)
110 continue
111
112 line, lines = full_line, full_lines
113
114 target, dep = m.group(1), m.group(2)
115
116 if target.endswith(".lo") or target.endswith(".o"):
117 if not dep.endswith(".h"):
118 bcdeps.append("%s.bc: %s" % (target, target))
119 bcdeps.append("\t$(AM_V_LLVM_BC)$(COMPILE) -emit-llvm -c -o $@ %s" % (dep))
120 if m.group(2) in clippy_scan:
121 out_lines.append(
122 clippyauxdep.substitute(target=m.group(1), clippybase=m.group(2)[:-2])
123 )
124
125 out_lines.append(line)
126
127 out_lines.append("# clippy{\n# main clippy targets")
128 for clippy_file in clippy_scan:
129 out_lines.append(clippydep.substitute(clippybase=clippy_file[:-2]))
130
131 out_lines.append("")
132 out_lines.append("xrefs = %s" % (" ".join(["%s.xref" % target for target in xref_targets])))
133 out_lines.append("frr.xref: $(xrefs)")
134 out_lines.append("")
135
136 #frr.xref: $(bin_PROGRAMS) $(sbin_PROGRAMS) $(lib_LTLIBRARIES) $(module_LTLIBRARIES)
137 # $(AM_V_XRELFO) $(CLIPPY) $(top_srcdir)/python/xrelfo.py -o $@ $^
138
139 out_lines.append("")
140 out_lines.extend(bcdeps)
141 out_lines.append("")
142 bc_targets = []
143 for varname in [
144 "bin_PROGRAMS",
145 "sbin_PROGRAMS",
146 "lib_LTLIBRARIES",
147 "module_LTLIBRARIES",
148 "noinst_LIBRARIES",
149 ]:
150 bc_targets.extend(mv[varname].strip().split())
151 for target in bc_targets:
152 amtgt = target.replace("/", "_").replace(".", "_").replace("-", "_")
153 objs = mv[amtgt + "_OBJECTS"].strip().split()
154 objs = [obj + ".bc" for obj in objs]
155 deps = mv.get(amtgt + "_DEPENDENCIES", "").strip().split()
156 deps = [d + ".bc" for d in deps if d.endswith(".a")]
157 objs.extend(deps)
158 out_lines.append("%s.bc: %s" % (target, " ".join(objs)))
159 out_lines.append("\t$(AM_V_LLVM_LD)$(LLVM_LINK) -o $@ $^")
160 out_lines.append("")
161
162 out_lines.append("# }clippy")
163 out_lines.append("")
164
165 after = "\n".join(out_lines)
166 if after == before:
167 sys.exit(0)
168
169 with open("Makefile.pyout", "w") as fd:
170 fd.write(after)
171 os.rename("Makefile.pyout", "Makefile")