xref: /minix3/external/bsd/llvm/dist/clang/utils/analyzer/SATestAdd.py (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc#!/usr/bin/env python
2*f4a2713aSLionel Sambuc
3*f4a2713aSLionel Sambuc"""
4*f4a2713aSLionel SambucStatic Analyzer qualification infrastructure: adding a new project to
5*f4a2713aSLionel Sambucthe Repository Directory.
6*f4a2713aSLionel Sambuc
7*f4a2713aSLionel Sambuc Add a new project for testing: build it and add to the Project Map file.
8*f4a2713aSLionel Sambuc   Assumes it's being run from the Repository Directory.
9*f4a2713aSLionel Sambuc   The project directory should be added inside the Repository Directory and
10*f4a2713aSLionel Sambuc   have the same name as the project ID
11*f4a2713aSLionel Sambuc
12*f4a2713aSLionel Sambuc The project should use the following files for set up:
13*f4a2713aSLionel Sambuc      - pre_run_static_analyzer.sh - prepare the build environment.
14*f4a2713aSLionel Sambuc                                     Ex: make clean can be a part of it.
15*f4a2713aSLionel Sambuc      - run_static_analyzer.cmd - a list of commands to run through scan-build.
16*f4a2713aSLionel Sambuc                                     Each command should be on a separate line.
17*f4a2713aSLionel Sambuc                                     Choose from: configure, make, xcodebuild
18*f4a2713aSLionel Sambuc"""
19*f4a2713aSLionel Sambucimport SATestBuild
20*f4a2713aSLionel Sambuc
21*f4a2713aSLionel Sambucimport os
22*f4a2713aSLionel Sambucimport csv
23*f4a2713aSLionel Sambucimport sys
24*f4a2713aSLionel Sambuc
25*f4a2713aSLionel Sambucdef isExistingProject(PMapFile, projectID) :
26*f4a2713aSLionel Sambuc    PMapReader = csv.reader(PMapFile)
27*f4a2713aSLionel Sambuc    for I in PMapReader:
28*f4a2713aSLionel Sambuc        if projectID == I[0]:
29*f4a2713aSLionel Sambuc            return True
30*f4a2713aSLionel Sambuc    return False
31*f4a2713aSLionel Sambuc
32*f4a2713aSLionel Sambuc# Add a new project for testing: build it and add to the Project Map file.
33*f4a2713aSLionel Sambuc# Params:
34*f4a2713aSLionel Sambuc#   Dir is the directory where the sources are.
35*f4a2713aSLionel Sambuc#   ID is a short string used to identify a project.
36*f4a2713aSLionel Sambucdef addNewProject(ID, BuildMode) :
37*f4a2713aSLionel Sambuc    CurDir = os.path.abspath(os.curdir)
38*f4a2713aSLionel Sambuc    Dir = SATestBuild.getProjectDir(ID)
39*f4a2713aSLionel Sambuc    if not os.path.exists(Dir):
40*f4a2713aSLionel Sambuc        print "Error: Project directory is missing: %s" % Dir
41*f4a2713aSLionel Sambuc        sys.exit(-1)
42*f4a2713aSLionel Sambuc
43*f4a2713aSLionel Sambuc    # Build the project.
44*f4a2713aSLionel Sambuc    SATestBuild.testProject(ID, BuildMode, IsReferenceBuild=True, Dir=Dir)
45*f4a2713aSLionel Sambuc
46*f4a2713aSLionel Sambuc    # Add the project ID to the project map.
47*f4a2713aSLionel Sambuc    ProjectMapPath = os.path.join(CurDir, SATestBuild.ProjectMapFile)
48*f4a2713aSLionel Sambuc    if os.path.exists(ProjectMapPath):
49*f4a2713aSLionel Sambuc        PMapFile = open(ProjectMapPath, "r+b")
50*f4a2713aSLionel Sambuc    else:
51*f4a2713aSLionel Sambuc        print "Warning: Creating the Project Map file!!"
52*f4a2713aSLionel Sambuc        PMapFile = open(ProjectMapPath, "w+b")
53*f4a2713aSLionel Sambuc    try:
54*f4a2713aSLionel Sambuc        if (isExistingProject(PMapFile, ID)) :
55*f4a2713aSLionel Sambuc            print >> sys.stdout, 'Warning: Project with ID \'', ID, \
56*f4a2713aSLionel Sambuc                                 '\' already exists.'
57*f4a2713aSLionel Sambuc            print >> sys.stdout, "Reference output has been regenerated."
58*f4a2713aSLionel Sambuc        else:
59*f4a2713aSLionel Sambuc            PMapWriter = csv.writer(PMapFile)
60*f4a2713aSLionel Sambuc            PMapWriter.writerow( (ID, int(BuildMode)) );
61*f4a2713aSLionel Sambuc            print "The project map is updated: ", ProjectMapPath
62*f4a2713aSLionel Sambuc    finally:
63*f4a2713aSLionel Sambuc        PMapFile.close()
64*f4a2713aSLionel Sambuc
65*f4a2713aSLionel Sambuc
66*f4a2713aSLionel Sambuc# TODO: Add an option not to build.
67*f4a2713aSLionel Sambuc# TODO: Set the path to the Repository directory.
68*f4a2713aSLionel Sambucif __name__ == '__main__':
69*f4a2713aSLionel Sambuc    if len(sys.argv) < 2:
70*f4a2713aSLionel Sambuc        print >> sys.stderr, 'Usage: ', sys.argv[0],\
71*f4a2713aSLionel Sambuc                             'project_ID <mode>' \
72*f4a2713aSLionel Sambuc                             'mode - 0 for single file project; ' \
73*f4a2713aSLionel Sambuc                             '1 for scan_build; ' \
74*f4a2713aSLionel Sambuc                             '2 for single file c++11 project'
75*f4a2713aSLionel Sambuc        sys.exit(-1)
76*f4a2713aSLionel Sambuc
77*f4a2713aSLionel Sambuc    BuildMode = 1
78*f4a2713aSLionel Sambuc    if (len(sys.argv) >= 3):
79*f4a2713aSLionel Sambuc        BuildMode = int(sys.argv[2])
80*f4a2713aSLionel Sambuc    assert((BuildMode == 0) | (BuildMode == 1) | (BuildMode == 2))
81*f4a2713aSLionel Sambuc
82*f4a2713aSLionel Sambuc    addNewProject(sys.argv[1], BuildMode)
83