WebUI.cmake 1.0 KB

12345678910111213141516171819202122232425262728
  1. # Builds the React web UI via npm at CMake build time, into ${CMAKE_BINARY_DIR}/webui/dist.
  2. find_program(NPM_EXECUTABLE npm)
  3. if(NOT NPM_EXECUTABLE)
  4. message(WARNING "npm not found — skipping web UI build (set BUILD_WEBUI=OFF to silence)")
  5. return()
  6. endif()
  7. set(WEBUI_SRC "${CMAKE_SOURCE_DIR}/webui")
  8. set(WEBUI_DIST "${CMAKE_BINARY_DIR}/webui/dist")
  9. # Install dependencies once (when node_modules is absent).
  10. add_custom_command(
  11. OUTPUT "${WEBUI_SRC}/node_modules/.package-lock.json"
  12. COMMAND ${NPM_EXECUTABLE} ci --no-audit --no-fund
  13. WORKING_DIRECTORY "${WEBUI_SRC}"
  14. COMMENT "webui: npm ci"
  15. VERBATIM)
  16. # Build the UI into the CMake build tree (outDir override).
  17. add_custom_command(
  18. OUTPUT "${WEBUI_DIST}/index.html"
  19. COMMAND ${NPM_EXECUTABLE} run build -- --outDir "${WEBUI_DIST}" --emptyOutDir
  20. WORKING_DIRECTORY "${WEBUI_SRC}"
  21. DEPENDS "${WEBUI_SRC}/node_modules/.package-lock.json"
  22. COMMENT "webui: npm run build -> ${WEBUI_DIST}"
  23. VERBATIM)
  24. add_custom_target(webui ALL DEPENDS "${WEBUI_DIST}/index.html")