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. 16from __future__ import print_function 17 18import argparse 19import logging 20import os 21import subprocess 22import shutil 23 24 25TOOLS_DIR = os.path.dirname(os.path.abspath(__file__)) 26ROOT_DIR = os.path.dirname(TOOLS_DIR) 27SRCMERGER = os.path.join(TOOLS_DIR, 'srcmerger.py') 28JERRY_CORE = os.path.join(ROOT_DIR, 'jerry-core') 29JERRY_PORT = os.path.join(ROOT_DIR, 'jerry-port', 'default') 30JERRY_LIBM = os.path.join(ROOT_DIR, 'jerry-libm') 31 32 33def 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 44def 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 # Add the global built-in by default to include some common items 52 # to avoid problems with common built-in headers 53 '--input={}/ecma/builtin-objects/ecma-builtins.c'.format(JERRY_CORE), 54 '--remove-include=jerryscript.h', 55 '--remove-include=jerryscript-port.h', 56 '--remove-include=jerryscript-compiler.h', 57 '--remove-include=jerryscript-core.h', 58 '--remove-include=jerryscript-debugger.h', 59 '--remove-include=jerryscript-debugger-transport.h', 60 '--remove-include=jerryscript-port.h', 61 '--remove-include=jerryscript-snapshot.h', 62 '--remove-include=config.h', 63 '--push-include=jerryscript.h', 64 ] 65 66 cmd_jerry_h_gen = [ 67 'python', SRCMERGER, 68 '--base-dir', JERRY_CORE, 69 '--input={}/include/jerryscript.h'.format(JERRY_CORE), 70 '--output={}/jerryscript.h'.format(output_dir), 71 '--remove-include=config.h', 72 '--push-include=jerryscript-config.h', 73 ] 74 75 run_commands(cmd_jerry_c_gen, cmd_jerry_h_gen, verbose=verbose) 76 77 shutil.copyfile('{}/config.h'.format(JERRY_CORE), 78 '{}/jerryscript-config.h'.format(output_dir)) 79 80 81def generate_jerry_port_default(output_dir, verbose=False): 82 cmd_port_c_gen = [ 83 'python', SRCMERGER, 84 '--base-dir', JERRY_PORT, 85 '--output={}/jerryscript-port-default.c'.format(output_dir), 86 '--append-c-files', 87 '--remove-include=jerryscript-port.h', 88 '--remove-include=jerryscript-port-default.h', 89 '--remove-include=jerryscript-debugger.h', 90 '--push-include=jerryscript.h', 91 '--push-include=jerryscript-port-default.h', 92 ] 93 94 cmd_port_h_gen = [ 95 'python', SRCMERGER, 96 '--base-dir', JERRY_PORT, 97 '--input={}/include/jerryscript-port-default.h'.format(JERRY_PORT), 98 '--output={}/jerryscript-port-default.h'.format(output_dir), 99 '--remove-include=jerryscript-port.h', 100 '--remove-include=jerryscript.h', 101 '--push-include=jerryscript.h', 102 ] 103 104 run_commands(cmd_port_c_gen, cmd_port_h_gen, verbose=verbose) 105 106 107def generate_jerry_libm(output_dir, verbose=False): 108 cmd_libm_c_gen = [ 109 'python', SRCMERGER, 110 '--base-dir', JERRY_LIBM, 111 '--output={}/jerryscript-libm.c'.format(output_dir), 112 '--append-c-files', 113 ] 114 115 run_commands(cmd_libm_c_gen, verbose=verbose) 116 117 shutil.copyfile('{}/include/math.h'.format(JERRY_LIBM), 118 '{}/math.h'.format(output_dir)) 119 120def main(): 121 parser = argparse.ArgumentParser(description='Generate single sources.') 122 parser.add_argument('--jerry-core', action='store_true', dest='jerry_core', 123 help='Generate jerry-core files', default=False) 124 parser.add_argument('--jerry-port-default', action='store_true', dest='jerry_port_default', 125 help='Generate jerry-port-default files', default=False) 126 parser.add_argument('--jerry-libm', action='store_true', dest='jerry_libm', 127 help='Generate jerry-libm files', default=False) 128 parser.add_argument('--output-dir', metavar='DIR', type=str, dest='output_dir', 129 default='gen_src', help='Output dir') 130 parser.add_argument('--verbose', '-v', action='store_true', default=False) 131 132 args = parser.parse_args() 133 134 if args.verbose: 135 logging.basicConfig(level=logging.DEBUG) 136 137 try: 138 os.makedirs(args.output_dir) 139 except os.error: 140 pass 141 142 if args.jerry_core: 143 generate_jerry_core(args.output_dir, args.verbose) 144 145 if args.jerry_port_default: 146 generate_jerry_port_default(args.output_dir, args.verbose) 147 148 if args.jerry_libm: 149 generate_jerry_libm(args.output_dir, args.verbose) 150 151 152if __name__ == '__main__': 153 main() 154