blob: 041b24409fe35de2984afc53d5b0fc470ff6aec7 [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.
George Karpenkova8076602017-10-02 17:59:12 +000030 - changes_for_analyzer.patch - An optional patch file for any local
31 changes
Devin Coughlin2cb767d2015-11-07 18:27:35 +000032 (e.g., to adapt to newer version of clang)
33 that should be applied to CachedSource
34 before analysis. To construct this patch,
Hiroshi Inoue56939f72018-01-22 07:44:38 +000035 run the download script to download
Devin Coughlin2cb767d2015-11-07 18:27:35 +000036 the project to CachedSource, copy the
37 CachedSource to another directory (for
George Karpenkova8076602017-10-02 17:59:12 +000038 example, PatchedSource) and make any
Hiroshi Inoue56939f72018-01-22 07:44:38 +000039 needed modifications to the copied
George Karpenkova8076602017-10-02 17:59:12 +000040 source.
Devin Coughlin2cb767d2015-11-07 18:27:35 +000041 Then run:
42 diff -ur CachedSource PatchedSource \
43 > changes_for_analyzer.patch
Anna Zaksf0c41162011-10-06 23:26:27 +000044"""
45import SATestBuild
46
47import os
48import csv
49import sys
50
George Karpenkova8076602017-10-02 17:59:12 +000051
52def isExistingProject(PMapFile, projectID):
Anna Zaksa1b56e22011-11-08 22:41:22 +000053 PMapReader = csv.reader(PMapFile)
54 for I in PMapReader:
55 if projectID == I[0]:
56 return True
Ted Kremenek3a0678e2015-09-08 03:50:52 +000057 return False
Anna Zaksa1b56e22011-11-08 22:41:22 +000058
George Karpenkova8076602017-10-02 17:59:12 +000059
60def addNewProject(ID, BuildMode):
61 """
62 Add a new project for testing: build it and add to the Project Map file.
63 :param ID: is a short string used to identify a project.
64 """
65
Anna Zaksf0c41162011-10-06 23:26:27 +000066 CurDir = os.path.abspath(os.curdir)
67 Dir = SATestBuild.getProjectDir(ID)
68 if not os.path.exists(Dir):
69 print "Error: Project directory is missing: %s" % Dir
70 sys.exit(-1)
Ted Kremenek3a0678e2015-09-08 03:50:52 +000071
Anna Zaksf0c41162011-10-06 23:26:27 +000072 # Build the project.
George Karpenkov3abfc3b2017-09-22 01:41:16 +000073 SATestBuild.testProject(ID, BuildMode, IsReferenceBuild=True)
Anna Zaksf0c41162011-10-06 23:26:27 +000074
75 # Add the project ID to the project map.
76 ProjectMapPath = os.path.join(CurDir, SATestBuild.ProjectMapFile)
George Karpenkova8076602017-10-02 17:59:12 +000077
Anna Zaksf0c41162011-10-06 23:26:27 +000078 if os.path.exists(ProjectMapPath):
George Karpenkova8076602017-10-02 17:59:12 +000079 FileMode = "r+b"
Anna Zaksf0c41162011-10-06 23:26:27 +000080 else:
81 print "Warning: Creating the Project Map file!!"
George Karpenkova8076602017-10-02 17:59:12 +000082 FileMode = "w+b"
83
84 with open(ProjectMapPath, FileMode) as PMapFile:
85 if (isExistingProject(PMapFile, ID)):
Anna Zaksa1b56e22011-11-08 22:41:22 +000086 print >> sys.stdout, 'Warning: Project with ID \'', ID, \
87 '\' already exists.'
88 print >> sys.stdout, "Reference output has been regenerated."
Ted Kremenek3a0678e2015-09-08 03:50:52 +000089 else:
Anna Zaksa1b56e22011-11-08 22:41:22 +000090 PMapWriter = csv.writer(PMapFile)
George Karpenkova8076602017-10-02 17:59:12 +000091 PMapWriter.writerow((ID, int(BuildMode)))
Anna Zaksa1b56e22011-11-08 22:41:22 +000092 print "The project map is updated: ", ProjectMapPath
Anna Zaksf0c41162011-10-06 23:26:27 +000093
Ted Kremenek3a0678e2015-09-08 03:50:52 +000094
95# TODO: Add an option not to build.
Anna Zaksf0c41162011-10-06 23:26:27 +000096# TODO: Set the path to the Repository directory.
97if __name__ == '__main__':
George Karpenkov2d155092017-09-21 21:47:33 +000098 if len(sys.argv) < 2 or sys.argv[1] in ('-h', '--help'):
George Karpenkova8076602017-10-02 17:59:12 +000099 print >> sys.stderr, 'Add a new project for testing to the analyzer'\
George Karpenkov2d155092017-09-21 21:47:33 +0000100 '\nUsage: ', sys.argv[0],\
101 'project_ID <mode>\n' \
102 'mode: 0 for single file project, ' \
103 '1 for scan_build, ' \
Anna Zaksa2f970b2012-09-06 23:30:27 +0000104 '2 for single file c++11 project'
Anna Zaksf0c41162011-10-06 23:26:27 +0000105 sys.exit(-1)
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000106
107 BuildMode = 1
Anna Zaks4720a732011-11-05 05:20:48 +0000108 if (len(sys.argv) >= 3):
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000109 BuildMode = int(sys.argv[2])
Anna Zaksa2f970b2012-09-06 23:30:27 +0000110 assert((BuildMode == 0) | (BuildMode == 1) | (BuildMode == 2))
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000111
Anna Zaksa2f970b2012-09-06 23:30:27 +0000112 addNewProject(sys.argv[1], BuildMode)