diff --git a/jerry-core/CMakeLists.txt b/jerry-core/CMakeLists.txt index c75ddbf9f..4c812b822 100644 --- a/jerry-core/CMakeLists.txt +++ b/jerry-core/CMakeLists.txt @@ -169,7 +169,7 @@ if(ENABLE_ALL_IN_ONE_SOURCE) set(ALL_IN_FILE_H "${CMAKE_BINARY_DIR}/src/jerryscript.h") set(JERRYSCRIPT_CONFIG_H "${CMAKE_BINARY_DIR}/src/jerryscript-config.h") - add_custom_command(OUTPUT ${ALL_IN_FILE} ${ALL_IN_FILE_H} ${JERRYSCRIPT_CONFIG_H} + add_custom_command(OUTPUT ${ALL_IN_FILE} ${ALL_IN_FILE_H} COMMAND python ${CMAKE_SOURCE_DIR}/tools/srcgenerator.py --jerry-core --output-dir ${CMAKE_BINARY_DIR}/src @@ -178,6 +178,14 @@ if(ENABLE_ALL_IN_ONE_SOURCE) ${CMAKE_SOURCE_DIR}/tools/srcgenerator.py ${CMAKE_SOURCE_DIR}/tools/srcmerger.py ) + + # The "true" jerryscript-config.h will be generated by the configure_file below, + # which contains the default options and the ones passed for the CMake. + # The input for this is the jerryscript-config.h generated by the command above. + set(JERRYSCRIPT_GEN_CONFIG_H ${CMAKE_CURRENT_BINARY_DIR}/jerryscript-config.h) + add_custom_command(OUTPUT ${JERRYSCRIPT_CONFIG_H} + COMMAND ${CMAKE_COMMAND} -E copy ${JERRYSCRIPT_GEN_CONFIG_H} ${JERRYSCRIPT_CONFIG_H} + DEPENDS ${ALL_IN_FILE_C} ${ALL_IN_FILE_H}) add_custom_target(generate-single-source-jerry DEPENDS ${ALL_IN_FILE} ${ALL_IN_FILE_H}) add_dependencies(generate-single-source generate-single-source-jerry) @@ -245,6 +253,16 @@ if(EXISTS ${JERRY_PROFILE}) file(READ "${JERRY_PROFILE}" PROFILE_SETTINGS) string(REGEX REPLACE "^#.*$" "" PROFILE_SETTINGS "${PROFILE_SETTINGS}") string(REGEX REPLACE "[\r|\n]" ";" PROFILE_SETTINGS "${PROFILE_SETTINGS}") + + # Process entries and save them as CMake variables. + # This is required to correctly generate the jerryscript-config.h file. + foreach(PROFILE_ENTRY ${PROFILE_SETTINGS}) + string(REPLACE "=" ";" PROFILE_ENTRY "${PROFILE_ENTRY}") + list(GET PROFILE_ENTRY 0 PROFILE_KEY) + list(GET PROFILE_ENTRY 1 PROFILE_VALUE) + set(${PROFILE_KEY} ${PROFILE_VALUE}) + endforeach() + set(DEFINES_JERRY ${DEFINES_JERRY} ${PROFILE_SETTINGS}) else() message(FATAL_ERROR "Profile file: '${JERRY_PROFILE}' doesn't exist!") @@ -280,6 +298,88 @@ set(DEFINES_JERRY ${DEFINES_JERRY} JERRY_GLOBAL_HEAP_SIZE=${JERRY_GLOBAL_HEAP_SI # Maximum size of stack memory usage set(DEFINES_JERRY ${DEFINES_JERRY} JERRY_STACK_LIMIT=${JERRY_STACK_LIMIT}) +## This function is to read "config.h" for default values +function(read_set_defines FILE PREFIX OUTPUTVAR) + file(READ "${CMAKE_CURRENT_SOURCE_DIR}/${FILE}" INPUT_FILE_CONTENTS) + + # match all "#define \n" lines + # notes: + # * before the "#" there must be a newline and any number of spaces. + # * after the "#" there can be any number of spaces. + string(REGEX MATCHALL "\r?\n[ ]*#[ ]*define ${PREFIX}[^\n]*" + RAW_DEFINES "${INPUT_FILE_CONTENTS}") + + set(SELECTED_VARS ) + + # Transform the defines to a list of (; ; ; ; ...) list + foreach(DEFINE_ENTRY ${RAW_DEFINES}) + # by default every define value is empty + set(DEFINE_VALUE " ") + + # split up the define at the space between the define name and value (if there is any) + + # first remove "#define" part of the string + string(REGEX REPLACE "\r?\n[ ]*#[ ]*define[ ]+" "" DEFINE_KEY_VALUE "${DEFINE_ENTRY}") + string(FIND "${DEFINE_KEY_VALUE}" " " DEFINE_KEY_IDX) + string(LENGTH "${DEFINE_KEY_VALUE}" DEFINE_LENGTH) + + if (DEFINE_KEY_IDX EQUAL "-1") + set(DEFINE_KEY ${DEFINE_KEY_VALUE}) + else() + string(SUBSTRING "${DEFINE_KEY_VALUE}" 0 ${DEFINE_KEY_IDX} DEFINE_KEY) + string(SUBSTRING "${DEFINE_KEY_VALUE}" ${DEFINE_KEY_IDX} -1 DEFINE_VALUE) + string(STRIP "${DEFINE_VALUE}" DEFINE_VALUE) + endif() + + list(APPEND SELECTED_VARS ${DEFINE_KEY} ${DEFINE_VALUE}) + endforeach() + + set(${OUTPUTVAR} ${SELECTED_VARS} PARENT_SCOPE) +endfunction(read_set_defines) + +# CONFIG_DEFAULTS contains define name and values which have the JERRY_ prefix +# as a list of (; ; ; ; ...) +read_set_defines("config.h" JERRY_ CONFIG_DEFAULTS) + + +## Process the default values and build options to generate build config defines +list(LENGTH CONFIG_DEFAULTS CONFIG_DEFAULT_LENGTH) +math(EXPR CONFIG_DEFAULT_LENGTH "${CONFIG_DEFAULT_LENGTH} - 1") + +set(JERRY_MODIFIED_OPTIONS) +foreach(CONFIG_IDX RANGE 0 ${CONFIG_DEFAULT_LENGTH} 2) + list(GET CONFIG_DEFAULTS ${CONFIG_IDX} KEY) + math(EXPR VALUE_IDX "${CONFIG_IDX} + 1") + list(GET CONFIG_DEFAULTS ${VALUE_IDX} VALUE) + + # ${KEY} is the value for the given variable (aka define) + # normalize ON/OFF cmake values to 1/0 for easier processing. + if(${KEY} STREQUAL "ON") + set(${KEY} 1) + elseif(${KEY} STREQUAL "OFF") + set(${KEY} 0) + endif() + + # Generate "#define JERRY_ " entries if it is different from + # the config default. + + # If the define loaded from the config file have a different value than the + # relevant option passed for the CMake means that it does not have a default value. + if(DEFINED ${KEY} AND NOT (${KEY} STREQUAL ${VALUE})) + set(JERRY_MODIFIED_OPTIONS "${JERRY_MODIFIED_OPTIONS}#define ${KEY} ${${KEY}}\n") + endif() +endforeach() + +# Generate the jerryscript-config.h file into the build directory +# This file will contain the options different from the default (aka it's the build config). +if(JERRY_MODIFIED_OPTIONS) + set(JERRY_BUILD_CFG + "Generated differences from default by CMake based on build options:\n${JERRY_MODIFIED_OPTIONS}") +else() + set(JERRY_BUILD_CFG "JerryScript configuration") +endif() +configure_file(config.h jerryscript-config.h @ONLY) + add_library(${JERRY_CORE_NAME} ${SOURCE_CORE_FILES}) target_compile_definitions(${JERRY_CORE_NAME} PUBLIC ${DEFINES_JERRY}) @@ -304,4 +404,5 @@ configure_file(libjerry-core.pc.in libjerry-core.pc @ONLY) install(TARGETS ${JERRY_CORE_NAME} DESTINATION lib) install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libjerry-core.pc DESTINATION lib/pkgconfig) +install(FILES ${CMAKE_CURRENT_BINARY_DIR}/jerryscript-config.h DESTINATION include) install(DIRECTORY ${INCLUDE_CORE_PUBLIC}/ DESTINATION include) diff --git a/jerry-core/config.h b/jerry-core/config.h index 3fd3a3221..77bacb49a 100644 --- a/jerry-core/config.h +++ b/jerry-core/config.h @@ -13,8 +13,10 @@ * limitations under the License. */ -#ifndef CONFIG_H -#define CONFIG_H +#ifndef JERRYSCRIPT_CONFIG_H +#define JERRYSCRIPT_CONFIG_H + +// @JERRY_BUILD_CFG@ /** * Built-in configurations @@ -698,4 +700,4 @@ # error "Date does not support float32" #endif -#endif /* !CONFIG_H */ +#endif /* !JERRYSCRIPT_CONFIG_H */