From f300dd79279d47d53b364e11268abdb5f7c0dead Mon Sep 17 00:00:00 2001 From: Felddy Date: Mon, 22 Jan 2024 12:14:44 -0500 Subject: [PATCH 1/9] Remove version and c implementation files --- src/_version.py | 3 --- src/arch_info.c | 29 ----------------------------- 2 files changed, 32 deletions(-) delete mode 100644 src/_version.py delete mode 100644 src/arch_info.c diff --git a/src/_version.py b/src/_version.py deleted file mode 100644 index 40eaca0..0000000 --- a/src/_version.py +++ /dev/null @@ -1,3 +0,0 @@ -"""This file defines the version of this module.""" - -__version__ = "0.0.1" diff --git a/src/arch_info.c b/src/arch_info.c deleted file mode 100644 index 893b702..0000000 --- a/src/arch_info.c +++ /dev/null @@ -1,29 +0,0 @@ -#include - -int main() -{ -#if defined(__x86_64__) - printf("linux/amd64\n"); -#elif defined(__aarch64__) - printf("linux/arm64\n"); -#elif defined(__riscv) && (__riscv_xlen == 64) - printf("linux/riscv64\n"); -#elif defined(__PPC64__) || defined(__ppc64__) - printf("linux/ppc64le\n"); -#elif defined(__s390x__) - printf("linux/s390x\n"); -#elif defined(__i386__) - printf("linux/386\n"); -#elif defined(__mips64) && defined(__MIPSEL__) - printf("linux/mips64le\n"); -#elif defined(__mips64) - printf("linux/mips64\n"); -#elif defined(__arm__) && defined(__ARM_ARCH_7A__) - printf("linux/arm/v7\n"); -#elif defined(__arm__) && (defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__) || defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6Z__) || defined(__ARM_ARCH_6ZK__) || defined(__ARM_ARCH_6T2__)) - printf("linux/arm/v6\n"); -#else - printf("Architecture: Unknown\n"); -#endif - return 0; -} From 5bd48edaa5ece1a7f83de393396499808ec6a614 Mon Sep 17 00:00:00 2001 From: Felddy Date: Mon, 22 Jan 2024 12:16:25 -0500 Subject: [PATCH 2/9] Add Cargo.toml file with project metadata --- Cargo.toml | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Cargo.toml diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..e7514a1 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,23 @@ +[package] +authors = ["Mark Feldhousen "] +categories = ["command-line-utilities"] +description = "A tool to determine the architecture of a system." +documentation = "https://github.com/felddy/reusable-workflows/blob/develop/README.md" +edition = "2021" +homepage = "https://github.com/felddy/reusable-workflows" +keywords = ["architecture", "system", "cross-platform"] +license = "CC0-1.0" +name = "arch-info" +repository = "https://github.com/felddy/reusable-workflows" +version = "0.0.1" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] + +[dev-dependencies] + +[profile.release] +codegen-units = 1 +lto = true +opt-level = 'z' # Optimize for size. From 45cd9b672f3933f9ee0f79fa4eb5a2a0c0d461ec Mon Sep 17 00:00:00 2001 From: Felddy Date: Mon, 22 Jan 2024 12:17:13 -0500 Subject: [PATCH 3/9] Update version extraction in setup.py --- setup.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/setup.py b/setup.py index e457913..c7885fd 100644 --- a/setup.py +++ b/setup.py @@ -10,7 +10,8 @@ # Standard Python Libraries from glob import glob -from os.path import basename, splitext +from os.path import basename, dirname, join, splitext +import re # Third-Party Libraries from setuptools import find_packages, setup @@ -22,18 +23,23 @@ def readme(): return f.read() -def package_vars(version_file): - """Read in and return the variables defined by the version_file.""" - pkg_vars = {} - with open(version_file) as f: - exec(f.read(), pkg_vars) # nosec - return pkg_vars +def cargo_version(): + """Manually extract version from Cargo.toml.""" + cargo_toml_path = join(dirname(__file__), "Cargo.toml") + version_pattern = r'^version\s*=\s*"(.*?)"$' + with open(cargo_toml_path, encoding="utf-8") as f: + for line in f: + match = re.match(version_pattern, line.strip()) + if match: + return match.group(1) + # Raise an exception if version is not found + raise RuntimeError("Version not found in Cargo.toml") setup( name="reusable-workflows", # Versions should comply with PEP440 - version=package_vars("src/_version.py")["__version__"], + version=cargo_version(), description="reusable workflows for GitHub Actions", long_description=readme(), long_description_content_type="text/markdown", From 8952d43498f4f9933b33c91a4cac2d003d78440b Mon Sep 17 00:00:00 2001 From: Felddy Date: Mon, 22 Jan 2024 12:20:07 -0500 Subject: [PATCH 4/9] Add main.rs file with architecture detection logic --- src/main.rs | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 src/main.rs diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..22e1c25 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,53 @@ +/// Prints the architecture string based on the current compilation target. +/// - If the architecture is known, prints the architecture string and exits with code 0 (success). +/// - If the architecture is unknown, prints a message and exits with code 1 (error). +use std::process; + +/// Entry point of the application. +fn main() { + match get_architecture() { + // If an architecture is identified, print it and exit with code 0 (success). + Some(arch) => { + println!("{}", arch); + process::exit(0); + } + // If the architecture is unknown, print a message and exit with code 1 (error). + None => { + println!("Architecture: Unknown"); + process::exit(1); + } + } +} + +/// Returns the architecture string based on the current compilation target. +/// +/// Uses Rust's conditional compilation features to determine the architecture. +/// Returns `Some(arch_string)` if known, or `None` if the architecture is unknown. +fn get_architecture() -> Option<&'static str> { + match std::env::consts::ARCH { + // ARM 64-bit architecture + "aarch64" => Some("linux/arm64"), + // ARM architecture with further checks for specific versions + "arm" => Some(if cfg!(target_feature = "v7") { + "linux/arm/v7" + } else if cfg!(target_feature = "v6") { + "linux/arm/v6" + } else { + "linux/arm" + }), + // MIPS architecture with endian check + "mips64" => Some(if cfg!(target_endian = "little") { + "linux/mips64le" + } else { + "linux/mips64" + }), + // Other architectures without specific version checks + "powerpc64" => Some("linux/ppc64le"), + "riscv64" => Some("linux/riscv64"), + "s390x" => Some("linux/s390x"), + "x86_64" => Some("linux/amd64"), + "x86" => Some("linux/386"), + // Fallback case for unknown architectures + _ => None, + } +} From 9e29c7723822cb96f0bab5aa5f60f92a03693372 Mon Sep 17 00:00:00 2001 From: Felddy Date: Mon, 22 Jan 2024 12:20:47 -0500 Subject: [PATCH 5/9] Update Dockerfile to build the Rust executable --- Dockerfile | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/Dockerfile b/Dockerfile index 39a407d..479f6e4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,31 +2,37 @@ FROM --platform=$BUILDPLATFORM tonistiigi/xx:latest AS xx # Base image for the build -FROM --platform=$BUILDPLATFORM debian:bookworm AS build +FROM --platform=$BUILDPLATFORM rust:alpine AS build # Copy the xx scripts for setting up the cross-compilation environment COPY --from=xx / / -# Install build dependencies -RUN apt-get update && apt-get install -y \ - clang - # Set up the working directory WORKDIR /workspace -# Copy the C source file into the image -COPY src/arch_info.c . +# Install clang and other necessary tools +RUN apk add clang lld + +# Copy the Cargo.toml (and Cargo.lock if available) and source file into the image +COPY Cargo.toml Cargo.lock* ./ + +# Copy the Rust source file into the image +COPY src/main.rs ./src/ # Compile the program for the target platform ARG TARGETPLATFORM -RUN xx-apt install -y libc6-dev gcc -RUN xx-clang --static -o arch_info arch_info.c + +RUN xx-cargo build --release --target-dir ./build && \ + xx-verify ./build/$(xx-cargo --print-target-triple)/release/arch-info + +# Link the compiled binary into the workspace root +RUN ln ./build/$(xx-cargo --print-target-triple)/release/arch-info . # Stage 2: Create the final minimal output image FROM scratch # Copy the compiled binary from the build stage -COPY --from=build /workspace/arch_info / +COPY --from=build /workspace/arch-info / # Set the entry point to the compiled binary -ENTRYPOINT ["/arch_info"] +ENTRYPOINT ["/arch-info"] From 63b391a203efcf5df19d8dd25e8a679e0541e939 Mon Sep 17 00:00:00 2001 From: Felddy Date: Mon, 22 Jan 2024 12:44:44 -0500 Subject: [PATCH 6/9] Update Dockerfile to use rust:bookworm --- Dockerfile | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Dockerfile b/Dockerfile index 479f6e4..71ad852 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,7 +2,7 @@ FROM --platform=$BUILDPLATFORM tonistiigi/xx:latest AS xx # Base image for the build -FROM --platform=$BUILDPLATFORM rust:alpine AS build +FROM --platform=$BUILDPLATFORM rust:bookworm AS build # Copy the xx scripts for setting up the cross-compilation environment COPY --from=xx / / @@ -11,7 +11,8 @@ COPY --from=xx / / WORKDIR /workspace # Install clang and other necessary tools -RUN apk add clang lld +RUN apt-get update && \ + apt-get install -y clang lld # Copy the Cargo.toml (and Cargo.lock if available) and source file into the image COPY Cargo.toml Cargo.lock* ./ @@ -21,9 +22,9 @@ COPY src/main.rs ./src/ # Compile the program for the target platform ARG TARGETPLATFORM - -RUN xx-cargo build --release --target-dir ./build && \ - xx-verify ./build/$(xx-cargo --print-target-triple)/release/arch-info +RUN xx-apt install -y gcc libc6-dev +RUN xx-cargo build --release --target-dir ./build +RUN xx-verify ./build/$(xx-cargo --print-target-triple)/release/arch-info # Link the compiled binary into the workspace root RUN ln ./build/$(xx-cargo --print-target-triple)/release/arch-info . From c76a0b0b3a7cb9cd24577056768ae2050ccead13 Mon Sep 17 00:00:00 2001 From: Felddy Date: Mon, 22 Jan 2024 13:34:21 -0500 Subject: [PATCH 7/9] Add RUSTFLAGS for static linking with GNU toolchain --- Dockerfile | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 71ad852..e10fa4b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,6 +10,9 @@ COPY --from=xx / / # Set up the working directory WORKDIR /workspace +# Set RUSTFLAGS for static linking with the GNU toolchain +ENV RUSTFLAGS='-C target-feature=+crt-static' + # Install clang and other necessary tools RUN apt-get update && \ apt-get install -y clang lld @@ -27,7 +30,7 @@ RUN xx-cargo build --release --target-dir ./build RUN xx-verify ./build/$(xx-cargo --print-target-triple)/release/arch-info # Link the compiled binary into the workspace root -RUN ln ./build/$(xx-cargo --print-target-triple)/release/arch-info . +RUN ln -v ./build/$(xx-cargo --print-target-triple)/release/arch-info . # Stage 2: Create the final minimal output image FROM scratch From e51de349e49e020dc23f934224b5dc9466a02a7e Mon Sep 17 00:00:00 2001 From: Felddy Date: Mon, 22 Jan 2024 13:58:10 -0500 Subject: [PATCH 8/9] Add linux/386 to platforms --- .github/workflows/_config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/_config.yml b/.github/workflows/_config.yml index 1141e19..73f7009 100644 --- a/.github/workflows/_config.yml +++ b/.github/workflows/_config.yml @@ -6,7 +6,7 @@ on: inputs: platforms: description: "The platforms to build (CSV)" - default: linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64,linux/ppc64le,linux/s390x + default: linux/386,linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64,linux/ppc64le,linux/s390x required: false type: string outputs: From 9fbc6af3ff4576fa21b8fd2a3935edcd9cded945 Mon Sep 17 00:00:00 2001 From: Felddy Date: Mon, 22 Jan 2024 14:11:25 -0500 Subject: [PATCH 9/9] Add new label for Rust programming language --- .github/labels.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/labels.yml b/.github/labels.yml index 9c8fb4e..186a774 100644 --- a/.github/labels.yml +++ b/.github/labels.yml @@ -66,6 +66,9 @@ - name: "regression :boom:" color: "cd8703" description: "" +- name: "rust :crab:" + color: "dea584" + description: "" - name: "sponsor :sparkling_heart:" color: "fedbf0" description: ""