mirror of
https://github.com/zebrajr/pytorch.git
synced 2025-12-07 00:21:07 +01:00
Summary: Currently when building extensions, variables such as USE_CUDA, USE_CUDNN are used to determine what libraries should be linked. But we should use what CMake has detected, because: 1. If CMake found them unavailable but the variables say some libraries should be linked, the build would fail. 2. If the first build is made using a set of non-default build options, rebuild must have these option passed to setup.py again, otherwise the extension build process is inconsistent with CMake. For example, ```bash # First build USE_CUDA=0 python setup.py install # Subsequent builds like this would fail, unless "build/" is deleted python setup.py install ``` This commit addresses the above issues by using variables from CMakeCache.txt when building the extensions. --- The changes in `setup.py` may look lengthy, but the biggest changed block is mostly moving them into a function `configure_extension_build` (along with some variable names changed to `cmake_cache_vars['variable name']` and other minor changes), because it must be called after CMake has been called (and thus the options used and system environment detected by CMake become available). Pull Request resolved: https://github.com/pytorch/pytorch/pull/21653 Differential Revision: D15824506 Pulled By: ezyang fbshipit-source-id: 1e1eb7eec7debba30738f65472ccad966ee74028
70 lines
2.5 KiB
Python
70 lines
2.5 KiB
Python
import os
|
|
import sys
|
|
from glob import glob
|
|
import shutil
|
|
|
|
from .setup_helpers import escape_path
|
|
from .setup_helpers.env import IS_64BIT, IS_WINDOWS, check_negative_env_flag
|
|
from .setup_helpers.cmake import USE_NINJA
|
|
from .setup_helpers.cuda import USE_CUDA, CUDA_HOME
|
|
from .setup_helpers.cudnn import CUDNN_INCLUDE_DIR, CUDNN_LIBRARY, USE_CUDNN
|
|
|
|
|
|
def _overlay_windows_vcvars(env):
|
|
if sys.version_info >= (3, 5):
|
|
from distutils._msvccompiler import _get_vc_env
|
|
vc_arch = 'x64' if IS_64BIT else 'x86'
|
|
vc_env = _get_vc_env(vc_arch)
|
|
# Keys in `_get_vc_env` are always lowercase.
|
|
# We turn them into uppercase before overlaying vcvars
|
|
# because OS environ keys are always uppercase on Windows.
|
|
# https://stackoverflow.com/a/7797329
|
|
vc_env = {k.upper(): v for k, v in vc_env.items()}
|
|
for k, v in env.items():
|
|
uk = k.upper()
|
|
if uk not in vc_env:
|
|
vc_env[uk] = v
|
|
return vc_env
|
|
else:
|
|
return env
|
|
|
|
|
|
def _create_build_env():
|
|
# XXX - our cmake file sometimes looks at the system environment
|
|
# and not cmake flags!
|
|
# you should NEVER add something to this list. It is bad practice to
|
|
# have cmake read the environment
|
|
my_env = os.environ.copy()
|
|
if USE_CUDNN:
|
|
my_env['CUDNN_LIBRARY'] = escape_path(CUDNN_LIBRARY)
|
|
my_env['CUDNN_INCLUDE_DIR'] = escape_path(CUDNN_INCLUDE_DIR)
|
|
if USE_CUDA:
|
|
my_env['CUDA_BIN_PATH'] = escape_path(CUDA_HOME)
|
|
|
|
if IS_WINDOWS and USE_NINJA:
|
|
# When using Ninja under Windows, the gcc toolchain will be chosen as
|
|
# default. But it should be set to MSVC as the user's first choice.
|
|
my_env = _overlay_windows_vcvars(my_env)
|
|
my_env.setdefault('CC', 'cl')
|
|
my_env.setdefault('CXX', 'cl')
|
|
return my_env
|
|
|
|
|
|
def build_caffe2(version, cmake_python_library, build_python, rerun_cmake, cmake_only, cmake):
|
|
my_env = _create_build_env()
|
|
build_test = not check_negative_env_flag('BUILD_TEST')
|
|
cmake.generate(version,
|
|
cmake_python_library,
|
|
build_python,
|
|
build_test,
|
|
my_env,
|
|
rerun_cmake)
|
|
if cmake_only:
|
|
return
|
|
cmake.build(my_env)
|
|
if build_python:
|
|
caffe2_proto_dir = os.path.join(cmake.build_dir, 'caffe2', 'proto')
|
|
for proto_file in glob(os.path.join(caffe2_proto_dir, '*.py')):
|
|
if proto_file != os.path.join(caffe2_proto_dir, '__init__.py'):
|
|
shutil.copy(proto_file, os.path.join('caffe2', 'proto'))
|