]>
git.proxmox.com Git - mirror_frr.git/blob - python/makefile.py
3 # FRR extended automake/Makefile functionality helper
5 # This script is executed on/after generating Makefile to add some pieces for
13 from string
import Template
14 from makevars
import MakeReVars
16 argp
= argparse
.ArgumentParser(description
="FRR Makefile extensions")
21 help="run additional developer checks",
23 args
= argp
.parse_args()
25 with
open("Makefile", "r") as fd
:
28 mv
= MakeReVars(before
)
30 clippy_scan
= mv
["clippy_scan"].strip().split()
31 for clippy_file
in clippy_scan
:
32 assert clippy_file
.endswith(".c")
41 xref_targets
.extend(mv
[varname
].strip().split())
43 # check for files using clippy but not listed in clippy_scan
45 basepath
= os
.path
.dirname(os
.path
.dirname(os
.path
.abspath(__file__
)))
46 if os
.path
.exists(os
.path
.join(basepath
, ".git")):
47 clippy_ref
= subprocess
.check_output(
55 "^#\s*include.*_clippy.c",
61 clippy_ref
= set(clippy_ref
.splitlines())
62 missing
= clippy_ref
- set(clippy_scan
)
66 'error: files seem to be using clippy, but not listed in "clippy_scan" in subdir.am:\n\t%s\n'
67 % ("\n\t".join(sorted(missing
)))
71 # this additional-dependency rule is stuck onto all compile targets that
72 # compile a file which uses clippy-generated input, so it has a dependency to
76 ${clippybase}.$$(OBJEXT): ${clippybase}_clippy.c
77 ${clippybase}.lo: ${clippybase}_clippy.c
78 ${clippybase}_clippy.c: $$(CLIPPY_DEPS)"""
81 # this one is used when one .c file is built multiple times with different
83 clippyauxdep
= Template(
85 # auxiliary clippy target
86 ${target}: ${clippybase}_clippy.c
90 lines
= before
.splitlines()
91 autoderp
= "#AUTODERP# "
94 make_rule_re
= re
.compile("^([^:\s]+):\s*([^:\s]+)\s*($|\n)")
98 if line
.startswith(autoderp
):
99 line
= line
[len(autoderp
) :]
101 # allow rerunning on already-clippified Makefile
102 if line
== "# clippy{":
105 if line
== "# }clippy":
109 if line
.startswith("#"):
110 out_lines
.append(line
)
114 full_lines
= lines
[:]
115 while full_line
.endswith("\\"):
116 full_line
= full_line
[:-1] + full_lines
.pop(0)
118 m
= make_rule_re
.match(full_line
)
120 out_lines
.append(line
)
123 line
, lines
= full_line
, full_lines
125 target
, dep
= m
.group(1), m
.group(2)
127 filename
= os
.path
.basename(target
)
129 # dashes in output filename = building same .c with different CFLAGS
130 am_name
, _
= filename
.split("-", 1)
131 am_name
= os
.path
.join(os
.path
.dirname(target
), am_name
)
132 am_name
= am_name
.replace("/", "_")
133 extraflags
= " $(%s_CFLAGS)" % (am_name
,)
135 # this path isn't really triggered because automake is using a generic
136 # .c => .o rule unless CFLAGS are customized for a target
139 if target
.endswith(".lo") or target
.endswith(".o"):
140 if not dep
.endswith(".h"):
141 # LLVM bitcode targets for analysis tools
142 bcdeps
.append("%s.bc: %s" % (target
, target
))
144 "\t$(AM_V_LLVM_BC)$(COMPILE)%s -emit-llvm -c -o $@ %s"
147 if m
.group(2) in clippy_scan
:
148 # again - this is only hit for targets with custom CFLAGS, because
149 # automake uses a generic .c -> .o rule for standard CFLAGS
151 clippyauxdep
.substitute(target
=m
.group(1), clippybase
=m
.group(2)[:-2])
154 out_lines
.append(line
)
156 # now, cover all the .c files that don't have special build rules
157 out_lines
.append("# clippy{\n# main clippy targets")
158 for clippy_file
in clippy_scan
:
159 out_lines
.append(clippydep
.substitute(clippybase
=clippy_file
[:-2]))
161 # combine daemon .xref files into frr.xref
164 "xrefs = %s" % (" ".join(["%s.xref" % target
for target
in xref_targets
]))
166 out_lines
.append("frr.xref: $(xrefs)")
169 # analog but slower way to get the same frr.xref
170 # frr.xref: $(bin_PROGRAMS) $(sbin_PROGRAMS) $(lib_LTLIBRARIES) $(module_LTLIBRARIES)
171 # $(AM_V_XRELFO) $(CLIPPY) $(top_srcdir)/python/xrelfo.py -o $@ $^
173 # LLVM bitcode link targets creating a .bc file for whole daemon or lib
175 out_lines
.extend(bcdeps
)
182 "module_LTLIBRARIES",
185 bc_targets
.extend(mv
[varname
].strip().split())
186 for target
in bc_targets
:
187 amtgt
= target
.replace("/", "_").replace(".", "_").replace("-", "_")
188 objs
= mv
[amtgt
+ "_OBJECTS"].strip().split()
189 objs
= [obj
+ ".bc" for obj
in objs
]
190 deps
= mv
.get(amtgt
+ "_DEPENDENCIES", "").strip().split()
191 deps
= [d
+ ".bc" for d
in deps
if d
.endswith(".a")]
193 out_lines
.append("%s.bc: %s" % (target
, " ".join(objs
)))
194 out_lines
.append("\t$(AM_V_LLVM_LD)$(LLVM_LINK) -o $@ $^")
197 out_lines
.append("# }clippy")
200 after
= "\n".join(out_lines
)
204 with
open("Makefile.pyout", "w") as fd
:
206 os
.rename("Makefile.pyout", "Makefile")