blob: c8c7069a261d7e4167f3a544dec5f762a362c4b3 [file] [log] [blame]
Anna Zaksf0c41162011-10-06 23:26:27 +00001#!/usr/bin/env python
2
3"""
Ted Kremenek3a0678e2015-09-08 03:50:52 +00004Static Analyzer qualification infrastructure: adding a new project to
Anna Zaksf0c41162011-10-06 23:26:27 +00005the Repository Directory.
6
7 Add a new project for testing: build it and add to the Project Map file.
8 Assumes it's being run from the Repository Directory.
Ted Kremenek3a0678e2015-09-08 03:50:52 +00009 The project directory should be added inside the Repository Directory and
Anna Zaksf0c41162011-10-06 23:26:27 +000010 have the same name as the project ID
Ted Kremenek3a0678e2015-09-08 03:50:52 +000011
Anna Zaksf0c41162011-10-06 23:26:27 +000012 The project should use the following files for set up:
Devin Coughlin2cb767d2015-11-07 18:27:35 +000013 - cleanup_run_static_analyzer.sh - prepare the build environment.
Anna Zaksf0c41162011-10-06 23:26:27 +000014 Ex: make clean can be a part of it.
15 - run_static_analyzer.cmd - a list of commands to run through scan-build.
16 Each command should be on a separate line.
Ted Kremenek3a0678e2015-09-08 03:50:52 +000017 Choose from: configure, make, xcodebuild
Devin Coughlin2cb767d2015-11-07 18:27:35 +000018 - download_project.sh - download the project into the CachedSource/
19 directory. For example, download a zip of
20 the project source from GitHub, unzip it,
21 and rename the unzipped directory to
22 'CachedSource'. This script is not called
23 when 'CachedSource' is already present,
24 so an alternative is to check the
25 'CachedSource' directory into the
26 repository directly.
27 - CachedSource/ - An optional directory containing the source of the
28 project being analyzed. If present,
29 download_project.sh will not be called.
30 - changes_for_analyzer.patch - An optional patch file for any local changes
31 (e.g., to adapt to newer version of clang)
32 that should be applied to CachedSource
33 before analysis. To construct this patch,
34 run the the download script to download
35 the project to CachedSource, copy the
36 CachedSource to another directory (for
37 example, PatchedSource) and make any needed
38 modifications to the the copied source.
39 Then run:
40 diff -ur CachedSource PatchedSource \
41 > changes_for_analyzer.patch
Anna Zaksf0c41162011-10-06 23:26:27 +000042"""
43import SATestBuild
44
45import os
46import csv
47import sys
48
Anna Zaksa1b56e22011-11-08 22:41:22 +000049def isExistingProject(PMapFile, projectID) :
50 PMapReader = csv.reader(PMapFile)
51 for I in PMapReader:
52 if projectID == I[0]:
53 return True
Ted Kremenek3a0678e2015-09-08 03:50:52 +000054 return False
Anna Zaksa1b56e22011-11-08 22:41:22 +000055
Anna Zaksf0c41162011-10-06 23:26:27 +000056# Add a new project for testing: build it and add to the Project Map file.
57# Params:
58# Dir is the directory where the sources are.
59# ID is a short string used to identify a project.
Anna Zaksa2f970b2012-09-06 23:30:27 +000060def addNewProject(ID, BuildMode) :
Anna Zaksf0c41162011-10-06 23:26:27 +000061 CurDir = os.path.abspath(os.curdir)
62 Dir = SATestBuild.getProjectDir(ID)
63 if not os.path.exists(Dir):
64 print "Error: Project directory is missing: %s" % Dir
65 sys.exit(-1)
Ted Kremenek3a0678e2015-09-08 03:50:52 +000066
Anna Zaksf0c41162011-10-06 23:26:27 +000067 # Build the project.
Anna Zaksa2f970b2012-09-06 23:30:27 +000068 SATestBuild.testProject(ID, BuildMode, IsReferenceBuild=True, Dir=Dir)
Anna Zaksf0c41162011-10-06 23:26:27 +000069
70 # Add the project ID to the project map.
71 ProjectMapPath = os.path.join(CurDir, SATestBuild.ProjectMapFile)
72 if os.path.exists(ProjectMapPath):
73 PMapFile = open(ProjectMapPath, "r+b")
74 else:
75 print "Warning: Creating the Project Map file!!"
76 PMapFile = open(ProjectMapPath, "w+b")
77 try:
Ted Kremenek3a0678e2015-09-08 03:50:52 +000078 if (isExistingProject(PMapFile, ID)) :
Anna Zaksa1b56e22011-11-08 22:41:22 +000079 print >> sys.stdout, 'Warning: Project with ID \'', ID, \
80 '\' already exists.'
81 print >> sys.stdout, "Reference output has been regenerated."
Ted Kremenek3a0678e2015-09-08 03:50:52 +000082 else:
Anna Zaksa1b56e22011-11-08 22:41:22 +000083 PMapWriter = csv.writer(PMapFile)
Anna Zaksa2f970b2012-09-06 23:30:27 +000084 PMapWriter.writerow( (ID, int(BuildMode)) );
Anna Zaksa1b56e22011-11-08 22:41:22 +000085 print "The project map is updated: ", ProjectMapPath
Anna Zaksf0c41162011-10-06 23:26:27 +000086 finally:
87 PMapFile.close()
Anna Zaksf0c41162011-10-06 23:26:27 +000088
Ted Kremenek3a0678e2015-09-08 03:50:52 +000089
90# TODO: Add an option not to build.
Anna Zaksf0c41162011-10-06 23:26:27 +000091# TODO: Set the path to the Repository directory.
92if __name__ == '__main__':
George Karpenkov2d155092017-09-21 21:47:33 +000093 if len(sys.argv) < 2 or sys.argv[1] in ('-h', '--help'):
94 print >> sys.stderr, 'Add a new project for testing to static analyzer'\
95 '\nUsage: ', sys.argv[0],\
96 'project_ID <mode>\n' \
97 'mode: 0 for single file project, ' \
98 '1 for scan_build, ' \
Anna Zaksa2f970b2012-09-06 23:30:27 +000099 '2 for single file c++11 project'
Anna Zaksf0c41162011-10-06 23:26:27 +0000100 sys.exit(-1)
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000101
102 BuildMode = 1
Anna Zaks4720a732011-11-05 05:20:48 +0000103 if (len(sys.argv) >= 3):
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000104 BuildMode = int(sys.argv[2])
Anna Zaksa2f970b2012-09-06 23:30:27 +0000105 assert((BuildMode == 0) | (BuildMode == 1) | (BuildMode == 2))
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000106
Anna Zaksa2f970b2012-09-06 23:30:27 +0000107 addNewProject(sys.argv[1], BuildMode)