Debian Codegraph @

share/analysis-scripts/build.py

raw
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 ##########################################################################
4 # #
5 # This file is part of Frama-C. #
6 # #
7 # Copyright (C) 2007-2022 #
8 # CEA (Commissariat à l'énergie atomique et aux énergies #
9 # alternatives) #
10 # #
11 # you can redistribute it and/or modify it under the terms of the GNU #
12 # Lesser General Public License as published by the Free Software #
13 # Foundation, version 2.1. #
14 # #
15 # It is distributed in the hope that it will be useful, #
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
18 # GNU Lesser General Public License for more details. #
19 # #
20 # See the GNU Lesser General Public License version 2.1 #
21 # for more details (enclosed in the file licenses/LGPLv2.1). #
22 # #
23 ##########################################################################
24
25 """This script uses blug and a build_commands.json file to produce an
26 analysis GNUmakefile, as automatically as possible."""
27
28 import argparse
29 import json
30 import logging
31 import os
32 from pathlib import Path
33 import re
34 import shutil
35 import sys
36 import subprocess
37
38 import function_finder
39 import source_filter
40
41 script_dir = os.path.dirname(sys.argv[0])
42
43 # Command-line parsing ########################################################
44
45 parser = argparse.ArgumentParser(
46 description="""Produces a GNUmakefile
47 for analysis with Frama-C. Tries to use a build_commands.json file if
48 available."""
49 )
50 parser.add_argument(
51 "--debug",
52 metavar="FILE",
53 help="enable debug mode and redirect output to the specified file",
54 )
55 parser.add_argument("--force", action="store_true", help="overwrite files without prompting")
56 parser.add_argument(
57 "--jbdb",
58 metavar="FILE",
59 default="build_commands.json",
60 help="path to JBDB (default: build_commands.json)",
61 )
62 parser.add_argument(
63 "--machdep", metavar="MACHDEP", help="analysis machdep (default: Frama-C's default)"
64 )
65 parser.add_argument(
66 "--main",
67 metavar="FUNCTION",
68 default="main",
69 help="name of the main function (default: main)",
70 )
71 parser.add_argument(
72 "--sources",
73 metavar="FILE",
74 nargs="+",
75 help="list of sources to parse (overrides --jbdb)",
76 type=Path,
77 )
78 parser.add_argument(
79 "--targets",
80 metavar="FILE",
81 nargs="+",
82 help="targets to build. When using --sources, " + "only a single target is allowed.",
83 type=Path,
84 )
85
86 args = parser.parse_args()
87 force = args.force
88 jbdb_path = args.jbdb
89 machdep = args.machdep
90 main = args.main
91 sources = args.sources
92 targets = args.targets
93 debug = args.debug
94
95 debug_level = logging.DEBUG if debug else logging.INFO
96 # special values for debug filename
97 if debug == "stdout":
98 logging.basicConfig(stream=sys.stdout, level=debug_level, format="[%(levelname)s] %(message)s")
99 elif debug == "stderr":
100 logging.basicConfig(stream=sys.stderr, level=debug_level, format="[%(levelname)s] %(message)s")
101 elif debug:
102 logging.basicConfig(
103 filename=debug,
104 level=debug_level,
105 filemode="w",
106 format="[%(levelname)s] %(message)s",
107 )
108 else:
109 logging.basicConfig(level=logging.INFO, format="[%(levelname)s] %(message)s")
110
111 dot_framac_dir = Path(".frama-c")
112
113 # Check required environment variables and commands in the PATH ###############
114
115 framac_bin = os.getenv("FRAMAC_BIN")
116 if not framac_bin:
117 sys.exit("error: FRAMAC_BIN not in environment (set by frama-c-script)")
118 framac_bin = Path(framac_bin)
119
120 under_test = os.getenv("PTESTS_TESTING")
121
122 # Prepare blug-related variables and functions ################################
123
124 blug = os.getenv("BLUG")
125 if not blug:
126 blug = shutil.which("blug")
127 if not blug:
128 sys.exit("error: path to 'blug' binary must be in PATH or variable BLUG")
129 blug = Path(blug)
130 blug_dir = blug.resolve().parent
131 # to import blug_jbdb
132 sys.path.insert(0, blug_dir.as_posix())
133 import blug_jbdb
134 from blug_jbdb import prettify
135
136
137 # Auxiliary functions #########################################################
138
139
140 def call_and_get_output(command_and_args):
141 try:
142 return subprocess.check_output(command_and_args, stderr=subprocess.STDOUT).decode()
143 except subprocess.CalledProcessError as e:
144 sys.exit(f"error running command: {command_and_args}\n{e}")
145
146
147 def ask_if_overwrite(path):
148 yn = input(f"warning: {path} already exists. Overwrite? [y/N] ")
149 if yn == "" or not (yn[0] == "Y" or yn[0] == "y"):
150 sys.exit("Exiting without overwriting.")
151
152
153 def insert_lines_after(lines, line_pattern, new_lines):
154 re_line = re.compile(line_pattern)
155 for i, line in enumerate(lines):
156 if re_line.search(line):
157 for j, new_line in enumerate(new_lines):
158 lines.insert(i + 1 + j, new_line)
159 return
160 sys.exit(f"error: no lines found matching pattern: {line_pattern}")
161
162
163 # delete the first occurrence of [line_pattern]
164 def delete_line(lines, line_pattern):
165 re_line = re.compile(line_pattern)
166 for i, line in enumerate(lines):
167 if re_line.search(line):
168 del lines[i]
169 return
170 sys.exit(f"error: no lines found matching pattern: {line_pattern}")
171
172
173 def replace_line(lines, line_pattern, value, all_occurrences=False):
174 replaced = False
175 re_line = re.compile(line_pattern)
176 for i, line in enumerate(lines):
177 if re_line.search(line):
178 lines[i] = value
179 replaced = True
180 if not all_occurrences:
181 return
182 if replaced:
183 return
184 sys.exit(f"error: no lines found matching pattern: {line_pattern}")
185
186
187 # replaces '/' and '.' with '_' so that a valid target name is created
188 def make_target_name(target):
189 return prettify(target).replace("/", "_").replace(".", "_")
190
191
192 # sources are pretty-printed relatively to the .frama-c directory, where the
193 # GNUmakefile will reside
194 def rel_prefix(path):
195 return path if os.path.isabs(path) else os.path.relpath(path, start=dot_framac_dir)
196
197
198 def pretty_sources(sources):
199 return [f" {rel_prefix(source)} \\" for source in sources]
200
201
202 def lines_of_file(path):
203 return path.read_text().splitlines()
204
205
206 fc_stubs_copied = False
207
208
209 def copy_fc_stubs():
210 global fc_stubs_copied
211 dest = dot_framac_dir / "fc_stubs.c"
212 if not fc_stubs_copied:
213 fc_stubs = lines_of_file(share_dir / "analysis-scripts" / "fc_stubs.c")
214 re_main = re.compile(r"\bmain\b")
215 for i, line in enumerate(fc_stubs):
216 if line.startswith("//"):
217 continue
218 fc_stubs[i] = re.sub(re_main, main, line)
219 if not force and dest.exists():
220 ask_if_overwrite(dest)
221 with open(dest, "w") as f:
222 f.write("\n".join(fc_stubs))
223 logging.info("wrote: %s", dest)
224 fc_stubs_copied = True
225 return dest
226
227
228 # Returns pairs (line_number, has_args) for each likely definition of
229 # [funcname] in [filename].
230 # [has_args] is used to distinguish between main(void) and main(int, char**).
231 def find_definitions(funcname, filename):
232 file_content = source_filter.open_and_filter(filename, not under_test)
233 file_lines = file_content.splitlines(keepends=True)
234 newlines = function_finder.compute_newline_offsets(file_lines)
235 defs = function_finder.find_definitions_and_declarations(
236 True, False, filename, file_content, file_lines, newlines, funcname
237 )
238 res = []
239 for d in defs:
240 defining_line = file_lines[d[2] - 1]
241 after_funcname = defining_line[defining_line.find(funcname) + len(funcname) :]
242 # heuristics: if there is a comma after the function name,
243 # it is very likely the signature contains arguments;
244 # otherwise, the function is either defined in several lines,
245 # or we somehow missed it. By default, we assume it has no arguments
246 # if we miss it.
247 has_args = "," in after_funcname
248 res.append((d[2], has_args))
249 return res
250
251
252 def list_partition(f, l):
253 """Equivalent to OCaml's List.partition: returns 2 lists with the elements of l,
254 partitioned according to predicate f."""
255 l1 = []
256 l2 = []
257 for e in l:
258 if f(l):
259 l1.append(e)
260 else:
261 l2.append(e)
262 return l1, l2
263
264
265 def pp_list(l):
266 """Applies prettify to a list of sources/targets and sorts the result."""
267 return sorted([prettify(e) for e in l])
268
269
270 # End of auxiliary functions ##################################################
271
272 sources_map = dict()
273 if sources:
274 if not targets:
275 sys.exit("error: option --targets is mandatory when --sources is specified")
276 if len(targets) > 1:
277 sys.exit(
278 "error: option --targets can only have a single target when --sources is specified"
279 )
280 sources_map[targets[0]] = sources
281 elif os.path.isfile(jbdb_path):
282 # JBDB exists
283 with open(jbdb_path, "r") as data:
284 jbdb = json.load(data)
285 blug_jbdb.absolutize_jbdb(jbdb)
286 filter_source, filter_target = blug_jbdb.get_filters("c-programs")
287 # program_targets are those we prefer, and the only ones used in "automatic" mode;
288 # non_program_targets are only used if the user specified them in the command line.
289 program_targets = []
290 non_program_targets = []
291 for f in jbdb:
292 programs, other_targets = list_partition(filter_target, f["targets"])
293 program_targets += programs
294 non_program_targets += other_targets
295 logging.debug("program_targets: %s", pp_list(program_targets))
296 logging.debug("non_program_targets: %s", pp_list(non_program_targets))
297 all_jbdb_targets = program_targets + non_program_targets
298 if not all_jbdb_targets:
299 sys.exit(f"no targets found in JBDB ({jbdb_path})")
300 if not targets:
301 # no targets specified in command line; use all programs from JBDB
302 targets = program_targets
303 logging.info("Computing sources for each target (%d target(s))...", len(targets))
304 unknown_targets_from_cmdline = []
305 graph = blug_jbdb.build_graph(jbdb)
306 for target in targets:
307 if target not in all_jbdb_targets:
308 unknown_targets_from_cmdline.append(target)
309 # do not return immediately; we want to report all invalid targets at once
310 else:
311 if unknown_targets_from_cmdline != []:
312 continue # keep looping to accumulate all invalid targets, but avoid extra work
313 sources = blug_jbdb.collect_leaves(graph, [target])
314 c_sources, non_c_sources = list_partition(filter_source, sources)
315 logging.debug("non_c_sources: %s", pp_list(non_c_sources))
316 sources_map[target] = c_sources
317 if unknown_targets_from_cmdline:
318 targets_pretty = "\n".join(unknown_targets_from_cmdline)
319 sys.exit("target(s) not found in JBDB:\n{targets_pretty}")
320 else:
321 if not jbdb_path:
322 sys.exit("error: either a JBDB or option --sources are required")
323 else:
324 sys.exit(f"error: invalid JBDB path: '{jbdb_path}'")
325
326 logging.debug(
327 "sources_map: %s",
328 sorted([prettify(k) + ": " + ", ".join(pp_list(v)) for (k, v) in sources_map.items()]),
329 )
330 logging.debug("targets: %s", pp_list(targets))
331
332 # check that source files exist
333 unknown_sources = sorted({s for sources in sources_map.values() for s in sources if not s.exists()})
334 if unknown_sources:
335 sys.exit("error: source(s) not found:\n" + "\n".join(pp_list(unknown_sources)))
336
337 # Check that the main function is defined exactly once per target.
338 # note: this is only based on heuristics (and fails on a few real case studies),
339 # so we cannot emit errors, only warnings.
340 # We also need to check if the main function uses a 'main(void)'-style
341 # signature, to patch fc_stubs.c.
342
343 main_definitions = {}
344 for target, sources in sources_map.items():
345 main_definitions[target] = []
346 for source in sources:
347 fundefs = find_definitions(main, source)
348 main_definitions[target] += [(source, fundef[0], fundef[1]) for fundef in fundefs]
349 if main_definitions[target] == []:
350 logging.warning(
351 "function '%s' seems to be never defined in the sources of target '%s'",
352 main,
353 prettify(target),
354 )
355 elif len(main_definitions[target]) > 1:
356 logging.warning(
357 "function '%s' seems to be defined multiple times in the sources of target '%s':",
358 main,
359 prettify(target),
360 )
361 for (filename, line, _) in main_definitions[target]:
362 print(f"- definition at {filename}:{line}")
363
364 # End of checks; start writing GNUmakefile and stubs from templates ###########
365
366 if not dot_framac_dir.is_dir():
367 logging.debug("creating %s", dot_framac_dir)
368 dot_framac_dir.mkdir(parents=True, exist_ok=False)
369
370 fc_config = json.loads(call_and_get_output([framac_bin / "frama-c", "-print-config-json"]))
371 share_dir = Path(fc_config["datadir"])
372
373 # copy fc_stubs if at least one main function has arguments
374 any_has_arguments = False
375 for defs in main_definitions.values():
376 if any(d[2] for d in defs):
377 any_has_arguments = True
378 break
379
380 if any_has_arguments:
381 fc_stubs = copy_fc_stubs()
382 for target in targets:
383 if any(d[2] for d in main_definitions[target]):
384 logging.debug(
385 "target %s has main with args, adding fc_stubs.c to its sources",
386 prettify(target),
387 )
388 sources_map[target].insert(0, fc_stubs)
389
390 gnumakefile = dot_framac_dir / "GNUmakefile"
391
392 template = lines_of_file(share_dir / "analysis-scripts" / "template.mk")
393
394 if machdep:
395 machdeps = fc_config["machdeps"]
396 if machdep not in machdeps:
397 logging.warning(
398 "unknown machdep (%s) not in Frama-C's default machdeps:\n%s",
399 machdep,
400 " ".join(machdeps),
401 )
402 replace_line(template, "^MACHDEP = .*", f"MACHDEP = {machdep}")
403
404 if jbdb_path:
405 insert_lines_after(
406 template,
407 "^FCFLAGS",
408 [f" -json-compilation-database {rel_prefix(jbdb_path)} \\"],
409 )
410
411 targets_eva = [f" {make_target_name(target)}.eva \\" for target in targets]
412 replace_line(template, "^TARGETS = main.eva", "TARGETS = \\")
413 insert_lines_after(template, r"^TARGETS = \\", targets_eva)
414
415 delete_line(template, r"^main.parse: \\")
416 delete_line(template, r"^ main.c \\")
417 for target, sources in reversed(sources_map.items()):
418 pp_target = make_target_name(target)
419 new_lines = [f"{pp_target}.parse: \\"] + pretty_sources(sources) + [""]
420 if any(d[2] for d in main_definitions[target]):
421 logging.debug(
422 "target %s has main with args, adding -main eva_main to its FCFLAGS",
423 prettify(target),
424 )
425 new_lines += [f"{pp_target}.parse: FCFLAGS += -main eva_main", ""]
426 insert_lines_after(template, "^### Each target <t>.eva", new_lines)
427
428 gnumakefile.write_text("\n".join(template))
429
430 logging.info("wrote: %s", gnumakefile)
431
432 # write path.mk, but only if it does not exist.
433 path_mk = dot_framac_dir / "path.mk"
434 if not force and path_mk.exists():
435 logging.info("%s already exists, will not overwrite it", path_mk)
436 else:
437 path_mk.write_text(
438 f"""FRAMAC_BIN={framac_bin}
439 ifeq ($(wildcard $(FRAMAC_BIN)),)
440 # Frama-C not installed locally; using the version in the PATH
441 else
442 FRAMAC=$(FRAMAC_BIN)/frama-c
443 FRAMAC_GUI=$(FRAMAC_BIN)/frama-c-gui
444 endif
445 """
446 )
447 logging.info("wrote: %s", path_mk)