Skip to content

Commit c8a4f1b

Browse files
committed
cmake - build without Python libraries on Unix
1 parent 5be1858 commit c8a4f1b

3 files changed

Lines changed: 23 additions & 58 deletions

File tree

nix/build-all.py

Lines changed: 12 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
- ``USE_OCCT`` - whether to use official Open CASCADE instead of Community Edition
5555
(`true` by default, any other value is considered `false`)
5656
- ``WASM_PYTHON_PATH`` - path to WASM Python installation,
57-
used to deduce `PYMAJOR`, `PYMINOR`, `PYMICRO`, `TARGETINSTALLDIR`, `PYTHONINCLUDE`,
57+
used to deduce `PYVERSION` (e.g. '3.13.2'), `PYTHONINCLUDE`,
5858
`SIDE_MODULE_CFLAGS`, `SIDE_MODULE_LDFLAGS`.
5959
Allows to build wasm without pyodide build environment, which can be useful for debugging build issues.
6060
Example value: 'pyodide/cpython/installs/python-3.13.2'
@@ -216,27 +216,19 @@ def cecho(message, color=NO_COLOR):
216216
wasm_python_path = os.environ["WASM_PYTHON_PATH"]
217217
# Deduce version from path, assuming format .../python-X.Y.Z
218218
version_match = re.search(r"python-(\d+)\.(\d+)\.(\d+)", wasm_python_path)
219-
if version_match:
220-
os.environ["PYMAJOR"] = version_match.group(1)
221-
os.environ["PYMINOR"] = version_match.group(2)
222-
os.environ["PYMICRO"] = version_match.group(3)
223-
os.environ["TARGETINSTALLDIR"] = wasm_python_path
224-
os.environ["PYTHONINCLUDE"] = (
225-
f"{wasm_python_path}/include/python{os.environ['PYMAJOR']}.{os.environ['PYMINOR']}"
226-
)
219+
assert version_match, f"Could not deduce python version from '{wasm_python_path}'"
220+
python_version = version_match.group(1)
221+
os.environ["PYVERSION"] = python_version
222+
os.environ["PYTHONINCLUDE"] = f"{wasm_python_path}/include/python{python_version.rpartition('.')[0]}"
227223
os.environ["SIDE_MODULE_CFLAGS"] = ""
228224
# Required, otherwise library will compile as .a, not .so.
229225
os.environ["SIDE_MODULE_LDFLAGS"] = "-s SIDE_MODULE=1"
230226
# We're outside pyodide build environment, so need to provide toolchain file ourselves.
231227
assert "WASM_TOOLCHAIN_FILE" in os.environ, "WASM_TOOLCHAIN_FILE must be set when WASM_PYTHON_PATH is provided"
232228
WASM_DEBUG = True
233229
required_vars = (
234-
"PYMAJOR",
235-
"PYMINOR",
236-
"PYMICRO",
237-
# Folder where WASM-Python was installed.
238-
# e.g '/pyodide/cpython/installs/python-3.13.2'.
239-
"TARGETINSTALLDIR",
230+
# E.g. '3.13.2'.
231+
"PYVERSION",
240232
# 'include' folder in WASM-Python installation.
241233
# e.g. '/pyodide/cpython/installs/python-3.13.2/include/python3.13'
242234
"PYTHONINCLUDE",
@@ -1448,15 +1440,14 @@ def get_cmake_args_prefix_path(additional_paths: "Sequence[str]" = ()) -> "list[
14481440

14491441
def compile_python_wrapper(
14501442
python_version: str,
1451-
python_library: Union[str, None] = None,
14521443
python_include: Union[str, None] = None,
14531444
python_executable: Union[str, None] = None,
14541445
python_path: Union[Path, None] = None,
14551446
) -> Union[str, None]:
14561447
"""
14571448
:return: Path to module dir if ``python_executable`` was provided, otherwise ``None``.
14581449
"""
1459-
assert bool(python_path) ^ bool(python_library and python_include)
1450+
assert bool(python_path) ^ bool(python_include)
14601451

14611452
logger.info(f"\rConfiguring python {python_version} wrapper...")
14621453

@@ -1469,7 +1460,7 @@ def compile_python_wrapper(
14691460
prefix_paths.append(f"{DEPS_DIR}/install/swig")
14701461
if python_path:
14711462
# We couldn't just prefix PATH and have to provide all variables explicitly,
1472-
# see run-cmake.bat note for details.
1463+
# see ifcwrap/cmake for the details.
14731464
python_executable = (Path(python_path) / "bin" / "python3").__str__()
14741465
python_include = run(
14751466
[
@@ -1478,20 +1469,8 @@ def compile_python_wrapper(
14781469
"import sysconfig; print(sysconfig.get_config_var('INCLUDEPY'))",
14791470
]
14801471
)
1481-
python_library = run(
1482-
[
1483-
python_executable,
1484-
"-c",
1485-
"import sysconfig, pathlib; "
1486-
"lib_dir = pathlib.Path(sysconfig.get_config_var('LIBDIR')); "
1487-
"print((lib_dir / sysconfig.get_config_var('LIBRARY')).__str__())",
1488-
]
1489-
)
1490-
if platform.system() == "Darwin":
1491-
# Oddly on Mac `LIBRARY` returns .a that doesn't even exists.
1492-
python_library = Path(python_library).with_suffix(".dylib").__str__()
14931472

1494-
assert python_library and python_include
1473+
assert python_include
14951474
run_cmake(
14961475
"",
14971476
cmake_args
@@ -1500,7 +1479,6 @@ def compile_python_wrapper(
15001479
*([f"-DPYTHON_EXECUTABLE={python_executable}"] if python_executable else []),
15011480
# Needed because pyodide is expecting setup.py to be in the root.
15021481
*([f"-DPYTHON_MODULE_INSTALL_DIR={REPO_PATH}"] * WASM),
1503-
f"-DPYTHON_LIBRARY={python_library}",
15041482
f"-DPYTHON_INCLUDE_DIR={python_include}",
15051483
f"-DCMAKE_INSTALL_PREFIX={DEPS_DIR}/install/ifcopenshell/tmp",
15061484
"-DUSERSPACE_PYTHON_PREFIX="
@@ -1538,26 +1516,13 @@ def compile_python_wrapper(
15381516
return module_dir
15391517

15401518
if "wasm" in flags:
1541-
compile_python_wrapper(
1542-
f"{os.environ['PYMAJOR']}.{os.environ['PYMINOR']}.{os.environ['PYMICRO']}",
1543-
f"{os.environ['TARGETINSTALLDIR']}/lib/libpython{os.environ['PYMAJOR']}.{os.environ['PYMINOR']}.a",
1544-
os.environ["PYTHONINCLUDE"],
1545-
None,
1546-
)
1519+
compile_python_wrapper(os.environ["PYVERSION"], os.environ["PYTHONINCLUDE"])
15471520
# Copy setup.py where pyodide build system expects it.
15481521
shutil.copy(REPO_PATH / "pyodide" / "setup.py", REPO_PATH)
15491522

15501523
elif USE_CURRENT_PYTHON_VERSION:
15511524
python_info = sysconfig.get_paths()
1552-
1553-
py_path_components = [sysconfig.get_config_var("LIBDIR"), sysconfig.get_config_var("INSTSONAME")]
1554-
1555-
if sysconfig.get_config_var("multiarchsubdir"):
1556-
py_path_components.insert(1, sysconfig.get_config_var("multiarchsubdir").replace("/", ""))
1557-
1558-
python_lib = os.path.join(*py_path_components)
1559-
1560-
compile_python_wrapper(platform.python_version(), python_lib, python_info["include"], sys.executable)
1525+
compile_python_wrapper(platform.python_version(), python_info["include"], sys.executable)
15611526
else:
15621527
for python_version in PYTHON_VERSIONS:
15631528
python_path = Path(DEPS_DIR) / "install" / f"python-{python_version}"

src/ifcwrap/CMakeLists.txt

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,20 @@ IF(NOT "${PYTHON_LIBRARY}" STREQUAL "")
4646
MESSAGE(STATUS "Looking for Python library file in: ${Python_LIBRARY}")
4747
ENDIF()
4848

49-
# NOTE PYTHONLIBS_FOUND and PYTHONINTERP_FOUND cannot seem to be trusted so
50-
# we need further checks to see whether the packages were actually found or not.
51-
FIND_PACKAGE(Python COMPONENTS Development)
52-
IF(NOT Python_Development_FOUND)
49+
# Development.Module = headers on Unix, headers+libraries on Windows.
50+
# Required variables:
51+
# - Windows - Python_INCLUDE_DIR, Python_LIBRARY (MSVC doesn't allow undefined symbols at link time)
52+
# - Unix - Python_INCLUDE_DIR
53+
# Unfortunately all paths must be always provided explicitly, not by prefixing PATH.
54+
# Otherwise FindPython will break on newer Python versions (list of supported versions is hardcoded).
55+
find_package(Python COMPONENTS Development.Module)
56+
IF(NOT Python_Development.Module_FOUND)
5357
MESSAGE(FATAL_ERROR "BUILD_IFCPYTHON enabled, but unable to find Python lib or header. Disable BUILD_IFCPYTHON or fix Python paths to proceed.")
5458
ENDIF()
5559

5660
# Ensure version is saved here, from wasm libraries,
5761
# not from Python interpreter that might be unrelated to pyodide Python version.
5862
set(_python_libs_version "${Python_VERSION_MAJOR}${Python_VERSION_MINOR}")
59-
INCLUDE_DIRECTORIES(${Python_INCLUDE_DIRS})
6063
INCLUDE_DIRECTORIES(BEFORE ${CMAKE_CURRENT_SOURCE_DIR})
6164

6265
SET(CMAKE_SWIG_FLAGS ${SWIG_DEFINES})
@@ -90,6 +93,7 @@ SET_PROPERTY(
9093
set(SWIG_MODULE_ifcopenshell_wrapper_EXTRA_FLAGS "-interface" "_ifcopenshell_wrapper")
9194

9295
swig_add_library(ifcopenshell_wrapper LANGUAGE python SOURCES IfcPython.i)
96+
swig_link_libraries(ifcopenshell_wrapper PRIVATE Python::Module)
9397
SET_PROPERTY(TARGET ${SWIG_MODULE_ifcopenshell_wrapper_REAL_NAME} PROPERTY SWIG_DEPENDS ${IFCOPENSHELL_LIBRARIES})
9498
if (WASM_BUILD)
9599
# SIDE_MODULE=1 - add to .so all symbols from linked archives (default used by pyodide).
@@ -113,9 +117,9 @@ if (WASM_BUILD)
113117
endif()
114118
if("$ENV{LDFLAGS}" MATCHES ".undefined.suppress")
115119
# On osx there is some state in the python dylib. With `-Wl,undefined,suppress` we can ignore the missing symbols at compile time.
116-
SWIG_LINK_LIBRARIES(ifcopenshell_wrapper ${IFCOPENSHELL_LIBRARIES} ${OPENCASCADE_LIBRARIES} ${Boost_LIBRARIES} ${LIBSVGFILL})
120+
swig_link_libraries(ifcopenshell_wrapper PRIVATE ${IFCOPENSHELL_LIBRARIES} ${OPENCASCADE_LIBRARIES} ${Boost_LIBRARIES} ${LIBSVGFILL})
117121
else()
118-
SWIG_LINK_LIBRARIES(ifcopenshell_wrapper ${IFCOPENSHELL_LIBRARIES} ${Python_LIBRARIES} ${LIBSVGFILL})
122+
swig_link_libraries(ifcopenshell_wrapper PRIVATE ${IFCOPENSHELL_LIBRARIES} ${LIBSVGFILL})
119123
endif()
120124
if ((NOT WIN32) AND BUILD_SHARED_LIBS)
121125
SET_INSTALL_RPATHS(${SWIG_MODULE_ifcopenshell_wrapper_REAL_NAME} "${IFCDIRS};${OCC_LIBRARY_DIR}")

win/run-cmake.bat

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,6 @@ set LIBXML2_INCLUDE_DIR=%DEPS_DIR%\OpenCOLLADA\Externals\LibXML\include
9292
set LIBXML2_LIBRARIES=%INSTALL_DIR%\OpenCOLLADA\lib\opencollada\xml.lib
9393
set HDF5_INSTALL_DIR=%INSTALL_DIR%\HDF5-%HDF5_VERSION%-win%ARCH_BITS%
9494

95-
:: Unfortunately we have to provide all 3 paths explicitly,
96-
:: because if just prefix PATH, then FindPython will have an issue with newer versions of Python.
97-
:: E.g. older FindPython that didn't s added explicit support for Python 3.14 will fail to find.
98-
:: So setting paths expliicitly is more robust.
9995
set PYTHON_EXECUTABLE=%PYTHONHOME%\python.exe
10096
for /f "usebackq delims=" %%v in (`
10197
call "%PYTHON_EXECUTABLE%" -c "import sys; print(f'{sys.version_info[0]}{sys.version_info[1]}')"

0 commit comments

Comments
 (0)