diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index cf48c14..39d18da 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -29,6 +29,16 @@ jobs: expect-stripped: "--expect-stripped" can-execute: true + - platform_name: Linux-x86_64 (force cross) + runs-on: ubuntu-24.04 + target: x86_64-unknown-linux-musl + force-use-cross: true + cache-cross-binary: true + expect-file-re: "ELF.+x86-64" + expect-cross: "--expect-cross" + expect-stripped: "" + can-execute: true + - platform_name: Linux-aarch64 runs-on: ubuntu-24.04 target: aarch64-unknown-linux-musl @@ -229,6 +239,7 @@ jobs: uses: ./ with: command: both + force-use-cross: ${{ matrix.platform.force-use-cross || false }} cross-version: ${{ matrix.platform.cross-version }} cache-cross-binary: ${{ matrix.platform.cache-cross-binary }} target: ${{ matrix.platform.target }} @@ -238,6 +249,7 @@ jobs: uses: ./ with: command: test + force-use-cross: ${{ matrix.platform.force-use-cross || false }} cross-version: ${{ matrix.platform.cross-version }} cache-cross-binary: ${{ matrix.platform.cache-cross-binary }} target: ${{ matrix.platform.target }} @@ -247,6 +259,7 @@ jobs: uses: ./ with: command: test + force-use-cross: ${{ matrix.platform.force-use-cross || false }} cross-version: ${{ matrix.platform.cross-version }} cache-cross-binary: ${{ matrix.platform.cache-cross-binary }} target: ${{ matrix.platform.target }} @@ -257,6 +270,7 @@ jobs: uses: ./ with: command: build + force-use-cross: ${{ matrix.platform.force-use-cross || false }} cross-version: ${{ matrix.platform.cross-version }} cache-cross-binary: ${{ matrix.platform.cache-cross-binary }} target: ${{ matrix.platform.target }} @@ -278,6 +292,7 @@ jobs: uses: ./ with: command: build + force-use-cross: ${{ matrix.platform.force-use-cross || false }} cross-version: ${{ matrix.platform.cross-version }} cache-cross-binary: ${{ matrix.platform.cache-cross-binary }} working-directory: subcrate @@ -301,6 +316,7 @@ jobs: uses: ./ with: command: bench + force-use-cross: ${{ matrix.platform.force-use-cross || false }} cross-version: ${{ matrix.platform.cross-version }} cache-cross-binary: ${{ matrix.platform.cache-cross-binary }} target: ${{ matrix.platform.target }} diff --git a/Changes.md b/Changes.md index 158927e..c66b734 100644 --- a/Changes.md +++ b/Changes.md @@ -1,3 +1,9 @@ +## 1.0.2 + +- Added a new `force-use-cross` input, which does what it says. It will force the use of `cross` + even when it is not required for given platform/target combination. Note that this only works on + Linux hosts. + ## 1.0.1 - 2025-01-20 - Fixed a bug where this action would attempt to use `cross` when compiling for an ARM Linux target diff --git a/README.md b/README.md index 54daa23..8a062c7 100644 --- a/README.md +++ b/README.md @@ -79,6 +79,7 @@ This action takes the following parameters: | `args` | string | no | A string-separated list of arguments to be passed to `cross build`, like `--release --locked`. | | `strip` | boolean (`true` or `false`) | no | If this is true, then the resulting binaries will be stripped if possible. This is only possible for binaries which weren't cross-compiled. | | `cross-version` | string | no | This can be used to set the version of `cross` to use. If specified, it should be a specific `cross` release tag (like `v0.2.3`) or a git ref (commit hash, `HEAD`, etc.). If this is not set then the latest released version will always be used. If this is set to a git ref then the version corresponding to that ref will be installed. | +| `force-use-cross` | boolean (`true` or `false`) | no | If this is true, then the action will use `cross` even if it is not needed for the given target. If this is set to `true`, then the resulting binary will not be stripped, regardless of whether `strip` is `true` or not. This only works on Linux hosts. Forcing the use of `cross` on other hosts is not supported. | | `use-rust-cache` | boolean | no | Whether or not to use [the `Swatinem/rust-cache@v2` action](https://github.com/Swatinem/rust-cache). This defaults to true. | | `rust-cache-parameters` | string (containing JSON) | no | This must be a string containing valid JSON. The JSON should be an object where the keys are the parameters for [the `Swatinem/rust-cache@v2` action](https://github.com/Swatinem/rust-cache). | diff --git a/action.yml b/action.yml index 7eac068..4ad54c0 100644 --- a/action.yml +++ b/action.yml @@ -53,6 +53,11 @@ inputs: tests of this action. default: true + force-use-cross: + description: | + If this is true, the action will use cross even for targets where it is not needed. + default: false + use-rust-cache: description: | Use `Swatinem/rust-cache@v2`. Defaults to true. @@ -87,13 +92,14 @@ runs: INPUTS_working_directory: ${{ inputs.working-directory }} INPUTS_strip: ${{ inputs.strip }} INPUTS_cache_cross_binary: ${{ inputs.cache-cross-binary }} + INPUTS_force_use_cross: ${{ inputs.force-use-cross }} INPUTS_use_rust_cache: ${{ inputs.use-rust-cache }} INPUTS_rust_cache_parameters: ${{ inputs.rust-cache-parameters }} - name: Determine whether we need to cross-compile id: determine-cross-compile shell: bash - run: set-cross-compile.py ${{ inputs.target }} + run: set-cross-compile.py ${{ inputs.target }} ${{ inputs.force-use-cross }} - name: Install toolchain uses: dtolnay/rust-toolchain@master diff --git a/set-cross-compile.py b/set-cross-compile.py index d0dfc5f..3ffc7e6 100755 --- a/set-cross-compile.py +++ b/set-cross-compile.py @@ -16,12 +16,20 @@ def main() -> int: Returns: Exit code (0 for success) """ - if len(sys.argv) < 2: - print("Error: Target architecture argument is required", file=sys.stderr) + if len(sys.argv) < 3: + print( + "Error: Target architecture and force cross arguments are required", + file=sys.stderr, + ) return 1 target = sys.argv[1] - needs_cross = check_needs_cross(target) + force_use_cross = sys.argv[2] + if force_use_cross == "true": + needs_cross = True + else: + needs_cross = check_needs_cross(target) + write_github_output(needs_cross) return 0 diff --git a/validate-inputs.py b/validate-inputs.py index ca955bd..65b2dd2 100755 --- a/validate-inputs.py +++ b/validate-inputs.py @@ -9,6 +9,8 @@ from typing import Dict, List, Union import tempfile import unittest +boolean_flags = {"cache_cross_binary", "force_use_cross", "strip", "use_rust_cache"} + class InputValidator: """Validate inputs for a GitHub Action.""" @@ -72,7 +74,6 @@ class InputValidator: ) # Validate boolean flags - boolean_flags = {"cache_cross_binary", "strip", "use_rust_cache"} for flag in boolean_flags: if flag in self.inputs and self.inputs[flag] not in {"true", "false"}: validation_errors.append(f"'{flag}' must be either 'true' or 'false'") @@ -212,7 +213,6 @@ class TestInputValidator(unittest.TestCase): def test_validate_boolean_flags(self) -> None: """Test validation of boolean flags.""" - boolean_flags = ["cache-cross-binary", "strip", "use-rust-cache"] # Test valid boolean values for flag in boolean_flags: