Skip to content

Commit e52a2e8

Browse files
authored
Merge pull request #81 from bluescarni/pr/build_improv
Modernise the CMake support
2 parents ad7da2f + 7173ce2 commit e52a2e8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+272
-459
lines changed

CMakeLists.txt

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
cmake_minimum_required(VERSION 3.8.0)
2-
# NOTE: policy for using the CheckIPOSupported module:
3-
# https://cmake.org/cmake/help/latest/policy/CMP0069.html
4-
cmake_policy(SET CMP0069 NEW)
1+
# NOTE: FindPython3 supported since CMake 3.12.0, but we use
2+
# options that appear in 3.17.
3+
cmake_minimum_required(VERSION 3.17.0)
54

65
# Set default build type to "Release".
76
# NOTE: this should be done before the project command since the latter can set
@@ -19,34 +18,12 @@ list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake" "${CMAKE_CURRE
1918
message(STATUS "System name: ${CMAKE_SYSTEM_NAME}")
2019
message(STATUS "pygmo version: ${pygmo_VERSION}")
2120

22-
option(PYGMO_ENABLE_IPO "Enable IPO (requires CMake >= 3.9 and compiler support)." OFF)
21+
option(PYGMO_ENABLE_IPO "Enable IPO (requires compiler support)." OFF)
2322
mark_as_advanced(PYGMO_ENABLE_IPO)
2423

2524
# Run the YACMA compiler setup.
2625
include(YACMACompilerLinkerSettings)
2726

28-
# NOTE: on Unix systems, the correct library installation path
29-
# could be something other than just "lib", such as "lib64",
30-
# "lib32", etc., depending on platform/configuration. Apparently,
31-
# CMake provides this information via the GNUInstallDirs module.
32-
# Let's enable this for now on all Unixes except OSX.
33-
# NOTE: potentially, this could be applicable to Cygwin as well.
34-
#
35-
# https://cmake.org/cmake/help/v3.15/module/GNUInstallDirs.html
36-
# https://cmake.org/pipermail/cmake/2013-July/055375.html
37-
if(UNIX AND NOT APPLE)
38-
include(GNUInstallDirs)
39-
set(_PYGMO_INSTALL_LIBDIR_DEFAULT "${CMAKE_INSTALL_LIBDIR}")
40-
else()
41-
set(_PYGMO_INSTALL_LIBDIR_DEFAULT "lib")
42-
endif()
43-
if(NOT PYGMO_INSTALL_LIBDIR)
44-
set(PYGMO_INSTALL_LIBDIR "${_PYGMO_INSTALL_LIBDIR_DEFAULT}" CACHE STRING
45-
"Library installation directory." FORCE)
46-
endif()
47-
mark_as_advanced(PYGMO_INSTALL_LIBDIR)
48-
message(STATUS "Library installation directory: ${PYGMO_INSTALL_LIBDIR}")
49-
5027
# Assemble the flags.
5128
set(PYGMO_CXX_FLAGS_DEBUG ${YACMA_CXX_FLAGS} ${YACMA_CXX_FLAGS_DEBUG})
5229
set(PYGMO_CXX_FLAGS_RELEASE ${YACMA_CXX_FLAGS})
@@ -160,16 +137,15 @@ if(${pagmo_VERSION} VERSION_LESS ${_PYGMO_MIN_PAGMO_VERSION})
160137
endif()
161138

162139
# python.
163-
include(YACMAPythonSetup)
140+
find_package(Python3 REQUIRED COMPONENTS Interpreter Development)
141+
message(STATUS "Python3 interpreter: ${Python3_EXECUTABLE}")
142+
message(STATUS "Python3 installation directory: ${Python3_SITEARCH}")
143+
set(PYGMO_INSTALL_PATH "" CACHE STRING "pygmo module installation path")
144+
mark_as_advanced(PYGMO_INSTALL_PATH)
164145

165146
# Boost setup.
166147
include(PygmoFindBoost)
167148

168-
# python version check.
169-
if(${PYTHON_VERSION_MAJOR} LESS 3 OR (${PYTHON_VERSION_MAJOR} EQUAL 3 AND ${PYTHON_VERSION_MINOR} LESS 4))
170-
message(FATAL_ERROR "Minimum supported python version is 3.4.")
171-
endif()
172-
173149
# pybind11.
174150
find_package(pybind11 REQUIRED)
175151
if(${pybind11_VERSION} VERSION_LESS "2.6")

doc/changelog.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Changelog
99
Changes
1010
~~~~~~~
1111

12+
- pygmo now requires CMake >= 3.17
13+
(`#81 <https://github.com/esa/pygmo2/pull/81>`__).
1214
- Various internal changes to the pickling implementation
1315
(`#79 <https://github.com/esa/pygmo2/pull/79>`__).
1416
- pygmo now requires pagmo >= 2.18

doc/conf.py.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
# -- Project information -----------------------------------------------------
1919

2020
project = 'pygmo'
21-
copyright = '2020, pagmo development team'
21+
copyright = '2021, pagmo development team'
2222
author = 'pagmo development team'
2323

2424
# The version info for the project you're documenting, acts as replacement for

doc/install.rst

Lines changed: 12 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ In order to install pygmo from source, you will need:
126126
* `pybind11 <https://github.com/pybind/pybind11>`__ (version >= 2.6),
127127
* the `pagmo C++ library <https://esa.github.io/pagmo2/>`__,
128128
* the `Boost libraries <https://www.boost.org/>`__,
129-
* `CMake <https://cmake.org/>`__, version 3.8 or later.
129+
* `CMake <https://cmake.org/>`__, version 3.17 or later.
130130

131131
After making sure the dependencies are installed on your system, you can
132132
download the pygmo source code from the
@@ -158,51 +158,24 @@ such as:
158158

159159
* ``CMAKE_BUILD_TYPE``: the build type (``Release``, ``Debug``, etc.),
160160
defaults to ``Release``.
161-
* ``CMAKE_INSTALL_PREFIX``: the path into which pygmo will be installed
162-
(e.g., this defaults to ``/usr/local`` on Unix-like platforms).
163161
* ``CMAKE_PREFIX_PATH``: additional paths that will be searched by CMake
164162
when looking for dependencies.
163+
* ``PYGMO_INSTALL_PATH``: the path into which the pygmo module
164+
will be installed. If left empty (the default), pygmo will be installed
165+
in the global modules directory of your Python installation.
166+
* ``PYGMO_ENABLE_IPO``: set this flag to ``ON`` to compile pygmo
167+
with link-time optimisations. Requires compiler support,
168+
defaults to ``OFF``.
165169

166170
Please consult `CMake's documentation <https://cmake.org/cmake/help/latest/>`_
167171
for more details about CMake's variables and options.
168172

169-
A critical setting for a pygmo installation is the
170-
value of the ``CMAKE_INSTALL_PREFIX`` variable. The pygmo
171-
build system will attempt to construct an appropriate
172-
installation path for the Python module by combining
173-
the value of ``CMAKE_INSTALL_PREFIX`` with the directory
174-
paths of the Python installation in use in a platform-dependent
175-
manner.
176-
177-
For instance, on a typical Linux installation
178-
of Python 3.6,
179-
``CMAKE_INSTALL_PREFIX`` will be set by default to
180-
``/usr/local``, and the pygmo build system will
181-
append ``lib/python3.6/site-packages`` to the install prefix.
182-
Thus, the overall install path for the pygmo module will be
183-
``/usr/local/lib/python3.6/site-packages``. If you want
184-
to avoid system-wide installations (which require root
185-
privileges), on Unix-like system it is possible to set
186-
the ``CMAKE_INSTALL_PREFIX`` variable to the directory
187-
``.local`` in your ``$HOME`` (e.g., ``/home/username/.local``).
188-
The pygmo install path will then be, in this case,
189-
``/home/username/.local/lib/python3.6/site-packages``,
190-
a path which is normally recognised by Python installations
191-
without the need to modify the ``PYTHONPATH`` variable.
192-
If you install pygmo in non-standard prefixes, you may
193-
have to tweak your Python installation in order for the
194-
Python interpreter to find the pygmo module.
195-
196-
A typical CMake invocation for pygmo may then
197-
look something like this:
173+
The ``PYGMO_INSTALL_PATH`` option is particularly important. If you
174+
want to install pygmo locally instead of globally (which is in general
175+
a good idea), you can set this variable to the output of
176+
``python -m site --user-site``.
198177

199-
.. code-block:: console
200-
201-
$ cmake ../ -DCMAKE_INSTALL_PREFIX=~/.local
202-
203-
That is, we will be installing pygmo into our home
204-
directory into the ``.local``
205-
subdirectory. If CMake runs without errors, we can then proceed to actually
178+
After configuring the build with CMake, we can then proceed to actually
206179
building pygmo:
207180

208181
.. code-block:: console

pygmo/CMakeLists.txt

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ foreach(PYGMO_PYTHON_FILE ${PYGMO_PYTHON_FILES})
5252
endforeach()
5353

5454
# Core module.
55-
YACMA_PYTHON_MODULE(core
55+
Python3_add_library(core MODULE WITH_SOABI
5656
core.cpp
5757
common_utils.cpp
5858
common_base.cpp
@@ -95,33 +95,39 @@ target_compile_features(core PRIVATE cxx_std_17)
9595
# Enforce vanilla C++ when compiling.
9696
set_property(TARGET core PROPERTY CXX_EXTENSIONS NO)
9797

98-
if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.9.0")
99-
if (PYGMO_ENABLE_IPO)
100-
include(CheckIPOSupported)
101-
check_ipo_supported(RESULT _PYGMO_IPO_RESULT OUTPUT _PYGMO_IPO_OUTPUT)
102-
if (_PYGMO_IPO_RESULT)
103-
message(STATUS "IPO requested and supported, enabling.")
104-
set_property(TARGET core PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
105-
else()
106-
message(STATUS "IPO requested, but it is not supported by the compiler:\n${_PYGMO_IPO_OUTPUT}")
107-
endif()
108-
unset(_PYGMO_IPO_RESULT)
109-
unset(_PYGMO_IPO_OUTPUT)
98+
if (PYGMO_ENABLE_IPO)
99+
include(CheckIPOSupported)
100+
check_ipo_supported(RESULT _PYGMO_IPO_RESULT OUTPUT _PYGMO_IPO_OUTPUT)
101+
if (_PYGMO_IPO_RESULT)
102+
message(STATUS "IPO requested and supported, enabling.")
103+
set_property(TARGET core PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
104+
else()
105+
message(STATUS "IPO requested, but it is not supported by the compiler:\n${_PYGMO_IPO_OUTPUT}")
110106
endif()
107+
unset(_PYGMO_IPO_RESULT)
108+
unset(_PYGMO_IPO_OUTPUT)
111109
endif()
112110

113-
# Setup the installation path.
114-
set(PYGMO_INSTALL_PATH "${YACMA_PYTHON_MODULES_INSTALL_PATH}/pygmo")
111+
# Installation setup.
112+
if(PYGMO_INSTALL_PATH STREQUAL "")
113+
message(STATUS "pygmo will be installed in the default location: ${Python3_SITEARCH}")
114+
set(_PYGMO_INSTALL_DIR "${Python3_SITEARCH}/pygmo")
115+
else()
116+
message(STATUS "pygmo will be installed in the custom location: ${PYGMO_INSTALL_PATH}")
117+
set(_PYGMO_INSTALL_DIR "${PYGMO_INSTALL_PATH}/pygmo")
118+
endif()
115119

116120
# Add submodules directories
117121
add_subdirectory(plotting)
118122

119123
# Install the core module.
120124
install(TARGETS core
121-
RUNTIME DESTINATION ${PYGMO_INSTALL_PATH}
122-
LIBRARY DESTINATION ${PYGMO_INSTALL_PATH}
125+
RUNTIME DESTINATION ${_PYGMO_INSTALL_DIR}
126+
LIBRARY DESTINATION ${_PYGMO_INSTALL_DIR}
123127
)
124128

125129
# Install the Python files.
126130
install(FILES ${PYGMO_PYTHON_FILES} "${CMAKE_CURRENT_BINARY_DIR}/_version.py"
127-
DESTINATION ${PYGMO_INSTALL_PATH})
131+
DESTINATION ${_PYGMO_INSTALL_DIR})
132+
133+
unset(_PYGMO_INSTALL_DIR)

pygmo/__init__.py

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2020 PaGMO development team
1+
# Copyright 2020, 2021 PaGMO development team
22
#
33
# This file is part of the pygmo library.
44
#
@@ -11,7 +11,6 @@
1111
from ._version import __version__
1212
# We import the sub-modules into the root namespace
1313
from .core import *
14-
from .core import _pagmo_version_major, _pagmo_version_minor, _pagmo_version_patch
1514
from .plotting import *
1615
from ._py_islands import *
1716
from ._py_problems import *
@@ -656,38 +655,39 @@ def _archi_set_topology(self, t):
656655
setattr(archipelago, "set_topology", _archi_set_topology)
657656

658657

659-
if _pagmo_version_major > 2 or (_pagmo_version_major == 2 and _pagmo_version_minor >= 15):
660-
# Override of the free_form constructor.
661-
__original_free_form_init = free_form.__init__
662-
663-
def _free_form_init(self, t=None):
664-
"""
665-
Args:
666-
t: the object that will be used for construction
667-
668-
Raises:
669-
ValueError: if the edges of the input :class:`networkx.DiGraph`
670-
do not all have a ``weight`` attribute, or if any edge weight
671-
is outside the :math:`\\left[ 0, 1 \\right]` range
672-
unspecified: any exception thrown by :func:`pygmo.topology.to_networkx()`, or by
673-
the construction of a :class:`~pygmo.topology` from a UDT
674-
675-
"""
676-
import networkx as nx
677-
678-
if t is None:
679-
# Default ctor.
680-
__original_free_form_init(self)
681-
elif type(t) == topology or isinstance(t, nx.DiGraph):
682-
# Ctors from topology or DiGraph are exposed
683-
# directly.
684-
__original_free_form_init(self, t)
685-
else:
686-
# If t is neither a topology, nor a DiGraph,
687-
# assume that it is a UDT.
688-
__original_free_form_init(self, topology(t))
689-
690-
setattr(free_form, "__init__", _free_form_init)
658+
# Override of the free_form constructor.
659+
__original_free_form_init = free_form.__init__
660+
661+
662+
def _free_form_init(self, t=None):
663+
"""
664+
Args:
665+
t: the object that will be used for construction
666+
667+
Raises:
668+
ValueError: if the edges of the input :class:`networkx.DiGraph`
669+
do not all have a ``weight`` attribute, or if any edge weight
670+
is outside the :math:`\\left[ 0, 1 \\right]` range
671+
unspecified: any exception thrown by :func:`pygmo.topology.to_networkx()`, or by
672+
the construction of a :class:`~pygmo.topology` from a UDT
673+
674+
"""
675+
import networkx as nx
676+
677+
if t is None:
678+
# Default ctor.
679+
__original_free_form_init(self)
680+
elif type(t) == topology or isinstance(t, nx.DiGraph):
681+
# Ctors from topology or DiGraph are exposed
682+
# directly.
683+
__original_free_form_init(self, t)
684+
else:
685+
# If t is neither a topology, nor a DiGraph,
686+
# assume that it is a UDT.
687+
__original_free_form_init(self, topology(t))
688+
689+
690+
setattr(free_form, "__init__", _free_form_init)
691691

692692

693693
def set_serialization_backend(name):

pygmo/_algorithm_test.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2020 PaGMO development team
1+
# Copyright 2020, 2021 PaGMO development team
22
#
33
# This file is part of the pygmo library.
44
#
@@ -41,7 +41,8 @@ def run_basic_tests(self):
4141

4242
# First a few non-algos.
4343
self.assertRaises(NotImplementedError, lambda: algorithm(1))
44-
self.assertRaises(NotImplementedError, lambda: algorithm("hello world"))
44+
self.assertRaises(NotImplementedError,
45+
lambda: algorithm("hello world"))
4546
self.assertRaises(NotImplementedError, lambda: algorithm([]))
4647
self.assertRaises(TypeError, lambda: algorithm(int))
4748
# Some algorithms missing methods, wrong arity, etc.
@@ -549,7 +550,6 @@ def run_scipy_wrapper_tests(self):
549550
scp.get_name()
550551
scp.set_verbosity(0)
551552

552-
553553
# testing constrained problem on incompatible methods
554554
prob = problem(luksan_vlcek1(10))
555555
pop = population(prob=prob, size=1, seed=0)
@@ -576,7 +576,8 @@ def run_scipy_wrapper_tests(self):
576576
# testing invalid selection policy
577577
prob = problem(luksan_vlcek1(10))
578578
pop = population(prob=prob, size=10, seed=0)
579-
scp = algorithm(scipy_optimize(selection = s_policy(select_best(rate=2))))
579+
scp = algorithm(scipy_optimize(
580+
selection=s_policy(select_best(rate=2))))
580581
self.assertRaises(ValueError, lambda: scp.evolve(pop))
581582

582583
# testing callback
@@ -590,7 +591,7 @@ def increment(self, *args, **kwargs):
590591
prob = problem(luksan_vlcek1(10))
591592
pop = population(prob=prob, size=10, seed=0)
592593
counter = callback_counter()
593-
scp = algorithm(scipy_optimize(callback = counter.increment))
594+
scp = algorithm(scipy_optimize(callback=counter.increment))
594595
scp.evolve(pop)
595596
self.assertTrue(counter.value > 0)
596597

@@ -620,7 +621,8 @@ def increment(self, *args, **kwargs):
620621

621622
# testing hessian wrapper generator
622623
prob = problem(rastrigin(10))
623-
f = scipy_optimize._fitness_wrapper(prob)._generate_hessian_sparsity_wrapper(0)
624+
f = scipy_optimize._fitness_wrapper(
625+
prob)._generate_hessian_sparsity_wrapper(0)
624626
hessian = f(array([0] * prob.get_nx()))
625627
self.assertEqual(len(hessian), prob.get_nx())
626628
self.assertEqual(len(hessian[0]), prob.get_nx())

pygmo/_bfe_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2020 PaGMO development team
1+
# Copyright 2020, 2021 PaGMO development team
22
#
33
# This file is part of the pygmo library.
44
#

pygmo/_check_deps.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2020 PaGMO development team
1+
# Copyright 2020, 2021 PaGMO development team
22
#
33
# This file is part of the pygmo library.
44
#

pygmo/_ipyparallel_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2020 PaGMO development team
1+
# Copyright 2020, 2021 PaGMO development team
22
#
33
# This file is part of the pygmo library.
44
#

0 commit comments

Comments
 (0)