Skip to content

Commit 1409a68

Browse files
galpeterrerobika
authored andcommitted
Generate jerryscript-config.h before build (#2934)
Previously after the library was build there was no easy way to get back the information of how it was build. This change adds a bit of CMake code to generate the jerryscript-config.h file containing the default options and adding the modified build options/features. JerryScript-DCO-1.0-Signed-off-by: Peter Gal [email protected]
1 parent c903064 commit 1409a68

File tree

2 files changed

+107
-4
lines changed

2 files changed

+107
-4
lines changed

jerry-core/CMakeLists.txt

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ if(ENABLE_ALL_IN_ONE_SOURCE)
169169
set(ALL_IN_FILE_H "${CMAKE_BINARY_DIR}/src/jerryscript.h")
170170
set(JERRYSCRIPT_CONFIG_H "${CMAKE_BINARY_DIR}/src/jerryscript-config.h")
171171

172-
add_custom_command(OUTPUT ${ALL_IN_FILE} ${ALL_IN_FILE_H} ${JERRYSCRIPT_CONFIG_H}
172+
add_custom_command(OUTPUT ${ALL_IN_FILE} ${ALL_IN_FILE_H}
173173
COMMAND python ${CMAKE_SOURCE_DIR}/tools/srcgenerator.py
174174
--jerry-core
175175
--output-dir ${CMAKE_BINARY_DIR}/src
@@ -178,6 +178,14 @@ if(ENABLE_ALL_IN_ONE_SOURCE)
178178
${CMAKE_SOURCE_DIR}/tools/srcgenerator.py
179179
${CMAKE_SOURCE_DIR}/tools/srcmerger.py
180180
)
181+
182+
# The "true" jerryscript-config.h will be generated by the configure_file below,
183+
# which contains the default options and the ones passed for the CMake.
184+
# The input for this is the jerryscript-config.h generated by the command above.
185+
set(JERRYSCRIPT_GEN_CONFIG_H ${CMAKE_CURRENT_BINARY_DIR}/jerryscript-config.h)
186+
add_custom_command(OUTPUT ${JERRYSCRIPT_CONFIG_H}
187+
COMMAND ${CMAKE_COMMAND} -E copy ${JERRYSCRIPT_GEN_CONFIG_H} ${JERRYSCRIPT_CONFIG_H}
188+
DEPENDS ${ALL_IN_FILE_C} ${ALL_IN_FILE_H})
181189
add_custom_target(generate-single-source-jerry DEPENDS ${ALL_IN_FILE} ${ALL_IN_FILE_H})
182190
add_dependencies(generate-single-source generate-single-source-jerry)
183191

@@ -245,6 +253,16 @@ if(EXISTS ${JERRY_PROFILE})
245253
file(READ "${JERRY_PROFILE}" PROFILE_SETTINGS)
246254
string(REGEX REPLACE "^#.*$" "" PROFILE_SETTINGS "${PROFILE_SETTINGS}")
247255
string(REGEX REPLACE "[\r|\n]" ";" PROFILE_SETTINGS "${PROFILE_SETTINGS}")
256+
257+
# Process entries and save them as CMake variables.
258+
# This is required to correctly generate the jerryscript-config.h file.
259+
foreach(PROFILE_ENTRY ${PROFILE_SETTINGS})
260+
string(REPLACE "=" ";" PROFILE_ENTRY "${PROFILE_ENTRY}")
261+
list(GET PROFILE_ENTRY 0 PROFILE_KEY)
262+
list(GET PROFILE_ENTRY 1 PROFILE_VALUE)
263+
set(${PROFILE_KEY} ${PROFILE_VALUE})
264+
endforeach()
265+
248266
set(DEFINES_JERRY ${DEFINES_JERRY} ${PROFILE_SETTINGS})
249267
else()
250268
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
280298
# Maximum size of stack memory usage
281299
set(DEFINES_JERRY ${DEFINES_JERRY} JERRY_STACK_LIMIT=${JERRY_STACK_LIMIT})
282300

301+
## This function is to read "config.h" for default values
302+
function(read_set_defines FILE PREFIX OUTPUTVAR)
303+
file(READ "${CMAKE_CURRENT_SOURCE_DIR}/${FILE}" INPUT_FILE_CONTENTS)
304+
305+
# match all "#define <PREFIX>\n" lines
306+
# notes:
307+
# * before the "#" there must be a newline and any number of spaces.
308+
# * after the "#" there can be any number of spaces.
309+
string(REGEX MATCHALL "\r?\n[ ]*#[ ]*define ${PREFIX}[^\n]*"
310+
RAW_DEFINES "${INPUT_FILE_CONTENTS}")
311+
312+
set(SELECTED_VARS )
313+
314+
# Transform the defines to a list of (<name>; <value>; <name 2>; <value 2>; ...) list
315+
foreach(DEFINE_ENTRY ${RAW_DEFINES})
316+
# by default every define value is empty
317+
set(DEFINE_VALUE " ")
318+
319+
# split up the define at the space between the define name and value (if there is any)
320+
321+
# first remove "#define" part of the string
322+
string(REGEX REPLACE "\r?\n[ ]*#[ ]*define[ ]+" "" DEFINE_KEY_VALUE "${DEFINE_ENTRY}")
323+
string(FIND "${DEFINE_KEY_VALUE}" " " DEFINE_KEY_IDX)
324+
string(LENGTH "${DEFINE_KEY_VALUE}" DEFINE_LENGTH)
325+
326+
if (DEFINE_KEY_IDX EQUAL "-1")
327+
set(DEFINE_KEY ${DEFINE_KEY_VALUE})
328+
else()
329+
string(SUBSTRING "${DEFINE_KEY_VALUE}" 0 ${DEFINE_KEY_IDX} DEFINE_KEY)
330+
string(SUBSTRING "${DEFINE_KEY_VALUE}" ${DEFINE_KEY_IDX} -1 DEFINE_VALUE)
331+
string(STRIP "${DEFINE_VALUE}" DEFINE_VALUE)
332+
endif()
333+
334+
list(APPEND SELECTED_VARS ${DEFINE_KEY} ${DEFINE_VALUE})
335+
endforeach()
336+
337+
set(${OUTPUTVAR} ${SELECTED_VARS} PARENT_SCOPE)
338+
endfunction(read_set_defines)
339+
340+
# CONFIG_DEFAULTS contains define name and values which have the JERRY_ prefix
341+
# as a list of (<name>; <value>; <name 2>; <value 2>; ...)
342+
read_set_defines("config.h" JERRY_ CONFIG_DEFAULTS)
343+
344+
345+
## Process the default values and build options to generate build config defines
346+
list(LENGTH CONFIG_DEFAULTS CONFIG_DEFAULT_LENGTH)
347+
math(EXPR CONFIG_DEFAULT_LENGTH "${CONFIG_DEFAULT_LENGTH} - 1")
348+
349+
set(JERRY_MODIFIED_OPTIONS)
350+
foreach(CONFIG_IDX RANGE 0 ${CONFIG_DEFAULT_LENGTH} 2)
351+
list(GET CONFIG_DEFAULTS ${CONFIG_IDX} KEY)
352+
math(EXPR VALUE_IDX "${CONFIG_IDX} + 1")
353+
list(GET CONFIG_DEFAULTS ${VALUE_IDX} VALUE)
354+
355+
# ${KEY} is the value for the given variable (aka define)
356+
# normalize ON/OFF cmake values to 1/0 for easier processing.
357+
if(${KEY} STREQUAL "ON")
358+
set(${KEY} 1)
359+
elseif(${KEY} STREQUAL "OFF")
360+
set(${KEY} 0)
361+
endif()
362+
363+
# Generate "#define JERRY_<CONFIG> <CONFIG_VALUE>" entries if it is different from
364+
# the config default.
365+
366+
# If the define loaded from the config file have a different value than the
367+
# relevant option passed for the CMake means that it does not have a default value.
368+
if(DEFINED ${KEY} AND NOT (${KEY} STREQUAL ${VALUE}))
369+
set(JERRY_MODIFIED_OPTIONS "${JERRY_MODIFIED_OPTIONS}#define ${KEY} ${${KEY}}\n")
370+
endif()
371+
endforeach()
372+
373+
# Generate the jerryscript-config.h file into the build directory
374+
# This file will contain the options different from the default (aka it's the build config).
375+
if(JERRY_MODIFIED_OPTIONS)
376+
set(JERRY_BUILD_CFG
377+
"Generated differences from default by CMake based on build options:\n${JERRY_MODIFIED_OPTIONS}")
378+
else()
379+
set(JERRY_BUILD_CFG "JerryScript configuration")
380+
endif()
381+
configure_file(config.h jerryscript-config.h @ONLY)
382+
283383
add_library(${JERRY_CORE_NAME} ${SOURCE_CORE_FILES})
284384

285385
target_compile_definitions(${JERRY_CORE_NAME} PUBLIC ${DEFINES_JERRY})
@@ -304,4 +404,5 @@ configure_file(libjerry-core.pc.in libjerry-core.pc @ONLY)
304404

305405
install(TARGETS ${JERRY_CORE_NAME} DESTINATION lib)
306406
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libjerry-core.pc DESTINATION lib/pkgconfig)
407+
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/jerryscript-config.h DESTINATION include)
307408
install(DIRECTORY ${INCLUDE_CORE_PUBLIC}/ DESTINATION include)

jerry-core/config.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
* limitations under the License.
1414
*/
1515

16-
#ifndef CONFIG_H
17-
#define CONFIG_H
16+
#ifndef JERRYSCRIPT_CONFIG_H
17+
#define JERRYSCRIPT_CONFIG_H
18+
19+
// @JERRY_BUILD_CFG@
1820

1921
/**
2022
* Built-in configurations
@@ -698,4 +700,4 @@
698700
# error "Date does not support float32"
699701
#endif
700702

701-
#endif /* !CONFIG_H */
703+
#endif /* !JERRYSCRIPT_CONFIG_H */

0 commit comments

Comments
 (0)