Skip to content

Commit 1df33b4

Browse files
committed
[Examples] Add ability to run and test examples
Add a `lit.cfg.py` in the `examples` directory which sets up the lit config for example Mojo files (notebooks not yet supported). Add a wrapper script, `run-examples.sh` which creates a build directory and invokes `lit` using this configuration. Go one step further and create a GitHub workflow that puts all of the pieces together to download the latest stable Mojo compiler and test the examples using the provided script. The `scripts/run-examples.sh` is something that can be run both locally and in CI. Add common artifacts to the `.gitignore` such as the new `build` directory, and other Python-related things.
1 parent 59818e6 commit 1df33b4

File tree

4 files changed

+159
-0
lines changed

4 files changed

+159
-0
lines changed

.github/workflows/examples.yml

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
name: Test Examples
2+
3+
on:
4+
pull_request:
5+
workflow_dispatch:
6+
7+
jobs:
8+
test-examples:
9+
runs-on: ubuntu-latest
10+
defaults:
11+
run:
12+
shell: bash
13+
env:
14+
DEBIAN_FRONTEND: noninteractive
15+
LLVM_VERSION: 17
16+
17+
steps:
18+
- name: Checkout repo
19+
uses: actions/checkout@v2
20+
21+
- name: Download Modular installer
22+
run: |
23+
curl -s https://get.modular.com | sh -
24+
25+
- name: Install stable Mojo compiler
26+
run: |
27+
# The <auth_token> of "examples" is arbitrary but something
28+
# needs to be provided.
29+
modular auth examples
30+
modular install mojo
31+
32+
- name: Setup python
33+
uses: actions/setup-python@0a5c61591373683505ea898e09a3ea4f39ef2b9c
34+
with:
35+
python-version: 3.12
36+
37+
- name: Install build tools
38+
run: |
39+
wget https://apt.llvm.org/llvm.sh
40+
chmod +x llvm.sh
41+
sudo ./llvm.sh ${{env.LLVM_VERSION}}
42+
43+
# Make common LLVM binaries (including FileCheck) in our PATH so they work when used in an unversioned context
44+
# For example, instead of saying `FileCheck-17` which exists in `/usr/bin`, this allows us to just call
45+
# FileCheck unqualified.
46+
sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-${{env.LLVM_VERSION}} 100
47+
sudo update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-${{env.LLVM_VERSION}} 100
48+
sudo update-alternatives --install /usr/bin/lld lld /usr/bin/lld-${{env.LLVM_VERSION}} 100
49+
sudo update-alternatives --install /usr/bin/ld.lld ld.lld /usr/bin/ld.lld-${{env.LLVM_VERSION}} 100
50+
sudo update-alternatives --install /usr/bin/lldb lldb /usr/bin/lldb-${{env.LLVM_VERSION}} 100
51+
sudo update-alternatives --install /usr/bin/FileCheck FileCheck /usr/bin/FileCheck-${{env.LLVM_VERSION}} 100
52+
53+
54+
python3 -m pip install lit
55+
56+
- name: Run examples
57+
run: |
58+
export MODULAR_HOME="/home/runner/.modular"
59+
export PATH="/home/runner/.modular/pkg/packages.modular.com_mojo/bin:$PATH"
60+
./scripts/run-examples.sh

.gitignore

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Build directory
2+
/build
3+
4+
# Reference: https://github.com/github/gitignore/blob/main/Python.gitignore
5+
6+
# Byte-compiled / optimized / DLL files
7+
__pycache__/
8+
*.py[cod]
9+
*$py.class
10+
11+
# Environments
12+
.env
13+
.venv
14+
env/
15+
venv/
16+
ENV/
17+
env.bak/
18+
venv.bak/

examples/lit.cfg.py

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# ===----------------------------------------------------------------------=== #
2+
# Copyright (c) 2024, Modular Inc. All rights reserved.
3+
#
4+
# Licensed under the Apache License v2.0 with LLVM Exceptions:
5+
# https://llvm.org/LICENSE.txt
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS,
9+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
# See the License for the specific language governing permissions and
11+
# limitations under the License.
12+
# ===----------------------------------------------------------------------=== #
13+
14+
from pathlib import Path
15+
16+
import lit.formats
17+
import lit.llvm
18+
19+
config.test_format = lit.formats.ShTest(True)
20+
21+
# name: The name of this test suite.
22+
config.name = "Mojo Public Examples"
23+
24+
# suffixes: A list of file extensions to treat as test files.
25+
# TODO: Enable notebooks
26+
config.suffixes = [".mojo", ".🔥"]
27+
28+
config.excludes = [
29+
# No RUN: directive, just bare examples
30+
"hello_interop.mojo",
31+
"matmul.mojo"
32+
]
33+
34+
# Have the examples run in the build directory.
35+
# The `run-examples.sh` script creates the build directory.
36+
build_root = Path.cwd().parent / "build"
37+
38+
# Execute the examples inside this part of the build
39+
# directory to avoid polluting the source tree.
40+
config.test_exec_root = build_root / "examples"
41+
42+
# test_source_root: The root path where tests are located.
43+
config.test_source_root = Path(__file__).parent.resolve()
44+
45+
# Substitute %mojo for just `mojo` itself.
46+
config.substitutions.insert(0, ("%mojo", "mojo"))
47+
48+
# Pass through several environment variables
49+
# to the underlying subprocesses that run the tests.
50+
lit.llvm.initialize(lit_config, config)
51+
lit.llvm.llvm_config.with_system_environment(
52+
[
53+
"MODULAR_HOME",
54+
"PATH",
55+
]
56+
)

scripts/run-examples.sh

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env bash
2+
# ===----------------------------------------------------------------------=== #
3+
# Copyright (c) 2024, Modular Inc. All rights reserved.
4+
#
5+
# Licensed under the Apache License v2.0 with LLVM Exceptions:
6+
# https://llvm.org/LICENSE.txt
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
# ===----------------------------------------------------------------------=== #
14+
15+
set -euo pipefail
16+
17+
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
18+
REPO_ROOT="${SCRIPT_DIR}"/../
19+
EXAMPLES_DIR="${REPO_ROOT}"/examples
20+
21+
BUILD_DIR="${REPO_ROOT}"/build
22+
mkdir -p "${BUILD_DIR}"
23+
24+
# Run the examples using `lit`.
25+
lit -sv "${EXAMPLES_DIR}"

0 commit comments

Comments
 (0)