blob: 83ff3d719ea1f7dfb2fe3b525fbd01250ea93eae [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
Valeriy Savchenkofb4b5652020-06-01 17:15:38 +030046from ProjectMap import ProjectMap, ProjectInfo
Anna Zaksf0c41162011-10-06 23:26:27 +000047
Valeriy Savchenko4902ca62020-05-21 18:28:36 +030048import os
Anna Zaksf0c41162011-10-06 23:26:27 +000049import sys
50
George Karpenkova8076602017-10-02 17:59:12 +000051
Valeriy Savchenko4902ca62020-05-21 18:28:36 +030052def add_new_project(name: str, build_mode: int):
George Karpenkova8076602017-10-02 17:59:12 +000053 """
54 Add a new project for testing: build it and add to the Project Map file.
Valeriy Savchenko4902ca62020-05-21 18:28:36 +030055 :param name: is a short string used to identify a project.
George Karpenkova8076602017-10-02 17:59:12 +000056 """
57
Valeriy Savchenkofb4b5652020-06-01 17:15:38 +030058 project_info = ProjectInfo(name, build_mode)
59 test_info = SATestBuild.TestInfo(project_info,
60 is_reference_build=True)
61 tester = SATestBuild.ProjectTester(test_info)
Valeriy Savchenko4902ca62020-05-21 18:28:36 +030062
63 project_dir = tester.get_project_dir()
64 if not os.path.exists(project_dir):
65 print(f"Error: Project directory is missing: {project_dir}")
Anna Zaksf0c41162011-10-06 23:26:27 +000066 sys.exit(-1)
Ted Kremenek3a0678e2015-09-08 03:50:52 +000067
Anna Zaksf0c41162011-10-06 23:26:27 +000068 # Build the project.
Valeriy Savchenko4902ca62020-05-21 18:28:36 +030069 tester.test()
Anna Zaksf0c41162011-10-06 23:26:27 +000070
Valeriy Savchenko4902ca62020-05-21 18:28:36 +030071 # Add the project name to the project map.
Valeriy Savchenkofb4b5652020-06-01 17:15:38 +030072 project_map = ProjectMap(should_exist=False)
George Karpenkova8076602017-10-02 17:59:12 +000073
Valeriy Savchenkofb4b5652020-06-01 17:15:38 +030074 if is_existing_project(project_map, name):
75 print(f"Warning: Project with name '{name}' already exists.",
76 file=sys.stdout)
77 print("Reference output has been regenerated.", file=sys.stdout)
Anna Zaksf0c41162011-10-06 23:26:27 +000078 else:
Valeriy Savchenkofb4b5652020-06-01 17:15:38 +030079 project_map.projects.append(project_info)
80 project_map.save()
Anna Zaksf0c41162011-10-06 23:26:27 +000081
Ted Kremenek3a0678e2015-09-08 03:50:52 +000082
Valeriy Savchenkofb4b5652020-06-01 17:15:38 +030083def is_existing_project(project_map: ProjectMap, project_name: str) -> bool:
84 return any(existing_project.name == project_name
85 for existing_project in project_map.projects)
Valeriy Savchenko4902ca62020-05-21 18:28:36 +030086
87
88# TODO: Use argparse
Ted Kremenek3a0678e2015-09-08 03:50:52 +000089# TODO: Add an option not to build.
Anna Zaksf0c41162011-10-06 23:26:27 +000090# TODO: Set the path to the Repository directory.
Valeriy Savchenko4902ca62020-05-21 18:28:36 +030091if __name__ == "__main__":
92 if len(sys.argv) < 2 or sys.argv[1] in ("-h", "--help"):
93 print("Add a new project for testing to the analyzer"
94 "\nUsage: ", sys.argv[0],
95 "project_ID <mode>\n"
96 "mode: 0 for single file project, "
97 "1 for scan_build, "
98 "2 for single file c++11 project", file=sys.stderr)
Anna Zaksf0c41162011-10-06 23:26:27 +000099 sys.exit(-1)
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000100
Valeriy Savchenko4902ca62020-05-21 18:28:36 +0300101 build_mode = 1
102 if len(sys.argv) >= 3:
103 build_mode = int(sys.argv[2])
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000104
Valeriy Savchenko4902ca62020-05-21 18:28:36 +0300105 assert((build_mode == 0) | (build_mode == 1) | (build_mode == 2))
106
107 add_new_project(sys.argv[1], build_mode)