|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Copyright 2017 WebAssembly Community Group participants |
| 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 | + |
| 17 | +import os |
| 18 | +import sys |
| 19 | +from support import run_command |
| 20 | +import shared |
| 21 | + |
| 22 | + |
| 23 | +def files_with_extensions(path, extensions): |
| 24 | + for file in sorted(os.listdir(path)): |
| 25 | + _, ext = os.path.splitext(file) |
| 26 | + if ext in extensions: |
| 27 | + yield file, ext |
| 28 | + |
| 29 | + |
| 30 | +def generate_object_files(clang_bin): |
| 31 | + print '\n[ building object files from C sources... ]\n' |
| 32 | + |
| 33 | + lld_path = os.path.join(shared.options.binaryen_test, 'lld') |
| 34 | + for src_file, ext in files_with_extensions(lld_path, ['.c', '.cpp']): |
| 35 | + print '..', src_file |
| 36 | + obj_file = src_file.replace(ext, '.o') |
| 37 | + |
| 38 | + src_path = os.path.join(lld_path, src_file) |
| 39 | + obj_path = os.path.join(lld_path, obj_file) |
| 40 | + run_command([ |
| 41 | + clang_bin, src_path, '-o', obj_path, |
| 42 | + '--target=wasm32-unknown-unknown-wasm', |
| 43 | + '-c', |
| 44 | + '-nostdinc', |
| 45 | + '-Xclang', '-nobuiltininc', |
| 46 | + '-Xclang', '-nostdsysteminc', |
| 47 | + '-Xclang', '-I/s/work/emscripten/system/include', |
| 48 | + '-O1', |
| 49 | + ]) |
| 50 | + |
| 51 | + |
| 52 | +def generate_wast_files(lld_bin): |
| 53 | + print '\n[ linking wasm files from object files... ]\n' |
| 54 | + |
| 55 | + lld_path = os.path.join(shared.options.binaryen_test, 'lld') |
| 56 | + for obj_file, ext in files_with_extensions(lld_path, ['.o']): |
| 57 | + print '..', obj_file |
| 58 | + wasm_file = obj_file.replace(ext, '.wasm') |
| 59 | + wast_file = obj_file.replace(ext, '.wast') |
| 60 | + |
| 61 | + obj_path = os.path.join(lld_path, obj_file) |
| 62 | + wasm_path = os.path.join(lld_path, wasm_file) |
| 63 | + wast_path = os.path.join(lld_path, wast_file) |
| 64 | + run_command([ |
| 65 | + lld_bin, '-flavor', 'wasm', |
| 66 | + '-z', '-stack-size=1048576', |
| 67 | + obj_path, '-o', wasm_path, |
| 68 | + '--entry=main', |
| 69 | + '--allow-undefined', |
| 70 | + '--export', '__wasm_call_ctors', |
| 71 | + ]) |
| 72 | + try: |
| 73 | + run_command(shared.WASM_DIS + [wasm_path, '-o', wast_path]) |
| 74 | + finally: |
| 75 | + # Don't need the .wasm file, don't leave it around |
| 76 | + shared.delete_from_orbit(wasm_path) |
| 77 | + |
| 78 | + |
| 79 | +if __name__ == '__main__': |
| 80 | + if len(sys.argv) != 3: |
| 81 | + print 'Usage: generate_lld_tests.py [path/to/clang] [path/to/lld]' |
| 82 | + sys.exit(1) |
| 83 | + generate_object_files(sys.argv[1]) |
| 84 | + generate_wast_files(sys.argv[2]) |
0 commit comments