xref: /openbsd-src/gnu/llvm/clang/utils/analyzer/SATestAdd.py (revision ec727ea710c91afd8ce4f788c5aaa8482b7b69b2)
1e5dd7070Spatrick#!/usr/bin/env python
2e5dd7070Spatrick
3e5dd7070Spatrick"""
4e5dd7070SpatrickStatic Analyzer qualification infrastructure: adding a new project to
5e5dd7070Spatrickthe Repository Directory.
6e5dd7070Spatrick
7e5dd7070Spatrick Add a new project for testing: build it and add to the Project Map file.
8e5dd7070Spatrick   Assumes it's being run from the Repository Directory.
9e5dd7070Spatrick   The project directory should be added inside the Repository Directory and
10e5dd7070Spatrick   have the same name as the project ID
11e5dd7070Spatrick
12e5dd7070Spatrick The project should use the following files for set up:
13e5dd7070Spatrick      - cleanup_run_static_analyzer.sh - prepare the build environment.
14e5dd7070Spatrick                                     Ex: make clean can be a part of it.
15e5dd7070Spatrick      - run_static_analyzer.cmd - a list of commands to run through scan-build.
16e5dd7070Spatrick                                     Each command should be on a separate line.
17e5dd7070Spatrick                                     Choose from: configure, make, xcodebuild
18e5dd7070Spatrick      - download_project.sh - download the project into the CachedSource/
19e5dd7070Spatrick                                     directory. For example, download a zip of
20e5dd7070Spatrick                                     the project source from GitHub, unzip it,
21e5dd7070Spatrick                                     and rename the unzipped directory to
22e5dd7070Spatrick                                     'CachedSource'. This script is not called
23e5dd7070Spatrick                                     when 'CachedSource' is already present,
24e5dd7070Spatrick                                     so an alternative is to check the
25e5dd7070Spatrick                                     'CachedSource' directory into the
26e5dd7070Spatrick                                     repository directly.
27e5dd7070Spatrick      - CachedSource/ - An optional directory containing the source of the
28e5dd7070Spatrick                                     project being analyzed. If present,
29e5dd7070Spatrick                                     download_project.sh will not be called.
30e5dd7070Spatrick      - changes_for_analyzer.patch - An optional patch file for any local
31e5dd7070Spatrick                                     changes
32e5dd7070Spatrick                                     (e.g., to adapt to newer version of clang)
33e5dd7070Spatrick                                     that should be applied to CachedSource
34e5dd7070Spatrick                                     before analysis. To construct this patch,
35e5dd7070Spatrick                                     run the download script to download
36e5dd7070Spatrick                                     the project to CachedSource, copy the
37e5dd7070Spatrick                                     CachedSource to another directory (for
38e5dd7070Spatrick                                     example, PatchedSource) and make any
39e5dd7070Spatrick                                     needed modifications to the copied
40e5dd7070Spatrick                                     source.
41e5dd7070Spatrick                                     Then run:
42e5dd7070Spatrick                                          diff -ur CachedSource PatchedSource \
43e5dd7070Spatrick                                              > changes_for_analyzer.patch
44e5dd7070Spatrick"""
45e5dd7070Spatrickimport SATestBuild
46*ec727ea7Spatrickfrom ProjectMap import ProjectMap, ProjectInfo
47e5dd7070Spatrick
48e5dd7070Spatrickimport os
49e5dd7070Spatrickimport sys
50e5dd7070Spatrick
51e5dd7070Spatrick
52*ec727ea7Spatrickdef add_new_project(project: ProjectInfo):
53e5dd7070Spatrick    """
54e5dd7070Spatrick    Add a new project for testing: build it and add to the Project Map file.
55*ec727ea7Spatrick    :param name: is a short string used to identify a project.
56e5dd7070Spatrick    """
57e5dd7070Spatrick
58*ec727ea7Spatrick    test_info = SATestBuild.TestInfo(project,
59*ec727ea7Spatrick                                     is_reference_build=True)
60*ec727ea7Spatrick    tester = SATestBuild.ProjectTester(test_info)
61*ec727ea7Spatrick
62*ec727ea7Spatrick    project_dir = tester.get_project_dir()
63*ec727ea7Spatrick    if not os.path.exists(project_dir):
64*ec727ea7Spatrick        print(f"Error: Project directory is missing: {project_dir}")
65e5dd7070Spatrick        sys.exit(-1)
66e5dd7070Spatrick
67e5dd7070Spatrick    # Build the project.
68*ec727ea7Spatrick    tester.test()
69e5dd7070Spatrick
70*ec727ea7Spatrick    # Add the project name to the project map.
71*ec727ea7Spatrick    project_map = ProjectMap(should_exist=False)
72e5dd7070Spatrick
73*ec727ea7Spatrick    if is_existing_project(project_map, project):
74*ec727ea7Spatrick        print(f"Warning: Project with name '{project.name}' already exists.",
75*ec727ea7Spatrick              file=sys.stdout)
76e5dd7070Spatrick        print("Reference output has been regenerated.", file=sys.stdout)
77e5dd7070Spatrick    else:
78*ec727ea7Spatrick        project_map.projects.append(project)
79*ec727ea7Spatrick        project_map.save()
80e5dd7070Spatrick
81e5dd7070Spatrick
82*ec727ea7Spatrickdef is_existing_project(project_map: ProjectMap, project: ProjectInfo) -> bool:
83*ec727ea7Spatrick    return any(existing_project.name == project.name
84*ec727ea7Spatrick               for existing_project in project_map.projects)
85e5dd7070Spatrick
86e5dd7070Spatrick
87*ec727ea7Spatrickif __name__ == "__main__":
88*ec727ea7Spatrick    print("SATestAdd.py should not be used on its own.")
89*ec727ea7Spatrick    print("Please use 'SATest.py add' instead")
90*ec727ea7Spatrick    sys.exit(1)
91