Skip to content

Commit

Permalink
Merge pull request #2514 from Flamefire/cmake_flags
Browse files Browse the repository at this point in the history
Reduce the number of command line args for CMake configure
  • Loading branch information
branfosj authored Jul 27, 2022
2 parents c5d76b0 + cd45d1c commit dc8d14d
Showing 1 changed file with 54 additions and 29 deletions.
83 changes: 54 additions & 29 deletions easybuild/easyblocks/generic/cmakemake.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,15 @@
"""
import glob
import os
from distutils.version import LooseVersion

from easybuild.easyblocks.generic.configuremake import ConfigureMake
from easybuild.framework.easyconfig import CUSTOM
from easybuild.tools.build_log import print_warning
from easybuild.tools.config import build_option
from easybuild.tools.filetools import change_dir, create_unused_dir, mkdir, which
from easybuild.tools.environment import setvar
from easybuild.tools.modules import get_software_root
from easybuild.tools.modules import get_software_root, get_software_version
from easybuild.tools.run import run_cmd
from easybuild.tools.systemtools import get_shared_lib_ext
from easybuild.tools.utilities import nub
Expand Down Expand Up @@ -117,6 +118,14 @@ def build_type(self):
build_type = 'Debug' if self.toolchain.options.get('debug', None) else 'Release'
return build_type

def prepend_config_opts(self, config_opts):
"""Prepends configure options (-Dkey=value) to configopts ignoring those already set"""
cfg_configopts = self.cfg['configopts']
# All options are of the form '-D<key>=<value>'
new_opts = ' '.join('-D%s=%s' % (key, value) for key, value in config_opts.items()
if '-D%s=' % key not in cfg_configopts)
self.cfg['configopts'] = ' '.join([new_opts, cfg_configopts])

def configure_step(self, srcdir=None, builddir=None):
"""Configure build using cmake"""

Expand Down Expand Up @@ -144,36 +153,38 @@ def configure_step(self, srcdir=None, builddir=None):
install_target_subdir = self.cfg.get('install_target_subdir')
if install_target_subdir:
install_target = os.path.join(install_target, install_target_subdir)
options = ['-DCMAKE_INSTALL_PREFIX=%s' % install_target]
options = {'CMAKE_INSTALL_PREFIX': install_target}

if self.installdir.startswith('/opt') or self.installdir.startswith('/usr'):
# https://cmake.org/cmake/help/latest/module/GNUInstallDirs.html
localstatedir = os.path.join(self.installdir, 'var')
runstatedir = os.path.join(localstatedir, 'run')
sysconfdir = os.path.join(self.installdir, 'etc')
options.append("-DCMAKE_INSTALL_LOCALSTATEDIR=%s" % localstatedir)
options.append("-DCMAKE_INSTALL_RUNSTATEDIR=%s" % runstatedir)
options.append("-DCMAKE_INSTALL_SYSCONFDIR=%s" % sysconfdir)
options['CMAKE_INSTALL_LOCALSTATEDIR'] = localstatedir
options['CMAKE_INSTALL_RUNSTATEDIR'] = runstatedir
options['CMAKE_INSTALL_SYSCONFDIR'] = sysconfdir

if '-DCMAKE_BUILD_TYPE=' in self.cfg['configopts']:
if self.cfg.get('build_type') is not None:
self.log.warning('CMAKE_BUILD_TYPE is set in configopts. Ignoring build_type')
else:
options.append('-DCMAKE_BUILD_TYPE=%s' % self.build_type)
options['CMAKE_BUILD_TYPE'] = self.build_type

# Add -fPIC flag if necessary
if self.toolchain.options['pic']:
options.append('-DCMAKE_POSITION_INDEPENDENT_CODE=ON')
options['CMAKE_POSITION_INDEPENDENT_CODE'] = 'ON'

if self.cfg['generator']:
options.append('-G "%s"' % self.cfg['generator'])
generator = '-G "%s"' % self.cfg['generator']
else:
generator = ''

# pass --sysroot value down to CMake,
# and enable using absolute paths to compiler commands to avoid
# that CMake picks up compiler from sysroot rather than toolchain compiler...
sysroot = build_option('sysroot')
if sysroot:
options.append('-DCMAKE_SYSROOT=%s' % sysroot)
options['CMAKE_SYSROOT'] = sysroot
self.log.info("Using absolute path to compiler commands because of alterate sysroot %s", sysroot)
self.cfg['abs_path_compilers'] = True

Expand All @@ -189,32 +200,49 @@ def configure_step(self, srcdir=None, builddir=None):
print_warning('Ignoring BUILD_SHARED_LIBS is set in configopts because build_shared_libs is set')
self.cfg.update('configopts', '-DBUILD_SHARED_LIBS=%s' % ('ON' if build_shared_libs else 'OFF'))

env_to_options = {
'CC': 'CMAKE_C_COMPILER',
'CFLAGS': 'CMAKE_C_FLAGS',
'CXX': 'CMAKE_CXX_COMPILER',
'CXXFLAGS': 'CMAKE_CXX_FLAGS',
'F90': 'CMAKE_Fortran_COMPILER',
'FFLAGS': 'CMAKE_Fortran_FLAGS',
}
# If the cache does not exist CMake reads the environment variables
cache_exists = os.path.exists('CMakeCache.txt')
env_to_options = dict()

# Setting compilers is not required unless we want absolute paths
if self.cfg.get('abs_path_compilers', False) or cache_exists:
env_to_options.update({
'CC': 'CMAKE_C_COMPILER',
'CXX': 'CMAKE_CXX_COMPILER',
'F90': 'CMAKE_Fortran_COMPILER',
})
else:
# Set the variable which CMake uses to init the compiler using F90 for backward compatibility
fc = os.getenv('F90')
if fc:
setvar('FC', fc)

# Flags are read from environment variables already since at least CMake 2.8.0
if LooseVersion(get_software_version('CMake')) < LooseVersion('2.8.0') or cache_exists:
env_to_options.update({
'CFLAGS': 'CMAKE_C_FLAGS',
'CXXFLAGS': 'CMAKE_CXX_FLAGS',
'FFLAGS': 'CMAKE_Fortran_FLAGS',
})

for env_name, option in env_to_options.items():
value = os.getenv(env_name)
if value is not None:
if option.endswith('_COMPILER') and self.cfg.get('abs_path_compilers', False):
value = which(value)
self.log.info("Using absolute path to compiler command: %s", value)
options.append("-D%s='%s'" % (option, value))
options[option] = value

if build_option('rpath'):
# instruct CMake not to fiddle with RPATH when --rpath is used, since it will undo stuff on install...
# https://github.com/LLNL/spack/blob/0f6a5cd38538e8969d11bd2167f11060b1f53b43/lib/spack/spack/build_environment.py#L416
options.append('-DCMAKE_SKIP_RPATH=ON')
options['CMAKE_SKIP_RPATH'] = 'ON'

# show what CMake is doing by default
options.append('-DCMAKE_VERBOSE_MAKEFILE=ON')
options['CMAKE_VERBOSE_MAKEFILE'] = 'ON'

# disable CMake user package repository
options.append('-DCMAKE_FIND_USE_PACKAGE_REGISTRY=FALSE')
options['CMAKE_FIND_USE_PACKAGE_REGISTRY'] = 'OFF'

if not self.cfg.get('allow_system_boost', False):
boost_root = get_software_root('Boost')
Expand All @@ -226,23 +254,20 @@ def configure_step(self, srcdir=None, builddir=None):
if len(cmake_files) > 1 and 'libboost_system-variant-shared.cmake' in cmake_files:
# disable search for Boost CMake package configuration files when conflicting variant configs
# are present (builds using the old EasyBlock)
options.append('-DBoost_NO_BOOST_CMAKE=ON')
options['Boost_NO_BOOST_CMAKE'] = 'ON'

# Don't pick up on system Boost if Boost is included as dependency
# - specify Boost location via -DBOOST_ROOT
# - instruct CMake to not search for Boost headers/libraries in other places
options.extend([
'-DBOOST_ROOT=%s' % boost_root,
'-DBoost_NO_SYSTEM_PATHS=ON',
])

options_string = ' '.join(options)
options['BOOST_ROOT'] = boost_root
options['Boost_NO_SYSTEM_PATHS'] = 'ON'

if self.cfg.get('configure_cmd') == DEFAULT_CONFIGURE_CMD:
self.prepend_config_opts(options)
command = ' '.join([
self.cfg['preconfigopts'],
DEFAULT_CONFIGURE_CMD,
options_string,
generator,
self.cfg['configopts'],
srcdir])
else:
Expand Down

0 comments on commit dc8d14d

Please sign in to comment.