Skip to content

Commit

Permalink
Fix quoting
Browse files Browse the repository at this point in the history
  • Loading branch information
thejohnfreeman committed Nov 6, 2024
1 parent 1f77ca1 commit 0992a5e
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 5 deletions.
3 changes: 2 additions & 1 deletion cmake/cupcake_add_executables.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ function(cupcake_add_executables)
# | { target :: string, scope? :: PUBLIC | PRIVATE | INTERFACE }
string(JSON type TYPE "${link}")
if(type STREQUAL STRING)
set(target "${link}")
# A JSON string is already quoted.
set(target ${link})
set(scope "PRIVATE")
else()
string(JSON target GET "${link}" target)
Expand Down
3 changes: 2 additions & 1 deletion cmake/cupcake_add_libraries.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ function(cupcake_add_libraries)
# | { target :: string, scope? :: PUBLIC | PRIVATE | INTERFACE }
string(JSON type TYPE "${link}")
if(type STREQUAL STRING)
set(target "${link}")
# A JSON string is already quoted.
set(target ${link})
set(scope "PUBLIC")
else()
string(JSON target GET "${link}" target)
Expand Down
3 changes: 2 additions & 1 deletion cmake/cupcake_add_tests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ function(cupcake_add_tests)
# | { target :: string, scope? :: PUBLIC | PRIVATE | INTERFACE }
string(JSON type TYPE "${link}")
if(type STREQUAL STRING)
set(target "${link}")
# A JSON string is already quoted.
set(target ${link})
set(scope "PRIVATE")
else()
string(JSON target GET "${link}" target)
Expand Down
3 changes: 2 additions & 1 deletion cmake/cupcake_find_packages.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ function(cupcake_find_packages group)
# Select only imports whose `groups` (default: `["main"]`) contain `group`.
cupcake_json_get(groups ARRAY "[\"main\"]" "${import}" groups)
cupcake_json_to_list(groups "${groups}")
list(FIND groups "${group}" i)
# Group names are quoted JSON strings in the list.
list(FIND groups "\"${group}\"" i)
if(i LESS 0)
continue()
endif()
Expand Down
17 changes: 17 additions & 0 deletions cmake/cupcake_json.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ function(cupcake_json_to_list variable array)
math(EXPR stop "${count} - 1")
foreach(i RANGE ${stop})
string(JSON item GET "${array}" ${i})
string(JSON type TYPE "${array}" ${i})
if(type STREQUAL STRING)
# CMake automatically unquotes JSON strings. Re-quote them here.
set(item "\"${item}\"")
endif()
list(APPEND list "${item}")
endforeach()
endif()
Expand All @@ -37,3 +42,15 @@ function(cupcake_json_get_list variable json)
cupcake_json_to_list(list "${list}")
set(${variable} "${list}" PARENT_SCOPE)
endfunction()

# Unquote all strings in a list.
# unquote(variable [string...])
function(cupcake_unquote variable)
foreach(string ${ARGN})
if(string MATCHES "^\"(.*)\"$")
set(string "${CMAKE_MATCH_1}")
endif()
string(APPEND list "${string}")
endforeach()
set(${variable} "${list}" PARENT_SCOPE)
endfunction()
4 changes: 3 additions & 1 deletion cmake/cupcake_link_libraries.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ function(cupcake_link_libraries target scope group)
# Select only imports whose `groups` (default: `["main"]`) contain `group`.
cupcake_json_get(groups ARRAY "[\"main\"]" "${import}" groups)
cupcake_json_to_list(groups "${groups}")
list(FIND groups "${group}" i)
# Group names are quoted JSON strings in the list.
list(FIND groups "\"${group}\"" i)
if(i LESS 0)
continue()
endif()
string(JSON name GET "${import}" name)
cupcake_json_get(targets ARRAY "[\"${name}::${name}\"]" "${import}" targets)
cupcake_json_to_list(targets "${targets}")
cupcake_unquote(targets "${targets}")
target_link_libraries(${target} ${scope} ${targets})
endforeach()
endfunction()

0 comments on commit 0992a5e

Please sign in to comment.