summaryrefslogtreecommitdiff
path: root/clang/utils/analyzer/SATestAdd.py
blob: ce64bc82f756fdd4bbd5e8b6904dfaacc8fc18a6 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env python

"""
Static Analyzer qualification infrastructure: adding a new project to 
the Repository Directory.

 Add a new project for testing: build it and add to the Project Map file.
   Assumes it's being run from the Repository Directory.
   The project directory should be added inside the Repository Directory and 
   have the same name as the project ID
   
 The project should use the following files for set up:
      - pre_run_static_analyzer.sh - prepare the build environment.
                                     Ex: make clean can be a part of it.
      - run_static_analyzer.cmd - a list of commands to run through scan-build.
                                     Each command should be on a separate line.
                                     Choose from: configure, make, xcodebuild 
"""
import SATestBuild

import os
import csv
import sys

def isExistingProject(PMapFile, projectID) :
    PMapReader = csv.reader(PMapFile)
    for I in PMapReader:
        if projectID == I[0]:
            return True
    return False    

# Add a new project for testing: build it and add to the Project Map file.
# Params:
#   Dir is the directory where the sources are.
#   ID is a short string used to identify a project.
def addNewProject(ID, IsScanBuild) :
    CurDir = os.path.abspath(os.curdir)
    Dir = SATestBuild.getProjectDir(ID)
    if not os.path.exists(Dir):
        print "Error: Project directory is missing: %s" % Dir
        sys.exit(-1)
        
    # Build the project.
    SATestBuild.testProject(ID, True, IsScanBuild, Dir)

    # Add the project ID to the project map.
    ProjectMapPath = os.path.join(CurDir, SATestBuild.ProjectMapFile)
    if os.path.exists(ProjectMapPath):
        PMapFile = open(ProjectMapPath, "r+b")
    else:
        print "Warning: Creating the Project Map file!!"
        PMapFile = open(ProjectMapPath, "w+b")
    try:
        if (isExistingProject(PMapFile, ID)) :        
            print >> sys.stdout, 'Warning: Project with ID \'', ID, \
                                 '\' already exists.'
            print >> sys.stdout, "Reference output has been regenerated."
        else:                     
            PMapWriter = csv.writer(PMapFile)
            PMapWriter.writerow( (ID, int(IsScanBuild)) );
            print "The project map is updated: ", ProjectMapPath
    finally:
        PMapFile.close()
            

# TODO: Add an option not to build. 
# TODO: Set the path to the Repository directory.
if __name__ == '__main__':
    if len(sys.argv) < 2:
        print >> sys.stderr, 'Usage: ', sys.argv[0],\
                             'project_ID <mode>' \
                             'mode - 0 for single file project; 1 for scan_build'
        sys.exit(-1)
    
    IsScanBuild = 1    
    if (len(sys.argv) >= 3):
        IsScanBuild = int(sys.argv[2])  
    assert((IsScanBuild == 0) | (IsScanBuild == 1))
        
    addNewProject(sys.argv[1], IsScanBuild)