From 8a07f45293191b084e535766e790621784743fe8 Mon Sep 17 00:00:00 2001
From: Mike Kruskal <mkruskal@google.com>
Date: Mon, 6 Mar 2023 10:20:14 -0800
Subject: [PATCH] Add release tests for statically linked binaries

For now, this only covers linux on the two architectures we have testing support for.  However, it serves as a good sanity check and can be expanded in the future.

PiperOrigin-RevId: 514449399
---
 .github/workflows/test_cpp.yml    | 45 ++++++++++++++++++++++++++++---
 .github/workflows/test_java.yml   |  2 +-
 .github/workflows/test_python.yml |  2 +-
 BUILD.bazel                       |  1 +
 CMakeLists.txt                    | 11 +++++---
 build_defs/BUILD.bazel            |  1 -
 cmake/protobuf-generate.cmake     |  4 +--
 7 files changed, 55 insertions(+), 11 deletions(-)

diff --git a/.github/workflows/test_cpp.yml b/.github/workflows/test_cpp.yml
index d5cf8b8a79..c46a72326e 100644
--- a/.github/workflows/test_cpp.yml
+++ b/.github/workflows/test_cpp.yml
@@ -32,7 +32,7 @@ jobs:
             targets: "//src/..."
           - config: { name: "aarch64" }
             targets: "//src/... //src/google/protobuf/compiler:protoc_aarch64_test"
-            image: "us-docker.pkg.dev/protobuf-build/containers/test/linux/emulation:aarch64-508417e5215994ade7585d28ba3aad681a25fa5d"
+            image: "us-docker.pkg.dev/protobuf-build/containers/test/linux/emulation:aarch64-3af05275178e16af30961976af126eabbbb2c733"
           - config: { name: "Bazel4" }
             targets: "//src/..."
             image: "us-docker.pkg.dev/protobuf-build/containers/common/linux/bazel:4.2.3-6361b3a6e5c97e9951d03a4de28542fc45f1adab"
@@ -51,6 +51,45 @@ jobs:
           bazel-cache: cpp_linux/${{ matrix.config.name }}
           bazel: test ${{ matrix.targets }} ${{ matrix.config.flags }} --distinct_host_configuration=false
 
+  linux-release:
+    strategy:
+      fail-fast: false   # Don't cancel all jobs if one fails.
+      matrix:
+        arch: [x86_64, aarch64]
+    name: Linux Release ${{ matrix.arch}}
+    runs-on: ubuntu-latest
+    steps:
+      - name: Checkout pending changes
+        uses: actions/checkout@ac593985615ec2ede58e132d2e21d2b1cbd6127c # v3.3.0
+        with:
+          submodules: recursive
+          ref: ${{ inputs.safe-checkout }}
+      - name: Cross compile protoc for ${{ matrix.arch }}
+        id: cross-compile
+        uses: protocolbuffers/protobuf-ci/cross-compile-protoc@v1
+        with:
+          image: us-docker.pkg.dev/protobuf-build/containers/common/linux/bazel:5.1.1-6361b3a6e5c97e9951d03a4de28542fc45f1adab
+          credentials: ${{ secrets.GAR_SERVICE_ACCOUNT }}
+          architecture: linux-${{ matrix.arch }}
+      - name: Setup ccache
+        uses: protocolbuffers/protobuf-ci/ccache@v1
+        with:
+          cache-prefix: linux-release-${{ matrix.arch }}
+      - name: Run tests
+        uses: protocolbuffers/protobuf-ci/docker@v1
+        with:
+          image: us-docker.pkg.dev/protobuf-build/containers/test/linux/emulation:${{ matrix.arch }}-3af05275178e16af30961976af126eabbbb2c733
+          credentials: ${{ secrets.GAR_SERVICE_ACCOUNT }}
+          skip-staleness-check: true
+          entrypoint: bash
+          command: >
+            -c "set -ex;
+            ccache -z;
+            cmake . -DWITH_PROTOC=/workspace/${{ steps.cross-compile.outputs.protoc }} -Dprotobuf_BUILD_CONFORMANCE=ON -DCMAKE_CXX_STANDARD=14 -Dprotobuf_WITH_ZLIB=OFF ${{ env.CCACHE_CMAKE_FLAGS }};
+            cmake --build . --parallel 20;
+            ctest --parallel 20;
+            ccache -s -v"
+
   linux-cmake:
     strategy:
       fail-fast: false   # Don't cancel all jobs if one fails.
@@ -91,7 +130,7 @@ jobs:
       - name: Run tests
         uses: ./.github/actions/docker
         with:
-          image: us-docker.pkg.dev/protobuf-build/containers/test/linux/cmake@sha256:e8282f42b1c1c0e6227d746e861954fd6fad0bd2e7424e6ceb23c249289a4016
+          image: us-docker.pkg.dev/protobuf-build/containers/test/linux/cmake@sha256:e0eb6c69b7551d89f0dbdbe11906077a1d501229c28db39623b945e0c5d7029a
           credentials: ${{ secrets.GAR_SERVICE_ACCOUNT }}
           command: ${{ matrix.command }} ${{ env.CCACHE_CMAKE_FLAGS }}
 
@@ -113,7 +152,7 @@ jobs:
       - name: Run tests
         uses: ./.github/actions/docker
         with:
