xref: /aosp_15_r20/external/clang/utils/analyzer/SATestAdd.py (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li#!/usr/bin/env python
2*67e74705SXin Li
3*67e74705SXin Li"""
4*67e74705SXin LiStatic Analyzer qualification infrastructure: adding a new project to
5*67e74705SXin Lithe Repository Directory.
6*67e74705SXin Li
7*67e74705SXin Li Add a new project for testing: build it and add to the Project Map file.
8*67e74705SXin Li   Assumes it's being run from the Repository Directory.
9*67e74705SXin Li   The project directory should be added inside the Repository Directory and
10*67e74705SXin Li   have the same name as the project ID
11*67e74705SXin Li
12*67e74705SXin Li The project should use the following files for set up:
13*67e74705SXin Li      - cleanup_run_static_analyzer.sh - prepare the build environment.
14*67e74705SXin Li                                     Ex: make clean can be a part of it.
15*67e74705SXin Li      - run_static_analyzer.cmd - a list of commands to run through scan-build.
16*67e74705SXin Li                                     Each command should be on a separate line.
17*67e74705SXin Li                                     Choose from: configure, make, xcodebuild
18*67e74705SXin Li      - download_project.sh - download the project into the CachedSource/
19*67e74705SXin Li                                     directory. For example, download a zip of
20*67e74705SXin Li                                     the project source from GitHub, unzip it,
21*67e74705SXin Li                                     and rename the unzipped directory to
22*67e74705SXin Li                                     'CachedSource'. This script is not called
23*67e74705SXin Li                                     when 'CachedSource' is already present,
24*67e74705SXin Li                                     so an alternative is to check the
25*67e74705SXin Li                                     'CachedSource' directory into the
26*67e74705SXin Li                                     repository directly.
27*67e74705SXin Li      - CachedSource/ - An optional directory containing the source of the
28*67e74705SXin Li                                     project being analyzed. If present,
29*67e74705SXin Li                                     download_project.sh will not be called.
30*67e74705SXin Li      - changes_for_analyzer.patch - An optional patch file for any local changes
31*67e74705SXin Li                                     (e.g., to adapt to newer version of clang)
32*67e74705SXin Li                                     that should be applied to CachedSource
33*67e74705SXin Li                                     before analysis. To construct this patch,
34*67e74705SXin Li                                     run the the download script to download
35*67e74705SXin Li                                     the project to CachedSource, copy the
36*67e74705SXin Li                                     CachedSource to another directory (for
37*67e74705SXin Li                                     example, PatchedSource) and make any needed
38*67e74705SXin Li                                     modifications to the the copied source.
39*67e74705SXin Li                                     Then run:
40*67e74705SXin Li                                          diff -ur CachedSource PatchedSource \
41*67e74705SXin Li                                              > changes_for_analyzer.patch
42*67e74705SXin Li"""
43*67e74705SXin Liimport SATestBuild
44*67e74705SXin Li
45*67e74705SXin Liimport os
46*67e74705SXin Liimport csv
47*67e74705SXin Liimport sys
48*67e74705SXin Li
49*67e74705SXin Lidef isExistingProject(PMapFile, projectID) :
50*67e74705SXin Li    PMapReader = csv.reader(PMapFile)
51*67e74705SXin Li    for I in PMapReader:
52*67e74705SXin Li        if projectID == I[0]:
53*67e74705SXin Li            return True
54*67e74705SXin Li    return False
55*67e74705SXin Li
56*67e74705SXin Li# Add a new project for testing: build it and add to the Project Map file.
57*67e74705SXin Li# Params:
58*67e74705SXin Li#   Dir is the directory where the sources are.
59*67e74705SXin Li#   ID is a short string used to identify a project.
60*67e74705SXin Lidef addNewProject(ID, BuildMode) :
61*67e74705SXin Li    CurDir = os.path.abspath(os.curdir)
62*67e74705SXin Li    Dir = SATestBuild.getProjectDir(ID)
63*67e74705SXin Li    if not os.path.exists(Dir):
64*67e74705SXin Li        print "Error: Project directory is missing: %s" % Dir
65*67e74705SXin Li        sys.exit(-1)
66*67e74705SXin Li
67*67e74705SXin Li    # Build the project.
68*67e74705SXin Li    SATestBuild.testProject(ID, BuildMode, IsReferenceBuild=True, Dir=Dir)
69*67e74705SXin Li
70*67e74705SXin Li    # Add the project ID to the project map.
71*67e74705SXin Li    ProjectMapPath = os.path.join(CurDir, SATestBuild.ProjectMapFile)
72*67e74705SXin Li    if os.path.exists(ProjectMapPath):
73*67e74705SXin Li        PMapFile = open(ProjectMapPath, "r+b")
74*67e74705SXin Li    else:
75*67e74705SXin Li        print "Warning: Creating the Project Map file!!"
76*67e74705SXin Li        PMapFile = open(ProjectMapPath, "w+b")
77*67e74705SXin Li    try:
78*67e74705SXin Li        if (isExistingProject(PMapFile, ID)) :
79*67e74705SXin Li            print >> sys.stdout, 'Warning: Project with ID \'', ID, \
80*67e74705SXin Li                                 '\' already exists.'
81*67e74705SXin Li            print >> sys.stdout, "Reference output has been regenerated."
82*67e74705SXin Li        else:
83*67e74705SXin Li            PMapWriter = csv.writer(PMapFile)
84*67e74705SXin Li            PMapWriter.writerow( (ID, int(BuildMode)) );
85*67e74705SXin Li            print "The project map is updated: ", ProjectMapPath
86*67e74705SXin Li    finally:
87*67e74705SXin Li        PMapFile.close()
88*67e74705SXin Li
89*67e74705SXin Li
90*67e74705SXin Li# TODO: Add an option not to build.
91*67e74705SXin Li# TODO: Set the path to the Repository directory.
92*67e74705SXin Liif __name__ == '__main__':
93*67e74705SXin Li    if len(sys.argv) < 2:
94*67e74705SXin Li        print >> sys.stderr, 'Usage: ', sys.argv[0],\
95*67e74705SXin Li                             'project_ID <mode>' \
96*67e74705SXin Li                             'mode - 0 for single file project; ' \
97*67e74705SXin Li                             '1 for scan_build; ' \
98*67e74705SXin Li                             '2 for single file c++11 project'
99*67e74705SXin Li        sys.exit(-1)
100*67e74705SXin Li
101*67e74705SXin Li    BuildMode = 1
102*67e74705SXin Li    if (len(sys.argv) >= 3):
103*67e74705SXin Li        BuildMode = int(sys.argv[2])
104*67e74705SXin Li    assert((BuildMode == 0) | (BuildMode == 1) | (BuildMode == 2))
105*67e74705SXin Li
106*67e74705SXin Li    addNewProject(sys.argv[1], BuildMode)
107