-
Notifications
You must be signed in to change notification settings - Fork 3
/
dict.cmake
44 lines (37 loc) · 1.31 KB
/
dict.cmake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
function(dict command dict )
if(command STREQUAL SET)
set(arg_key ${ARGV2})
set(arg_value ${ARGV3})
dict(_IDX ${dict} "${arg_key}" idx)
if(NOT idx STREQUAL -1)
list(REMOVE_AT ${dict} ${idx})
endif()
list(APPEND ${dict} "${arg_key}=${arg_value}")
set(${dict} "${${dict}}" PARENT_SCOPE)
elseif(command STREQUAL GET)
set(arg_key ${ARGV2})
set(arg_outvar ${ARGV3})
dict(_IDX ${dict} "${arg_key}" idx)
if(idx STREQUAL -1)
message(FATAL_ERROR "No key \"${arg_key}\" in dictionary")
endif()
list(GET ${dict} ${idx} kv)
string(REGEX REPLACE "^[^=]+=(.*)" "\\1" value "${kv}")
set(${arg_outvar} "${value}" PARENT_SCOPE)
elseif(command STREQUAL _IDX)
set(arg_key ${ARGV2})
set(arg_outvar ${ARGV3})
set(idx 0)
foreach(kv IN LISTS ${dict})
string(REGEX REPLACE "^([^=]+)=.*" "\\1" key "${kv}")
if(arg_key STREQUAL key)
set(${arg_outvar} "${idx}" PARENT_SCOPE)
return()
endif()
math(EXPR idx ${idx}+1)
endforeach()
set(${arg_outvar} "-1" PARENT_SCOPE)
else()
message(FATAL_ERROR "dict does not recognize sub-command ${command}")
endif()
endfunction()