OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python | |
2 # Copyright 2017 The Chromium Authors. All rights reserved. | |
3 # Use of this source code is governed by a BSD-style license that can be | |
4 # found in the LICENSE file. | |
5 """A script to compile the integration tests with llvm's clang, | |
6 instrument the code with llvm's Asan, and link it to the syzyasan_rtl | |
7 runtime environment. | |
8 """ | |
9 | |
10 import optparse | |
11 import os | |
12 import re | |
13 import subprocess | |
14 import sys | |
15 | |
16 | |
17 _SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__)) | |
18 _SRC_DIR = os.path.abspath(os.path.join(_SCRIPT_DIR, os.pardir, os.pardir)) | |
19 _CLANG_CL_PATH = os.path.join(_SRC_DIR, | |
20 'third_party', 'llvm-build', 'Release+Asserts', 'bin', 'clang-cl.exe') | |
21 | |
22 | |
23 def compile_with_asan(clang_path, source_files, src_dir, object_files, | |
24 target_name): | |
25 """Compiles the files and instruments them with LLVM's ASAN. | |
26 The linking is done with the method link below. | |
27 | |
28 Only compiles the files and instruments them with LLVM's Asan but does not | |
29 link them. The linking is done separately in the method link. | |
30 | |
31 Args: | |
32 clang_path: Path to the clang-cl compiler. | |
33 source_files: The source files to be compiled. | |
34 src_dir: The repository where the syzygy src is located. | |
35 object_files: The path where each object file should be generated. | |
36 target_name: The name of the target being build. | |
37 """ | |
38 | |
39 compiler_flags = [ | |
40 '-c', | |
41 '-m32', | |
42 '-fsanitize=address', | |
43 '-mllvm', | |
44 '-asan-instrumentation-with-call-threshold=0', | |
45 '-mllvm', | |
46 '-asan-stack=0', | |
47 '-DUNICODE', | |
48 '-D_UNICODE', | |
49 '-DNOMINMAX', | |
50 '-D_CRT_SECURE_NO_WARNINGS', | |
51 '-I', | |
52 src_dir, | |
53 ] | |
54 | |
55 compile_command_base = [clang_path] | |
56 compile_command_base.extend(compiler_flags) | |
57 | |
58 for source_file, object_file in zip(source_files, object_files): | |
59 compile_command = [compile_command_base, source_file, '-o', object_files] | |
60 | |
61 ret = subprocess.call(compile_command) | |
62 if ret != 0: | |
63 print 'ERROR: Failed compiling %s using clang-cl.' % target_name | |
64 return ret | |
65 return ret | |
66 | |
67 | |
68 def link(clang_path, object_files, build_dir, target_name): | |
69 """ Links the object files and produces the integration_tests_clang_dll.dll. | |
70 | |
71 Links the object files and produces the dll. The object files have to be | |
72 produced by the compile method above. | |
73 | |
74 Args: | |
75 clang_path: Path to the clang-cl compiler in the syzygy project. | |
76 source_files: The source file names which are converted to obj filenames. | |
77 build_dir: The directory where to produce the linked dll. | |
78 target_name: The name of the target being build. | |
79 """ | |
80 | |
81 linker_flags = [ | |
82 '-o', | |
83 os.path.join(build_dir, target_name + '.dll'), | |
84 '/link', | |
85 '/dll', | |
86 build_dir + '\export_dll.dll.lib', | |
etienneb
2017/07/10 14:55:40
nit: please use 'os.path.join'
| |
87 build_dir + '\syzyasan_rtl.dll.lib', | |
etienneb
2017/07/10 14:55:40
nit: please use 'os.path.join'
| |
88 '-defaultlib:libcmt' | |
89 ] | |
90 | |
91 linker_command = [clang_path, '-m32'] | |
92 linker_command.extend(object_files) | |
93 linker_command.extend(linker_flags) | |
94 ret = subprocess.call(linker_command) | |
95 | |
96 if ret != 0: | |
97 print 'ERROR: Failed to link %s using clang-cl.' % target_name | |
98 return ret | |
99 | |
100 | |
101 def main(): | |
102 parser = optparse.OptionParser(usage='%prog [options]') | |
103 parser.add_option('--output-dir', | |
104 help='Path to the Syzygy Release directory.') | |
105 parser.add_option('--input-files', help='Files to be compiled and linked.') | |
106 parser.add_option('--target-name', help='Name of the target to be compiled.') | |
107 | |
108 options, _ = parser.parse_args() | |
109 | |
110 if not options.output_dir: | |
111 parser.error('--output-dir is required.') | |
112 if not options.input_files: | |
113 parser.error('--input-files is required.') | |
114 if not options.target_name: | |
115 parser.error('--target-name is required.') | |
116 | |
117 def get_object_file_location(source_file, | |
118 output_dir, target_name): | |
119 return os.path.join(output_dir, 'obj', | |
etienneb
2017/07/10 14:55:40
os.path.split and [0] is complicated to read.
You
| |
120 os.path.split(os.path.relpath(source_file, | |
121 _SRC_DIR))[0], | |
122 '%s.%s.obj' % (target_name, | |
123 os.path.splitext(os.path.basename(source_file))[0]) | |
124 | |
125 source_files = options.input_files.split() | |
126 object_files = [] | |
127 | |
128 for source_file in source_files: | |
129 object_files.append(get_object_file_location(source_file, | |
130 options.output_dir, | |
131 options.target_name)) | |
132 | |
133 ret = compile_with_asan(_CLANG_CL_PATH, source_files, _SRC_DIR, | |
134 object_files, options.target_name) | |
135 | |
136 if ret == 0: | |
137 ret = link(_CLANG_CL_PATH, object_files, options.output_dir, | |
138 options.target_name) | |
139 else: | |
140 print ('ERROR: Compilation of %s failed, skipping link step.' | |
141 % options.target_name) | |
142 | |
143 return ret | |
144 | |
145 | |
146 if __name__ == '__main__': | |
147 sys.exit(main()) | |
OLD | NEW |