Skip to content

Commit 49b7cba

Browse files
authored
Accept CMake args as env vars for Python setup (#3123)
1 parent 23d73be commit 49b7cba

File tree

2 files changed

+29
-22
lines changed

2 files changed

+29
-22
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Doing so will make some things run faster.
2323

2424
Finally, Visual Studio users should usually do everything in 64bit mode. By default Visual Studio is 32bit, both in its outputs and its own execution, so you have to explicitly tell it to use 64bits. Since it's not the 1990s anymore you probably want to use 64bits. Do that with a cmake invocation like this:
2525
```bash
26-
cmake .. -G "Visual Studio 14 2015 Win64" -T host=x64
26+
cmake .. -G "Visual Studio 14 2015 Win64" -T host=x64
2727
```
2828

2929
## Compiling your own C++ programs that use dlib
@@ -48,6 +48,10 @@ cd dlib
4848
pip install .
4949
```
5050

51+
It's possible to change build settings by passing parameters to `setup.py` or `DLIB_*` environment variables.
52+
For example, setting the environment variable `DLIB_NO_GUI_SUPPORT` to `ON` will add the cmake option
53+
`-DDLIB_NO_GUI_SUPPORT=ON`.
54+
5155

5256
## Running the unit test suite
5357

@@ -69,4 +73,3 @@ This library is licensed under the Boost Software License, which can be found in
6973
## dlib sponsors
7074

7175
This research is based in part upon work supported by the Office of the Director of National Intelligence (ODNI), Intelligence Advanced Research Projects Activity (IARPA) under contract number 2014-14071600010. The views and conclusions contained herein are those of the authors and should not be interpreted as necessarily representing the official policies or endorsements, either expressed or implied, of ODNI, IARPA, or the U.S. Government.
72-

setup.py

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
To build and install:
1212
python setup.py install
1313
To upload the source distribution to PyPi
14-
python setup.py sdist
14+
python setup.py sdist
1515
twine upload dist/dlib-*.tar.gz
1616
To exclude certain options in the cmake config use --no:
1717
for example:
@@ -25,21 +25,21 @@
2525
--set: set arbitrary cmake options e.g. --set CUDA_HOST_COMPILER=/usr/bin/gcc-6.4.0
2626
passes -DCUDA_HOST_COMPILER=/usr/bin/gcc-6.4.0 to CMake.
2727
"""
28+
import errno
29+
import logging
30+
import multiprocessing
2831
import os
32+
import platform
2933
import re
30-
import sys
31-
import errno
32-
import stat
3334
import shutil
34-
import platform
35+
import stat
3536
import subprocess
36-
import multiprocessing
37+
import sys
3738
from math import floor
3839

3940
from packaging.version import Version, parse as parse_version
4041
from setuptools import find_packages, setup, Extension
4142
from setuptools.command.build_ext import build_ext
42-
import logging
4343

4444
logging.basicConfig(level=logging.INFO)
4545
log = logging.getLogger("setup")
@@ -89,8 +89,6 @@ def get_extra_cmake_options():
8989

9090
return _cmake_extra_options, _clean_build_folder
9191

92-
cmake_extra_options,clean_build_folder = get_extra_cmake_options()
93-
9492

9593
class CMakeExtension(Extension):
9694
def __init__(self, name, sourcedir=''):
@@ -143,7 +141,7 @@ def get_cmake_version(self):
143141
packager delete it and install an official cmake.
144142
145143
More generally, cmake is not installed if when you open a terminal window
146-
and type
144+
and type
147145
cmake --version
148146
you get an error. So you can use that as a very basic test to see if you
149147
have cmake installed. That is, if cmake --version doesn't run from the
@@ -157,7 +155,7 @@ def get_cmake_version(self):
157155
================================================================================
158156
================================================================================
159157
================================================================================
160-
""")
158+
""")
161159
sys.exit(1)
162160
return re.search(r'version\s*([\d.]+)', out.decode()).group(1)
163161

@@ -172,13 +170,19 @@ def run(self):
172170
self.build_extension(ext)
173171

174172
def build_extension(self, ext):
173+
# Build CMake args based on default settings, environment variables and CLI parameters
175174
extdir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name)))
176-
177-
cmake_args = ['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=' + extdir,
178-
'-DPYTHON_EXECUTABLE=' + sys.executable,
179-
'-DDLIB_USE_FFMPEG=OFF',]
180-
181-
cmake_args += cmake_extra_options
175+
cmake_extra_options, clean_build_folder = get_extra_cmake_options()
176+
cmake_args_dict = {
177+
'CMAKE_LIBRARY_OUTPUT_DIRECTORY': extdir,
178+
'DLIB_USE_FFMPEG': 'OFF',
179+
'PYTHON_EXECUTABLE': sys.executable,
180+
}
181+
for key, value in os.environ.items():
182+
if key.startswith("DLIB_"):
183+
cmake_args_dict[key] = value
184+
cmake_args = ['-D' + key + '=' + str(value) for key, value in cmake_args_dict.items()]
185+
cmake_args += cmake_extra_options
182186

183187
cfg = 'Debug' if self.debug else 'Release'
184188
build_args = ['--config', cfg]
@@ -188,7 +192,7 @@ def build_extension(self, ext):
188192
if sys.maxsize > 2**32:
189193
cmake_args += ['-A', 'x64']
190194
# Do a parallel build
191-
build_args += ['--', '/m']
195+
build_args += ['--', '/m']
192196
else:
193197
cmake_args += ['-DCMAKE_BUILD_TYPE=' + cfg]
194198
# Do a parallel build
@@ -220,9 +224,9 @@ def num_available_cpu_cores(ram_per_build_process_in_gb):
220224
elif 'CMAKE_BUILD_PARALLEL_LEVEL' in os.environ and os.environ['CMAKE_BUILD_PARALLEL_LEVEL'].isnumeric():
221225
return int(os.environ['CMAKE_BUILD_PARALLEL_LEVEL'])
222226
try:
223-
mem_bytes = os.sysconf('SC_PAGE_SIZE') * os.sysconf('SC_PHYS_PAGES')
227+
mem_bytes = os.sysconf('SC_PAGE_SIZE') * os.sysconf('SC_PHYS_PAGES')
224228
mem_gib = mem_bytes/(1024.**3)
225-
num_cores = multiprocessing.cpu_count()
229+
num_cores = multiprocessing.cpu_count()
226230
# make sure we have enough ram for each build process.
227231
mem_cores = int(floor(mem_gib/float(ram_per_build_process_in_gb)+0.5));
228232
# We are limited either by RAM or CPU cores. So pick the limiting amount

0 commit comments

Comments
 (0)