[engine] Add ctx.platform.arch

... and rename `ctx.os.name` to `ctx.platform.os` so the OS and arch
values are grouped together. There are no downstream users of
`ctx.os.name` yet, so it's safe to rename.

Change-Id: If6c47eaed0301203ba8d90759ef10921622db8e9
Reviewed-on: https://fuchsia-review.googlesource.com/c/shac-project/shac/+/897161
Commit-Queue: Auto-Submit <auto-submit@fuchsia-infra.iam.gserviceaccount.com>
Fuchsia-Auto-Submit: Oliver Newman <olivernewman@google.com>
Reviewed-by: Marc-Antoine Ruel <maruel@google.com>
diff --git a/doc/stdlib.md b/doc/stdlib.md
index 62d79b5..3998b6b 100644
--- a/doc/stdlib.md
+++ b/doc/stdlib.md
@@ -37,6 +37,7 @@
 - emit
 - io
 - os
+- platform
 - re
 - scm
 
@@ -196,13 +197,6 @@
 Fields:
 
 - exec
-- name
-
-## ctx.os.name
-
-ctx.os.name contains the OS as described by GOOS. Frequent values are
-"windows", "linux" and "darwin". The full exact list can be retrieved with
-the command "go tool dist list | cut -f 1 -d / | uniq"
 
 ## ctx.os.exec
 
@@ -257,6 +251,28 @@
 A subprocess object with a wait() method. wait() returns a
 struct(retcode=..., stdout="...", stderr="...")
 
+## ctx.platform
+
+ctx.platform exposes data about the underlying platform.
+
+Fields:
+
+- os
+- arch
+
+## ctx.platform.os
+
+ctx.platform.os contains the OS as described by GOOS. Frequent values are
+"windows", "linux" and "darwin". The full exact list can be retrieved with
+the command "go tool dist list | cut -f 1 -d / | uniq"
+
+## ctx.platform.arch
+
+ctx.platform.arch contains the CPU architecture as described by GOARCH.
+Frequent values are "amd64" and "arm64". The full exact list can be
+retrieved with the command "go tool dist list | cut -f 2 -d / | sort |
+uniq"
+
 ## ctx.re
 
 ctx.re is the object that exposes the API to run regular expressions on
diff --git a/doc/stdlib.star b/doc/stdlib.star
index 1dc6ca7..60bec72 100644
--- a/doc/stdlib.star
+++ b/doc/stdlib.star
@@ -379,10 +379,18 @@
   # system.
   os = struct(
     exec = _ctx_os_exec,
-    # ctx.os.name contains the OS as described by GOOS. Frequent values are
+  ),
+  # ctx.platform exposes data about the underlying platform.
+  platform = struct(
+    # ctx.platform.os contains the OS as described by GOOS. Frequent values are
     # "windows", "linux" and "darwin". The full exact list can be retrieved with
     # the command "go tool dist list | cut -f 1 -d / | uniq"
-    name = "",
+    os = "",
+    # ctx.platform.arch contains the CPU architecture as described by GOARCH.
+    # Frequent values are "amd64" and "arm64". The full exact list can be
+    # retrieved with the command "go tool dist list | cut -f 2 -d / | sort |
+    # uniq"
+    arch = "",
   ),
   # ctx.re is the object that exposes the API to run regular expressions on
   # starlark strings.
