feat: update cJSON and add JSON minification for MTR_LINE_STATION

This commit is contained in:
GW_MC
2026-01-20 20:11:04 +08:00
parent 654a0bc0f7
commit 4d19dd7294
4 changed files with 70 additions and 5 deletions

View File

@@ -1,6 +1,39 @@
set(requires esp-tls spi_flash nvs_flash esp_event esp_netif esp_http_client esp_wifi)
set(requires esp-tls spi_flash nvs_flash esp_event esp_netif esp_http_client esp_wifi esp_psram)
file(GLOB SRCS "main.cpp" "*.cpp" "*.c" "**/*.cpp" "**/*.c")
# Path to the source JSON in this component
set(ASSETS_SRC_DIR ${CMAKE_CURRENT_LIST_DIR}/../assets)
set(ASSETS_BINARY_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/assets)
set(MTR_JSON_SRC ${ASSETS_SRC_DIR}/MTR_LINE_STATION.json)
set(MTR_JSON_HEADER ${ASSETS_BINARY_OUTPUT_DIR}/MTR_LINE_STATION.h)
set(CUSTOM_CMAKE_MODULES_DIR ${CMAKE_CURRENT_LIST_DIR}/cmake)
## Generate a minified header at configure time using Python
find_package(Python3 COMPONENTS Interpreter)
file(MAKE_DIRECTORY ${ASSETS_BINARY_OUTPUT_DIR})
if (Python3_Interpreter_FOUND)
execute_process(
COMMAND ${Python3_EXECUTABLE} -c "import json,sys,io; sys.stdout.write(json.dumps(json.load(open(sys.argv[1], 'r', encoding='utf-8')),separators=(',',':')))"
"${MTR_JSON_SRC}"
RESULT_VARIABLE _mtr_json_minify_result
OUTPUT_VARIABLE MTR_JSON_MINIFIED
ERROR_VARIABLE _mtr_json_minify_error
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if (_mtr_json_minify_result)
message(WARNING "Python minify failed (code=${_mtr_json_minify_result}): ${_mtr_json_minify_error}\nEmbedding original ${MTR_JSON_SRC} instead.")
file(READ ${MTR_JSON_SRC} MTR_JSON_MINIFIED)
elseif (NOT MTR_JSON_MINIFIED)
message(WARNING "Python minified output empty; embedding original ${MTR_JSON_SRC} instead.")
file(READ ${MTR_JSON_SRC} MTR_JSON_MINIFIED)
endif()
else()
message(WARNING "Python3 not found; embedding original JSON without minification.")
file(READ ${MTR_JSON_SRC} MTR_JSON_MINIFIED)
endif()
file(WRITE ${MTR_JSON_HEADER} "#pragma once\nstatic const char MTR_LINE_STATION_JSON[] = R\"json(${MTR_JSON_MINIFIED})json\";\n")
idf_component_register(SRCS ${SRCS}
PRIV_REQUIRES ${requires}
INCLUDE_DIRS "." "display" "touch" "network" "ui" "io" "common")
PRIV_REQUIRES ${requires}
INCLUDE_DIRS "." "${CMAKE_CURRENT_BINARY_DIR}" "display" "touch" "network" "ui" "io" "common" "external")

View File

@@ -0,0 +1,20 @@
if(NOT DEFINED INPUT)
message(FATAL_ERROR "write_json_header.cmake: INPUT not defined")
endif()
if(NOT DEFINED OUTPUT)
message(FATAL_ERROR "write_json_header.cmake: OUTPUT not defined")
endif()
find_package(Python3 COMPONENTS Interpreter REQUIRED)
execute_process(
COMMAND ${Python3_EXECUTABLE} -c "import json,sys;print(json.dumps(json.load(open(sys.argv[1])),separators=(', ',':')) )" ${INPUT}
OUTPUT_VARIABLE MINIFIED_JSON
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if(NOT MINIFIED_JSON)
message(FATAL_ERROR "write_json_header.cmake: failed to minify ${INPUT}")
endif()
file(WRITE ${OUTPUT} "#pragma once\nstatic const char MTR_LINE_STATION_JSON[] = R\"json(${MINIFIED_JSON})json\";\n")

View File

@@ -17,3 +17,4 @@ dependencies:
lvgl/lvgl: ^9.4.0
espressif/esp_lcd_touch_gt911: ^1.2.0~1
espressif/esp_lvgl_port: ^2.7.0
espressif/cjson: ^1.7.19