Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

let CMakeMake easyblock also set Python_EXECUTABLE option, as well as Python3_EXECUTABLE and Python2_EXECUTABLE derivatives (when appropriate) #3463

Merged
merged 12 commits into from
Oct 11, 2024
32 changes: 32 additions & 0 deletions easybuild/easyblocks/generic/cmakemake.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,20 @@ def configure_step(self, srcdir=None, builddir=None):
# disable CMake user package repository
options['CMAKE_FIND_USE_PACKAGE_REGISTRY'] = 'OFF'

# ensure CMake uses EB python, not system or virtualenv python
python_root = get_software_root('Python')
if python_root:
python_version = LooseVersion(get_software_version('Python'))
python_exe = os.path.join(python_root, 'bin', 'python')
# This is required for (deprecated) `find_package(PythonInterp ...)`
options['PYTHON_EXECUTABLE'] = python_exe
# Ensure that both `find_package(Python) and find_package(Python2/3)` work as intended
options['Python_EXECUTABLE'] = python_exe
if python_version >= "3":
options['Python3_EXECUTABLE'] = python_exe
else:
options['Python2_EXECUTABLE'] = python_exe

if not self.cfg.get('allow_system_boost', False):
boost_root = get_software_root('Boost')
if boost_root:
Expand All @@ -302,6 +316,8 @@ def configure_step(self, srcdir=None, builddir=None):
options['BOOST_ROOT'] = boost_root
options['Boost_NO_SYSTEM_PATHS'] = 'ON'

self.cmake_options = options

if self.cfg.get('configure_cmd') == DEFAULT_CONFIGURE_CMD:
self.prepend_config_opts(options)
command = ' '.join([
Expand All @@ -320,6 +336,22 @@ def configure_step(self, srcdir=None, builddir=None):

return out

def set_cmake_env_vars_python(self):
Copy link
Member

Choose a reason for hiding this comment

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

I'm a bit confused by this one, what's the plan with this?

As is, it can only be used in easyblocks that derive from CMakeMake?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes I would make 2 free functions out of that:

  1. Return a configure string with -DPython*_EXECUTABLE=..., maybe optionally only as a dict
  2. Return these env variables

The first function is the safe way for every situation we can pass CMake variables. We can use that to enhance the GROMACS easyblock to move the CMake config options out from the EasyConfig to the easyblock and add those options.

The 2nd function can be used whenever we cannot use the first. It might still pick up a python from a virtualenv when Python*_FIND_STRATEGY isn't set to LOCATION ("older" codes default this to LOCATION if unset) but at least we did a best effort to avoid that which might work for most cases.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added the 2 separate functions to return the options as a dict or as a string:

  • get_cmake_python_config_dict
  • get_cmake_python_config_str

and changed the name of the function to set the env vars to set_cmake_python_env_hints (9a31f79)

There might be an argument of making that function a context manager to be used with with

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since we are taking the convenience function route, i am wondering if we should (probably another PR) also change the detection of all other options into separate/grouped convenience functions to make them reusable by other easyblocks than need to implement their own more complex configure step (eg the new LLVM one)

"""Convenience function to set CMake hints for FindPython[_2/3] as environment variables.
Needed to avoid wrong Python being picked up by CMake when not called directly by EasyBuild but as step in a
build and no option is provided to set custom CMake variables.
"""
if LooseVersion(self.cmake_version) < '3.12':
raise EasyBuildError("Setting Python hints for CMake requires CMake version 3.12 or newer")
python_root = get_software_root('Python')
if python_root:
python_version = LooseVersion(get_software_version('Python'))
setvar('Python_ROOT_DIR', python_root)
if python_version >= "3":
setvar('Python3_ROOT_DIR', python_root)
else:
setvar('Python2_ROOT_DIR', python_root)

def test_step(self):
"""CMake specific test setup"""
# When using ctest for tests (default) then show verbose output if a test fails
Expand Down
Loading