blob: 730316dd50b6e5e6e28c3eee53382d094464a983 [file] [log] [blame]
Fathi Boudra422bf772019-12-02 11:10:16 +02001#!/usr/bin/env python3
2#
Zachary Leafb6d86302024-10-29 10:29:15 +00003# Copyright (c) 2019-2024, Arm Limited. All rights reserved.
Fathi Boudra422bf772019-12-02 11:10:16 +02004#
5# SPDX-License-Identifier: BSD-3-Clause
6#
7
8"""
9Check if a given file includes the copyright boiler plate.
10This checker supports the following comment styles:
Zelalem219df412020-05-17 19:21:20 -050011 /*
12 *
13 //
14 #
Fathi Boudra422bf772019-12-02 11:10:16 +020015"""
16
17import argparse
18import datetime
19import collections
20import fnmatch
21import shlex
22import os
23import re
24import sys
25import utils
26from itertools import islice
27
Sandrine Afsae37c35b2025-07-09 10:38:50 +020028class TfaConfig:
29 def __init__(self):
30 # File extensions to check
31 self.valid_file_ext = (
32 '.c', '.conf', '.dts', '.dtsi', '.editorconfig',
33 '.h', '.i', '.ld', 'Makefile', '.mk', '.msvc',
34 '.py', '.S', '.scat', '.sh', '.rs'
35 )
Fathi Boudra422bf772019-12-02 11:10:16 +020036
Sandrine Afsae37c35b2025-07-09 10:38:50 +020037 # Paths inside the tree to ignore. Hidden folders and files are always
38 # ignored. They mustn't end in '/'.
39 self.ignored_folders = (
40 'include/lib/hob',
41 'include/lib/libfdt',
42 'lib/compiler-rt',
43 'lib/hob',
44 'lib/libfdt',
45 'lib/zlib'
46 )
Fathi Boudra422bf772019-12-02 11:10:16 +020047
Sandrine Afsae37c35b2025-07-09 10:38:50 +020048 # List of ignored files in folders that aren't ignored
49 self.ignored_files = (
50 'include/tools_share/uuid.h',
51 )
52
53 self.copyright_line = LINE_START + 'Copyright' + '.*' + TIME_PERIOD + '.*' + EOL
54 self.copyright_pattern = re.compile(self.copyright_line, re.MULTILINE)
55 self.copyright_check_year = True
56
57class RfaConfig:
58 def __init__(self):
59 # File extensions to check
60 self.valid_file_ext = (
61 '.c', '.conf', '.dts', '.dtsi', '.editorconfig',
62 '.h', '.i', '.ld', 'Makefile', '.mk', '.msvc',
63 '.py', '.S', '.scat', '.sh', '.rs'
64 )
65
66 # Paths inside the tree to ignore. Hidden folders and files are always
67 # ignored. They mustn't end in '/'.
68 self.ignored_folders = (
69 'include/lib/hob',
70 'include/lib/libfdt',
71 'lib/compiler-rt',
72 'lib/hob',
73 'lib/libfdt',
74 'lib/zlib'
75 )
76
77 # List of ignored files in folders that aren't ignored
78 self.ignored_files = (
79 'include/tools_share/uuid.h',
80 )
81
82 self.copyright_line = LINE_START + 'Copyright The Rusted Firmware-A Contributors.' + EOL
83 self.copyright_pattern = re.compile(self.copyright_line, re.MULTILINE)
84 self.copyright_check_year = False
Fathi Boudra422bf772019-12-02 11:10:16 +020085
86# Supported comment styles (Python regex)
Zelalem219df412020-05-17 19:21:20 -050087COMMENT_PATTERN = '(\*|/\*|\#|//)'
Fathi Boudra422bf772019-12-02 11:10:16 +020088
Zelalem219df412020-05-17 19:21:20 -050089# Any combination of spaces and/or tabs
90SPACING = '[ \t]*'
Fathi Boudra422bf772019-12-02 11:10:16 +020091
Zelalem219df412020-05-17 19:21:20 -050092# Line must start with a comment and optional spacing
93LINE_START = '^' + SPACING + COMMENT_PATTERN + SPACING
94
95# Line end with optional spacing
96EOL = SPACING + '$'
97
98# Year or period as YYYY or YYYY-YYYY
99TIME_PERIOD = '[0-9]{4}(-[0-9]{4})?'
100
Sandrine Afsae37c35b2025-07-09 10:38:50 +0200101CURRENT_YEAR = str(datetime.datetime.now().year)
102
Zelalem219df412020-05-17 19:21:20 -0500103# Any string with valid license ID, don't allow adding postfix
Chris Kay345873c2021-04-27 13:24:51 +0100104LICENSE_ID = '.*(BSD-3-Clause|BSD-2-Clause-FreeBSD|MIT)([ ,.\);].*)?'
Zelalem219df412020-05-17 19:21:20 -0500105LICENSE_ID_LINE = LINE_START + 'SPDX-License-Identifier:' + LICENSE_ID + EOL
Zelalem219df412020-05-17 19:21:20 -0500106LICENSE_ID_PATTERN = re.compile(LICENSE_ID_LINE, re.MULTILINE)
107
Fathi Boudra422bf772019-12-02 11:10:16 +0200108COPYRIGHT_OK = 0
109COPYRIGHT_ERROR = 1
Fathi Boudra422bf772019-12-02 11:10:16 +0200110
Sandrine Afsae37c35b2025-07-09 10:38:50 +0200111def check_copyright(path, copyright_pattern, check_year, encoding='utf-8'):
Fathi Boudra422bf772019-12-02 11:10:16 +0200112 '''Checks a file for a correct copyright header.'''
113
Zelalem219df412020-05-17 19:21:20 -0500114 result = COPYRIGHT_OK
115
116 with open(path, encoding=encoding) as file_:
Fathi Boudra422bf772019-12-02 11:10:16 +0200117 file_content = file_.read()
118
Tomás González3923b552025-06-04 14:04:00 +0100119 copyright_line = copyright_pattern.search(file_content)
Tomás González1150fb82025-06-04 10:05:21 +0100120
Zelalem219df412020-05-17 19:21:20 -0500121 if not copyright_line:
122 print("ERROR: Missing copyright in " + file_.name)
123 result = COPYRIGHT_ERROR
Tomás González3923b552025-06-04 14:04:00 +0100124 elif check_year and CURRENT_YEAR not in copyright_line.group():
Zelalem219df412020-05-17 19:21:20 -0500125 print("WARNING: Copyright is out of date in " + file_.name + ": '" +
126 copyright_line.group() + "'")
Fathi Boudra422bf772019-12-02 11:10:16 +0200127
Zelalem219df412020-05-17 19:21:20 -0500128 if not LICENSE_ID_PATTERN.search(file_content):
129 print("ERROR: License ID error in " + file_.name)
130 result = COPYRIGHT_ERROR
Fathi Boudra422bf772019-12-02 11:10:16 +0200131
Zelalem219df412020-05-17 19:21:20 -0500132 return result
Fathi Boudra422bf772019-12-02 11:10:16 +0200133
134def main(args):
Sandrine Afsae37c35b2025-07-09 10:38:50 +0200135 # Load the project's configuration (either TF-A's or RF-A's).
136 if not args.rusted:
137 config = TfaConfig()
138 else:
139 config = RfaConfig()
140
Fathi Boudra422bf772019-12-02 11:10:16 +0200141 print("Checking the copyrights in the code...")
142
Zelalem219df412020-05-17 19:21:20 -0500143 if args.verbose:
Sandrine Afsae37c35b2025-07-09 10:38:50 +0200144 print ("Copyright regexp: " + config.copyright_line)
Zelalem219df412020-05-17 19:21:20 -0500145 print ("License regexp: " + LICENSE_ID_LINE)
Fathi Boudra422bf772019-12-02 11:10:16 +0200146
147 if args.patch:
Harrison Mutai7a93cd22022-09-29 12:31:31 +0100148 print("Checking files added between patches " + args.from_ref
Fathi Boudra422bf772019-12-02 11:10:16 +0200149 + " and " + args.to_ref + "...")
150
151 (rc, stdout, stderr) = utils.shell_command(['git', 'diff',
Harrison Mutai7a93cd22022-09-29 12:31:31 +0100152 '--diff-filter=ACRT', '--name-only', args.from_ref, args.to_ref ])
Fathi Boudra422bf772019-12-02 11:10:16 +0200153 if rc:
Zelalem219df412020-05-17 19:21:20 -0500154 return COPYRIGHT_ERROR
Fathi Boudra422bf772019-12-02 11:10:16 +0200155
156 files = stdout.splitlines()
157
158 else:
159 print("Checking all files tracked by git...")
160
161 (rc, stdout, stderr) = utils.shell_command([ 'git', 'ls-files' ])
162 if rc:
Zelalem219df412020-05-17 19:21:20 -0500163 return COPYRIGHT_ERROR
Fathi Boudra422bf772019-12-02 11:10:16 +0200164
165 files = stdout.splitlines()
166
167 count_ok = 0
168 count_warning = 0
169 count_error = 0
170
171 for f in files:
172
Sandrine Afsae37c35b2025-07-09 10:38:50 +0200173 if utils.file_is_ignored(f, config.valid_file_ext, config.ignored_files, config.ignored_folders):
Fathi Boudra422bf772019-12-02 11:10:16 +0200174 if args.verbose:
175 print("Ignoring file " + f)
176 continue
177
178 if args.verbose:
179 print("Checking file " + f)
180
Sandrine Afsae37c35b2025-07-09 10:38:50 +0200181 rc = check_copyright(f, config.copyright_pattern, config.copyright_check_year)
Fathi Boudra422bf772019-12-02 11:10:16 +0200182
183 if rc == COPYRIGHT_OK:
184 count_ok += 1
Fathi Boudra422bf772019-12-02 11:10:16 +0200185 elif rc == COPYRIGHT_ERROR:
186 count_error += 1
Fathi Boudra422bf772019-12-02 11:10:16 +0200187
188 print("\nSummary:")
Zelalem219df412020-05-17 19:21:20 -0500189 print("\t{} files analyzed".format(count_ok + count_error))
Fathi Boudra422bf772019-12-02 11:10:16 +0200190
Zelalem219df412020-05-17 19:21:20 -0500191 if count_error == 0:
Fathi Boudra422bf772019-12-02 11:10:16 +0200192 print("\tNo errors found")
Zelalem219df412020-05-17 19:21:20 -0500193 return COPYRIGHT_OK
194 else:
Fathi Boudra422bf772019-12-02 11:10:16 +0200195 print("\t{} errors found".format(count_error))
Zelalem219df412020-05-17 19:21:20 -0500196 return COPYRIGHT_ERROR
Fathi Boudra422bf772019-12-02 11:10:16 +0200197
198def parse_cmd_line(argv, prog_name):
199 parser = argparse.ArgumentParser(
200 prog=prog_name,
201 formatter_class=argparse.RawTextHelpFormatter,
202 description="Check copyright of all files of codebase",
203 epilog="""
204For each source file in the tree, checks that the copyright header
205has the correct format.
206""")
207
208 parser.add_argument("--tree", "-t",
209 help="Path to the source tree to check (default: %(default)s)",
210 default=os.curdir)
211
Tomás González1150fb82025-06-04 10:05:21 +0100212 parser.add_argument("--rusted", "-r",
213 help="Check for Rusted Firmware CopyRight style (default: %(default)s)",
214 action='store_true', default=False)
215
Fathi Boudra422bf772019-12-02 11:10:16 +0200216 parser.add_argument("--verbose", "-v",
217 help="Increase verbosity to the source tree to check (default: %(default)s)",
218 action='store_true', default=False)
219
220 parser.add_argument("--patch", "-p",
221 help="""
222Patch mode.
223Instead of checking all files in the source tree, the script will consider
224only files that are modified by the latest patch(es).""",
225 action="store_true")
226 parser.add_argument("--from-ref",
227 help="Base commit in patch mode (default: %(default)s)",
Harrison Mutai1f1e6cf2025-06-24 09:28:18 +0000228 default="remotes/origin/integration")
Fathi Boudra422bf772019-12-02 11:10:16 +0200229 parser.add_argument("--to-ref",
230 help="Final commit in patch mode (default: %(default)s)",
231 default="HEAD")
232
233 args = parser.parse_args(argv)
234 return args
235
236
237if __name__ == "__main__":
238 args = parse_cmd_line(sys.argv[1:], sys.argv[0])
239
240 os.chdir(args.tree)
241
242 rc = main(args)
243
244 sys.exit(rc)