Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / scripts / gen_test_driver.py
1
2 #!/usr/bin/env python
3
4 # Copyright (c) 2020 Project CHIP Authors
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17
18 import optparse
19 import sys
20 import re
21
22 TEMPLATE_PREFIX = '''/*
23  *
24  *    Copyright (c) 2020 Project CHIP Authors
25  *    All rights reserved.
26  *
27  *    Licensed under the Apache License, Version 2.0 (the \"License\");
28  *    you may not use this file except in compliance with the License.
29  *    You may obtain a copy of the License at
30  *
31  *        http://www.apache.org/licenses/LICENSE-2.0
32  *
33  *    Unless required by applicable law or agreed to in writing, software
34  *    distributed under the License is distributed on an \"AS IS\" BASIS,
35  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
36  *    See the License for the specific language governing permissions and
37  *    limitations under the License.
38  *
39  *    THIS FILE WAS GENERATED AUTOMATICALLY BY THE BUILD SYSTEM.
40  */
41
42 #include <nlunit-test.h>
43
44 '''
45
46 ### Forward declarations will be added here
47
48 TEMPLATE_MAIN_START = '''
49 int main()
50 {
51     int code = 0;
52
53     nlTestSetOutputStyle(OUTPUT_CSV);
54
55 '''
56
57 ### Test invokation will be added here
58
59 TEMPLATE_SUFFIX = '''
60     return code;
61 }'''
62
63 def main(argv):
64   parser = optparse.OptionParser()
65
66   parser.add_option('--input_file')
67   parser.add_option('--output_file')
68
69   options, _ = parser.parse_args(argv)
70   
71   tests = []
72
73   TEST_SUITE_RE = re.compile(r'\s*CHIP_REGISTER_TEST_SUITE\(([^)]*)\)')
74
75   with open(options.input_file, 'r') as input_file:
76       for l in input_file.readlines():
77           match = TEST_SUITE_RE.match(l)
78           if not match: 
79               continue 
80         
81           tests.append(match.group(1))
82
83   if not tests:
84       print("ERROR: no tests found in '%s'" % input_file);
85       print("Did you forget to CHIP_REGISTER_TEST_SUITE?");
86       return 1
87
88   with open(options.output_file, 'w') as output_file:
89       output_file.write(TEMPLATE_PREFIX)
90
91       for test in tests:
92          output_file.write("int %s();\n" % test)
93       output_file.write("\n");
94
95       output_file.write(TEMPLATE_MAIN_START)
96
97       for test in tests:
98          output_file.write("    code = code | (%s());\n" % test)
99       output_file.write("\n");
100
101
102       output_file.write(TEMPLATE_SUFFIX)
103   
104
105   return 0
106
107
108 if __name__ == '__main__':
109     sys.exit(main(sys.argv[1:]))