|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Copyright JS Foundation and other contributors, http://js.foundation |
| 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 | +from __future__ import print_function |
| 17 | + |
| 18 | +import argparse |
| 19 | +import logging |
| 20 | +import os |
| 21 | +import subprocess |
| 22 | +import shutil |
| 23 | + |
| 24 | + |
| 25 | +TOOLS_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 26 | +ROOT_DIR = os.path.dirname(TOOLS_DIR) |
| 27 | +SRCMERGER = os.path.join(TOOLS_DIR, 'srcmerger.py') |
| 28 | +JERRY_CORE = os.path.join(ROOT_DIR, 'jerry-core') |
| 29 | +JERRY_PORT = os.path.join(ROOT_DIR, 'jerry-port', 'default') |
| 30 | +JERRY_LIBM = os.path.join(ROOT_DIR, 'jerry-libm') |
| 31 | + |
| 32 | + |
| 33 | +def run_commands(*cmds, **kwargs): |
| 34 | + log = logging.getLogger('sourcegenerator') |
| 35 | + verbose = kwargs.get('verbose', False) |
| 36 | + |
| 37 | + for cmd in cmds: |
| 38 | + if verbose: |
| 39 | + cmd.append('--verbose') |
| 40 | + log.debug('Run command: %s', cmd) |
| 41 | + subprocess.call(cmd) |
| 42 | + |
| 43 | + |
| 44 | +def generate_jerry_core(output_dir, verbose=False): |
| 45 | + cmd_jerry_c_gen = [ |
| 46 | + 'python', SRCMERGER, |
| 47 | + '--base-dir', JERRY_CORE, |
| 48 | + '--input={}/api/jerry.c'.format(JERRY_CORE), |
| 49 | + '--output={}/jerryscript.c'.format(output_dir), |
| 50 | + '--append-c-files', |
| 51 | + '--remove-include=jerryscript.h', |
| 52 | + '--remove-include=jerryscript-port.h', |
| 53 | + '--remove-include=jerryscript-compiler.h', |
| 54 | + '--remove-include=jerryscript-core.h', |
| 55 | + '--remove-include=jerryscript-debugger.h', |
| 56 | + '--remove-include=jerryscript-debugger-transport.h', |
| 57 | + '--remove-include=jerryscript-port.h', |
| 58 | + '--remove-include=jerryscript-snapshot.h' |
| 59 | + '--remove-include=config.h', |
| 60 | + '--push-include=jerryscript.h', |
| 61 | + ] |
| 62 | + |
| 63 | + cmd_jerry_h_gen = [ |
| 64 | + 'python', SRCMERGER, |
| 65 | + '--base-dir', JERRY_CORE, |
| 66 | + '--input={}/include/jerryscript.h'.format(JERRY_CORE), |
| 67 | + '--output={}/jerryscript.h'.format(output_dir), |
| 68 | + '--remove-include=config.h', |
| 69 | + '--push-include=jerryscript-config.h', |
| 70 | + ] |
| 71 | + |
| 72 | + run_commands(cmd_jerry_c_gen, cmd_jerry_h_gen, verbose=verbose) |
| 73 | + |
| 74 | + shutil.copyfile('{}/config.h'.format(JERRY_CORE), |
| 75 | + '{}/jerryscript-config.h'.format(output_dir)) |
| 76 | + |
| 77 | + |
| 78 | +def generate_jerry_port_default(output_dir, verbose=False): |
| 79 | + cmd_port_c_gen = [ |
| 80 | + 'python', SRCMERGER, |
| 81 | + '--base-dir', JERRY_PORT, |
| 82 | + '--output={}/jerryscript-port-default.c'.format(output_dir), |
| 83 | + '--append-c-files', |
| 84 | + '--remove-include=jerryscript-port.h', |
| 85 | + '--remove-include=jerryscript-port-default.h', |
| 86 | + '--remove-include=jerryscript-debugger.h', |
| 87 | + '--push-include=jerryscript.h', |
| 88 | + '--push-include=jerryscript-port-default.h', |
| 89 | + ] |
| 90 | + |
| 91 | + cmd_port_h_gen = [ |
| 92 | + 'python', SRCMERGER, |
| 93 | + '--base-dir', JERRY_PORT, |
| 94 | + '--input={}/include/jerryscript-port-default.h'.format(JERRY_PORT), |
| 95 | + '--output={}/jerryscript-port-default.h'.format(output_dir), |
| 96 | + '--remove-include=jerryscript-port.h', |
| 97 | + '--remove-include=jerryscript.h', |
| 98 | + '--push-include=jerryscript.h', |
| 99 | + ] |
| 100 | + |
| 101 | + run_commands(cmd_port_c_gen, cmd_port_h_gen, verbose=verbose) |
| 102 | + |
| 103 | + |
| 104 | +def generate_jerry_libm(output_dir, verbose=False): |
| 105 | + cmd_libm_c_gen = [ |
| 106 | + 'python', SRCMERGER, |
| 107 | + '--base-dir', JERRY_LIBM, |
| 108 | + '--output={}/jerryscript-libm.c'.format(output_dir), |
| 109 | + '--append-c-files', |
| 110 | + ] |
| 111 | + |
| 112 | + run_commands(cmd_libm_c_gen, verbose=verbose) |
| 113 | + |
| 114 | + shutil.copyfile('{}/include/math.h'.format(JERRY_LIBM), |
| 115 | + '{}/math.h'.format(output_dir)) |
| 116 | + |
| 117 | +def main(): |
| 118 | + parser = argparse.ArgumentParser(description='Generate single sources.') |
| 119 | + parser.add_argument('--jerry-core', action='store_true', dest='jerry_core', |
| 120 | + help='Generate jerry-core files', default=False) |
| 121 | + parser.add_argument('--jerry-port-default', action='store_true', dest='jerry_port_default', |
| 122 | + help='Generate jerry-port-default files', default=False) |
| 123 | + parser.add_argument('--jerry-libm', action='store_true', dest='jerry_libm', |
| 124 | + help='Generate jerry-libm files', default=False) |
| 125 | + parser.add_argument('--output-dir', metavar='DIR', type=str, dest='output_dir', |
| 126 | + default='gen_src', help='Output dir') |
| 127 | + parser.add_argument('--verbose', '-v', action='store_true', default=False) |
| 128 | + |
| 129 | + args = parser.parse_args() |
| 130 | + |
| 131 | + if args.verbose: |
| 132 | + logging.basicConfig(level=logging.DEBUG) |
| 133 | + |
| 134 | + try: |
| 135 | + os.makedirs(args.output_dir) |
| 136 | + except os.error: |
| 137 | + pass |
| 138 | + |
| 139 | + if args.jerry_core: |
| 140 | + generate_jerry_core(args.output_dir, args.verbose) |
| 141 | + |
| 142 | + if args.jerry_port_default: |
| 143 | + generate_jerry_port_default(args.output_dir, args.verbose) |
| 144 | + |
| 145 | + if args.jerry_libm: |
| 146 | + generate_jerry_libm(args.output_dir, args.verbose) |
| 147 | + |
| 148 | + |
| 149 | +if __name__ == '__main__': |
| 150 | + main() |
0 commit comments