From 9e4820a8e9c8ddd5fdc50a617b54d198dd77b451 Mon Sep 17 00:00:00 2001
From: Virgile Prevosto <virgile.prevosto@m4x.org>
Date: Tue, 21 Feb 2023 18:01:19 +0100
Subject: [PATCH] [machdeps] update schema: arch flags are a list, not a single
 string

Also allows script to update an existing machdep, by reusing its arch flags
---
 share/machdeps/machdep-schema.json          |  3 ++-
 share/machdeps/machdep_avr_16.json          |  6 ++++-
 share/machdeps/machdep_gcc_x86_16.json      |  2 +-
 share/machdeps/machdep_gcc_x86_32.json      |  4 ++-
 share/machdeps/machdep_gcc_x86_64.json      |  6 +++--
 share/machdeps/machdep_ppc_32.json          |  6 ++++-
 share/machdeps/machdep_x86_16.json          |  2 +-
 share/machdeps/machdep_x86_32.json          |  4 ++-
 share/machdeps/make_machdep/make_machdep.py | 28 +++++++++++++++++++--
 9 files changed, 50 insertions(+), 11 deletions(-)

diff --git a/share/machdeps/machdep-schema.json b/share/machdeps/machdep-schema.json
index ccb08477a96..378e1dfa111 100644
--- a/share/machdeps/machdep-schema.json
+++ b/share/machdeps/machdep-schema.json
@@ -13,7 +13,8 @@
         },
         "cpp_arch_flags": {
             "description": "arguments to be given to the compiler when this machdep is selected (e.g. '-m32')",
-            "type" : "string"
+            "type" : "array",
+            "items" : { "type": "string" }
         },
         "sizeof_short": {
             "description": "size of 'short' type",
diff --git a/share/machdeps/machdep_avr_16.json b/share/machdeps/machdep_avr_16.json
index 327f9bc286f..785839a0b5a 100644
--- a/share/machdeps/machdep_avr_16.json
+++ b/share/machdeps/machdep_avr_16.json
@@ -12,7 +12,11 @@
     "alignof_str": 1,
     "char_is_unsigned": false,
     "compiler": "clang",
-    "cpp_arch_flags": "-target avr -m16",
+    "cpp_arch_flags": [
+        "-target",
+        "avr",
+        "-m16"
+    ],
     "has__builtin_va_list": true,
     "little_endian": true,
     "ptrdiff_t": "int",
diff --git a/share/machdeps/machdep_gcc_x86_16.json b/share/machdeps/machdep_gcc_x86_16.json
index d6dc63d3c14..bc6a35bddbb 100644
--- a/share/machdeps/machdep_gcc_x86_16.json
+++ b/share/machdeps/machdep_gcc_x86_16.json
@@ -12,7 +12,7 @@
     "alignof_str": 1,
     "char_is_unsigned": false,
     "compiler": "gcc",
-    "cpp_arch_flags": "-m16",
+    "cpp_arch_flags": ["-m16"],
     "has__builtin_va_list": true,
     "little_endian": true,
     "ptrdiff_t": "int",
diff --git a/share/machdeps/machdep_gcc_x86_32.json b/share/machdeps/machdep_gcc_x86_32.json
index ac118c16125..f7030722574 100644
--- a/share/machdeps/machdep_gcc_x86_32.json
+++ b/share/machdeps/machdep_gcc_x86_32.json
@@ -12,7 +12,9 @@
     "alignof_str": 1,
     "char_is_unsigned": false,
     "compiler": "gcc",
-    "cpp_arch_flags": "-m32",
+    "cpp_arch_flags": [
+        "-m32"
+    ],
     "has__builtin_va_list": true,
     "little_endian": true,
     "ptrdiff_t": "int",
diff --git a/share/machdeps/machdep_gcc_x86_64.json b/share/machdeps/machdep_gcc_x86_64.json
index 40a9f3a209e..28932cf951b 100644
--- a/share/machdeps/machdep_gcc_x86_64.json
+++ b/share/machdeps/machdep_gcc_x86_64.json
@@ -12,8 +12,10 @@
     "alignof_str": 1,
     "char_is_unsigned": false,
     "compiler": "gcc",
-    "cpp_arch_flags": "-m64",
-    "has__builtin_va_list": false,
+    "cpp_arch_flags": [
+        "-m64"
+    ],
+    "has__builtin_va_list": true,
     "little_endian": true,
     "ptrdiff_t": "long",
     "size_t": "unsigned long",
diff --git a/share/machdeps/machdep_ppc_32.json b/share/machdeps/machdep_ppc_32.json
index de712c039d3..1a47569605b 100644
--- a/share/machdeps/machdep_ppc_32.json
+++ b/share/machdeps/machdep_ppc_32.json
@@ -12,7 +12,11 @@
     "alignof_str": 1,
     "char_is_unsigned": false,
     "compiler": "clang",
-    "cpp_arch_flags": "-target powerpc-apple-darwin -mcpu=603",
+    "cpp_arch_flags": [
+        "-target",
+        "powerpc-apple-darwin",
+        "-mcpu=603"
+    ],
     "has__builtin_va_list": false,
     "little_endian": false,
     "ptrdiff_t": "int",
diff --git a/share/machdeps/machdep_x86_16.json b/share/machdeps/machdep_x86_16.json
index 100978d6a56..59eff226c3e 100644
--- a/share/machdeps/machdep_x86_16.json
+++ b/share/machdeps/machdep_x86_16.json
@@ -12,7 +12,7 @@
     "alignof_str": 1,
     "char_is_unsigned": false,
     "compiler": "generic",
-    "cpp_arch_flags": "-m16",
+    "cpp_arch_flags": [ "-m16" ],
     "has__builtin_va_list": true,
     "little_endian": true,
     "ptrdiff_t": "int",
diff --git a/share/machdeps/machdep_x86_32.json b/share/machdeps/machdep_x86_32.json
index f0165c1d03e..44d776d1d3f 100644
--- a/share/machdeps/machdep_x86_32.json
+++ b/share/machdeps/machdep_x86_32.json
@@ -12,7 +12,9 @@
     "alignof_str": 1,
     "char_is_unsigned": false,
     "compiler": "generic",
-    "cpp_arch_flags": "-m32",
+    "cpp_arch_flags": [
+        "-m32"
+    ],
     "has__builtin_va_list": true,
     "little_endian": true,
     "ptrdiff_t": "int",
diff --git a/share/machdeps/make_machdep/make_machdep.py b/share/machdeps/make_machdep/make_machdep.py
index f3e922f099c..810acc91eb8 100755
--- a/share/machdeps/make_machdep/make_machdep.py
+++ b/share/machdeps/make_machdep/make_machdep.py
@@ -69,6 +69,17 @@ parser.add_argument(
     default="--version",
     help="option to pass to the compiler to obtain its version; default is --version",
 )
+
+parser.add_argument(
+    "--from-file",
+    help="reads compiler and arch flags from existing json file. Use -i to update it in place"
+)
+parser.add_argument(
+    "-i", "--in-place",
+    action="store_true",
+    help="when reading compiler config from json, update the file in place. unused otherwise"
+)
+
 parser.add_argument(
     "--cpp-arch-flags",
     nargs="+",
@@ -90,10 +101,23 @@ if not args.compiler_flags:
     args.compiler_flags = ["-c"]
 
 
+if args.from_file:
+    orig_file = open(args.from_file,"r")
+    orig_machdep = json.load(orig_file)
+    orig_file.close()
+    if not "compiler" in orig_machdep or not "cpp_arch_flags" in orig_machdep:
+        raise Exception("Missing fields in json file")
+    args.compiler = orig_machdep["compiler"]
+    if isinstance(orig_machdep["cpp_arch_flags"],list):
+        args.cpp_arch_flags = orig_machdep["cpp_arch_flags"]
+    else: # old version of the schema used a single string
+        args.cpp_arch_flags = orig_machdep["cpp_arch_flags"].split()
+
 def print_machdep(machdep):
+    if args.in_place:
+        args.dest_file = open(args.from_file,"w")
     json.dump(machdep, args.dest_file, indent=4, sort_keys=True)
 
-
 def check_machdep(machdep):
     try:
         from jsonschema import validate, ValidationError
@@ -238,7 +262,7 @@ version_output = subprocess.run(
 version = version_output.stdout.splitlines()[0]
 
 machdep["compiler"] = args.compiler
-machdep["cpp_arch_flags"] = " ".join(args.cpp_arch_flags)
+machdep["cpp_arch_flags"] = args.cpp_arch_flags
 machdep["version"] = version
 
 missing_fields = [f for [f, v] in machdep.items() if v is None]
-- 
GitLab