1*635a8641SAndroid Build Coastguard Worker#!/usr/bin/env python 2*635a8641SAndroid Build Coastguard Worker 3*635a8641SAndroid Build Coastguard Worker# Copyright (C) 2018 The Android Open Source Project 4*635a8641SAndroid Build Coastguard Worker# 5*635a8641SAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License"); 6*635a8641SAndroid Build Coastguard Worker# you may not use this file except in compliance with the License. 7*635a8641SAndroid Build Coastguard Worker# You may obtain a copy of the License at 8*635a8641SAndroid Build Coastguard Worker# 9*635a8641SAndroid Build Coastguard Worker# http://www.apache.org/licenses/LICENSE-2.0 10*635a8641SAndroid Build Coastguard Worker# 11*635a8641SAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software 12*635a8641SAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS, 13*635a8641SAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14*635a8641SAndroid Build Coastguard Worker# See the License for the specific language governing permissions and 15*635a8641SAndroid Build Coastguard Worker# limitations under the License. 16*635a8641SAndroid Build Coastguard Worker 17*635a8641SAndroid Build Coastguard Worker"""Generates wrapped include files to workaround -Wunused-parameter errors. 18*635a8641SAndroid Build Coastguard Worker 19*635a8641SAndroid Build Coastguard WorkerIn Chrome repository, "-Wunused-parameter" is disabled, and several header 20*635a8641SAndroid Build Coastguard Workerfiles in Chrome repository have actually unused-parameter. 21*635a8641SAndroid Build Coastguard WorkerOne of the typical scenarios is; in Chrome, Observer class is often defined 22*635a8641SAndroid Build Coastguard Workeras follows: 23*635a8641SAndroid Build Coastguard Worker 24*635a8641SAndroid Build Coastguard Workerclass Foo { 25*635a8641SAndroid Build Coastguard Worker public: 26*635a8641SAndroid Build Coastguard Worker class Observer { 27*635a8641SAndroid Build Coastguard Worker public: 28*635a8641SAndroid Build Coastguard Worker virtual void OnSomeEvent(EventArg arg) {} 29*635a8641SAndroid Build Coastguard Worker virtual void OnAnotherEvent(EventArg arg) {} 30*635a8641SAndroid Build Coastguard Worker ... 31*635a8641SAndroid Build Coastguard Worker }; 32*635a8641SAndroid Build Coastguard Worker ... 33*635a8641SAndroid Build Coastguard Worker}; 34*635a8641SAndroid Build Coastguard Worker 35*635a8641SAndroid Build Coastguard WorkerHere, On...Event() methods do nothing by default, and subclasses will override 36*635a8641SAndroid Build Coastguard Workeronly necessary ones. 37*635a8641SAndroid Build Coastguard WorkerIn this use case, argument names can also work as documentation, and overrides 38*635a8641SAndroid Build Coastguard Workercan use these good interface-defined default names as a starting point for 39*635a8641SAndroid Build Coastguard Workertheir implementation. 40*635a8641SAndroid Build Coastguard Worker 41*635a8641SAndroid Build Coastguard WorkerOn the other hand, in Android, -Wunused-parameter is enabled by default. 42*635a8641SAndroid Build Coastguard WorkerThus, if such a project includes header files from libchrome, it could cause 43*635a8641SAndroid Build Coastguard Workera compile error (by the warning and "-Werror"). 44*635a8641SAndroid Build Coastguard Worker 45*635a8641SAndroid Build Coastguard WorkerTo avoid such a situation, libchrome exports include files wrapped by the 46*635a8641SAndroid Build Coastguard Workerpragmas as follows. 47*635a8641SAndroid Build Coastguard Worker 48*635a8641SAndroid Build Coastguard Worker#pragma GCC diagnostic push 49*635a8641SAndroid Build Coastguard Worker#pragma GCC diagnostic ignored "-Wunused-parameter" 50*635a8641SAndroid Build Coastguard Worker${actual_include_file_content} 51*635a8641SAndroid Build Coastguard Worker#pragma GCC diagnostic pop 52*635a8641SAndroid Build Coastguard Worker 53*635a8641SAndroid Build Coastguard Workerso, the unused-parameter warning generated by the libchrome include headers 54*635a8641SAndroid Build Coastguard Workerwill be ignored. 55*635a8641SAndroid Build Coastguard WorkerNote that these GCC pragmas are also supported by clang for compatibility. cf) 56*635a8641SAndroid Build Coastguard Workerhttps://clang.llvm.org/docs/UsersManual.html#controlling-diagnostics-via-pragmas 57*635a8641SAndroid Build Coastguard Worker 58*635a8641SAndroid Build Coastguard WorkerUsage: include_generator.py $(in) $(out) 59*635a8641SAndroid Build Coastguard Worker""" 60*635a8641SAndroid Build Coastguard Worker 61*635a8641SAndroid Build Coastguard Workerimport sys 62*635a8641SAndroid Build Coastguard Worker 63*635a8641SAndroid Build Coastguard Worker 64*635a8641SAndroid Build Coastguard Workerdef _generate(input_path, output_path): 65*635a8641SAndroid Build Coastguard Worker """Generates a include file wrapped by pragmas. 66*635a8641SAndroid Build Coastguard Worker 67*635a8641SAndroid Build Coastguard Worker Reads the file at |input_path| and output the content with wrapping by 68*635a8641SAndroid Build Coastguard Worker #pragma to ignore unused-parameter warning into the file at |output_path|. 69*635a8641SAndroid Build Coastguard Worker If the parent directories of |output_path| do not exist, creates them. 70*635a8641SAndroid Build Coastguard Worker 71*635a8641SAndroid Build Coastguard Worker Args: 72*635a8641SAndroid Build Coastguard Worker input_path: Path to the source file. Expected this is a chrome's header 73*635a8641SAndroid Build Coastguard Worker file. 74*635a8641SAndroid Build Coastguard Worker output_path: Path to the output file. 75*635a8641SAndroid Build Coastguard Worker """ 76*635a8641SAndroid Build Coastguard Worker with open(input_path, 'r') as f: 77*635a8641SAndroid Build Coastguard Worker content = f.read() 78*635a8641SAndroid Build Coastguard Worker 79*635a8641SAndroid Build Coastguard Worker with open(output_path, 'w') as f: 80*635a8641SAndroid Build Coastguard Worker f.writelines([ 81*635a8641SAndroid Build Coastguard Worker '// Generated by %s\n' % sys.argv[0], 82*635a8641SAndroid Build Coastguard Worker '#pragma GCC diagnostic push\n' 83*635a8641SAndroid Build Coastguard Worker '#pragma GCC diagnostic ignored "-Wunused-parameter"\n', 84*635a8641SAndroid Build Coastguard Worker content, 85*635a8641SAndroid Build Coastguard Worker '#pragma GCC diagnostic pop\n']) 86*635a8641SAndroid Build Coastguard Worker 87*635a8641SAndroid Build Coastguard Worker 88*635a8641SAndroid Build Coastguard Workerdef main(): 89*635a8641SAndroid Build Coastguard Worker _generate(*sys.argv[1:]) 90*635a8641SAndroid Build Coastguard Worker 91*635a8641SAndroid Build Coastguard Worker 92*635a8641SAndroid Build Coastguard Workerif __name__ == '__main__': 93*635a8641SAndroid Build Coastguard Worker main() 94