Imported Upstream version 1.34.0
[platform/upstream/grpc.git] / tools / run_tests / sanity / check_port_platform.py
1 #!/usr/bin/env python
2
3 # Copyright 2017 gRPC authors.
4 #
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
8 #
9 #     http://www.apache.org/licenses/LICENSE-2.0
10 #
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.
16
17 import os
18 import sys
19
20 os.chdir(os.path.join(os.path.dirname(sys.argv[0]), '../../..'))
21
22
23 def check_port_platform_inclusion(directory_root):
24     bad_files = []
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
29             if path in [
30                     os.path.join('include', 'grpc', 'support',
31                                  'port_platform.h'),
32                     os.path.join('include', 'grpc', 'impl', 'codegen',
33                                  'port_platform.h'),
34             ]:
35                 continue
36             if filename.endswith('.pb.h') or filename.endswith('.pb.c'):
37                 continue
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')):
42                 continue
43             with open(path) as f:
44                 all_lines_in_file = f.readlines()
45                 for index, l in enumerate(all_lines_in_file):
46                     if '#include' in l:
47                         if l not in [
48                                 '#include <grpc/support/port_platform.h>\n',
49                                 '#include <grpc/impl/codegen/port_platform.h>\n'
50                         ]:
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)
57                         break
58     return bad_files
59
60
61 all_bad_files = []
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'))
64
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)
69     sys.exit(1)