Skip to content

Conversation

agriyakhetarpal
Copy link
Member

Building up on and partially reverting the changes in #61; I was able to figure out the problem CMake was having with the builds for CasADi in config mode, and this PR cuts down on a lot of unnecessary configuration. Comments below annotate the reasoning for changes.

@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.13)
cmake_policy(SET CMP0074 NEW)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This policy is too old; it prevents us from using https://cmake.org/cmake/help/latest/module/FindPython.html as it gets disabled when this policy is enabled.

@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.13)
cmake_policy(SET CMP0074 NEW)
cmake_minimum_required(VERSION 3.26)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The earliest version of CMake to support the new FindPython mode is 3.26. We don't pin CMake in pyproject.toml, and our use of CMake isn't too heavy either, so the current 4.1.0 works fine. Please let me know if you'd like me to upper-pin this in pyproject.toml as well, though I think it shouldn't be needed.

# casadi seems to compile without the newer versions of std::string
add_compile_definitions(_GLIBCXX_USE_CXX11_ABI=0)

set(PYBIND11_FINDPYTHON ON)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will help with WASM builds :)

Comment on lines +96 to +100
execute_process(
COMMAND "${Python_EXECUTABLE}" -c
"import pathlib, casadi; print(pathlib.Path(next(iter(casadi.__path__))).joinpath('cmake'))"
OUTPUT_VARIABLE CASADI_DIR
OUTPUT_STRIP_TRAILING_WHITESPACE)
Copy link
Member Author

@agriyakhetarpal agriyakhetarpal Aug 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea is that the previous version of this command, i.e.,

import os; import sysconfig; print(os.path.join(sysconfig.get_path('purelib'), 'casadi', 'cmake'))

thinks that it finds CasADi in something like /Users/agriyakhetarpal/Desktop/pybammsolvers/venv/lib/python3.12/site-packages/casadi/cmake, which will never exist – this is because it just constructs the path for where CasADi would be placed.

What we needed to do here is to import casadi – this is always guaranteed to work as we require casadi as a build-time dependency, and hence is available in sys.modules during build isolation. I switched to this, and we correctly get the build-time CasADi:

/private/var/folders/b3/2bq1m1_50bs4c7305j8vxcqr0000gn/T/pip-build-env-ihqwpmd7/overlay/lib/python3.12/site-packages/casadi/cmake

Copy link
Member Author

@agriyakhetarpal agriyakhetarpal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @MarcBerliner, I believe that with these changes, we should be able to perform an editable/non-editable installation pretty seamlessly, with the previous explicit specification of the CasADi library and include directories no longer needed. Could you please try this on your macOS machine? Thank you!

@agriyakhetarpal
Copy link
Member Author

The failing unit and integration tests are unrelated; the code compiles correctly. I know the fix as well; I'll push it later today. It is more important for the "Build wheels" job(s) to pass. :)

@agriyakhetarpal
Copy link
Member Author

Argh, it looks like casadi isn't found on the macOS wheels, though I'm pretty sure that this works locally. This looks like a bug in cibuildwheel as the Linux wheel builds are nearly identically configured and they pass.

Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR simplifies the CasADi configuration in CMake by utilizing CasADi's config mode and pybind11's FindPython mode. The changes remove manual path discovery logic and rely on CMake's package configuration system for more robust dependency management.

  • Removes custom CasADi include/library directory discovery code from setup.py
  • Switches to using find_package(casadi CONFIG) in CMake for both Python and non-Python CasADi builds
  • Enables pybind11's FindPython mode and updates build configurations to use pybind11[global]

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
setup.py Removes manual CasADi path discovery and simplifies build configuration
CMakeLists.txt Streamlines CasADi package finding using config mode and enables pybind11 FindPython
.github/workflows/build_wheels.yml Updates build frontend and pybind11 installation to use global mode

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

message(FATAL_ERROR "Could not find CasADi include directory")
endif ()
execute_process(
COMMAND "${Python_EXECUTABLE}" -c
Copy link
Preview

Copilot AI Aug 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable Python_EXECUTABLE is not defined. Since pybind11's FindPython mode is enabled, this should be Python3_EXECUTABLE or you need to ensure Python_EXECUTABLE is properly set from the FindPython module.

Suggested change
COMMAND "${Python_EXECUTABLE}" -c
COMMAND "${Python3_EXECUTABLE}" -c

Copilot uses AI. Check for mistakes.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is incorrect; FindPython defines Python_EXECUTABLE and not Python3_EXECUTABLE.

@agriyakhetarpal
Copy link
Member Author

I guess Copilot is useless at identifying fixes for failing CI jobs :P

@agriyakhetarpal
Copy link
Member Author

It looks like cibuildwheel is still finding Python 3.12, even if we are building for Python 3.10 – and since CasADi is being installed in Python 3.10, it isn't found by Python 3.12 and thus its import fails. I do not know why, for any reason, we pick up Python 3.12 at the time of building 3.10 wheels. :/

@agriyakhetarpal
Copy link
Member Author

Okay, switching to the cibuildwheel action has resolved the issues with the wheel builds. However, there's another problem during the wheel repair process for me to investigate. We recently encountered a similar issue with Python paths in cibuildwheel when it's used outside of the action, specifically with setup-python + pip-installing it, as we were doing in this case (see pypa/cibuildwheel#2566).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant