3 # Copyright 2017 gRPC authors.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
20 os.chdir(os.path.join(os.path.dirname(sys.argv[0]), '../../..'))
23 def check_port_platform_inclusion(directory_root):
25 for root, dirs, files in os.walk(directory_root):
26 for filename in files:
27 path = os.path.join(root, filename)
28 if os.path.splitext(path)[1] not in ['.c', '.cc', '.h']: continue
30 os.path.join('include', 'grpc', 'support',
32 os.path.join('include', 'grpc', 'impl', 'codegen',
36 if filename.endswith('.pb.h') or filename.endswith('.pb.c'):
38 # Skip check for upb generated code.
39 if (filename.endswith('.upb.h') or filename.endswith('.upb.c') or
40 filename.endswith('.upbdefs.h') or
41 filename.endswith('.upbdefs.c')):
44 all_lines_in_file = f.readlines()
45 for index, l in enumerate(all_lines_in_file):
48 '#include <grpc/support/port_platform.h>\n',
49 '#include <grpc/impl/codegen/port_platform.h>\n'
51 bad_files.append(path)
52 elif all_lines_in_file[index + 1] != '\n':
53 # Require a blank line after including port_platform.h in
54 # order to prevent the formatter from reording it's
55 # inclusion order upon future changes.
56 bad_files.append(path)
62 all_bad_files += check_port_platform_inclusion(os.path.join('src', 'core'))
63 all_bad_files += check_port_platform_inclusion(os.path.join('include', 'grpc'))
65 if len(all_bad_files) > 0:
66 for f in all_bad_files:
67 print(('port_platform.h is not the first included header or there '
68 'is not a blank line following its inclusion in %s') % f)