From 4748c0130dca2e14cc393aef1c0c8a4ae82e1086 Mon Sep 17 00:00:00 2001 From: Junhao Liao Date: Fri, 27 Dec 2024 00:19:25 -0500 Subject: [PATCH 1/6] chore(build): Extract CMake project name and version from package.json. --- CMakeLists.txt | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4d0f1eda..ac7b777a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,12 +9,35 @@ Emscripten.cmake" ) endif() +# Extract the project name & version from package.json +file(READ "${CMAKE_CURRENT_SOURCE_DIR}/package.json" PACKAGE_JSON_CONTENT) +if(${PACKAGE_JSON_CONTENT} MATCHES "\"name\":[ ]*\"([^\"]+)\"") + set(CLP_FFI_JS_PROJECT_NAME + ${CMAKE_MATCH_1} + CACHE STRING + "The name of the project parsed from `package.json`." + FORCE + ) +else() + set(CLP_FFI_JS_PROJECT_NAME "clp-ffi-js" CACHE STRING "Use a placeholder project name." FORCE) +endif() +if(${PACKAGE_JSON_CONTENT} MATCHES "\"version\":[ ]*\"([^\"]+)\"") + set(CLP_FFI_JS_VERSION + ${CMAKE_MATCH_1} + CACHE STRING + "The version of the project parsed from `package.json`." + FORCE + ) +else() + set(CLP_FFI_JS_VERSION "0.0.0" CACHE STRING "Use a placeholder version." FORCE) +endif() + project( - clp-ffi-js + ${CLP_FFI_JS_PROJECT_NAME} LANGUAGES C CXX - VERSION 0.3.1 + VERSION "${CLP_FFI_JS_VERSION}" ) # Enable exporting compile commands From eae6c3ee6ac6181072dc4b01f65332872d135804 Mon Sep 17 00:00:00 2001 From: Junhao Liao Date: Fri, 27 Dec 2024 00:34:12 -0500 Subject: [PATCH 2/6] Check for package.json existence before reading. --- CMakeLists.txt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ac7b777a..1376de6b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,7 +10,11 @@ Emscripten.cmake" endif() # Extract the project name & version from package.json -file(READ "${CMAKE_CURRENT_SOURCE_DIR}/package.json" PACKAGE_JSON_CONTENT) +if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/package.json") + file(READ "${CMAKE_CURRENT_SOURCE_DIR}/package.json" PACKAGE_JSON_CONTENT) +else() + message(FATAL_ERROR "`package.json` not found in ${CMAKE_CURRENT_SOURCE_DIR}") +endif() if(${PACKAGE_JSON_CONTENT} MATCHES "\"name\":[ ]*\"([^\"]+)\"") set(CLP_FFI_JS_PROJECT_NAME ${CMAKE_MATCH_1} From 3e768d6232d7bd0d8b366d4e1a5e469dbf2cca73 Mon Sep 17 00:00:00 2001 From: Junhao Liao Date: Fri, 27 Dec 2024 00:40:59 -0500 Subject: [PATCH 3/6] Quote `${CLP_FFI_JS_PROJECT_NAME}`. --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1376de6b..d3b07a3f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -37,7 +37,7 @@ else() endif() project( - ${CLP_FFI_JS_PROJECT_NAME} + "${CLP_FFI_JS_PROJECT_NAME}" LANGUAGES C CXX From 61498362d5af5115e2bc32c1226e430d14e0ae2d Mon Sep 17 00:00:00 2001 From: Junhao Liao Date: Sat, 28 Dec 2024 23:06:33 -0500 Subject: [PATCH 4/6] Wrap variables with quotes where appropriate - Apply suggestions from code review Co-authored-by: kirkrodrigues <2454684+kirkrodrigues@users.noreply.github.com> --- CMakeLists.txt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d3b07a3f..fc9481f4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,9 +15,9 @@ if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/package.json") else() message(FATAL_ERROR "`package.json` not found in ${CMAKE_CURRENT_SOURCE_DIR}") endif() -if(${PACKAGE_JSON_CONTENT} MATCHES "\"name\":[ ]*\"([^\"]+)\"") +if("${PACKAGE_JSON_CONTENT}" MATCHES "\"name\":[ ]*\"([^\"]+)\"") set(CLP_FFI_JS_PROJECT_NAME - ${CMAKE_MATCH_1} + "${CMAKE_MATCH_1}" CACHE STRING "The name of the project parsed from `package.json`." FORCE @@ -25,9 +25,9 @@ if(${PACKAGE_JSON_CONTENT} MATCHES "\"name\":[ ]*\"([^\"]+)\"") else() set(CLP_FFI_JS_PROJECT_NAME "clp-ffi-js" CACHE STRING "Use a placeholder project name." FORCE) endif() -if(${PACKAGE_JSON_CONTENT} MATCHES "\"version\":[ ]*\"([^\"]+)\"") +if("${PACKAGE_JSON_CONTENT}" MATCHES "\"version\":[ ]*\"([^\"]+)\"") set(CLP_FFI_JS_VERSION - ${CMAKE_MATCH_1} + "${CMAKE_MATCH_1}" CACHE STRING "The version of the project parsed from `package.json`." FORCE From 897eb50328f5ba698e980db186a54fb507f91244 Mon Sep 17 00:00:00 2001 From: Junhao Liao Date: Sat, 28 Dec 2024 23:07:36 -0500 Subject: [PATCH 5/6] Rename variable `PACKAGE_JSON_CONTENT` to `CLP_FFI_JS_PACKAGE_JSON_CONTENT`. --- CMakeLists.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index fc9481f4..3ce614c0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,11 +11,11 @@ endif() # Extract the project name & version from package.json if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/package.json") - file(READ "${CMAKE_CURRENT_SOURCE_DIR}/package.json" PACKAGE_JSON_CONTENT) + file(READ "${CMAKE_CURRENT_SOURCE_DIR}/package.json" CLP_FFI_JS_PACKAGE_JSON_CONTENT) else() message(FATAL_ERROR "`package.json` not found in ${CMAKE_CURRENT_SOURCE_DIR}") endif() -if("${PACKAGE_JSON_CONTENT}" MATCHES "\"name\":[ ]*\"([^\"]+)\"") +if("${CLP_FFI_JS_PACKAGE_JSON_CONTENT}" MATCHES "\"name\":[ ]*\"([^\"]+)\"") set(CLP_FFI_JS_PROJECT_NAME "${CMAKE_MATCH_1}" CACHE STRING @@ -25,7 +25,7 @@ if("${PACKAGE_JSON_CONTENT}" MATCHES "\"name\":[ ]*\"([^\"]+)\"") else() set(CLP_FFI_JS_PROJECT_NAME "clp-ffi-js" CACHE STRING "Use a placeholder project name." FORCE) endif() -if("${PACKAGE_JSON_CONTENT}" MATCHES "\"version\":[ ]*\"([^\"]+)\"") +if("${CLP_FFI_JS_PACKAGE_JSON_CONTENT}" MATCHES "\"version\":[ ]*\"([^\"]+)\"") set(CLP_FFI_JS_VERSION "${CMAKE_MATCH_1}" CACHE STRING From 319d74c55a427f3ec744f2dded5a729c0577dfab Mon Sep 17 00:00:00 2001 From: Junhao Liao Date: Sat, 28 Dec 2024 23:08:31 -0500 Subject: [PATCH 6/6] Remove unnecessary usage of CACHE in setting project name and version variables. --- CMakeLists.txt | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3ce614c0..19685032 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,24 +16,14 @@ else() message(FATAL_ERROR "`package.json` not found in ${CMAKE_CURRENT_SOURCE_DIR}") endif() if("${CLP_FFI_JS_PACKAGE_JSON_CONTENT}" MATCHES "\"name\":[ ]*\"([^\"]+)\"") - set(CLP_FFI_JS_PROJECT_NAME - "${CMAKE_MATCH_1}" - CACHE STRING - "The name of the project parsed from `package.json`." - FORCE - ) + set(CLP_FFI_JS_PROJECT_NAME "${CMAKE_MATCH_1}") else() - set(CLP_FFI_JS_PROJECT_NAME "clp-ffi-js" CACHE STRING "Use a placeholder project name." FORCE) + set(CLP_FFI_JS_PROJECT_NAME "clp-ffi-js") endif() if("${CLP_FFI_JS_PACKAGE_JSON_CONTENT}" MATCHES "\"version\":[ ]*\"([^\"]+)\"") - set(CLP_FFI_JS_VERSION - "${CMAKE_MATCH_1}" - CACHE STRING - "The version of the project parsed from `package.json`." - FORCE - ) + set(CLP_FFI_JS_VERSION "${CMAKE_MATCH_1}") else() - set(CLP_FFI_JS_VERSION "0.0.0" CACHE STRING "Use a placeholder version." FORCE) + set(CLP_FFI_JS_VERSION "0.0.0") endif() project(