3
0
Fork 0
mirror of https://github.com/YosysHQ/yosys synced 2026-06-06 09:00:54 +00:00
yosys/cmake/FindPyosysEnv.cmake
Catherine afc0e78d11 Update top-level Python project for CMake compatibility.
This commit reimplements the (no longer recommended) setuptools based
build system using a standards-based in-tree PEP517 build backend.
The implementation is partially based on
  https://codeberg.org/ziglang/zig-pypi/src/branch/main/make_wheels.py
which is licensed under BSD-0-clause.

It also adds a new option `YOSYS_BUILD_PYTHON_ONLY` that is available
only if the binary or the library aren't going to be installed, which
turns off these targets entirely, as well as some dependent ones
(e.g. tests).

Co-authored-by: Mohamed Gaber <me@donn.website>
2026-06-03 09:03:23 +00:00

44 lines
1.4 KiB
CMake

# We need a *third* `FindPython`-style call in this codebase because the host
# `Python3_EXECUTABLE` may not have pybind11 and cxxheaderparser installed,
# and installing it can be onerous. To work around this problem we try to detect
# whether the host interpreter has the necessary dependencies first, and if it
# does not, fall back to using `uv`.
foreach (strategy virtualenv host uv fail)
if (strategy STREQUAL "virtualenv")
set(PyosysEnv_PYTHON $ENV{VIRTUAL_ENV}/bin/python)
elseif (strategy STREQUAL "host")
set(PyosysEnv_PYTHON ${Python3_EXECUTABLE})
elseif (strategy STREQUAL "uv")
set(PyosysEnv_PYTHON uv run --no-project --with pybind11>3,<4 --with cxxheaderparser python)
else()
set(PyosysEnv_PYTHON)
break()
endif()
execute_process(
COMMAND ${PyosysEnv_PYTHON} -m pybind11 --includes
RESULT_VARIABLE result
OUTPUT_VARIABLE output
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET
)
if (result EQUAL 0)
string(REGEX REPLACE " ?-I" ";" pybind11_INCLUDE_DIR "${output}")
list(FILTER pybind11_INCLUDE_DIR INCLUDE REGEX "/pybind11/")
execute_process(
COMMAND ${PyosysEnv_PYTHON} ${CMAKE_SOURCE_DIR}/pyosys/generator.py --help
RESULT_VARIABLE result
OUTPUT_QUIET
ERROR_QUIET
)
if (result EQUAL 0)
break()
endif()
endif()
endforeach()
find_package_handle_standard_args(PyosysEnv
REQUIRED_VARS PyosysEnv_PYTHON pybind11_INCLUDE_DIR
)