From 476693606dba71260b85319f45abb49ac6d134b9 Mon Sep 17 00:00:00 2001
From: Robin De Schepper <robin.deschepper93@gmail.com>
Date: Thu, 7 Oct 2021 14:11:27 +0200
Subject: [PATCH] Add Makejobs arg to setup.py (#1673)

---
 doc/install/python.rst |  5 +++--
 setup.py               | 20 +++++++++++++++++---
 2 files changed, 20 insertions(+), 5 deletions(-)

diff --git a/doc/install/python.rst b/doc/install/python.rst
index 5e733552..766cdab8 100644
--- a/doc/install/python.rst
+++ b/doc/install/python.rst
@@ -21,7 +21,7 @@ The easiest way to get Arbor is with
     pip3 install arbor
 
 .. note::
-    For other platforms, `pip` will build Arbor from source. 
+    For other platforms, `pip` will build Arbor from source.
     You will need to have some development packages installed in order to build Arbor this way.
 
     * Ubuntu/Debian: `git cmake gcc python3-dev python3-pip libxml2-dev`
@@ -84,6 +84,7 @@ The following optional flags can be used to configure the installation:
   See :ref:`install-architecture` for details.
 * ``--arch``: CPU micro-architecture to target. The advised default is ``native``.
   See `here <https://gcc.gnu.org/onlinedocs/gcc/x86-Options.html>`_ for a full list of options.
+* ``--makejobs``: Specify the amount of jobs to ``make`` the project with for faster build times on multicore systems. By default set to ``2``.
 
 **Vanilla install** with no additional features enabled:
 
@@ -134,7 +135,7 @@ The following optional flags can be used to configure the installation:
     it with the flags you need.
 
 .. Note::
-    Detailed instructions on how to install using CMake are in the 
+    Detailed instructions on how to install using CMake are in the
     :ref:`Python configuration <install-python>` section of the :ref:`installation guide <in_build_install>`.
     CMake is recommended if you need more control over compilation and installation, plan to use Arbor with C++,
     or if you are integrating with package managers such as Spack and EasyBuild.
diff --git a/setup.py b/setup.py
index 71b0e0fa..094837d4 100644
--- a/setup.py
+++ b/setup.py
@@ -27,7 +27,8 @@ class CL_opt:
                                'vec': False,
                                'arch': 'none',
                                'neuroml': True,
-                               'bundled': True}
+                               'bundled': True,
+                               'makejobs': 2}
 
     def settings(self):
         return CL_opt.instance
@@ -43,7 +44,8 @@ user_options_ = [
         ('vec',   None, 'enable vectorization'),
         ('arch=', None, 'cpu architecture, e.g. haswell, skylake, armv8.2-a+sve, znver2 (default native).'),
         ('neuroml', None, 'enable parsing neuroml morphologies in Arbor (requires libxml)'),
-        ('sysdeps', None, 'don\'t use bundled 3rd party C++ dependencies (pybind11 and json). This flag forces use of dependencies installed on the system.')
+        ('sysdeps', None, 'don\'t use bundled 3rd party C++ dependencies (pybind11 and json). This flag forces use of dependencies installed on the system.'),
+        ('makejobs=', None, 'the amount of jobs to run `make` with.')
     ]
 
 # VERSION is in the same path as setup.py
@@ -98,9 +100,18 @@ class _command_template:
         self.vec  = None
         self.neuroml = None
         self.sysdeps = None
+        self.makejobs = 2
 
     def finalize_options(self):
         super().finalize_options()
+        try:
+            self.makejobs = int(self.makejobs)
+        except ValueError:
+            err = True
+        else:
+            err = False
+        if err or self.makejobs < 1:
+            raise AssertionError('makejobs must be a strictly positive integer')
 
     def run(self):
         # The options are stored in global variables:
@@ -118,6 +129,9 @@ class _command_template:
         #   bundled : use bundled/git-submoduled 3rd party libraries.
         #             By default use bundled libs.
         opt['bundled'] = self.sysdeps is None
+        #   makejobs : specify amount of jobs.
+        #              By default 2.
+        opt['makejobs'] = int(self.makejobs)
 
         super().run()
 
@@ -169,7 +183,7 @@ class cmake_build(build_ext):
         build_args = ['--config', 'Release']
 
         # Assuming Makefiles
-        build_args += ['--', '-j2']
+        build_args += ['--', f'-j{opt["makejobs"]}']
 
         env = os.environ.copy()
         env['CXXFLAGS'] = '{}'.format(env.get('CXXFLAGS', ''))
-- 
GitLab