forked from rraju1/cmake-buildscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PythonInterpreterConfig.cmake
133 lines (95 loc) · 5.8 KB
/
PythonInterpreterConfig.cmake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# script which finds a Python interpereter.
# It detects a system Anaconda, offers to download an internal Miniconda, and, as a last resort, will use the system Python.
# NOTE: It is a problem if the user has Anaconda on their PATH but we do not use it. It contains shared libraries
# with different symbols than the system versions, so CMake will complain that it can't generate a safe RPATH
# --------------------------------------------------------------------
# Offer to download Miniconda
if(NOT DEFINED DOWNLOAD_MINICONDA)
# Stop the build to give the user a choice
option(DOWNLOAD_MINICONDA "If true, then Amber will download its own Miniconda distribution. Recommended if you're having problems with the system Python interpereter." TRUE)
message(FATAL_ERROR "We highly recommend letting AMBER install a Python environment with all prerequisites inside \
Amber's install location via a Continuum Miniconda distribution. \
Miniconda is chosen because it comes with a great package manager, conda, which is \
specially designed for numerical and scientific computing. This makes compiling \
AMBER Python extensions much easier. \
This will only need to be done once. \
It may take several minutes and downloads around a hundred megabytes of data. \
Config variable DOWNLOAD_MINICONDA has been autoset to TRUE. To accept and download Miniconda, just run cmake again. \
If you do not want to download Miniconda, run cmake again with DOWNLOAD_MINICONDA set to FALSE.")
endif()
# --------------------------------------------------------------------
# Detect system Anaconda
find_program(CONDA conda)
# --------------------------------------------------------------------
# Find the actual interpreter
if(DOWNLOAD_MINICONDA)
# get system Conda out of our PATH if it's there, so that the Miniconda install can proceed in an orderly fashion
if(EXISTS "${CONDA}")
get_filename_component(ANACONDA_BIN "${CONDA}" DIRECTORY)
remove_from_path("${ANACONDA_BIN}")
# unset vars set by conda activate
unset(ENV{CONDA_DEFAULT_ENV})
unset(ENV{CONDA_PREFIX})
unset(ENV{CONDA_PATH_BACKUP})
unset(ENV{_CONDA_PYTHON2})
endif()
set(MINICONDA_VERSION 4.3.21)
option(MINICONDA_USE_PY3 "If true, Amber will download a Python 3 miniconda when DOWNLOAD_MINICONDA is enabled. Otherwise, Python 2.7 Miniconda will get downloaded." FALSE)
include(UseMiniconda)
download_and_use_miniconda()
set(PYTHON_EXECUTABLE ${MINICONDA_PYTHON})
set(HAS_PYTHON TRUE)
if(HOST_WINDOWS)
list(APPEND CMAKE_PROGRAM_PATH "${MINICONDA_INSTALL_DIR}" "${MINICONDA_INSTALL_DIR}/Scripts")
else()
list(APPEND CMAKE_PROGRAM_PATH "${MINICONDA_INSTALL_DIR}/bin")
endif()
else()
if(EXISTS "${CONDA}")
# tell cmake to use system Conda
get_filename_component(ANACONDA_BIN "${CONDA}" DIRECTORY)
get_filename_component(ANACONDA_ROOT "${ANACONDA_BIN}/.." REALPATH)
set(USING_SYSTEM_ANACONDA TRUE)
message(STATUS "Found system Anaconda at ${ANACONDA_ROOT}. It will be used as a Python interpereter, and for additional shared libraries. \
It is not possible to use the system Python if Anaconda is on the PATH. To build Amber without using Anaconda, remove it from your PATH. \
To use Amber's internal Minconda instead, set DOWNLOAD_MINICONDA to TRUE. \
To use a different Anaconda install, just activate it and rerun CMake. \
To change the Python interpreter in use to a different one inside Anaconda, set the PYTHON_EXECUTABLE variable to point to it.")
# allow CMake to pull libraries such as MKL and bzip2 from Anaconda, but keep the old paths so that we can disable this if we want
set(CMAKE_LIBRARY_PATH_NO_ANACONDA ${CMAKE_LIBRARY_PATH})
set(CMAKE_INCLUDE_PATH_NO_ANACONDA ${CMAKE_INCLUDE_PATH})
set(CMAKE_PROGRAM_PATH_NO_ANACONDA ${CMAKE_PROGRAM_PATH})
list(APPEND CMAKE_LIBRARY_PATH "${ANACONDA_ROOT}/lib")
list(APPEND CMAKE_INCLUDE_PATH "${ANACONDA_ROOT}/include")
list(APPEND CMAKE_PROGRAM_PATH "${ANACONDA_BIN}")
set(CMAKE_LIBRARY_PATH_ANACONDA ${CMAKE_LIBRARY_PATH})
set(CMAKE_INCLUDE_PATH_ANACONDA ${CMAKE_INCLUDE_PATH})
set(CMAKE_PROGRAM_PATH_ANACONDA ${CMAKE_PROGRAM_PATH})
endif()
#-------------------------------------------------------------------------------
# Find a system Python, or Anaconda's python if we are using external Anaconda
#-------------------------------------------------------------------------------
find_package(PythonInterp)
#used to know if we can run Python scripts
set(HAS_PYTHON ${PYTHONINTERP_FOUND})
# get a single version number
set(PYTHON_VERSION "${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}")
test(PYTHON_VERSION_OK (${PYTHON_VERSION} VERSION_GREATER 2.6 AND ${PYTHON_VERSION} VERSION_LESS 3.0) OR ${PYTHON_VERSION} VERSION_GREATER 3.3)
if(NOT HAS_PYTHON)
message(STATUS "Python interpereter not found. Python packages will not be built, and the updater will not run. \
Either enable DOWNLOAD_MINICONDA, or set PYTHON_EXECUTABLE to point to your Python interpreter.")
endif()
if(PYTHON_VERSION_OK)
message(STATUS "Python version ${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR} -- OK")
else()
message(STATUS "Python version ${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR} -- Not OK. Version 2.7 or >= 3.4 is required. \
Python packages will not be built, and the updater will not run. \
Either enable DOWNLOAD_MINICONDA, or set PYTHON_EXECUTABLE to point to a Python interpreter of the correct version.")
set(HAS_PYTHON FALSE)
endif()
# set up amber.python symlink to point to active python interpereter
# (this could break if the install is moved to a different computer, but it's the best we can do)
if((HOST_OSX OR HOST_LINUX) AND (TARGET_OSX OR TARGET_LINUX))
install(CODE "execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink ${PYTHON_EXECUTABLE} \$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_POSTFIX}bin/amber.python)" COMPONENT Python)
endif()
endif()