diff --git a/internal/engine/run_test.go b/internal/engine/run_test.go
index c386c3b..8a21456 100644
--- a/internal/engine/run_test.go
+++ b/internal/engine/run_test.go
@@ -1613,6 +1613,11 @@
 				"[//ctx-os-exec-success.star:23] stderr: hello from stderr\n",
 		},
 		{
+			"ctx-platform.star",
+			"[//ctx-platform.star:16] OS: " + runtime.GOOS + "\n" +
+				"[//ctx-platform.star:17] Arch: " + runtime.GOARCH + "\n",
+		},
+		{
 			"ctx-re-allmatches.star",
 			"[//ctx-re-allmatches.star:17] ()\n" +
 				"[//ctx-re-allmatches.star:19] (match(groups = (\"TODO(foo)\",), offset = 4), match(groups = (\"TODO(bar)\",), offset = 14))\n" +
@@ -1627,7 +1632,7 @@
 		},
 		{
 			"dir-ctx.star",
-			"[//dir-ctx.star:16] [\"emit\", \"io\", \"os\", \"re\", \"scm\"]\n",
+			"[//dir-ctx.star:16] [\"emit\", \"io\", \"os\", \"platform\", \"re\", \"scm\"]\n",
 		},
 		{
 			"dir-shac.star",
diff --git a/internal/engine/runtime_ctx.go b/internal/engine/runtime_ctx.go
index 92b5533..942b431 100644
--- a/internal/engine/runtime_ctx.go
+++ b/internal/engine/runtime_ctx.go
@@ -36,7 +36,10 @@
 		}),
 		"os": toValue("ctx.os", starlark.StringDict{
 			"exec": newBuiltin("ctx.os.exec", ctxOsExec),
-			"name": starlark.String(runtime.GOOS),
+		}),
+		"platform": toValue("ctx.platform", starlark.StringDict{
+			"os":   starlark.String(runtime.GOOS),
+			"arch": starlark.String(runtime.GOARCH),
 		}),
 		// Implemented in runtime_ctx_re.go
 		"re": toValue("ctx.re", starlark.StringDict{
diff --git a/internal/engine/testdata/bench/ctx-os-exec.star b/internal/engine/testdata/bench/ctx-os-exec.star
index 344d17e..460fc33 100644
--- a/internal/engine/testdata/bench/ctx-os-exec.star
+++ b/internal/engine/testdata/bench/ctx-os-exec.star
@@ -13,7 +13,7 @@
 # limitations under the License.
 
 def cb(ctx):
-  if ctx.os.name == "windows":
+  if ctx.platform.os == "windows":
     cmd = ["cmd.exe", "/c", "stdio.bat"]
   else:
     cmd = ["./stdio.sh"]
diff --git a/internal/engine/testdata/bench/ctx-os-exec100.star b/internal/engine/testdata/bench/ctx-os-exec100.star
index 3cb872c..6c0a0c0 100644
--- a/internal/engine/testdata/bench/ctx-os-exec100.star
+++ b/internal/engine/testdata/bench/ctx-os-exec100.star
@@ -13,7 +13,7 @@
 # limitations under the License.
 
 def cb(ctx):
-  if ctx.os.name == "windows":
+  if ctx.platform.os == "windows":
     cmd = ["cmd.exe", "/c", "stdio.bat"]
   else:
     cmd = ["./stdio.sh"]
diff --git a/internal/engine/testdata/fail_or_throw/ctx-os-exec-ok_retcodes.star b/internal/engine/testdata/fail_or_throw/ctx-os-exec-ok_retcodes.star
index 7fbca9b..003231d 100644
--- a/internal/engine/testdata/fail_or_throw/ctx-os-exec-ok_retcodes.star
+++ b/internal/engine/testdata/fail_or_throw/ctx-os-exec-ok_retcodes.star
@@ -13,7 +13,7 @@
 # limitations under the License.
 
 def cb(ctx):
-  if ctx.os.name == "windows":
+  if ctx.platform.os == "windows":
     cmd = ["cmd.exe", "/c", "exit", "1"]
   else:
     cmd = ["false"]
@@ -21,7 +21,7 @@
   if res.retcode != 1:
     fail("unexpected retcode %d" % res.retcode)
 
-  if ctx.os.name == "windows":
+  if ctx.platform.os == "windows":
     cmd = ["cmd.exe", "/c", "exit", "0"]
   else:
     cmd = ["true"]
diff --git a/internal/engine/testdata/print/ctx-os-exec-abspath.star b/internal/engine/testdata/print/ctx-os-exec-abspath.star
index 3eaf672..0aaa811 100644
--- a/internal/engine/testdata/print/ctx-os-exec-abspath.star
+++ b/internal/engine/testdata/print/ctx-os-exec-abspath.star
@@ -13,7 +13,7 @@
 # limitations under the License.
 
 def cb(ctx):
-  ext = "bat" if ctx.os.name == "windows" else "sh"
+  ext = "bat" if ctx.platform.os == "windows" else "sh"
   print(ctx.os.exec([ctx.scm.root+"/hello_world." + ext]).wait().stdout)
 
 shac.register_check(cb)
diff --git a/internal/engine/testdata/print/ctx-os-exec-env.star b/internal/engine/testdata/print/ctx-os-exec-env.star
index 863021a..a15f3ea 100644
--- a/internal/engine/testdata/print/ctx-os-exec-env.star
+++ b/internal/engine/testdata/print/ctx-os-exec-env.star
@@ -13,7 +13,7 @@
 # limitations under the License.
 
 def cb(ctx):
-  if ctx.os.name == "windows":
+  if ctx.platform.os == "windows":
     cmd = ["cmd.exe", "/c", "env.bat"]
   else:
     cmd = ["./env.sh"]
diff --git a/internal/engine/testdata/print/ctx-os-exec-parallel.star b/internal/engine/testdata/print/ctx-os-exec-parallel.star
index 92d2ccf..6fedc0e 100644
--- a/internal/engine/testdata/print/ctx-os-exec-parallel.star
+++ b/internal/engine/testdata/print/ctx-os-exec-parallel.star
@@ -13,7 +13,7 @@
 # limitations under the License.
 
 def cb(ctx):
-  if ctx.os.name == "windows":
+  if ctx.platform.os == "windows":
     cmd = ["cmd.exe", "/c", "hello_world.bat"]
   else:
     cmd = ["./hello_world.sh"]
diff --git a/internal/engine/testdata/print/ctx-os-exec-success.star b/internal/engine/testdata/print/ctx-os-exec-success.star
index 344d17e..460fc33 100644
--- a/internal/engine/testdata/print/ctx-os-exec-success.star
+++ b/internal/engine/testdata/print/ctx-os-exec-success.star
@@ -13,7 +13,7 @@
 # limitations under the License.
 
 def cb(ctx):
-  if ctx.os.name == "windows":
+  if ctx.platform.os == "windows":
     cmd = ["cmd.exe", "/c", "stdio.bat"]
   else:
     cmd = ["./stdio.sh"]
diff --git a/internal/engine/testdata/print/ctx-platform.star b/internal/engine/testdata/print/ctx-platform.star
new file mode 100644
index 0000000..997090c
--- /dev/null
+++ b/internal/engine/testdata/print/ctx-platform.star
@@ -0,0 +1,19 @@
+# Copyright 2023 The Shac Authors
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+def cb(ctx):
+  print("OS: %s" % ctx.platform.os)
+  print("Arch: %s" % ctx.platform.arch)
+
+shac.register_check(cb)