|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# ============================================================================ # |
| 4 | +# Copyright (c) 2022 - 2026 NVIDIA Corporation & Affiliates. # |
| 5 | +# All rights reserved. # |
| 6 | +# # |
| 7 | +# This source code and the accompanying materials are made available under # |
| 8 | +# the terms of the Apache License 2.0 which accompanies this distribution. # |
| 9 | +# ============================================================================ # |
| 10 | + |
| 11 | +# Builds the MLIR Python bindings against an existing LLVM build tree and |
| 12 | +# installs them into LLVM_INSTALL_PREFIX. Requires the manylinux devdeps |
| 13 | +# base image, where build_llvm.sh has already configured and built the |
| 14 | +# LLVM/MLIR libraries (without python-bindings) at LLVM_SOURCE/build. |
| 15 | +# |
| 16 | +# This avoids reconfiguring/rebuilding the full LLVM tree once per Python |
| 17 | +# version: only the binding objects (linked against the active interpreter |
| 18 | +# ABI) and a small amount of tablegen output get produced. |
| 19 | +# |
| 20 | +# Required environment: |
| 21 | +# LLVM_SOURCE path to the llvm-project source (default: /llvm-project) |
| 22 | +# LLVM_INSTALL_PREFIX install destination (default: /usr/local/llvm) |
| 23 | +# Python3_EXECUTABLE interpreter to bind against |
| 24 | +# NANOBIND_INSTALL_PREFIX nanobind install dir (default: /usr/local/nanobind) |
| 25 | +# |
| 26 | +# Usage: |
| 27 | +# Python3_EXECUTABLE=$(which python3.11) bash scripts/build_mlir_python_bindings.sh |
| 28 | + |
| 29 | +set -e |
| 30 | + |
| 31 | +LLVM_SOURCE=${LLVM_SOURCE:-/llvm-project} |
| 32 | +LLVM_INSTALL_PREFIX=${LLVM_INSTALL_PREFIX:-/usr/local/llvm} |
| 33 | +NANOBIND_INSTALL_PREFIX=${NANOBIND_INSTALL_PREFIX:-/usr/local/nanobind} |
| 34 | +LLVM_BUILD_FOLDER=${LLVM_BUILD_FOLDER:-build} |
| 35 | + |
| 36 | +if [ -z "$Python3_EXECUTABLE" ]; then |
| 37 | + echo "Python3_EXECUTABLE must be set." |
| 38 | + exit 1 |
| 39 | +fi |
| 40 | + |
| 41 | +llvm_build_dir="$LLVM_SOURCE/$LLVM_BUILD_FOLDER" |
| 42 | +if [ ! -f "$llvm_build_dir/CMakeCache.txt" ]; then |
| 43 | + echo "Expected pre-configured LLVM build at $llvm_build_dir." |
| 44 | + echo "This script must run on top of the manylinux devdeps base image." |
| 45 | + exit 1 |
| 46 | +fi |
| 47 | + |
| 48 | +this_file_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 49 | + |
| 50 | +# nanobind links against the active interpreter ABI; build it if missing. |
| 51 | +if [ ! -d "$NANOBIND_INSTALL_PREFIX" ] || [ -z "$(ls -A "$NANOBIND_INSTALL_PREFIX"/* 2>/dev/null)" ]; then |
| 52 | + echo "Building nanobind..." |
| 53 | + cd "$this_file_dir/.." && repo_root=$(git rev-parse --show-toplevel) && cd "$repo_root" |
| 54 | + git submodule update --init --recursive --recommend-shallow --single-branch tpls/nanobind |
| 55 | + mkdir -p tpls/nanobind/build && cd tpls/nanobind/build |
| 56 | + cmake -G Ninja ../ \ |
| 57 | + -DCMAKE_INSTALL_PREFIX="$NANOBIND_INSTALL_PREFIX" \ |
| 58 | + -DPython3_EXECUTABLE="$Python3_EXECUTABLE" \ |
| 59 | + -DNB_TEST=False |
| 60 | + cmake --build . --target install --config Release |
| 61 | +fi |
| 62 | + |
| 63 | +cd "$llvm_build_dir" |
| 64 | + |
| 65 | +# Reconfigure the existing build tree: enable MLIR python bindings and |
| 66 | +# point at the active interpreter. Other cache entries (compiler flags, |
| 67 | +# enabled projects, ccache launcher, build type) are preserved. |
| 68 | +echo "Reconfiguring LLVM build for python bindings (Python: $Python3_EXECUTABLE)..." |
| 69 | +cmake . \ |
| 70 | + -DMLIR_ENABLE_BINDINGS_PYTHON=ON \ |
| 71 | + -DPython3_EXECUTABLE="$Python3_EXECUTABLE" \ |
| 72 | + -Dnanobind_DIR="$NANOBIND_INSTALL_PREFIX/nanobind/cmake" |
| 73 | + |
| 74 | +# Build and install only the python-binding components. Other targets in |
| 75 | +# the existing tree are left untouched and remain installed at |
| 76 | +# LLVM_INSTALL_PREFIX from the base image. |
| 77 | +echo "Building and installing MLIR python bindings..." |
| 78 | +ninja install-MLIRPythonModules install-mlir-python-sources |
| 79 | + |
| 80 | +echo "Installed MLIR python bindings into $LLVM_INSTALL_PREFIX." |
0 commit comments