Update PHP and Ruby amalgamations in regenerate_stale_files.sh

This removes the need for us to manually rebuild these amalgamation files. I
added the "manual" tag to the corresponding staleness tests so that we won't
get presubmit errors if the amalgamation files are out of date. I also had to
tweak the Ruby BUILD.bazel file to make sure it uses Bazel-generated
amalgamations instead of relying on the checked-in files.

I added deliberate errors to the Ruby and PHP amalgamation files to test that
everything is working. We should not see any CI errors, and the files should
quickly be auto-updated after the change lands on main.

PiperOrigin-RevId: 561187352
diff --git a/.github/workflows/test_php.yml b/.github/workflows/test_php.yml
index a698cde..43d9b2a 100644
--- a/.github/workflows/test_php.yml
+++ b/.github/workflows/test_php.yml
@@ -101,7 +101,6 @@
         uses: protocolbuffers/protobuf-ci/docker@v2
         with:
           image: ${{ env.image }}
-          skip-staleness-check: true
           platform: linux/386
           credentials: ${{ secrets.GAR_SERVICE_ACCOUNT }}
           extra-flags: -e COMPOSER_HOME=/workspace/composer-cache -e PROTOC=/workspace/${{ steps.cross-compile.outputs.protoc }}
@@ -140,7 +139,6 @@
         with:
           image: us-docker.pkg.dev/protobuf-build/containers/test/linux/php-aarch64@sha256:77ff9fdec867bbfb290ee0b10d8b7a3e5e434155daa5ec93de7341c7592b858d
           platform: linux/arm64
-          skip-staleness-check: true
           credentials: ${{ secrets.GAR_SERVICE_ACCOUNT }}
           extra-flags: -e COMPOSER_HOME=/workspace/composer-cache -e PROTOC=/workspace/${{ steps.cross-compile.outputs.protoc }}
           command: >-
@@ -191,6 +189,7 @@
           credentials: ${{ secrets.GAR_SERVICE_ACCOUNT }}
           bazel-cache: php_macos/${{ matrix.version }}
           bash: |
+            ./regenerate_stale_files.sh
             pushd php
             php -v
             php -m
diff --git a/.github/workflows/test_php_ext.yml b/.github/workflows/test_php_ext.yml
index d922859..6c9bfc7 100644
--- a/.github/workflows/test_php_ext.yml
+++ b/.github/workflows/test_php_ext.yml
@@ -27,6 +27,7 @@
           credentials: ${{ secrets.GAR_SERVICE_ACCOUNT }}
           bazel-cache: php_ext/${{ matrix.version }}
           bash: >
+            ./regenerate_stale_files.sh;
             bazel build //php:release $BAZEL_FLAGS;
             cp bazel-bin/php/protobuf-*.tgz .
 
diff --git a/.github/workflows/test_ruby.yml b/.github/workflows/test_ruby.yml
index 46f6d12..15a8e86 100644
--- a/.github/workflows/test_ruby.yml
+++ b/.github/workflows/test_ruby.yml
@@ -67,7 +67,6 @@
         uses: protocolbuffers/protobuf-ci/docker@v2
         with:
           image: i386/ruby:2.7.3-buster
-          skip-staleness-check: true
           credentials: ${{ secrets.GAR_SERVICE_ACCOUNT }}
           command: >-
             /bin/bash -cex '
@@ -99,7 +98,6 @@
         uses: protocolbuffers/protobuf-ci/docker@v2
         with:
           image: arm64v8/ruby:2.7.3-buster
-          skip-staleness-check: true
           credentials: ${{ secrets.GAR_SERVICE_ACCOUNT }}
           command: >-
             /bin/bash -cex '
@@ -181,6 +179,7 @@
           bash: >
             bazel --version;
             ruby --version;
+            ./regenerate_stale_files.sh;
             bazel build //ruby:release //:protoc ${{ matrix.ffi == 'FFI' && '--//ruby:ffi=enabled' || '' }} $BAZEL_FLAGS;
             gem install bazel-bin/ruby/google-protobuf-*;
             bazel-bin/protoc --proto_path=src --proto_path=ruby/tests --proto_path=ruby --ruby_out=ruby tests/test_import_proto2.proto;
diff --git a/php/BUILD.bazel b/php/BUILD.bazel
index 86b19e6..dc40603 100644
--- a/php/BUILD.bazel
+++ b/php/BUILD.bazel
@@ -104,6 +104,7 @@
         "ext/google/protobuf/php-upb.c",
     ],
     generated_pattern = "generated-in/%s",
+    tags = ["manual"],
 )
 
 ################################################################################
diff --git a/php/ext/google/protobuf/php-upb.c b/php/ext/google/protobuf/php-upb.c
index 640e5a4..67b9cfe 100644
--- a/php/ext/google/protobuf/php-upb.c
+++ b/php/ext/google/protobuf/php-upb.c
@@ -1,6 +1,8 @@
 /* Amalgamated source file */
 #include "php-upb.h"
 
