forked from ruslo/hunter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-predefined-list.py
executable file
·195 lines (164 loc) · 4.72 KB
/
create-predefined-list.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/env python3
# Copyright (c) 2014, Ruslan Baratov
# All rights reserved.
import argparse
import os
import re
import subprocess
import urllib.request
argparser = argparse.ArgumentParser(
description="""
Create C++ source file `ShowPredefined.cpp` which will be used to verify
toolchain compatibility.
"""
)
argparser.add_argument(
'--raw',
help="""
File with raw list of macroses (used simultaneously as initial list and
file to append result list)
"""
)
argparser.add_argument(
'--site',
help="""
Read macroses <strong>_...</strong> from site.
Example: http://msdn.microsoft.com/en-us/library/b0084kay.aspx
"""
)
argparser.add_argument(
'--compiler',
help="""
GCC-like unix compiler that accept syntax: '-E -x c++ -dM /dev/null'
"""
)
argparser.add_argument(
'--arch',
help="""
Architecture for compiler (e.g. armv7, armv7s, arm64, i386, x86_64, ...)
"""
)
argparser.add_argument(
'--boost-predef',
action='store_true',
help="""
Read macroses from repository: https://github.com/boostorg/predef
"""
)
args = argparser.parse_args()
# This macroses is not quite helpful
exclude_list = [
'__DATE__',
'__FILE__',
'__LINE__',
'__TIME__',
'__TIMESTAMP__',
'__NO_INLINE__',
'_DEBUG',
'__FUNCTION__',
# gcc & clang preprocessor extensions
# see https://github.com/ruslo/hunter/pull/425
'__has_include',
'__has_include_next',
]
macros_list = []
if args.raw:
data = open(args.raw, "r").read()
macros_list += data.split()
if args.site:
content = urllib.request.urlopen(args.site).read().decode('utf-8')
parsed = re.findall('<strong>_[^<]*</strong>', content)
for x in parsed:
macros_list.append(re.sub(r'<strong>(.*)</strong>', r'\1', x))
if args.boost_predef:
temp_dir = os.path.join(os.getcwd(), '__temp-git-predef')
if not os.path.exists(temp_dir):
subprocess.check_call(
['git', 'clone', 'https://github.com/boostorg/predef', temp_dir]
)
gitdir = os.path.join(temp_dir, '.git')
docdir = os.path.join(temp_dir, 'doc')
assert(os.path.exists(gitdir))
assert(os.path.exists(docdir))
macro = re.compile(r'\bdefined\(_[^)]*\)')
boost_macro_list = []
for root, dirs, files in os.walk(temp_dir):
if root.startswith(gitdir):
continue
if root.startswith(docdir):
continue
for x in files:
filename = os.path.join(root, x)
boost_macro_list += macro.findall(open(filename).read())
boost_macro_list = list(set(boost_macro_list))
macros_list += [
re.sub(r'^defined\((.*)\)$', r'\1', x) for x in boost_macro_list
]
if args.compiler:
run_args = [args.compiler, '-E', '-x', 'c++', '-dM', '/dev/null']
if args.arch:
run_args += ['-arch', args.arch]
macroses = subprocess.check_output(run_args, universal_newlines=True)
compiler_macro_list = macroses.split('\n')
for x in compiler_macro_list:
if re.match(r'^#define _', x):
macros_list.append(re.sub(r'^#define ([^ (]*).*', r'\1', x))
macros_list = sorted(set(macros_list))
for to_exclude in exclude_list:
if to_exclude in macros_list:
macros_list.remove(to_exclude)
for x in macros_list:
print("> {}".format(x))
if args.raw:
result_fl = open(args.raw, 'w')
for macro in macros_list:
result_fl.write('{}\n'.format(macro))
# https://msdn.microsoft.com/en-us/library/windows/desktop/aa383745(v=vs.85).aspx
cpp_head = """
// This file generated automatically by `create-predefined-list.py` script.
// * https://github.com/ruslo/hunter
#define HUNTER_QUOTE(x) #x
#define HUNTER_STRING(x) HUNTER_QUOTE(x)
#define HUNTER_INFO(x) \\
"__HUNTER_MACRO_CHECK_BEGIN__" \\
"#define " #x " " HUNTER_STRING(x) \\
"__HUNTER_MACRO_CHECK_END__"
#include <exception> // Check std library version
#if defined(__ANDROID__)
# include <android/api-level.h> // Header with __ANDROID_API__
#endif
#if defined(_MSC_VER)
# include <SdkDdkVer.h> // Header with _WIN32_WINNT
#endif
"""
cpp_one_check = """
#if defined({})
# pragma message(HUNTER_INFO({}))
#endif
"""
# http://clang.llvm.org/docs/AddressSanitizer.html#conditional-compilation-with-has-feature-address-sanitizer
sanitize_detect_check = """
#if defined(__has_feature)
# if __has_feature({}_sanitizer)
# pragma message(HUNTER_INFO(__HUNTER_DETECT_FEATURE_{}_sanitizer))
# endif
#endif
"""
sanitizers_list = [
'address',
# 'leak', # Not detected!
'memory',
'thread'
]
cpp_end = """
int main() {
}
"""
if macros_list:
cpp_result = open('ShowPredefined.cpp', 'w')
cpp_result.write(cpp_head)
for x in macros_list:
cpp_result.write(cpp_one_check.format(x, x))
for x in sanitizers_list:
cpp_result.write(sanitize_detect_check.format(x, x))
cpp_result.write(cpp_end)