Colin Cross | ee8b102 | 2018-11-10 21:48:59 -0800 | [diff] [blame] | 1 | 4space :=$= $(space)$(space)$(space)$(space) |
| 2 | invert_bool =$= $(if $(strip $(1)),,true) |
| 3 | |
| 4 | # Converts a list to a JSON list. |
| 5 | # $1: List separator. |
| 6 | # $2: List. |
| 7 | _json_list =$= [$(if $(2),"$(subst $(1),"$(comma)",$(2))")] |
| 8 | |
| 9 | # Converts a space-separated list to a JSON list. |
| 10 | json_list =$= $(call _json_list,$(space),$(1)) |
| 11 | |
| 12 | # Converts a comma-separated list to a JSON list. |
| 13 | csv_to_json_list =$= $(call _json_list,$(comma),$(1)) |
| 14 | |
| 15 | # Adds or removes 4 spaces from _json_indent |
| 16 | json_increase_indent =$= $(eval _json_indent := $$(_json_indent)$$(4space)) |
| 17 | json_decrease_indent =$= $(eval _json_indent := $$(subst _,$$(space),$$(patsubst %____,%,$$(subst $$(space),_,$$(_json_indent))))) |
| 18 | |
| 19 | # 1: Key name |
| 20 | # 2: Value |
| 21 | add_json_val =$= $(eval _json_contents := $$(_json_contents)$$(_json_indent)"$$(strip $$(1))": $$(strip $$(2))$$(comma)$$(newline)) |
| 22 | add_json_str =$= $(call add_json_val,$(1),"$(strip $(2))") |
| 23 | add_json_list =$= $(call add_json_val,$(1),$(call json_list,$(patsubst %,%,$(2)))) |
| 24 | add_json_csv =$= $(call add_json_val,$(1),$(call csv_to_json_list,$(strip $(2)))) |
| 25 | add_json_bool =$= $(call add_json_val,$(1),$(if $(strip $(2)),true,false)) |
| 26 | add_json_map =$= $(eval _json_contents := $$(_json_contents)$$(_json_indent)"$$(strip $$(1))": {$$(newline))$(json_increase_indent) |
Ulya Trafimovich | 8edad8f | 2021-02-11 16:58:25 +0000 | [diff] [blame] | 27 | add_json_map_anon =$= $(eval _json_contents := $$(_json_contents)$$(_json_indent){$$(newline))$(json_increase_indent) |
Colin Cross | ee8b102 | 2018-11-10 21:48:59 -0800 | [diff] [blame] | 28 | end_json_map =$= $(json_decrease_indent)$(eval _json_contents := $$(_json_contents)$$(if $$(filter %$$(comma),$$(lastword $$(_json_contents))),__SV_END)$$(_json_indent)},$$(newline)) |
Ulya Trafimovich | 8edad8f | 2021-02-11 16:58:25 +0000 | [diff] [blame] | 29 | add_json_array =$= $(eval _json_contents := $$(_json_contents)$$(_json_indent)"$$(strip $$(1))": [$$(newline))$(json_increase_indent) |
| 30 | end_json_array =$= $(json_decrease_indent)$(eval _json_contents := $$(_json_contents)$$(if $$(filter %$$(comma),$$(lastword $$(_json_contents))),__SV_END)$$(_json_indent)],$$(newline)) |
Colin Cross | ee8b102 | 2018-11-10 21:48:59 -0800 | [diff] [blame] | 31 | |
| 32 | # Clears _json_contents to start a new json file |
| 33 | json_start =$= $(eval _json_contents := {$$(newline))$(eval _json_indent := $$(4space)) |
| 34 | |
| 35 | # Adds the trailing close brace to _json_contents, and removes any trailing commas if necessary |
| 36 | json_end =$= $(eval _json_contents := $$(subst $$(comma)$$(newline)__SV_END,$$(newline),$$(_json_contents)__SV_END}$$(newline))) |
| 37 | |
| 38 | json_contents =$= $(_json_contents) |