diff --git a/CMakeLists.txt b/CMakeLists.txt index d8152889f..30e6e0616 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,10 +23,11 @@ string(TOUPPER "${PLATFORM}" PLATFORM) set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS ) # Components -set(JERRY_CMDLINE ON CACHE BOOL "Build jerry command line tool?") -set(JERRY_LIBC ON CACHE BOOL "Build and use jerry-libc?") -set(JERRY_LIBM ON CACHE BOOL "Build and use jerry-libm?") -set(UNITTESTS OFF CACHE BOOL "Build unit tests?") +set(JERRY_CMDLINE ON CACHE BOOL "Build jerry command line tool?") +set(JERRY_CMDLINE_MINIMAL OFF CACHE BOOL "Build jerry minimal command line tool?") +set(JERRY_LIBC ON CACHE BOOL "Build and use jerry-libc?") +set(JERRY_LIBM ON CACHE BOOL "Build and use jerry-libm?") +set(UNITTESTS OFF CACHE BOOL "Build unit tests?") # Optional build settings set(ENABLE_ALL_IN_ONE OFF CACHE BOOL "Enable all-in-one build?") @@ -56,6 +57,7 @@ message(STATUS "ENABLE_LTO " ${ENABLE_LTO}) message(STATUS "ENABLE_STATIC_LINK " ${ENABLE_STATIC_LINK}) message(STATUS "ENABLE_STRIP " ${ENABLE_STRIP}) message(STATUS "JERRY_CMDLINE " ${JERRY_CMDLINE}) +message(STATUS "JERRY_CMDLINE_MINIMAL " ${JERRY_CMDLINE_MINIMAL}) message(STATUS "JERRY_LIBC " ${JERRY_LIBC}) message(STATUS "JERRY_LIBM " ${JERRY_LIBM}) message(STATUS "PORT_DIR " ${PORT_DIR}) @@ -193,7 +195,7 @@ endif() add_subdirectory(jerry-core) # Jerry command line tool -if(JERRY_CMDLINE) +if(JERRY_CMDLINE OR JERRY_CMDLINE_MINIMAL) add_subdirectory(jerry-main) endif() diff --git a/jerry-main/CMakeLists.txt b/jerry-main/CMakeLists.txt index 50a053de9..e03ebd6b1 100644 --- a/jerry-main/CMakeLists.txt +++ b/jerry-main/CMakeLists.txt @@ -13,7 +13,6 @@ # limitations under the License. cmake_minimum_required (VERSION 2.8.12) -set(JERRY_NAME jerry) project (${JERRY_NAME} C) # Optional build settings @@ -22,10 +21,6 @@ set(ENABLE_LINK_MAP OFF CACHE BOOL "Enable generating a link map file?") # Status messages message(STATUS "ENABLE_LINK_MAP " ${ENABLE_LINK_MAP}) -# Sources -# Jerry standalone -set(SOURCE_JERRY_STANDALONE_MAIN main-unix.c) - # Generate map file if(ENABLE_LINK_MAP) if("${PLATFORM}" STREQUAL "DARWIN") @@ -48,13 +43,24 @@ endif() set(DEFINES_JERRY ${DEFINES_JERRY} JERRY_COMMIT_HASH="${JERRY_COMMIT_HASH}") -add_executable(${JERRY_NAME} ${SOURCE_JERRY_STANDALONE_MAIN}) -set_property(TARGET ${JERRY_NAME} - PROPERTY LINK_FLAGS "${LINKER_FLAGS_COMMON}") -target_compile_definitions(${JERRY_NAME} PRIVATE ${DEFINES_JERRY}) -target_include_directories(${JERRY_NAME} PRIVATE ${PORT_DIR}) -link_directories(${CMAKE_BINARY_DIR}) +macro(jerry_create_executable JERRY_NAME SOURCE_JERRY_STANDALONE_MAIN) + add_executable(${JERRY_NAME} ${SOURCE_JERRY_STANDALONE_MAIN}) + set_property(TARGET ${JERRY_NAME} + PROPERTY LINK_FLAGS "${LINKER_FLAGS_COMMON}") + target_compile_definitions(${JERRY_NAME} PRIVATE ${DEFINES_JERRY}) + target_include_directories(${JERRY_NAME} PRIVATE ${PORT_DIR}) + link_directories(${CMAKE_BINARY_DIR}) -target_link_libraries(${JERRY_NAME} jerry-core) + target_link_libraries(${JERRY_NAME} jerry-core) -install(TARGETS ${JERRY_NAME} DESTINATION bin) + install(TARGETS ${JERRY_NAME} DESTINATION bin) +endmacro() + +# Jerry standalones +if(JERRY_CMDLINE) + jerry_create_executable("jerry" "main-unix.c") +endif() + +if(JERRY_CMDLINE_MINIMAL) + jerry_create_executable("jerry-minimal" "main-unix-minimal.c") +endif() \ No newline at end of file diff --git a/jerry-main/main-unix-minimal.c b/jerry-main/main-unix-minimal.c new file mode 100644 index 000000000..c5a6dd1ae --- /dev/null +++ b/jerry-main/main-unix-minimal.c @@ -0,0 +1,127 @@ +/* Copyright JS Foundation and other contributors, http://js.foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include "jerry-api.h" +#include "jerry-port-default.h" + +/** + * Maximum size of source code / snapshots buffer + */ +#define JERRY_BUFFER_SIZE (1048576) + +/** + * Standalone Jerry exit codes + */ +#define JERRY_STANDALONE_EXIT_CODE_OK (0) +#define JERRY_STANDALONE_EXIT_CODE_FAIL (1) + +static uint8_t buffer[ JERRY_BUFFER_SIZE ]; + +static const uint8_t * +read_file (const char *file_name, + size_t *out_size_p) +{ + FILE *file = fopen (file_name, "r"); + if (file == NULL) + { + jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: failed to open file: %s\n", file_name); + return NULL; + } + + size_t bytes_read = fread (buffer, 1u, sizeof (buffer), file); + if (!bytes_read) + { + jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: failed to read file: %s\n", file_name); + fclose (file); + return NULL; + } + + fclose (file); + + *out_size_p = bytes_read; + return (const uint8_t *) buffer; +} /* read_file */ + +static void +print_help (char *name) +{ + jerry_port_console ("Usage: %s [OPTION]... [FILE]...\n" + "\n" + "Options:\n" + " -h, --help\n" + "\n", + name); +} /* print_help */ + +int +main (int argc, + char **argv) +{ + if (argc <= 1 || (argc == 2 && (!strcmp ("-h", argv[1]) || !strcmp ("--help", argv[1])))) + { + print_help (argv[0]); + return JERRY_STANDALONE_EXIT_CODE_OK; + } + + jerry_init (JERRY_INIT_EMPTY); + jerry_value_t ret_value = jerry_create_undefined (); + + const char *file_name; + + for (int i = 1; i < argc; i++) + { + file_name = argv[i]; + size_t source_size; + + const jerry_char_t *source_p = read_file (file_name, &source_size); + + if (source_p == NULL) + { + ret_value = jerry_create_error (JERRY_ERROR_COMMON, (jerry_char_t *) ""); + break; + } + else + { + ret_value = jerry_parse (source_p, source_size, false); + + if (!jerry_value_has_error_flag (ret_value)) + { + jerry_value_t func_val = ret_value; + ret_value = jerry_run (func_val); + jerry_release_value (func_val); + } + } + + if (jerry_value_has_error_flag (ret_value)) + { + break; + } + } + + int ret_code = JERRY_STANDALONE_EXIT_CODE_OK; + + if (jerry_value_has_error_flag (ret_value)) + { + jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Unhandled exception: Script Error!\n"); + ret_code = JERRY_STANDALONE_EXIT_CODE_FAIL; + } + + jerry_release_value (ret_value); + jerry_cleanup (); + + return ret_code; +} /* main */ diff --git a/tools/build.py b/tools/build.py index 39afc31d8..a48975bcc 100755 --- a/tools/build.py +++ b/tools/build.py @@ -51,6 +51,7 @@ def get_arguments(): parser.add_argument('--error-messages', metavar='X', choices=['ON', 'OFF'], default='OFF', type=str.upper, help='enable error messages (%(choices)s; default: %(default)s)') parser.add_argument('-j', '--jobs', metavar='N', action='store', type=int, default=multiprocessing.cpu_count() + 1, help='Allowed N build jobs at once (default: %(default)s)') parser.add_argument('--jerry-cmdline', metavar='X', choices=['ON', 'OFF'], default='ON', type=str.upper, help='build jerry command line tool (%(choices)s; default: %(default)s)') + parser.add_argument('--jerry-cmdline-minimal', metavar='X', choices=['ON', 'OFF'], default='OFF', type=str.upper, help='build minimal version of the jerry command line tool (%(choices)s; default: %(default)s)') parser.add_argument('--jerry-libc', metavar='X', choices=['ON', 'OFF'], default='ON', type=str.upper, help='build and use jerry-libc (%(choices)s; default: %(default)s)') parser.add_argument('--jerry-libm', metavar='X', choices=['ON', 'OFF'], default='ON', type=str.upper, help='build and use jerry-libm (%(choices)s; default: %(default)s)') parser.add_argument('--link-lib', metavar='OPT', action='append', default=[], help='add custom library to be linked') @@ -91,6 +92,7 @@ def generate_build_options(arguments): build_options.append('-DFEATURE_CPOINTER_32_BIT=%s' % arguments.cpointer_32bit) build_options.append('-DFEATURE_ERROR_MESSAGES=%s' % arguments.error_messages) build_options.append('-DJERRY_CMDLINE=%s' % arguments.jerry_cmdline) + build_options.append('-DJERRY_CMDLINE_MINIMAL=%s' % arguments.jerry_cmdline_minimal) build_options.append('-DJERRY_LIBC=%s' % arguments.jerry_libc) build_options.append('-DJERRY_LIBM=%s' % arguments.jerry_libm) build_options.append('-DEXTERNAL_LINK_LIBS=' + ' '.join(arguments.link_lib))