Using MLIR as a C++ Library with a Relocatable Install
Motivation
MLIR is widely used as reusable compiler infrastructure, but the practical workflow for consuming it as a C++ library is often less explicit than the workflow for building it in-tree. In practice, many users do not merely want to compile MLIR itself; they want to produce a stable SDK for external projects.
MLIR exposes both build-tree and install-tree CMake package configurations. The build tree is primarily an internal artifact for compilation, testing, and iterative development. It commonly contains absolute paths, generated intermediates, and configuration files intended for that particular build location. By contrast, the install tree is the artifact intended for downstream consumption. For users who wish to link against MLIR as a library, preserve a reusable package, archive it, copy it to another machine, or point multiple external projects at the same dependency prefix, the install tree is the correct deliverable.
The purpose of this document is therefore to give a concise, end-to-end workflow for producing a relocatable MLIR installation specifically for downstream library use.
Workflow
1. Configure
From the LLVM monorepo root:
1 | |
Notes:
-S llvmusesllvmas the CMake source root-B buildcreates a separate build directory-G Ninjagenerates Ninja files-DCMAKE_INSTALL_PREFIX=...defines the install location
2. Build with Ninja
Use Ninja directly, with whatever flags you want:
1 | |
3. Install
Install into the chosen prefix:
1 | |
This creates the installed tree under:
1 | |
This installed tree is the artifact you should use as your MLIR SDK.
4. Verify the install tree
Before deleting the build directory, verify that the install contains what you need.
Check the main install contents:
1 | |
Confirm these exist:
1 | |
5. Delete the build directory
If your explicit goal is to use MLIR as a library, and the install tree is complete, then you can delete the build directory:
1 | |
This is normally safe for downstream library use.
6. Keep only the install directory
After that, keep only the install tree, for example:
1 | |
You can also move it elsewhere:
1 | |
That installed prefix is what your downstream projects should use.