-          image: us-docker.pkg.dev/protobuf-build/containers/test/linux/cmake@sha256:e8282f42b1c1c0e6227d746e861954fd6fad0bd2e7424e6ceb23c249289a4016
+          image: us-docker.pkg.dev/protobuf-build/containers/test/linux/cmake@sha256:e0eb6c69b7551d89f0dbdbe11906077a1d501229c28db39623b945e0c5d7029a
           credentials: ${{ secrets.GAR_SERVICE_ACCOUNT }}
           command: >-
             /install.sh -DCMAKE_CXX_STANDARD=14 ${{ env.CCACHE_CMAKE_FLAGS }} \&\&
diff --git a/.github/workflows/test_java.yml b/.github/workflows/test_java.yml
index 34347a9554..cea3223f12 100644
--- a/.github/workflows/test_java.yml
+++ b/.github/workflows/test_java.yml
@@ -28,7 +28,7 @@ jobs:
             targets: //java/... //java/internal:java_version
           - name: aarch64
             version: 'aarch64'
-            image: us-docker.pkg.dev/protobuf-build/containers/test/linux/emulation:aarch64-508417e5215994ade7585d28ba3aad681a25fa5d
+            image: us-docker.pkg.dev/protobuf-build/containers/test/linux/emulation:aarch64-3af05275178e16af30961976af126eabbbb2c733
             targets: //java/... //src/google/protobuf/compiler:protoc_aarch64_test
 
     name: Linux ${{ matrix.name }}
diff --git a/.github/workflows/test_python.yml b/.github/workflows/test_python.yml
index 18f29855c8..3547635277 100644
--- a/.github/workflows/test_python.yml
+++ b/.github/workflows/test_python.yml
@@ -27,7 +27,7 @@ jobs:
             targets: //python/... //python:aarch64_test
             # TODO(b/262628111) Enable this once conformance tests are fixed.
             flags: --define=use_fast_cpp_protos=true --test_tag_filters=-conformance
-            image: us-docker.pkg.dev/protobuf-build/containers/test/linux/emulation:aarch64-508417e5215994ade7585d28ba3aad681a25fa5d
+            image: us-docker.pkg.dev/protobuf-build/containers/test/linux/emulation:aarch64-3af05275178e16af30961976af126eabbbb2c733
 
     name: Linux ${{ matrix.type }} ${{ matrix.version }}
     runs-on: ubuntu-latest
diff --git a/BUILD.bazel b/BUILD.bazel
index b0b29f8128..3f8e5635e8 100644
--- a/BUILD.bazel
+++ b/BUILD.bazel
@@ -179,6 +179,7 @@ cc_binary(
     linkopts = LINK_OPTS,
     features = select({
       # This isn't possible on mac because there is no static library for lcrt0.o
+      "@platforms//os:osx": [],
       "//build_defs:config_osx": [],
       # When cross-compiling we need to statically link all C++ libraries.
       "//conditions:default": ["fully_static_link"],
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 2c3e002697..7b6389cca6 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -50,9 +50,6 @@ endif()
 
 # Options
 option(protobuf_INSTALL "Install protobuf binaries and files" ON)
-if(WITH_PROTOC)
-  set(protobuf_PROTOC_EXE ${WITH_PROTOC} CACHE FILEPATH "Protocol Buffer Compiler executable" FORCE)
-endif()
 option(protobuf_BUILD_TESTS "Build tests" ON)
 option(protobuf_BUILD_CONFORMANCE "Build conformance tests" OFF)
 option(protobuf_BUILD_EXAMPLES "Build examples" OFF)
@@ -76,6 +73,14 @@ option(protobuf_WITH_ZLIB "Build with zlib support" ${protobuf_WITH_ZLIB_DEFAULT
 set(protobuf_DEBUG_POSTFIX "d"
   CACHE STRING "Default debug postfix")
 mark_as_advanced(protobuf_DEBUG_POSTFIX)
+
+if(WITH_PROTOC)
+  set(protobuf_PROTOC_EXE protoc)
+  set(protobuf_BUILD_PROTOC_BINARIES OFF)
+  add_executable(protoc IMPORTED)
+  set_property(TARGET protoc PROPERTY IMPORTED_LOCATION ${WITH_PROTOC})
+endif()
+
 # User options
 include(${protobuf_SOURCE_DIR}/cmake/protobuf-options.cmake)
 
diff --git a/build_defs/BUILD.bazel b/build_defs/BUILD.bazel
index 1f1b21b8b0..023d4cb089 100644
--- a/build_defs/BUILD.bazel
+++ b/build_defs/BUILD.bazel
@@ -97,7 +97,6 @@ config_setting(
 selects.config_setting_group(
     name = "config_osx",
     match_any = [
-        "@platforms//os:osx",
         ":config_osx_aarch64",
         ":config_osx_x86_64",
     ]
diff --git a/cmake/protobuf-generate.cmake b/cmake/protobuf-generate.cmake
index 10fd616a53..5a4ff391e0 100644
--- a/cmake/protobuf-generate.cmake
+++ b/cmake/protobuf-generate.cmake
@@ -137,9 +137,9 @@ function(protobuf_generate)
 
     add_custom_command(
       OUTPUT ${_generated_srcs}
-      COMMAND protobuf::protoc
+      COMMAND ${protobuf_PROTOC_EXE}
       ARGS ${protobuf_generate_PROTOC_OPTIONS} --${protobuf_generate_LANGUAGE}_out ${_plugin_options}:${protobuf_generate_PROTOC_OUT_DIR} ${_plugin} ${_protobuf_include_path} ${_abs_file}
-      DEPENDS ${_abs_file} protobuf::protoc ${protobuf_generate_DEPENDENCIES}
+      DEPENDS ${_abs_file} ${protobuf_PROTOC_EXE} ${protobuf_generate_DEPENDENCIES}
       COMMENT ${_comment}
       VERBATIM )
   endforeach()