crytic-compile is an abstraction layer for smart contract build systems, facilitating the compilation of Solidity and Vyper projects across various environments. It supports direct `solc` compilation as well as frameworks like Foundry, Hardhat, Truffle, and fetching verified contracts from block explorers like Etherscan and Sourcify. The library is actively maintained by Trail of Bits and receives frequent minor updates.
pip install crytic-compileVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to compile a local Solidity project using `crytic-compile`. It first creates a dummy Foundry project structure and then calls `compile_all` to process it. The output provides access to various compilation artifacts like contract names from the detected platform. An commented example for Etherscan compilation is also provided.
To include test and script files, explicitly use the `--foundry-compile-all` flag when running `crytic-compile` from the command line, or target the specific files directly if using the API.
Ensure your Python environment is not running version 3.12.0. Use Python >=3.8 but avoid 3.12.0.
Review any custom logic that processes the `filenames` or `contracts_names` attributes of `CompilationUnit` or `SourceUnit` to ensure compatibility with list types and potential order changes.
Always double-check the package name during installation to ensure you are installing `crytic-compile` and not a similarly named malicious package.
Ensure `solc-select` is installed (`pip install solc-select`) and the correct Solidity version for your project is installed via `solc-select install <version>` (e.g., `solc-select install 0.8.19`). Also, verify `solc-select` is in your system's PATH. If using a framework like Foundry, ensure the `solc_version` is correctly specified in `foundry.toml`.
Verify that all necessary dependencies (e.g., node_modules, submodules) are correctly installed and accessible relative to the project root. Ensure remappings are properly configured. If compiling a single file with external imports, ensure the compilation context allows for discovery of those imports, or compile the entire project directory instead.
Modify your Solidity contract to reduce stack usage, for example by refactoring complex functions or using structs. Alternatively, enable the `--via-ir` option if your `solc` version supports it, which can sometimes mitigate this issue by optimizing stack usage. This can often be done via `crytic-compile` arguments if exposed, or by adjusting your project's `solc` configuration.
Ensure the `target` path provided to `crytic-compile` (e.g., `CryticCompile('./my_project')`) is correct and points to an existing file or directory. For project compilation, ensure you are running `crytic-compile` from the root of your project or pointing directly to it. Avoid reusing `--export-format` and `--export-dir` in the `args` parameter if they are already handled by the compilation process.