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 17import os 18import sys 19 20os.chdir(os.path.join(os.path.dirname(sys.argv[0]), '../../..')) 21 22 23def 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'): 40 continue 41 with open(path) as f: 42 all_lines_in_file = f.readlines() 43 for index, l in enumerate(all_lines_in_file): 44 if '#include' in l: 45 if l not in [ 46 '#include <grpc/support/port_platform.h>\n', 47 '#include <grpc/impl/codegen/port_platform.h>\n' 48 ]: 49 bad_files.append(path) 50 elif all_lines_in_file[index + 1] != '\n': 51 # Require a blank line after including port_platform.h in 52 # order to prevent the formatter from reording it's 53 # inclusion order upon future changes. 54 bad_files.append(path) 55 break 56 return bad_files 57 58 59all_bad_files = [] 60all_bad_files += check_port_platform_inclusion(os.path.join('src', 'core')) 61all_bad_files += check_port_platform_inclusion(os.path.join('include', 'grpc')) 62 63if len(all_bad_files) > 0: 64 for f in all_bad_files: 65 print(('port_platform.h is not the first included header or there ' 66 'is not a blank line following its inclusion in %s') % f) 67 sys.exit(1) 68