+test-xyz
+
 /*
  * This is where we define internal portability macros used across upb.
  *
diff --git a/regenerate_stale_files.sh b/regenerate_stale_files.sh
index cf76f10..db0d13c 100755
--- a/regenerate_stale_files.sh
+++ b/regenerate_stale_files.sh
@@ -16,6 +16,8 @@
 ${BazelBin} test src:cmake_lists_staleness_test "$@" || ./bazel-bin/src/cmake_lists_staleness_test --fix
 ${BazelBin} test src/google/protobuf:well_known_types_staleness_test "$@" || ./bazel-bin/src/google/protobuf/well_known_types_staleness_test --fix
 ${BazelBin} test objectivec:well_known_types_staleness_test "$@" || ./bazel-bin/objectivec/well_known_types_staleness_test --fix
+${BazelBin} test php:test_amalgamation_staleness "$@" || ./bazel-bin/php/test_amalgamation_staleness --fix
+${BazelBin} test ruby/ext/google/protobuf_c:test_amalgamation_staleness "$@" || ./bazel-bin/ruby/ext/google/protobuf_c/test_amalgamation_staleness --fix
 (cd upb; ${BazelBin} test cmake:test_generated_files "$@" || ./bazel-bin/cmake/test_generated_files --fix)
 
 # Generate C# code.
diff --git a/ruby/ext/google/protobuf_c/BUILD.bazel b/ruby/ext/google/protobuf_c/BUILD.bazel
index 36f646f..b2a6310 100644
--- a/ruby/ext/google/protobuf_c/BUILD.bazel
+++ b/ruby/ext/google/protobuf_c/BUILD.bazel
@@ -4,28 +4,48 @@
 
 package(default_visibility = ["//ruby:__subpackages__"])
 
+PROTOBUF_C_SOURCES = [
+    "convert.c",
+    "convert.h",
+    "defs.c",
+    "defs.h",
+    "map.c",
+    "map.h",
+    "message.c",
+    "message.h",
+    "protobuf.c",
+    "protobuf.h",
+    "repeated_field.c",
+    "repeated_field.h",
+    "shared_convert.c",
+    "shared_convert.h",
+    "shared_message.c",
+    "shared_message.h",
+    "wrap_memcpy.c",
+]
+
+# We copy everything into a copy/ subdirectory so that we can use the
+# up-to-date Bazel-generated amalgamation files without conflicting with the
+# possibly stale checked-in amalgamations.
+genrule(
+    name = "copy_sources",
+    srcs = PROTOBUF_C_SOURCES + [
+        "glue.c",
+        "@upb//:gen_ruby_amalgamation",
+    ],
+    outs = ["copy/%s" % src for src in PROTOBUF_C_SOURCES] + [
+        "copy/glue.c",
+        "copy/ruby-upb.h",
+        "copy/ruby-upb.c",
+    ],
+    cmd = "cp $(SRCS) $(RULEDIR)/copy",
+)
+
 cc_library(
     name = "protobuf_c",
-    srcs = [
-        "convert.c",
-        "convert.h",
-        "defs.c",
-        "defs.h",
-        "map.c",
-        "map.h",
-        "message.c",
-        "message.h",
-        "protobuf.c",
-        "protobuf.h",
-        "repeated_field.c",
-        "repeated_field.h",
-        "ruby-upb.c",
-        "ruby-upb.h",
-        "shared_convert.c",
-        "shared_convert.h",
-        "shared_message.c",
-        "shared_message.h",
-        "wrap_memcpy.c",
+    srcs = ["copy/%s" % src for src in PROTOBUF_C_SOURCES] + [
+        "copy/ruby-upb.c",
+        "copy/ruby-upb.h",
     ],
     linkstatic = True,
     target_compatible_with = select({
@@ -44,10 +64,10 @@
 cc_library(
     name = "upb_api",
     srcs = [
-        "ruby-upb.c",
+        "copy/ruby-upb.c",
     ],
     hdrs = [
-        "ruby-upb.h",
+        "copy/ruby-upb.h",
     ],
     copts = ["-fvisibility=hidden"],
     linkstatic = False,
@@ -65,13 +85,13 @@
 
 cc_library(
     name = "protobuf_c_ffi",
-    srcs = [
+    srcs = ["copy/%s" % src for src in [
         "glue.c",
         "shared_convert.c",
         "shared_convert.h",
         "shared_message.c",
         "shared_message.h",
-    ],
+    ]],
     copts = [
         "-std=gnu99",
         "-O3",
@@ -154,4 +174,5 @@
         "ruby-upb.h",
     ],
     generated_pattern = "generated-in/%s",
+    tags = ["manual"],
 )
diff --git a/ruby/ext/google/protobuf_c/ruby-upb.c b/ruby/ext/google/protobuf_c/ruby-upb.c
index 23be9a1..7bf191a 100644
--- a/ruby/ext/google/protobuf_c/ruby-upb.c
+++ b/ruby/ext/google/protobuf_c/ruby-upb.c
@@ -1,6 +1,8 @@
 /* Amalgamated source file */
 #include "ruby-upb.h"
 
+test-xyz
+
 /*
  * This is where we define internal portability macros used across upb.
  *