2021-10-26 21:49:28 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
|
|
|
|
# Ensure copyright headers match this format and are followed by a blank line:
|
|
|
|
# /*
|
|
|
|
# * Copyright (c) YYYY(-YYYY), Whatever
|
|
|
|
# * ... more of these ...
|
|
|
|
# *
|
|
|
|
# * SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
# */
|
|
|
|
GOOD_LICENSE_HEADER_PATTERN = re.compile(
|
|
|
|
'^/\\*\n' +
|
|
|
|
'( \\* Copyright \\(c\\) [0-9]{4}(-[0-9]{4})?, .*\n)+' +
|
|
|
|
' \\*\n' +
|
|
|
|
' \\* SPDX-License-Identifier: BSD-2-Clause\n' +
|
|
|
|
' \\*/\n' +
|
|
|
|
'\n')
|
|
|
|
LICENSE_HEADER_CHECK_EXCLUDES = {
|
|
|
|
'AK/Checked.h',
|
|
|
|
'AK/Function.h',
|
2022-09-24 09:56:43 +00:00
|
|
|
'Userland/Libraries/LibJS/SafeFunction.h',
|
2021-10-26 21:49:28 +00:00
|
|
|
'Userland/Libraries/LibC/elf.h',
|
2022-05-14 14:09:24 +00:00
|
|
|
'Userland/Libraries/LibCodeComprehension/Cpp/Tests/',
|
2021-10-26 21:49:28 +00:00
|
|
|
'Userland/Libraries/LibCpp/Tests/parser/',
|
|
|
|
'Userland/Libraries/LibCpp/Tests/preprocessor/'
|
|
|
|
}
|
2023-01-03 14:30:44 +00:00
|
|
|
LIBC_CHECK_EXCLUDES = {
|
|
|
|
'Kernel/',
|
|
|
|
'Userland/Libraries/LibELF/',
|
|
|
|
'Userland/Libraries/LibRegex/'
|
|
|
|
}
|
2021-10-26 21:49:28 +00:00
|
|
|
|
|
|
|
# We check that "#pragma once" is present
|
|
|
|
PRAGMA_ONCE_STRING = '#pragma once'
|
2021-12-16 09:54:57 +00:00
|
|
|
PRAGMA_ONCE_CHECK_EXCLUDES = {
|
2021-12-14 16:44:43 +00:00
|
|
|
'Userland/Libraries/LibC/assert.h',
|
2021-12-16 09:54:57 +00:00
|
|
|
}
|
2021-10-26 21:49:28 +00:00
|
|
|
|
|
|
|
# We make sure that there's a blank line before and after pragma once
|
|
|
|
GOOD_PRAGMA_ONCE_PATTERN = re.compile('(^|\\S\n\n)#pragma once(\n\n\\S.|$)')
|
|
|
|
|
2023-01-03 14:30:44 +00:00
|
|
|
# LibC is supposed to be a system library; don't mention the directory.
|
|
|
|
BAD_INCLUDE_LIBC = re.compile("# *include <LibC/")
|
|
|
|
|
2021-10-26 21:49:28 +00:00
|
|
|
|
2022-09-18 18:10:57 +00:00
|
|
|
def should_check_file(filename):
|
|
|
|
if not filename.endswith('.cpp') and not filename.endswith('.h'):
|
|
|
|
return False
|
|
|
|
if filename.startswith('Base/'):
|
|
|
|
return False
|
2022-10-24 07:02:37 +00:00
|
|
|
if filename == 'Kernel/FileSystem/Ext2FS/Definitions.h':
|
2022-09-18 18:10:57 +00:00
|
|
|
return False
|
|
|
|
return True
|
2021-10-26 21:49:28 +00:00
|
|
|
|
|
|
|
|
2022-09-18 18:10:57 +00:00
|
|
|
def find_files_here_or_argv():
|
|
|
|
if len(sys.argv) > 1:
|
|
|
|
raw_list = sys.argv[1:]
|
|
|
|
else:
|
|
|
|
process = subprocess.run(["git", "ls-files"], check=True, capture_output=True)
|
|
|
|
raw_list = process.stdout.decode().strip('\n').split('\n')
|
|
|
|
|
|
|
|
return filter(should_check_file, raw_list)
|
|
|
|
|
|
|
|
|
|
|
|
def run():
|
2021-10-26 21:49:28 +00:00
|
|
|
errors_license = []
|
|
|
|
errors_pragma_once_bad = []
|
|
|
|
errors_pragma_once_missing = []
|
2023-01-03 14:30:44 +00:00
|
|
|
errors_include_libc = []
|
2021-10-26 21:49:28 +00:00
|
|
|
|
2022-09-18 18:10:57 +00:00
|
|
|
for filename in find_files_here_or_argv():
|
2021-10-26 21:49:28 +00:00
|
|
|
with open(filename, "r") as f:
|
|
|
|
file_content = f.read()
|
|
|
|
if not any(filename.startswith(forbidden_prefix) for forbidden_prefix in LICENSE_HEADER_CHECK_EXCLUDES):
|
|
|
|
if not GOOD_LICENSE_HEADER_PATTERN.search(file_content):
|
|
|
|
errors_license.append(filename)
|
|
|
|
if filename.endswith('.h'):
|
2021-12-16 09:54:57 +00:00
|
|
|
if any(filename.startswith(forbidden_prefix) for forbidden_prefix in PRAGMA_ONCE_CHECK_EXCLUDES):
|
|
|
|
# File was excluded
|
|
|
|
pass
|
|
|
|
elif GOOD_PRAGMA_ONCE_PATTERN.search(file_content):
|
2021-10-26 21:49:28 +00:00
|
|
|
# Excellent, the formatting is correct.
|
|
|
|
pass
|
|
|
|
elif PRAGMA_ONCE_STRING in file_content:
|
|
|
|
# Bad, the '#pragma once' is present but it's formatted wrong.
|
|
|
|
errors_pragma_once_bad.append(filename)
|
|
|
|
else:
|
|
|
|
# Bad, the '#pragma once' is missing completely.
|
|
|
|
errors_pragma_once_missing.append(filename)
|
2023-01-03 14:30:44 +00:00
|
|
|
if not any(filename.startswith(forbidden_prefix) for forbidden_prefix in LIBC_CHECK_EXCLUDES):
|
|
|
|
if BAD_INCLUDE_LIBC.search(file_content):
|
|
|
|
errors_include_libc.append(filename)
|
2021-10-26 21:49:28 +00:00
|
|
|
|
|
|
|
if errors_license:
|
|
|
|
print("Files with bad licenses:", " ".join(errors_license))
|
|
|
|
if errors_pragma_once_missing:
|
|
|
|
print("Files without #pragma once:", " ".join(errors_pragma_once_missing))
|
|
|
|
if errors_pragma_once_bad:
|
|
|
|
print("Files with a bad #pragma once:", " ".join(errors_pragma_once_bad))
|
2023-01-03 14:30:44 +00:00
|
|
|
if errors_include_libc:
|
|
|
|
print("Files that include a LibC header using #include <LibC/...>:", " ".join(errors_include_libc))
|
2021-10-26 21:49:28 +00:00
|
|
|
|
2023-01-03 14:30:44 +00:00
|
|
|
if errors_license or errors_pragma_once_missing or errors_pragma_once_bad or errors_include_libc:
|
2021-10-26 21:49:28 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
os.chdir(os.path.dirname(__file__) + "/..")
|
|
|
|
run()
|