From 6a09deef3c5617d590d5d14285fd55a255a72c4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Justen=20=28=40turicas=29?= Date: Thu, 27 Nov 2025 01:42:51 -0300 Subject: [PATCH] Accept CMake args as env vars for Python setup --- README.md | 7 +++++-- setup.py | 44 ++++++++++++++++++++++++-------------------- 2 files changed, 29 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 8b6ba5364e..51b2aeeecd 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ Doing so will make some things run faster. 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: ```bash -cmake .. -G "Visual Studio 14 2015 Win64" -T host=x64 +cmake .. -G "Visual Studio 14 2015 Win64" -T host=x64 ``` ## Compiling your own C++ programs that use dlib @@ -48,6 +48,10 @@ cd dlib pip install . ``` +It's possible to change build settings by passing parameters to `setup.py` or `DLIB_*` environment variables. +For example, setting the environment variable `DLIB_NO_GUI_SUPPORT` to `ON` will add the cmake option +`-DDLIB_NO_GUI_SUPPORT=ON`. + ## Running the unit test suite @@ -69,4 +73,3 @@ This library is licensed under the Boost Software License, which can be found in ## dlib sponsors 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. - diff --git a/setup.py b/setup.py index 5053e0e255..59b65a622d 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ To build and install: python setup.py install To upload the source distribution to PyPi - python setup.py sdist + python setup.py sdist twine upload dist/dlib-*.tar.gz To exclude certain options in the cmake config use --no: for example: @@ -25,21 +25,21 @@ --set: set arbitrary cmake options e.g. --set CUDA_HOST_COMPILER=/usr/bin/gcc-6.4.0 passes -DCUDA_HOST_COMPILER=/usr/bin/gcc-6.4.0 to CMake. """ +import errno +import logging +import multiprocessing import os +import platform import re -import sys -import errno -import stat import shutil -import platform +import stat import subprocess -import multiprocessing +import sys from math import floor from packaging.version import Version, parse as parse_version from setuptools import find_packages, setup, Extension from setuptools.command.build_ext import build_ext -import logging logging.basicConfig(level=logging.INFO) log = logging.getLogger("setup") @@ -89,8 +89,6 @@ def get_extra_cmake_options(): return _cmake_extra_options, _clean_build_folder -cmake_extra_options,clean_build_folder = get_extra_cmake_options() - class CMakeExtension(Extension): def __init__(self, name, sourcedir=''): @@ -143,7 +141,7 @@ def get_cmake_version(self): packager delete it and install an official cmake. More generally, cmake is not installed if when you open a terminal window - and type + and type cmake --version you get an error. So you can use that as a very basic test to see if you have cmake installed. That is, if cmake --version doesn't run from the @@ -157,7 +155,7 @@ def get_cmake_version(self): ================================================================================ ================================================================================ ================================================================================ -""") +""") sys.exit(1) return re.search(r'version\s*([\d.]+)', out.decode()).group(1) @@ -172,13 +170,19 @@ def run(self): self.build_extension(ext) def build_extension(self, ext): + # Build CMake args based on default settings, environment variables and CLI parameters extdir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name))) - - cmake_args = ['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=' + extdir, - '-DPYTHON_EXECUTABLE=' + sys.executable, - '-DDLIB_USE_FFMPEG=OFF',] - - cmake_args += cmake_extra_options + cmake_extra_options, clean_build_folder = get_extra_cmake_options() + cmake_args_dict = { + 'CMAKE_LIBRARY_OUTPUT_DIRECTORY': extdir, + 'DLIB_USE_FFMPEG': 'OFF', + 'PYTHON_EXECUTABLE': sys.executable, + } + for key, value in os.environ.items(): + if key.startswith("DLIB_"): + cmake_args_dict[key] = value + cmake_args = ['-D' + key + '=' + str(value) for key, value in cmake_args_dict.items()] + cmake_args += cmake_extra_options cfg = 'Debug' if self.debug else 'Release' build_args = ['--config', cfg] @@ -188,7 +192,7 @@ def build_extension(self, ext): if sys.maxsize > 2**32: cmake_args += ['-A', 'x64'] # Do a parallel build - build_args += ['--', '/m'] + build_args += ['--', '/m'] else: cmake_args += ['-DCMAKE_BUILD_TYPE=' + cfg] # Do a parallel build @@ -220,9 +224,9 @@ def num_available_cpu_cores(ram_per_build_process_in_gb): elif 'CMAKE_BUILD_PARALLEL_LEVEL' in os.environ and os.environ['CMAKE_BUILD_PARALLEL_LEVEL'].isnumeric(): return int(os.environ['CMAKE_BUILD_PARALLEL_LEVEL']) try: - mem_bytes = os.sysconf('SC_PAGE_SIZE') * os.sysconf('SC_PHYS_PAGES') + mem_bytes = os.sysconf('SC_PAGE_SIZE') * os.sysconf('SC_PHYS_PAGES') mem_gib = mem_bytes/(1024.**3) - num_cores = multiprocessing.cpu_count() + num_cores = multiprocessing.cpu_count() # make sure we have enough ram for each build process. mem_cores = int(floor(mem_gib/float(ram_per_build_process_in_gb)+0.5)); # We are limited either by RAM or CPU cores. So pick the limiting amount