diff --git a/.github/workflows/build-and-test-macos14.yaml b/.github/workflows/build-and-test-macos14.yaml new file mode 100644 index 0000000..8e4e862 --- /dev/null +++ b/.github/workflows/build-and-test-macos14.yaml @@ -0,0 +1,52 @@ +# Copyright 2024, Giordano Salvador +# SPDX-License-Identifier: BSD-3-Clause + +name: Build-and-test + +on: + + workflow_call: + inputs: + build_mode: + description: Build optimization level + required: true + type: string + distro: + description: OS distribution name + required: true + type: string + os_ver: + description: OS distribution version + required: true + type: string + os: + description: OS string identifier + required: true + type: string + script_type: + description: Script type ("-container" | "-native") + required: true + type: string + +jobs: + + build-and-test: + runs-on: macos-14 + defaults: + run: + working-directory: ${{ github.workspace }} + steps: + + - name: Checkout homebrew + uses: Homebrew/actions/setup-homebrew@master + + - name: Checkout source + uses: actions/checkout@v4 + + - name: Build source and test + run: | + export BUILD_MODE=${{ inputs.build_mode }} + export DISTRO=${{ inputs.distro }} + export OS=${{ inputs.os }} + export OS_VER=${{ inputs.os_ver }} + bash -c "./scripts/build-and-test-macos14-${{ inputs.script_type }}.sh" diff --git a/.github/workflows/build-and-test.yaml b/.github/workflows/build-and-test-ubuntu2404.yaml similarity index 100% rename from .github/workflows/build-and-test.yaml rename to .github/workflows/build-and-test-ubuntu2404.yaml diff --git a/.github/workflows/fedora-40.yaml b/.github/workflows/fedora-40.yaml index ded4301..19ff54f 100644 --- a/.github/workflows/fedora-40.yaml +++ b/.github/workflows/fedora-40.yaml @@ -20,7 +20,7 @@ on: jobs: run: - uses: e3m3/calcc-rust/.github/workflows/build-and-test.yaml@main + uses: e3m3/calcc-rust/.github/workflows/build-and-test-ubuntu2404.yaml@main with: build_mode: ${{ inputs.build_mode }} distro: fedora diff --git a/.github/workflows/macos-14-container.yaml b/.github/workflows/macos-14-container.yaml new file mode 100644 index 0000000..ad72b14 --- /dev/null +++ b/.github/workflows/macos-14-container.yaml @@ -0,0 +1,29 @@ +# Copyright 2024, Giordano Salvador +# SPDX-License-Identifier: BSD-3-Clause + +name: MacOS 14 (Container) + +on: + + workflow_dispatch: + branches: [ "main" ] + inputs: + build_mode: + description: Build optimization level + required: true + default: debug + type: choice + options: + - debug + - release + +jobs: + + run: + uses: e3m3/calcc-rust/.github/workflows/build-and-test-macos14.yaml@main + with: + build_mode: ${{ inputs.build_mode }} + distro: fedora + os_ver: 40 + os: fedora-40 + script_type: container diff --git a/.github/workflows/macos-14-native.yaml b/.github/workflows/macos-14-native.yaml new file mode 100644 index 0000000..0d2afb3 --- /dev/null +++ b/.github/workflows/macos-14-native.yaml @@ -0,0 +1,29 @@ +# Copyright 2024, Giordano Salvador +# SPDX-License-Identifier: BSD-3-Clause + +name: MacOS 14 (Native) + +on: + + workflow_dispatch: + branches: [ "main" ] + inputs: + build_mode: + description: Build optimization level + required: true + default: debug + type: choice + options: + - debug + - release + +jobs: + + run: + uses: e3m3/calcc-rust/.github/workflows/build-and-test-macos14.yaml@main + with: + build_mode: ${{ inputs.build_mode }} + distro: fedora + os_ver: 40 + os: fedora-40 + script_type: native diff --git a/.github/workflows/ubuntu-2204.yaml b/.github/workflows/ubuntu-2204.yaml index 761087a..ee4a7e6 100644 --- a/.github/workflows/ubuntu-2204.yaml +++ b/.github/workflows/ubuntu-2204.yaml @@ -20,7 +20,7 @@ on: jobs: run: - uses: e3m3/calcc-rust/.github/workflows/build-and-test.yaml@main + uses: e3m3/calcc-rust/.github/workflows/build-and-test-ubuntu2404.yaml@main with: build_mode: ${{ inputs.build_mode }} distro: ubuntu diff --git a/.github/workflows/ubuntu-2404.yaml b/.github/workflows/ubuntu-2404.yaml index 11c139f..e00b5df 100644 --- a/.github/workflows/ubuntu-2404.yaml +++ b/.github/workflows/ubuntu-2404.yaml @@ -20,7 +20,7 @@ on: jobs: run: - uses: e3m3/calcc-rust/.github/workflows/build-and-test.yaml@main + uses: e3m3/calcc-rust/.github/workflows/build-and-test-ubuntu2404.yaml@main with: build_mode: ${{ inputs.build_mode }} distro: ubuntu diff --git a/README.md b/README.md index 7753e7a..77a3090 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,8 @@ Author/Maintainer: Giordano Salvador <73959795+e3m3@users.noreply.github.com> [![Ubuntu 22.04](https://github.com/e3m3/calcc-rust/actions/workflows/ubuntu-2204.yaml/badge.svg?event=workflow_dispatch)](https://github.com/e3m3/calcc-rust/actions/workflows/ubuntu-2204.yaml) [![Ubuntu 24.04](https://github.com/e3m3/calcc-rust/actions/workflows/ubuntu-2404.yaml/badge.svg?event=workflow_dispatch)](https://github.com/e3m3/calcc-rust/actions/workflows/ubuntu-2404.yaml) [![Fedora 40](https://github.com/e3m3/calcc-rust/actions/workflows/fedora-40.yaml/badge.svg?event=workflow_dispatch)](https://github.com/e3m3/calcc-rust/actions/workflows/fedora-40.yaml) +[![MacOS 14 (Container)](https://github.com/e3m3/calcc-rust/actions/workflows/macos-14-container.yaml/badge.svg?event=workflow_dispatch)](https://github.com/e3m3/calcc-rust/actions/workflows/macos-14-container.yaml) +[![MacOS 14 (Native)](https://github.com/e3m3/calcc-rust/actions/workflows/macos-14-native.yaml/badge.svg?event=workflow_dispatch)](https://github.com/e3m3/calcc-rust/actions/workflows/macos-14-native.yaml) Learning [Rust][1] [[1]] by implementing the calc langauge using the [llvm-sys][2] [[2]] crate. Implements the calc language, inspired by the [C++][3] [[3]] implementation presented by Macke and Kwan in [[4]] and [[5]]. diff --git a/container/Containerfile.fedora40 b/container/Containerfile.fedora40 index e995a19..389525c 100644 --- a/container/Containerfile.fedora40 +++ b/container/Containerfile.fedora40 @@ -1,3 +1,6 @@ +# Copyright 2024, Giordano Salvador +# SPDX-License-Identifier: BSD-3-Clause + FROM registry.fedoraproject.org/fedora:40 LABEL maintainer="Giordano Salvador <73959795+e3m3@users.noreply.github.com>" diff --git a/container/Containerfile.ubuntu22 b/container/Containerfile.ubuntu22 index 23e3cee..4f2f7e2 100644 --- a/container/Containerfile.ubuntu22 +++ b/container/Containerfile.ubuntu22 @@ -1,3 +1,6 @@ +# Copyright 2024, Giordano Salvador +# SPDX-License-Identifier: BSD-3-Clause + FROM docker.io/ubuntu:22.04 LABEL maintainer="Giordano Salvador <73959795+e3m3@users.noreply.github.com>" diff --git a/container/Containerfile.ubuntu22-build-llvm b/container/Containerfile.ubuntu22-build-llvm index 4bc0e3c..24c4df9 100644 --- a/container/Containerfile.ubuntu22-build-llvm +++ b/container/Containerfile.ubuntu22-build-llvm @@ -1,3 +1,6 @@ +# Copyright 2024, Giordano Salvador +# SPDX-License-Identifier: BSD-3-Clause + FROM docker.io/ubuntu:22.04 LABEL maintainer="Giordano Salvador <73959795+e3m3@users.noreply.github.com>" diff --git a/container/Containerfile.ubuntu24 b/container/Containerfile.ubuntu24 index 7a08eee..e755c8e 100644 --- a/container/Containerfile.ubuntu24 +++ b/container/Containerfile.ubuntu24 @@ -1,3 +1,6 @@ +# Copyright 2024, Giordano Salvador +# SPDX-License-Identifier: BSD-3-Clause + FROM docker.io/ubuntu:24.04 LABEL maintainer="Giordano Salvador <73959795+e3m3@users.noreply.github.com>" diff --git a/scripts/build-and-test-macos14-container.sh b/scripts/build-and-test-macos14-container.sh new file mode 100644 index 0000000..0f7648c --- /dev/null +++ b/scripts/build-and-test-macos14-container.sh @@ -0,0 +1,23 @@ +#!/bin/bash +# Copyright 2024, Giordano Salvador +# SPDX-License-Identifier: BSD-3-Clause + +HOMEBREW_HOME=/opt/homebrew +eval "$(${HOMEBREW_HOME}/bin/brew shellenv)" + +brew install -y podman +podman + +case ${BUILD_MODE} + debug) build_mode= + release) build_mode=--release + *) echo "Error: BUILD_MODE=$BUILD_MODE" >2 && exit 1 +esac + +podman machine init +podman machine start +podman build \ + -f container/Containerfile.${DISTRO}${OS_VER} \ + -t ${OS}-calcc-${build_mode} \ + --build-arg=BUILD_MODE=${build_mode} \ + . diff --git a/scripts/build-and-test-macos14-native.sh b/scripts/build-and-test-macos14-native.sh new file mode 100644 index 0000000..9b988cd --- /dev/null +++ b/scripts/build-and-test-macos14-native.sh @@ -0,0 +1,42 @@ +#!/bin/bash +# Copyright 2024, Giordano Salvador +# SPDX-License-Identifier: BSD-3-Clause + +HOMEBREW_HOME=/opt/homebrew +eval "$(${HOMEBREW_HOME}/bin/brew shellenv)" + +LLVM_VER=18 +PYTHON_VER=3.12 + +brew install -y python@${PYTHON_VER} llvm@${LLVM_VER} lit rustup + +RUSTUP_CHANNEL=stable +RUSTUP_HOME=/root/.rustup +RUSTUP_TOOLCHAIN=${RUSTUP_CHANNEL}-aarch64-apple-darwin +PATH=${RUSTUP_HOME}/toolchains/${RUSTUP_TOOLCHAIN}/bin${PATH:+:$PATH} +LD_LIBRARY_PATH=${RUSTUP_HOME}/toolchains/${RUSTUP_TOOLCHAIN}/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH} + +rustup default ${RUSTUP_CHANNEL} + +LLVM_HOME=${HOMEBREW_HOME}/opt/llvm@${LLVM_VER} +PATH=${LLVM_HOME}/bin${PATH:+:$PATH} +LD_LIBRARY_PATH=${LLVM_HOME}/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH} +C_INCLUDE_PATH=${LLVM_HOME}/include${C_INCLUDE_PATH:+:$C_INCLUDE_PATH} +CPLUS_INCLUDE_PATH=${LLVM_HOME}/include${CPLUS_INCLUDE_PATH:+:$CPLUS_INCLUDE_PATH} + +PYTHON_VENV_PATH=/root/.python/venv +python3 -m venv ${PYTHON_VENV_PATH} +PATH=${PYTHON_VENV_PATH}/bin${PATH:+:$PATH} + +export HOMEBREW_HOME +export LLVM_HOME +export PYTHON_VENV_PATH + +case ${BUILD_MODE} + debug) build_mode= + release) build_mode=--release + *) echo "Error: BUILD_MODE=$BUILD_MODE" >2 && exit 1 +esac + +cargo build --verbose ${BUILD_MODE} +cargo test --verbose ${BUILD_MODE} -- --nocapture diff --git a/tests/lit-llvm/codegen_type.calc b/tests/lit-llvm/codegen_type.calc index 7b93b35..e76a516 100644 --- a/tests/lit-llvm/codegen_type.calc +++ b/tests/lit-llvm/codegen_type.calc @@ -12,3 +12,5 @@ // CHECK_B: Bytecode file type (missing '-S' flag) should match output name ('.bc' extension) // CHECK_C-NOT: ^0 + +// XFAIL: ARCH_X86_64 && OS_LINUX diff --git a/tests/lit-llvm/end_to_end1.calc b/tests/lit-llvm/end_to_end1.calc index 6ac8b60..ab804e8 100644 --- a/tests/lit-llvm/end_to_end1.calc +++ b/tests/lit-llvm/end_to_end1.calc @@ -5,3 +5,6 @@ // RUN: @calcc -e "with: a: 1024*32/a" -o %t4.bc && @clang -O0 -o %t4.out %t4.bc && %t4.out 8 | @filecheck %s // CHECK: calcc_main result: 4096 + +// XFAIL: ARCH_AARCH64 && OS_MACOS +// XFAIL: ARCH_AARCH64 && OS_LINUX diff --git a/tests/lit-llvm/end_to_end2.calc b/tests/lit-llvm/end_to_end2.calc index d117182..99a5470 100644 --- a/tests/lit-llvm/end_to_end2.calc +++ b/tests/lit-llvm/end_to_end2.calc @@ -45,3 +45,6 @@ // CHECK_E: calcc_main result: -3 // CHECK_F: calcc_main result: 0 + +// XFAIL: ARCH_AARCH64 && OS_MACOS +// XFAIL: ARCH_AARCH64 && OS_LINUX diff --git a/tests/lit-llvm/input_file1.calc b/tests/lit-llvm/input_file1.calc index a95b421..9013f24 100644 --- a/tests/lit-llvm/input_file1.calc +++ b/tests/lit-llvm/input_file1.calc @@ -9,3 +9,6 @@ // Implement Pythagorean theorem check (for integer triangles): 0 result => passing check with: a,b,c: c*c - (a*a + b*b) + +// XFAIL: ARCH_AARCH64 && OS_MACOS +// XFAIL: ARCH_AARCH64 && OS_LINUX diff --git a/tests/lit-llvm/irgen1.calc b/tests/lit-llvm/irgen1.calc index 5a54f37..b350a2a 100644 --- a/tests/lit-llvm/irgen1.calc +++ b/tests/lit-llvm/irgen1.calc @@ -16,3 +16,5 @@ // CHECK: %v3 = add nsw i64 %v2, 10 // CHECK: ret i64 %v3 // CHECK: } + +// XFAIL: ARCH_X86_64 && OS_LINUX diff --git a/tests/lit-llvm/irgen2.calc b/tests/lit-llvm/irgen2.calc index 79e7148..150e2e7 100644 --- a/tests/lit-llvm/irgen2.calc +++ b/tests/lit-llvm/irgen2.calc @@ -11,3 +11,5 @@ // CHECK-LABEL: entry: // CHECK: ret i64 4096 // CHECK: } + +// XFAIL: ARCH_X86_64 && OS_LINUX diff --git a/tests/lit-llvm/lit.cfg b/tests/lit-llvm/lit.cfg index 81898e6..1f10c1f 100644 --- a/tests/lit-llvm/lit.cfg +++ b/tests/lit-llvm/lit.cfg @@ -8,7 +8,7 @@ config.substitutions.append(("@clang", "clang")) config.substitutions.append(("@calcc", "calcc")) config.substitutions.append(("@count", "wc")) config.substitutions.append(("@diff", "diff")) -config.substitutions.append(("@filecheck", "FileCheck --color")) +config.substitutions.append(("@filecheck", "FileCheck --color --vv")) config.substitutions.append(("@head", "head")) config.substitutions.append(("@llc", "llc")) config.substitutions.append(("@llvm-as", "llvm-as")) @@ -17,6 +17,24 @@ config.substitutions.append(("@llvm-opt", "opt")) config.substitutions.append(("@tail", "tail")) config.substitutions.append(("@tee", "tee")) +if "OS_NAME" in lit_config.params: + os_name = lit_config.params["OS_NAME"] + if os_name == "linux": + config.available_features.add("OS_LINUX") + elif os_name == "macos": + config.available_features.add("OS_MACOS") + elif os_name == "windows": + config.available_features.add("OS_WINDOWS") + +if "ARCH" in lit_config.params: + arch = lit_config.params["OS_NAME"] + if arch == "x86": + config.available_features.add("ARCH_X86") + elif arch == "x86_64": + config.available_features.add("ARCH_X86_64") + elif arch == "aarch64": + config.available_features.add("ARCH_AARCH64") + config.test_format = lit.formats.ShTest(False) config.test_source_root = os.path.dirname(__file__) diff --git a/tests/lit-llvm/maingen1.calc b/tests/lit-llvm/maingen1.calc index 5f64b6c..69c0cd4 100644 --- a/tests/lit-llvm/maingen1.calc +++ b/tests/lit-llvm/maingen1.calc @@ -74,3 +74,5 @@ // CHECK: %v3 = add nsw i64 %v2, 10 // CHECK: ret i64 %v3 // CHECK: } + +// XFAIL: ARCH_X86_64 && OS_LINUX diff --git a/tests/lit-llvm/maingen2.calc b/tests/lit-llvm/maingen2.calc index 2382db4..11fbf9a 100644 --- a/tests/lit-llvm/maingen2.calc +++ b/tests/lit-llvm/maingen2.calc @@ -53,3 +53,5 @@ // CHECK-LABEL: entry: // CHECK: ret i64 4096 // CHECK: } + +// XFAIL: ARCH_X86_64 && OS_LINUX diff --git a/tests/lit-llvm/option_notarget.calc b/tests/lit-llvm/option_notarget.calc index a6d29d7..35da2f6 100644 --- a/tests/lit-llvm/option_notarget.calc +++ b/tests/lit-llvm/option_notarget.calc @@ -4,7 +4,7 @@ // CHECK_TARGET-LABEL: ; ModuleID = 'calcc' // CHECK_TARGET: source_filename = "calcc" // CHECK_TARGET: target datalayout = "{{[a-zA-Z0-9\:\-]+}}" -// CHECK_TARGET: target triple = "{{[_a-zA-Z0-9\-]+}}" +// CHECK_TARGET: target triple = "{{[\.a-zA-Z0-9\-]+}}" // CHECK_TARGET-LABEL: define i64 @calcc_main() { // CHECK_TARGET-LABEL: entry: @@ -14,9 +14,11 @@ // CHECK_NOTARGET-LABEL: ; ModuleID = 'calcc' // CHECK_NOTARGET: source_filename = "calcc" // CHECK_NOTARGET-NOT: target datalayout = "{{[a-zA-Z0-9\:\-]+}}" -// CHECK_NOTARGET-NOT: target triple = "{{[a-zA-Z0-9\-]+}}" +// CHECK_NOTARGET-NOT: target triple = "{{[\.a-zA-Z0-9\-]+}}" // CHECK_NOTARGET-LABEL: define i64 @calcc_main() { // CHECK_NOTARGET-LABEL: entry: // CHECK_NOTARGET: ret i64 4096 // CHECK_NOTARGET: } + +// XFAIL: ARCH_X86_64 && OS_LINUX diff --git a/tests/lit-llvm/option_opt.calc b/tests/lit-llvm/option_opt.calc index 086a4a6..b3cf230 100644 --- a/tests/lit-llvm/option_opt.calc +++ b/tests/lit-llvm/option_opt.calc @@ -22,3 +22,4 @@ // CHECK_O1: ret i64 %v[[REG]] // CHECK_O1: } +// XFAIL: ARCH_X86_64 && OS_LINUX diff --git a/tests/lit-tests-llvm.rs b/tests/lit-tests-llvm.rs index f086d45..733a986 100644 --- a/tests/lit-tests-llvm.rs +++ b/tests/lit-tests-llvm.rs @@ -32,55 +32,94 @@ mod tests{ }).unwrap() } - fn get_shell() -> String { - String::from( - if cfg!(target_os = "linux") || cfg!(target_os = "macos") { - "bash" - } else if cfg!(target_os = "windows") { - "cmd" - } else { + fn get_shell(os_name: &String) -> String { + String::from(match os_name.as_str() { + "linux" => "bash", + "macos" => "bash", + "windows" => "cmd", + _ => { eprintln!("Unexpected target_os"); assert!(false); "" - } - ) + }, + }) } - fn get_lit() -> String { + fn get_lit(os_name: &String) -> String { let append_lit: fn(&Path) -> String = |path| { String::from(path.join("bin").join("lit").to_str().unwrap()) }; - if cfg!(target_os = "linux") || cfg!(target_os = "macos") { - match env::var("PYTHON_VENV_PATH") { - Ok(path) => append_lit(Path::new(&path)), - Err(_) => append_lit(Path::new("/usr")), - } - } else if cfg!(target_os = "windows") { - eprintln!("Windows not supported"); - assert!(false); - String::new() - } else { - eprintln!("OS not supported"); - assert!(false); - String::new() + match os_name.as_str() { + "linux" => + match env::var("PYTHON_VENV_PATH") { + Ok(path) => append_lit(Path::new(&path)), + Err(_) => append_lit(Path::new("/usr")), + }, + "macos" => + match env::var("HOMEBREW_HOME") { + Ok(path) => append_lit(Path::new(&path)), + Err(_) => match env::var("PYTHON_VENV_PATH") { + Ok(path) => append_lit(Path::new(&path)), + Err(_) => append_lit(Path::new("/usr")), + } + }, + "windows" => { + eprintln!("Windows not supported"); + assert!(false); + String::new() + }, + _ => { + eprintln!("OS not supported"); + assert!(false); + String::new() + }, } } #[test] fn lit() { - if !cfg!(target_os = "linux") && !cfg!(target_os = "macos") && !cfg!(target_os = "windows") { - eprintln!("OS not yet supported."); + let os_name: String = String::from( + if cfg!(target_os = "linux") { + "linux" + } else if cfg!(target_os = "macos") { + "macos" + } else if cfg!(target_os = "windows") { + "windows" + } else { + "" + } + ); + let arch: String = String::from( + if cfg!(target_arch = "x86") { + "x86" + } else if cfg!(target_arch = "x86_64") { + "x86_64" + } else if cfg!(target_arch = "aarch64") { + "aarch64" + } else { + "" + } + ); + + if os_name.is_empty() { + eprintln!("Target OS '{}' not yet supported.", os_name); + assert!(false); + } + if arch.is_empty() { + eprintln!("Target arch '{}' not yet supported.", arch); assert!(false); } let calcc_dir: PathBuf = get_bin_dir(); - let lit_bin_str: String = get_lit(); + let lit_bin_str: String = get_lit(&os_name); let lit_bin: &Path = Path::new(lit_bin_str.as_str()); - let shell: String = get_shell(); + let shell: String = get_shell(&os_name); let tests_dir: PathBuf = get_tests_dir(); let lit_dir: PathBuf = tests_dir.join("lit-llvm"); let cfg_path: PathBuf = lit_dir.join("lit.cfg"); + eprintln!("Lit binary path: {}", lit_bin_str); + assert!(calcc_dir.is_dir()); assert!(lit_bin.is_file()); assert!(lit_dir.is_dir()); @@ -99,6 +138,8 @@ mod tests{ "--order=lexical", "--show-all", "--workers=4", + format!("--param=OS_NAME={}", os_name).as_str(), + format!("--param=ARCH={}", arch).as_str(), format!("--path={}", env_path_str).as_str(), ].join(" ");