Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | """ |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 4 | Static Analyzer qualification infrastructure: adding a new project to |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 5 | the 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 Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 9 | The project directory should be added inside the Repository Directory and |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 10 | have the same name as the project ID |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 11 | |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 12 | The project should use the following files for set up: |
Devin Coughlin | 2cb767d | 2015-11-07 18:27:35 +0000 | [diff] [blame] | 13 | - cleanup_run_static_analyzer.sh - prepare the build environment. |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 14 | 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 Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 17 | Choose from: configure, make, xcodebuild |
Devin Coughlin | 2cb767d | 2015-11-07 18:27:35 +0000 | [diff] [blame] | 18 | - 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 Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 42 | """ |
| 43 | import SATestBuild |
| 44 | |
| 45 | import os |
| 46 | import csv |
| 47 | import sys |
| 48 | |
Anna Zaks | a1b56e2 | 2011-11-08 22:41:22 +0000 | [diff] [blame] | 49 | def isExistingProject(PMapFile, projectID) : |
| 50 | PMapReader = csv.reader(PMapFile) |
| 51 | for I in PMapReader: |
| 52 | if projectID == I[0]: |
| 53 | return True |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 54 | return False |
Anna Zaks | a1b56e2 | 2011-11-08 22:41:22 +0000 | [diff] [blame] | 55 | |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 56 | # 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 Zaks | a2f970b | 2012-09-06 23:30:27 +0000 | [diff] [blame] | 60 | def addNewProject(ID, BuildMode) : |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 61 | 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 Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 66 | |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 67 | # Build the project. |
Anna Zaks | a2f970b | 2012-09-06 23:30:27 +0000 | [diff] [blame] | 68 | SATestBuild.testProject(ID, BuildMode, IsReferenceBuild=True, Dir=Dir) |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 69 | |
| 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 Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 78 | if (isExistingProject(PMapFile, ID)) : |
Anna Zaks | a1b56e2 | 2011-11-08 22:41:22 +0000 | [diff] [blame] | 79 | print >> sys.stdout, 'Warning: Project with ID \'', ID, \ |
| 80 | '\' already exists.' |
| 81 | print >> sys.stdout, "Reference output has been regenerated." |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 82 | else: |
Anna Zaks | a1b56e2 | 2011-11-08 22:41:22 +0000 | [diff] [blame] | 83 | PMapWriter = csv.writer(PMapFile) |
Anna Zaks | a2f970b | 2012-09-06 23:30:27 +0000 | [diff] [blame] | 84 | PMapWriter.writerow( (ID, int(BuildMode)) ); |
Anna Zaks | a1b56e2 | 2011-11-08 22:41:22 +0000 | [diff] [blame] | 85 | print "The project map is updated: ", ProjectMapPath |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 86 | finally: |
| 87 | PMapFile.close() |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 88 | |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 89 | |
| 90 | # TODO: Add an option not to build. |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 91 | # TODO: Set the path to the Repository directory. |
| 92 | if __name__ == '__main__': |
George Karpenkov | 2d15509 | 2017-09-21 21:47:33 +0000 | [diff] [blame^] | 93 | 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 Zaks | a2f970b | 2012-09-06 23:30:27 +0000 | [diff] [blame] | 99 | '2 for single file c++11 project' |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 100 | sys.exit(-1) |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 101 | |
| 102 | BuildMode = 1 |
Anna Zaks | 4720a73 | 2011-11-05 05:20:48 +0000 | [diff] [blame] | 103 | if (len(sys.argv) >= 3): |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 104 | BuildMode = int(sys.argv[2]) |
Anna Zaks | a2f970b | 2012-09-06 23:30:27 +0000 | [diff] [blame] | 105 | assert((BuildMode == 0) | (BuildMode == 1) | (BuildMode == 2)) |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 106 | |
Anna Zaks | a2f970b | 2012-09-06 23:30:27 +0000 | [diff] [blame] | 107 | addNewProject(sys.argv[1], BuildMode) |