1# Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2# file Copyright.txt or https://cmake.org/licensing for details.
3
4#
5# This file is a "template" file used by various FindPython modules.
6#
7
8#HM3 cmake_policy (GET CMP0094 _${_PYTHON_PREFIX}_LOOKUP_POLICY)
9
10cmake_policy (VERSION 3.7)
11
12#HM3 if (_${_PYTHON_PREFIX}_LOOKUP_POLICY)
13#HM3   cmake_policy (SET CMP0094 ${_${_PYTHON_PREFIX}_LOOKUP_POLICY})
14#HM3 endif()
15
16function(list_transform_prepend var prefix)
17    set(temp "")
18    foreach(f ${${var}})
19        list(APPEND temp "${prefix}${f}")
20    endforeach()
21    set(${var} "${temp}" PARENT_SCOPE)
22endfunction()
23
24function(list_transform_append var suffix)
25    set(temp "")
26    foreach(f ${${var}})
27        list(APPEND temp "${f}${suffix}")
28    endforeach()
29    set(${var} "${temp}" PARENT_SCOPE)
30endfunction()
31
32
33
34
35
36#
37# Initial configuration
38#
39if (NOT DEFINED _PYTHON_PREFIX)
40  message (FATAL_ERROR "FindPython: INTERNAL ERROR")
41endif()
42if (NOT DEFINED _${_PYTHON_PREFIX}_REQUIRED_VERSION_MAJOR)
43  message (FATAL_ERROR "FindPython: INTERNAL ERROR")
44endif()
45if (_${_PYTHON_PREFIX}_REQUIRED_VERSION_MAJOR EQUAL "3")
46  set(_${_PYTHON_PREFIX}_VERSIONS 3.12 3.11 3.10 3.9 3.8 3.7 3.6 3.5 3.4 3.3 3.2 3.1 3.0)
47elseif (_${_PYTHON_PREFIX}_REQUIRED_VERSION_MAJOR EQUAL "2")
48  set(_${_PYTHON_PREFIX}_VERSIONS 2.7 2.6 2.5 2.4 2.3 2.2 2.1 2.0)
49else()
50  message (FATAL_ERROR "FindPython: INTERNAL ERROR")
51endif()
52
53get_property(_${_PYTHON_PREFIX}_CMAKE_ROLE GLOBAL PROPERTY CMAKE_ROLE)
54
55
56#
57# helper commands
58#
59macro (_PYTHON_DISPLAY_FAILURE _PYTHON_MSG)
60  if (${_PYTHON_PREFIX}_FIND_REQUIRED)
61    message (FATAL_ERROR "${_PYTHON_MSG}")
62  else()
63    if (NOT ${_PYTHON_PREFIX}_FIND_QUIETLY)
64      message(STATUS "${_PYTHON_MSG}")
65    endif ()
66  endif()
67
68  set (${_PYTHON_PREFIX}_FOUND FALSE)
69  string (TOUPPER "${_PYTHON_PREFIX}" _${_PYTHON_PREFIX}_UPPER_PREFIX)
70  set (${_PYTHON_UPPER_PREFIX}_FOUND FALSE)
71  return()
72endmacro()
73
74
75function (_PYTHON_MARK_AS_INTERNAL)
76  foreach (var IN LISTS ARGV)
77    if (DEFINED CACHE{${var}})
78#HM3      set_property (CACHE ${var} PROPERTY TYPE INTERNAL)
79    endif()
80  endforeach()
81endfunction()
82
83
84macro (_PYTHON_SELECT_LIBRARY_CONFIGURATIONS _PYTHON_BASENAME)
85  if(NOT DEFINED ${_PYTHON_BASENAME}_LIBRARY_RELEASE)
86    set(${_PYTHON_BASENAME}_LIBRARY_RELEASE "${_PYTHON_BASENAME}_LIBRARY_RELEASE-NOTFOUND")
87  endif()
88  if(NOT DEFINED ${_PYTHON_BASENAME}_LIBRARY_DEBUG)
89    set(${_PYTHON_BASENAME}_LIBRARY_DEBUG "${_PYTHON_BASENAME}_LIBRARY_DEBUG-NOTFOUND")
90  endif()
91
92  get_property(_PYTHON_isMultiConfig GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
93  if (${_PYTHON_BASENAME}_LIBRARY_DEBUG AND ${_PYTHON_BASENAME}_LIBRARY_RELEASE AND
94      NOT ${_PYTHON_BASENAME}_LIBRARY_DEBUG STREQUAL ${_PYTHON_BASENAME}_LIBRARY_RELEASE AND
95      (_PYTHON_isMultiConfig OR CMAKE_BUILD_TYPE))
96    # if the generator is multi-config or if CMAKE_BUILD_TYPE is set for
97    # single-config generators, set optimized and debug libraries
98    set (${_PYTHON_BASENAME}_LIBRARIES "")
99    foreach (_PYTHON_libname IN LISTS ${_PYTHON_BASENAME}_LIBRARY_RELEASE)
100      list( APPEND ${_PYTHON_BASENAME}_LIBRARIES optimized "${_PYTHON_libname}")
101    endforeach()
102    foreach (_PYTHON_libname IN LISTS ${_PYTHON_BASENAME}_LIBRARY_DEBUG)
103      list( APPEND ${_PYTHON_BASENAME}_LIBRARIES debug "${_PYTHON_libname}")
104    endforeach()
105  elseif (${_PYTHON_BASENAME}_LIBRARY_RELEASE)
106    set (${_PYTHON_BASENAME}_LIBRARIES "${${_PYTHON_BASENAME}_LIBRARY_RELEASE}")
107  elseif (${_PYTHON_BASENAME}_LIBRARY_DEBUG)
108    set (${_PYTHON_BASENAME}_LIBRARIES "${${_PYTHON_BASENAME}_LIBRARY_DEBUG}")
109  else()
110    set (${_PYTHON_BASENAME}_LIBRARIES "${_PYTHON_BASENAME}_LIBRARY-NOTFOUND")
111  endif()
112endmacro()
113
114
115macro (_PYTHON_FIND_FRAMEWORKS)
116  if (CMAKE_HOST_APPLE OR APPLE)
117    file(TO_CMAKE_PATH "$ENV{CMAKE_FRAMEWORK_PATH}" _pff_CMAKE_FRAMEWORK_PATH)
118    set (_pff_frameworks ${CMAKE_FRAMEWORK_PATH}
119                         ${_pff_CMAKE_FRAMEWORK_PATH}
120                         ~/Library/Frameworks
121                         /usr/local/Frameworks
122                         ${CMAKE_SYSTEM_FRAMEWORK_PATH})
123    list (REMOVE_DUPLICATES _pff_frameworks)
124    foreach (_pff_implementation IN LISTS _${_PYTHON_PREFIX}_FIND_IMPLEMENTATIONS)
125      unset (_${_PYTHON_PREFIX}_${_pff_implementation}_FRAMEWORKS)
126      if (_pff_implementation STREQUAL "CPython")
127        foreach (_pff_framework IN LISTS _pff_frameworks)
128          if (EXISTS ${_pff_framework}/Python${_${_PYTHON_PREFIX}_REQUIRED_VERSION_MAJOR}.framework)
129            list (APPEND _${_PYTHON_PREFIX}_${_pff_implementation}_FRAMEWORKS ${_pff_framework}/Python${_${_PYTHON_PREFIX}_REQUIRED_VERSION_MAJOR}.framework)
130          endif()
131          if (EXISTS ${_pff_framework}/Python.framework)
132            list (APPEND _${_PYTHON_PREFIX}_${_pff_implementation}_FRAMEWORKS ${_pff_framework}/Python.framework)
133          endif()
134        endforeach()
135      elseif (_pff_implementation STREQUAL "IronPython")
136        foreach (_pff_framework IN LISTS _pff_frameworks)
137          if (EXISTS ${_pff_framework}/IronPython.framework)
138            list (APPEND _${_PYTHON_PREFIX}_${_pff_implementation}_FRAMEWORKS ${_pff_framework}/IronPython.framework)
139          endif()
140        endforeach()
141      endif()
142    endforeach()
143    unset (_pff_implementation)
144    unset (_pff_frameworks)
145    unset (_pff_framework)
146  endif()
147endmacro()
148
149function (_PYTHON_GET_FRAMEWORKS _PYTHON_PGF_FRAMEWORK_PATHS)
150  cmake_parse_arguments (PARSE_ARGV 1 _PGF "" "" "IMPLEMENTATIONS;VERSION")
151
152  if (NOT _PGF_IMPLEMENTATIONS)
153    set (_PGF_IMPLEMENTATIONS ${_${_PYTHON_PREFIX}_FIND_IMPLEMENTATIONS})
154  endif()
155
156  set (framework_paths)
157
158  foreach (implementation IN LISTS _PGF_IMPLEMENTATIONS)
159    if (implementation STREQUAL "CPython")
160      foreach (version IN LISTS _PGF_VERSION)
161        foreach (framework IN LISTS _${_PYTHON_PREFIX}_${implementation}_FRAMEWORKS)
162          if (EXISTS "${framework}/Versions/${version}")
163            list (APPEND framework_paths "${framework}/Versions/${version}")
164          endif()
165        endforeach()
166      endforeach()
167    elseif (implementation STREQUAL "IronPython")
168      foreach (version IN LISTS _PGF_VERSION)
169        foreach (framework IN LISTS _${_PYTHON_PREFIX}_${implementation}_FRAMEWORKS)
170          # pick-up all available versions
171          file (GLOB versions LIST_DIRECTORIES true RELATIVE "${framework}/Versions/"
172                              "${framework}/Versions/${version}*")
173          list (SORT versions ORDER DESCENDING)
174#HM3          list (TRANSFORM versions PREPEND "${framework}/Versions/")
175           list_transform_prepend(versions "${framework}/Versions/")
176          list (APPEND framework_paths ${versions})
177        endforeach()
178      endforeach()
179    endif()
180  endforeach()
181
182  set (${_PYTHON_PGF_FRAMEWORK_PATHS} ${framework_paths} PARENT_SCOPE)
183endfunction()
184
185function (_PYTHON_GET_REGISTRIES _PYTHON_PGR_REGISTRY_PATHS)
186  cmake_parse_arguments (PARSE_ARGV 1 _PGR "" "" "IMPLEMENTATIONS;VERSION")
187
188  if (NOT _PGR_IMPLEMENTATIONS)
189    set (_PGR_IMPLEMENTATIONS ${_${_PYTHON_PREFIX}_FIND_IMPLEMENTATIONS})
190  endif()
191
192  set (registries)
193
194  foreach (implementation IN LISTS _PGR_IMPLEMENTATIONS)
195    if (implementation STREQUAL "CPython")
196      foreach (version IN LISTS _PGR_VERSION)
197        string (REPLACE "." "" version_no_dots ${version})
198        list (APPEND registries
199                     [HKEY_CURRENT_USER\\SOFTWARE\\Python\\PythonCore\\${version}-${_${_PYTHON_PREFIX}_ARCH}\\InstallPath]
200                     [HKEY_CURRENT_USER\\SOFTWARE\\Python\\PythonCore\\${version}-${_${_PYTHON_PREFIX}_ARCH2}\\InstallPath])
201        if (version VERSION_GREATER_EQUAL "3.5")
202          get_filename_component (arch "[HKEY_CURRENT_USER\\Software\\Python\\PythonCore\\${version};SysArchitecture]" NAME)
203          if (arch MATCHES "(${_${_PYTHON_PREFIX}_ARCH}|${_${_PYTHON_PREFIX}_ARCH2})bit")
204            list (APPEND registries
205                         [HKEY_CURRENT_USER\\SOFTWARE\\Python\\PythonCore\\${version}\\InstallPath])
206          endif()
207        else()
208          list (APPEND registries
209                       [HKEY_CURRENT_USER\\SOFTWARE\\Python\\PythonCore\\${version}\\InstallPath])
210        endif()
211        list (APPEND registries
212                     [HKEY_CURRENT_USER\\SOFTWARE\\Python\\ContinuumAnalytics\\Anaconda${version_no_dots}-${_${_PYTHON_PREFIX}_ARCH}\\InstallPath]
213                     [HKEY_CURRENT_USER\\SOFTWARE\\Python\\ContinuumAnalytics\\Anaconda${version_no_dots}-${_${_PYTHON_PREFIX}_ARCH2}\\InstallPath]
214                     [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${version}-${_${_PYTHON_PREFIX}_ARCH}\\InstallPath]
215                     [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${version}-${_${_PYTHON_PREFIX}_ARCH2}\\InstallPath]
216                     [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${version}\\InstallPath]
217                     [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\ContinuumAnalytics\\Anaconda${version_no_dots}-${_${_PYTHON_PREFIX}_ARCH}\\InstallPath]
218                     [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\ContinuumAnalytics\\Anaconda${version_no_dots}-${_${_PYTHON_PREFIX}_ARCH2}\\InstallPath])
219      endforeach()
220    elseif (implementation STREQUAL "IronPython")
221      foreach (version  IN LISTS _PGR_VERSION)
222        list (APPEND registries [HKEY_LOCAL_MACHINE\\SOFTWARE\\IronPython\\${version}\\InstallPath])
223      endforeach()
224    endif()
225  endforeach()
226
227  set (${_PYTHON_PGR_REGISTRY_PATHS} "${registries}" PARENT_SCOPE)
228endfunction()
229
230
231function (_PYTHON_GET_ABIFLAGS _PGABIFLAGS)
232  set (abiflags)
233  list (GET _${_PYTHON_PREFIX}_FIND_ABI 0 pydebug)
234  list (GET _${_PYTHON_PREFIX}_FIND_ABI 1 pymalloc)
235  list (GET _${_PYTHON_PREFIX}_FIND_ABI 2 unicode)
236
237  if (pymalloc STREQUAL "ANY" AND unicode STREQUAL "ANY")
238    set (abiflags "mu" "m" "u" "")
239  elseif (pymalloc STREQUAL "ANY" AND unicode STREQUAL "ON")
240    set (abiflags "mu" "u")
241  elseif (pymalloc STREQUAL "ANY" AND unicode STREQUAL "OFF")
242    set (abiflags "m" "")
243  elseif (pymalloc STREQUAL "ON" AND unicode STREQUAL "ANY")
244    set (abiflags "mu" "m")
245  elseif (pymalloc STREQUAL "ON" AND unicode STREQUAL "ON")
246    set (abiflags "mu")
247  elseif (pymalloc STREQUAL "ON" AND unicode STREQUAL "OFF")
248    set (abiflags "m")
249  elseif (pymalloc STREQUAL "ON" AND unicode STREQUAL "ANY")
250    set (abiflags "u" "")
251  elseif (pymalloc STREQUAL "OFF" AND unicode STREQUAL "ON")
252    set (abiflags "u")
253  endif()
254
255  if (pydebug STREQUAL "ON")
256    if (abiflags)
257#HM3      list (TRANSFORM abiflags PREPEND "d")
258list_transform_prepend(abiflags  "d")
259    else()
260      set (abiflags "d")
261    endif()
262  elseif (pydebug STREQUAL "ANY")
263    if (abiflags)
264      set (flags "${abiflags}")
265#HM3     list (TRANSFORM flags PREPEND "d")
266list_transform_prepend(flags "d")
267      list (APPEND abiflags "${flags}")
268    else()
269      set (abiflags "" "d")
270    endif()
271  endif()
272
273  set (${_PGABIFLAGS} "${abiflags}" PARENT_SCOPE)
274endfunction()
275
276function (_PYTHON_GET_PATH_SUFFIXES _PYTHON_PGPS_PATH_SUFFIXES)
277  cmake_parse_arguments (PARSE_ARGV 1 _PGPS "INTERPRETER;COMPILER;LIBRARY;INCLUDE" "" "IMPLEMENTATIONS;VERSION")
278
279  if (NOT _PGPS_IMPLEMENTATIONS)
280    set (_PGPS_IMPLEMENTATIONS ${_${_PYTHON_PREFIX}_FIND_IMPLEMENTATIONS})
281  endif()
282
283  if (DEFINED _${_PYTHON_PREFIX}_ABIFLAGS)
284    set (abi "${_${_PYTHON_PREFIX}_ABIFLAGS}")
285  else()
286    set (abi "mu" "m" "u" "")
287  endif()
288
289  set (path_suffixes)
290
291  foreach (implementation IN LISTS _PGPS_IMPLEMENTATIONS)
292    if (implementation STREQUAL "CPython")
293      if (_PGPS_INTERPRETER)
294        list (APPEND path_suffixes bin Scripts)
295      else()
296        foreach (version IN LISTS _PGPS_VERSION)
297          if (_PGPS_LIBRARY)
298            if (CMAKE_LIBRARY_ARCHITECTURE)
299              list (APPEND path_suffixes lib/${CMAKE_LIBRARY_ARCHITECTURE})
300            endif()
301            list (APPEND path_suffixes lib libs)
302
303            if (CMAKE_LIBRARY_ARCHITECTURE)
304              set (suffixes "${abi}")
305              if (suffixes)
306#HM3                list (TRANSFORM suffixes PREPEND "lib/python${_PGPS_VERSION}/config-${_PGPS_VERSION}")
307list_transform_prepend(suffixes "lib/python${_PGPS_VERSION}/config-${_PGPS_VERSION}")
308#HM3                list (TRANSFORM suffixes APPEND "-${CMAKE_LIBRARY_ARCHITECTURE}")
309list_transform_append(suffixes  "-${CMAKE_LIBRARY_ARCHITECTURE}")
310              else()
311                set (suffixes "lib/python${_PGPS_VERSION}/config-${_PGPS_VERSION}-${CMAKE_LIBRARY_ARCHITECTURE}")
312              endif()
313              list (APPEND path_suffixes ${suffixes})
314            endif()
315            set (suffixes "${abi}")
316            if (suffixes)
317#HM3              list (TRANSFORM suffixes PREPEND "lib/python${_PGPS_VERSION}/config-${_PGPS_VERSION}")
318list_transform_prepend(suffixes  "lib/python${_PGPS_VERSION}/config-${_PGPS_VERSION}")
319            else()
320              set (suffixes "lib/python${_PGPS_VERSION}/config-${_PGPS_VERSION}")
321            endif()
322            list (APPEND path_suffixes ${suffixes})
323          elseif (_PGPS_INCLUDE)
324            set (suffixes "${abi}")
325            if (suffixes)
326#HM3              list (TRANSFORM suffixes PREPEND "include/python${_PGPS_VERSION}")
327list_transform_prepend(suffixes  "include/python${_PGPS_VERSION}")
328            else()
329              set (suffixes "include/python${_PGPS_VERSION}")
330            endif()
331            list (APPEND path_suffixes ${suffixes} include)
332          endif()
333        endforeach()
334      endif()
335    elseif (implementation STREQUAL "IronPython")
336      if (_PGPS_INTERPRETER OR _PGPS_COMPILER)
337        foreach (version IN LISTS _PGPS_VERSION)
338          list (APPEND path_suffixes "share/ironpython${version}")
339        endforeach()
340        list (APPEND path_suffixes ${_${_PYTHON_PREFIX}_IRON_PYTHON_PATH_SUFFIXES})
341      endif()
342    elseif (implementation STREQUAL "PyPy")
343      if (_PGPS_INTERPRETER)
344        list (APPEND path_suffixes ${_${_PYTHON_PREFIX}_PYPY_EXECUTABLE_PATH_SUFFIXES})
345      elseif (_PGPS_LIBRARY)
346        list (APPEND path_suffixes ${_${_PYTHON_PREFIX}_PYPY_LIBRARY_PATH_SUFFIXES})
347      elseif (_PGPS_INCLUDE)
348        list (APPEND path_suffixes ${_${_PYTHON_PREFIX}_PYPY_INCLUDE_PATH_SUFFIXES})
349      endif()
350    endif()
351  endforeach()
352 if (path_suffixes)
353  list (REMOVE_DUPLICATES path_suffixes)
354    endif()
355  set (${_PYTHON_PGPS_PATH_SUFFIXES} ${path_suffixes} PARENT_SCOPE)
356endfunction()
357
358function (_PYTHON_GET_NAMES _PYTHON_PGN_NAMES)
359  cmake_parse_arguments (PARSE_ARGV 1 _PGN "POSIX;INTERPRETER;COMPILER;CONFIG;LIBRARY;WIN32;DEBUG" "" "IMPLEMENTATIONS;VERSION")
360
361  if (NOT _PGN_IMPLEMENTATIONS)
362    set (_PGN_IMPLEMENTATIONS ${_${_PYTHON_PREFIX}_FIND_IMPLEMENTATIONS})
363  endif()
364
365  set (names)
366
367  foreach (implementation IN LISTS _PGN_IMPLEMENTATIONS)
368    if (implementation STREQUAL "CPython")
369      foreach (version IN LISTS _PGN_VERSION)
370        if (_PGN_WIN32)
371          string (REPLACE "." "" version_no_dots ${version})
372
373          set (name python${version_no_dots})
374          if (_PGN_DEBUG)
375            string (APPEND name "_d")
376          endif()
377
378          list (APPEND names "${name}")
379        endif()
380
381        if (_PGN_POSIX)
382          if (DEFINED _${_PYTHON_PREFIX}_ABIFLAGS)
383            set (abi "${_${_PYTHON_PREFIX}_ABIFLAGS}")
384          else()
385            if (_PGN_INTERPRETER OR _PGN_CONFIG)
386              set (abi "")
387            else()
388              set (abi "mu" "m" "u" "")
389            endif()
390          endif()
391
392          if (abi)
393            if (_PGN_CONFIG AND DEFINED CMAKE_LIBRARY_ARCHITECTURE)
394              set (abinames "${abi}")
395#HM3              list (TRANSFORM abinames PREPEND "${CMAKE_LIBRARY_ARCHITECTURE}-python${version}")
396list_transform_prepend(abinames  "${CMAKE_LIBRARY_ARCHITECTURE}-python${version}")
397#HM3              list (TRANSFORM abinames APPEND "-config")
398list_transform_append( abinames  "-config")
399              list (APPEND names ${abinames})
400            endif()
401            set (abinames "${abi}")
402#HM3            list (TRANSFORM abinames PREPEND "python${version}")
403list_transform_prepend(abinames  "python${version}")
404            if (_PGN_CONFIG)
405#HM3              list (TRANSFORM abinames APPEND "-config")
406list_transform_append(abinames "-config")
407            endif()
408            list (APPEND names ${abinames})
409          else()
410            unset (abinames)
411            if (_PGN_CONFIG AND DEFINED CMAKE_LIBRARY_ARCHITECTURE)
412              set (abinames "${CMAKE_LIBRARY_ARCHITECTURE}-python${version}")
413            endif()
414            list (APPEND abinames "python${version}")
415            if (_PGN_CONFIG)
416#HM3              list (TRANSFORM abinames APPEND "-config")
417list_transform_append(abinames "-config")
418            endif()
419            list (APPEND names ${abinames})
420          endif()
421        endif()
422      endforeach()
423      if (_PGN_INTERPRETER)
424        list (APPEND names python${_${_PYTHON_PREFIX}_REQUIRED_VERSION_MAJOR} python)
425      endif()
426    elseif (implementation STREQUAL "IronPython")
427      if (_PGN_INTERPRETER)
428        if (NOT CMAKE_SYSTEM_NAME STREQUAL "Linux")
429          # Do not use wrapper script on Linux because it is buggy: -c interpreter option cannot be used
430          foreach (version IN LISTS _PGN_VERSION)
431            list (APPEND names "ipy${version}")
432          endforeach()
433        endif()
434        list (APPEND names ${_${_PYTHON_PREFIX}_IRON_PYTHON_INTERPRETER_NAMES})
435      elseif (_PGN_COMPILER)
436        list (APPEND names ${_${_PYTHON_PREFIX}_IRON_PYTHON_COMPILER_NAMES})
437      endif()
438    elseif (implementation STREQUAL "PyPy")
439      if (_PGN_INTERPRETER)
440        list (APPEND names ${_${_PYTHON_PREFIX}_PYPY_NAMES})
441      elseif (_PGN_LIBRARY)
442        if (_PGN_WIN32)
443          foreach (version IN LISTS _PGN_VERSION)
444            string (REPLACE "." "" version_no_dots ${version})
445
446            set (name "python${version_no_dots}")
447            if (_PGN_DEBUG)
448              string (APPEND name "_d")
449            endif()
450            list (APPEND names "${name}")
451          endforeach()
452        endif()
453        list (APPEND names ${_${_PYTHON_PREFIX}_PYPY_LIB_NAMES})
454      endif()
455    endif()
456  endforeach()
457
458  set (${_PYTHON_PGN_NAMES} ${names} PARENT_SCOPE)
459endfunction()
460
461function (_PYTHON_GET_CONFIG_VAR _PYTHON_PGCV_VALUE NAME)
462  unset (${_PYTHON_PGCV_VALUE} PARENT_SCOPE)
463
464  if (NOT NAME MATCHES "^(PREFIX|ABIFLAGS|CONFIGDIR|INCLUDES|LIBS|SOABI)$")
465    return()
466  endif()
467
468  if (_${_PYTHON_PREFIX}_CONFIG)
469    if (NAME STREQUAL "SOABI")
470      set (config_flag "--extension-suffix")
471    else()
472      set (config_flag "--${NAME}")
473    endif()
474    string (TOLOWER "${config_flag}" config_flag)
475    execute_process (COMMAND "${_${_PYTHON_PREFIX}_CONFIG}" ${config_flag}
476                     RESULT_VARIABLE _result
477                     OUTPUT_VARIABLE _values
478                     ERROR_QUIET
479                     OUTPUT_STRIP_TRAILING_WHITESPACE)
480    if (_result)
481      unset (_values)
482    else()
483      if (NAME STREQUAL "INCLUDES")
484        # do some clean-up
485        string (REGEX MATCHALL "(-I|-iwithsysroot)[ ]*[^ ]+" _values "${_values}")
486        string (REGEX REPLACE "(-I|-iwithsysroot)[ ]*" "" _values "${_values}")
487        list (REMOVE_DUPLICATES _values)
488      elseif (NAME STREQUAL "SOABI")
489        # clean-up: remove prefix character and suffix
490        string (REGEX REPLACE "^[.-](.+)(${CMAKE_SHARED_LIBRARY_SUFFIX}|\\.(so|pyd))$" "\\1" _values "${_values}")
491      endif()
492    endif()
493  endif()
494
495  if (_${_PYTHON_PREFIX}_EXECUTABLE AND NOT CMAKE_CROSSCOMPILING)
496    if (NAME STREQUAL "PREFIX")
497      execute_process (COMMAND ${_${_PYTHON_PREFIX}_INTERPRETER_LAUNCHER} "${_${_PYTHON_PREFIX}_EXECUTABLE}" -c "import sys\ntry:\n   from distutils import sysconfig\n   sys.stdout.write(';'.join([sysconfig.PREFIX,sysconfig.EXEC_PREFIX,sysconfig.BASE_EXEC_PREFIX]))\nexcept Exception:\n   import sysconfig\n   sys.stdout.write(';'.join([sysconfig.get_config_var('base') or '', sysconfig.get_config_var('installed_base') or '']))"
498                       RESULT_VARIABLE _result
499                       OUTPUT_VARIABLE _values
500                       ERROR_QUIET
501                       OUTPUT_STRIP_TRAILING_WHITESPACE)
502      if (_result)
503        unset (_values)
504      else()
505        list (REMOVE_DUPLICATES _values)
506      endif()
507    elseif (NAME STREQUAL "INCLUDES")
508      if (WIN32)
509        set (_scheme "nt")
510      else()
511        set (_scheme "posix_prefix")
512      endif()
513      execute_process (COMMAND ${_${_PYTHON_PREFIX}_INTERPRETER_LAUNCHER} "${_${_PYTHON_PREFIX}_EXECUTABLE}" -c
514                               "import sys\ntry:\n   from distutils import sysconfig\n   sys.stdout.write(';'.join([sysconfig.get_python_inc(plat_specific=True),sysconfig.get_python_inc(plat_specific=False)]))\nexcept Exception:\n   import sysconfig\n   sys.stdout.write(';'.join([sysconfig.get_path('platinclude'),sysconfig.get_path('platinclude','${_scheme}'),sysconfig.get_path('include'),sysconfig.get_path('include','${_scheme}')]))"
515                       RESULT_VARIABLE _result
516                       OUTPUT_VARIABLE _values
517                       ERROR_QUIET
518                       OUTPUT_STRIP_TRAILING_WHITESPACE)
519      if (_result)
520        unset (_values)
521      else()
522        list (REMOVE_DUPLICATES _values)
523      endif()
524    elseif (NAME STREQUAL "SOABI")
525      execute_process (COMMAND ${_${_PYTHON_PREFIX}_INTERPRETER_LAUNCHER} "${_${_PYTHON_PREFIX}_EXECUTABLE}" -c
526                               "import sys\ntry:\n   from distutils import sysconfig\n   sys.stdout.write(';'.join([sysconfig.get_config_var('SOABI') or '',sysconfig.get_config_var('EXT_SUFFIX') or '']))\nexcept Exception:\n   import sysconfig;sys.stdout.write(';'.join([sysconfig.get_config_var('SOABI') or '',sysconfig.get_config_var('EXT_SUFFIX') or '']))"
527                       RESULT_VARIABLE _result
528                       OUTPUT_VARIABLE _soabi
529                       ERROR_QUIET
530                       OUTPUT_STRIP_TRAILING_WHITESPACE)
531      if (_result)
532        unset (_values)
533      else()
534        foreach (_item IN LISTS _soabi)
535          if (_item)
536            set (_values "${_item}")
537            break()
538          endif()
539        endforeach()
540        if (_values)
541          # clean-up: remove prefix character and suffix
542          string (REGEX REPLACE "^[.-](.+)(${CMAKE_SHARED_LIBRARY_SUFFIX}|\\.(so|pyd))$" "\\1" _values "${_values}")
543        endif()
544      endif()
545    else()
546      set (config_flag "${NAME}")
547      if (NAME STREQUAL "CONFIGDIR")
548        set (config_flag "LIBPL")
549      endif()
550      execute_process (COMMAND ${_${_PYTHON_PREFIX}_INTERPRETER_LAUNCHER} "${_${_PYTHON_PREFIX}_EXECUTABLE}" -c
551                               "import sys\ntry:\n   from distutils import sysconfig\n   sys.stdout.write(sysconfig.get_config_var('${config_flag}'))\nexcept Exception:\n   import sysconfig\n   sys.stdout.write(sysconfig.get_config_var('${config_flag}'))"
552                       RESULT_VARIABLE _result
553                       OUTPUT_VARIABLE _values
554                       ERROR_QUIET
555                       OUTPUT_STRIP_TRAILING_WHITESPACE)
556      if (_result)
557        unset (_values)
558      endif()
559    endif()
560  endif()
561
562  if (config_flag STREQUAL "ABIFLAGS")
563    set (${_PYTHON_PGCV_VALUE} "${_values}" PARENT_SCOPE)
564    return()
565  endif()
566
567  if (NOT _values OR _values STREQUAL "None")
568    return()
569  endif()
570
571  if (NAME STREQUAL "LIBS")
572    # do some clean-up
573    string (REGEX MATCHALL "-(l|framework)[ ]*[^ ]+" _values "${_values}")
574    # remove elements relative to python library itself
575    list (FILTER _values EXCLUDE REGEX "-lpython")
576    list (REMOVE_DUPLICATES _values)
577  endif()
578
579  if (WIN32 AND NAME MATCHES "^(PREFIX|CONFIGDIR|INCLUDES)$")
580    file (TO_CMAKE_PATH "${_values}" _values)
581  endif()
582
583  set (${_PYTHON_PGCV_VALUE} "${_values}" PARENT_SCOPE)
584endfunction()
585
586function (_PYTHON_GET_VERSION)
587  cmake_parse_arguments (PARSE_ARGV 0 _PGV "LIBRARY;INCLUDE" "PREFIX" "")
588
589  unset (${_PGV_PREFIX}VERSION PARENT_SCOPE)
590  unset (${_PGV_PREFIX}VERSION_MAJOR PARENT_SCOPE)
591  unset (${_PGV_PREFIX}VERSION_MINOR PARENT_SCOPE)
592  unset (${_PGV_PREFIX}VERSION_PATCH PARENT_SCOPE)
593  unset (${_PGV_PREFIX}ABI PARENT_SCOPE)
594
595  if (_PGV_LIBRARY)
596    # retrieve version and abi from library name
597    if (_${_PYTHON_PREFIX}_LIBRARY_RELEASE)
598      # extract version from library name
599      if (_${_PYTHON_PREFIX}_LIBRARY_RELEASE MATCHES "python([23])([0-9]+)")
600        set (${_PGV_PREFIX}VERSION_MAJOR "${CMAKE_MATCH_1}" PARENT_SCOPE)
601        set (${_PGV_PREFIX}VERSION_MINOR "${CMAKE_MATCH_2}" PARENT_SCOPE)
602        set (${_PGV_PREFIX}VERSION "${CMAKE_MATCH_1}.${CMAKE_MATCH_2}" PARENT_SCOPE)
603        set (${_PGV_PREFIX}ABI "" PARENT_SCOPE)
604      elseif (_${_PYTHON_PREFIX}_LIBRARY_RELEASE MATCHES "python([23])\\.([0-9]+)([dmu]*)")
605        set (${_PGV_PREFIX}VERSION_MAJOR "${CMAKE_MATCH_1}" PARENT_SCOPE)
606        set (${_PGV_PREFIX}VERSION_MINOR "${CMAKE_MATCH_2}" PARENT_SCOPE)
607        set (${_PGV_PREFIX}VERSION "${CMAKE_MATCH_1}.${CMAKE_MATCH_2}" PARENT_SCOPE)
608        set (${_PGV_PREFIX}ABI "${CMAKE_MATCH_3}" PARENT_SCOPE)
609      elseif (_${_PYTHON_PREFIX}_LIBRARY_RELEASE MATCHES "pypy(3)?")
610        set (version "${CMAKE_MATCH_1}")
611        if (version EQUAL "3")
612          set (${_PGV_PREFIX}VERSION_MAJOR "3" PARENT_SCOPE)
613          set (${_PGV_PREFIX}VERSION "3" PARENT_SCOPE)
614        else()
615          set (${_PGV_PREFIX}VERSION_MAJOR "2" PARENT_SCOPE)
616          set (${_PGV_PREFIX}VERSION "2" PARENT_SCOPE)
617        endif()
618        set (${_PGV_PREFIX}ABI "" PARENT_SCOPE)
619      endif()
620    endif()
621  else()
622    if (_${_PYTHON_PREFIX}_INCLUDE_DIR)
623      # retrieve version from header file
624      file (STRINGS "${_${_PYTHON_PREFIX}_INCLUDE_DIR}/patchlevel.h" version
625            REGEX "^#define[ \t]+PY_VERSION[ \t]+\"[^\"]+\"")
626      string (REGEX REPLACE "^#define[ \t]+PY_VERSION[ \t]+\"([^\"]+)\".*" "\\1"
627                            version "${version}")
628      string (REGEX MATCHALL "[0-9]+" versions "${version}")
629      list (GET versions 0 version_major)
630      list (GET versions 1 version_minor)
631      list (GET versions 2 version_patch)
632
633      set (${_PGV_PREFIX}VERSION "${version_major}.${version_minor}.${version_patch}" PARENT_SCOPE)
634      set (${_PGV_PREFIX}VERSION_MAJOR ${version_major} PARENT_SCOPE)
635      set (${_PGV_PREFIX}VERSION_MINOR ${version_minor} PARENT_SCOPE)
636      set (${_PGV_PREFIX}VERSION_PATCH ${version_patch} PARENT_SCOPE)
637
638      # compute ABI flags
639      if (version_major VERSION_GREATER "2")
640        file (STRINGS "${_${_PYTHON_PREFIX}_INCLUDE_DIR}/pyconfig.h" config REGEX "(Py_DEBUG|WITH_PYMALLOC|Py_UNICODE_SIZE|MS_WIN32)")
641        set (abi)
642        if (config MATCHES "#[ ]*define[ ]+MS_WIN32")
643          # ABI not used on Windows
644          set (abi "")
645        else()
646          if (config MATCHES "#[ ]*define[ ]+Py_DEBUG[ ]+1")
647            string (APPEND abi "d")
648          endif()
649          if (config MATCHES "#[ ]*define[ ]+WITH_PYMALLOC[ ]+1")
650            string (APPEND abi "m")
651          endif()
652          if (config MATCHES "#[ ]*define[ ]+Py_UNICODE_SIZE[ ]+4")
653            string (APPEND abi "u")
654          endif()
655          set (${_PGV_PREFIX}ABI "${abi}" PARENT_SCOPE)
656        endif()
657      else()
658        # ABI not supported
659        set (${_PGV_PREFIX}ABI "" PARENT_SCOPE)
660      endif()
661    endif()
662  endif()
663endfunction()
664
665function (_PYTHON_GET_LAUNCHER _PYTHON_PGL_NAME)
666  cmake_parse_arguments (PARSE_ARGV 1 _PGL "INTERPRETER;COMPILER" "" "")
667
668  unset ({_PYTHON_PGL_NAME} PARENT_SCOPE)
669
670  if ((_PGL_INTERPRETER AND NOT _${_PYTHON_PREFIX}_EXECUTABLE)
671      OR (_PGL_COMPILER AND NOT _${_PYTHON_PREFIX}_COMPILER))
672    return()
673  endif()
674
675  if ("IronPython" IN_LIST _${_PYTHON_PREFIX}_FIND_IMPLEMENTATIONS
676      AND NOT SYSTEM_NAME MATCHES "Windows|Linux")
677    if (_PGL_INTERPRETER)
678      get_filename_component (name "${_${_PYTHON_PREFIX}_EXECUTABLE}" NAME)
679      get_filename_component (ext "${_${_PYTHON_PREFIX}_EXECUTABLE}" LAST_EXT)
680      if (name IN_LIST _${_PYTHON_PREFIX}_IRON_PYTHON_INTERPRETER_NAMES
681          AND ext STREQUAL ".exe")
682        set (${_PYTHON_PGL_NAME} "${${_PYTHON_PREFIX}_DOTNET_LAUNCHER}" PARENT_SCOPE)
683      endif()
684    else()
685      get_filename_component (name "${_${_PYTHON_PREFIX}_COMPILER}" NAME)
686      get_filename_component (ext "${_${_PYTHON_PREFIX}_COMPILER}" LAST_EXT)
687      if (name IN_LIST _${_PYTHON_PREFIX}_IRON_PYTHON_COMPILER_NAMES
688          AND ext STREQUAL ".exe")
689        set (${_PYTHON_PGL_NAME} "${${_PYTHON_PREFIX}_DOTNET_LAUNCHER}" PARENT_SCOPE)
690      endif()
691    endif()
692  endif()
693endfunction()
694
695
696function (_PYTHON_VALIDATE_INTERPRETER)
697  if (NOT _${_PYTHON_PREFIX}_EXECUTABLE)
698    return()
699  endif()
700
701  cmake_parse_arguments (PARSE_ARGV 0 _PVI "EXACT;CHECK_EXISTS" "" "")
702  if (_PVI_UNPARSED_ARGUMENTS)
703    set (expected_version "${_PVI_UNPARSED_ARGUMENTS}")
704  else()
705    unset (expected_version)
706  endif()
707
708  if (_PVI_CHECK_EXISTS AND NOT EXISTS "${_${_PYTHON_PREFIX}_EXECUTABLE}")
709    # interpreter does not exist anymore
710    set (_${_PYTHON_PREFIX}_Interpreter_REASON_FAILURE "Cannot find the interpreter \"${_${_PYTHON_PREFIX}_EXECUTABLE}\"" PARENT_SCOPE)
711#HM3    set_property (CACHE _${_PYTHON_PREFIX}_EXECUTABLE PROPERTY VALUE "${_PYTHON_PREFIX}_EXECUTABLE-NOTFOUND")
712    return()
713  endif()
714
715  _python_get_launcher (launcher INTERPRETER)
716
717  # validate ABI compatibility
718  if (DEFINED _${_PYTHON_PREFIX}_FIND_ABI)
719    execute_process (COMMAND ${launcher} "${_${_PYTHON_PREFIX}_EXECUTABLE}" -c
720                             "import sys; sys.stdout.write(sys.abiflags)"
721                     RESULT_VARIABLE result
722                     OUTPUT_VARIABLE abi
723                     ERROR_QUIET
724                     OUTPUT_STRIP_TRAILING_WHITESPACE)
725    if (result)
726      # assume ABI is not supported
727      set (abi "")
728    endif()
729    if (NOT abi IN_LIST _${_PYTHON_PREFIX}_ABIFLAGS)
730      # incompatible ABI
731      set (_${_PYTHON_PREFIX}_Interpreter_REASON_FAILURE "Wrong ABI for the interpreter \"${_${_PYTHON_PREFIX}_EXECUTABLE}\"" PARENT_SCOPE)
732#HM3      set_property (CACHE _${_PYTHON_PREFIX}_EXECUTABLE PROPERTY VALUE "${_PYTHON_PREFIX}_EXECUTABLE-NOTFOUND")
733      return()
734    endif()
735  endif()
736
737  get_filename_component (python_name "${_${_PYTHON_PREFIX}_EXECUTABLE}" NAME)
738
739  if (expected_version)
740    if (NOT python_name STREQUAL "python${expected_version}${abi}${CMAKE_EXECUTABLE_SUFFIX}")
741      # compute number of components for version
742      string (REGEX REPLACE "[^.]" "" dots "${expected_version}")
743      # add one dot because there is one dot less than there are components
744      string (LENGTH "${dots}." count)
745      if (count GREATER 3)
746        set (count 3)
747      endif()
748
749      # executable found must have a specific version
750      execute_process (COMMAND ${launcher} "${_${_PYTHON_PREFIX}_EXECUTABLE}" -c
751                               "import sys; sys.stdout.write('.'.join([str(x) for x in sys.version_info[:${count}]]))"
752                       RESULT_VARIABLE result
753                       OUTPUT_VARIABLE version
754                       ERROR_QUIET
755                       OUTPUT_STRIP_TRAILING_WHITESPACE)
756      if (result)
757        # interpreter is not usable
758        set (_${_PYTHON_PREFIX}_Interpreter_REASON_FAILURE "Cannot use the interpreter \"${_${_PYTHON_PREFIX}_EXECUTABLE}\"" PARENT_SCOPE)
759#HM3        set_property (CACHE _${_PYTHON_PREFIX}_EXECUTABLE PROPERTY VALUE "${_PYTHON_PREFIX}_EXECUTABLE-NOTFOUND")
760      else()
761        if (_PVI_EXACT AND NOT version VERSION_EQUAL expected_version)
762          # interpreter has wrong version
763          set (_${_PYTHON_PREFIX}_Interpreter_REASON_FAILURE "Wrong version for the interpreter \"${_${_PYTHON_PREFIX}_EXECUTABLE}\"" PARENT_SCOPE)
764#HM3          set_property (CACHE _${_PYTHON_PREFIX}_EXECUTABLE PROPERTY VALUE "${_PYTHON_PREFIX}_EXECUTABLE-NOTFOUND")
765        else()
766          # check that version is OK
767          string(REGEX REPLACE "^([0-9]+)\\.?.*$" "\\1" major_version "${version}")
768          string(REGEX REPLACE "^([0-9]+)\\.?.*$" "\\1" expected_major_version "${expected_version}")
769          if (NOT major_version VERSION_EQUAL expected_major_version
770              OR NOT version VERSION_GREATER_EQUAL expected_version)
771            set (_${_PYTHON_PREFIX}_Interpreter_REASON_FAILURE "Wrong version for the interpreter \"${_${_PYTHON_PREFIX}_EXECUTABLE}\"" PARENT_SCOPE)
772#HM3            set_property (CACHE _${_PYTHON_PREFIX}_EXECUTABLE PROPERTY VALUE "${_PYTHON_PREFIX}_EXECUTABLE-NOTFOUND")
773          endif()
774        endif()
775      endif()
776      if (NOT _${_PYTHON_PREFIX}_EXECUTABLE)
777        return()
778      endif()
779    endif()
780  else()
781    if (NOT python_name STREQUAL "python${_${_PYTHON_PREFIX}_REQUIRED_VERSION_MAJOR}${CMAKE_EXECUTABLE_SUFFIX}")
782      # executable found do not have version in name
783      # ensure major version is OK
784      execute_process (COMMAND ${launcher} "${_${_PYTHON_PREFIX}_EXECUTABLE}" -c
785                               "import sys; sys.stdout.write(str(sys.version_info[0]))"
786                       RESULT_VARIABLE result
787                       OUTPUT_VARIABLE version
788                       ERROR_QUIET
789                       OUTPUT_STRIP_TRAILING_WHITESPACE)
790      if (result OR NOT version EQUAL _${_PYTHON_PREFIX}_REQUIRED_VERSION_MAJOR)
791        # interpreter not usable or has wrong major version
792        if (result)
793          set (_${_PYTHON_PREFIX}_Interpreter_REASON_FAILURE "Cannot use the interpreter \"${_${_PYTHON_PREFIX}_EXECUTABLE}\"" PARENT_SCOPE)
794        else()
795          set (_${_PYTHON_PREFIX}_Interpreter_REASON_FAILURE "Wrong major version for the interpreter \"${_${_PYTHON_PREFIX}_EXECUTABLE}\"" PARENT_SCOPE)
796        endif()
797#HM3        set_property (CACHE _${_PYTHON_PREFIX}_EXECUTABLE PROPERTY VALUE "${_PYTHON_PREFIX}_EXECUTABLE-NOTFOUND")
798        return()
799      endif()
800    endif()
801  endif()
802
803  if (CMAKE_SIZEOF_VOID_P AND ("Development.Module" IN_LIST ${_PYTHON_PREFIX}_FIND_COMPONENTS
804        OR "Development.Embed" IN_LIST ${_PYTHON_PREFIX}_FIND_COMPONENTS)
805      AND NOT CMAKE_CROSSCOMPILING)
806    # In this case, interpreter must have same architecture as environment
807    execute_process (COMMAND ${launcher} "${_${_PYTHON_PREFIX}_EXECUTABLE}" -c
808                             "import sys, struct; sys.stdout.write(str(struct.calcsize(\"P\")))"
809                     RESULT_VARIABLE result
810                     OUTPUT_VARIABLE size
811                     ERROR_QUIET
812                     OUTPUT_STRIP_TRAILING_WHITESPACE)
813    if (result OR NOT size EQUAL CMAKE_SIZEOF_VOID_P)
814      # interpreter not usable or has wrong architecture
815      if (result)
816        set (_${_PYTHON_PREFIX}_Interpreter_REASON_FAILURE "Cannot use the interpreter \"${_${_PYTHON_PREFIX}_EXECUTABLE}\"" PARENT_SCOPE)
817      else()
818        set (_${_PYTHON_PREFIX}_Interpreter_REASON_FAILURE "Wrong architecture for the interpreter \"${_${_PYTHON_PREFIX}_EXECUTABLE}\"" PARENT_SCOPE)
819      endif()
820#HM3      set_property (CACHE _${_PYTHON_PREFIX}_EXECUTABLE PROPERTY VALUE "${_PYTHON_PREFIX}_EXECUTABLE-NOTFOUND")
821      return()
822    endif()
823  endif()
824endfunction()
825
826
827function (_PYTHON_VALIDATE_COMPILER)
828  if (NOT _${_PYTHON_PREFIX}_COMPILER)
829    return()
830  endif()
831
832  cmake_parse_arguments (PARSE_ARGV 0 _PVC "EXACT;CHECK_EXISTS" "" "")
833  if (_PVC_UNPARSED_ARGUMENTS)
834    set (major_version FALSE)
835    set (expected_version "${_PVC_UNPARSED_ARGUMENTS}")
836  else()
837    set (major_version TRUE)
838    set (expected_version "${_${_PYTHON_PREFIX}_REQUIRED_VERSION_MAJOR}")
839    set (_PVC_EXACT TRUE)
840  endif()
841
842  if (_PVC_CHECK_EXISTS AND NOT EXISTS "${_${_PYTHON_PREFIX}_COMPILER}")
843    # Compiler does not exist anymore
844    set (_${_PYTHON_PREFIX}_Compiler_REASON_FAILURE "Cannot find the compiler \"${_${_PYTHON_PREFIX}_COMPILER}\"" PARENT_SCOPE)
845#HM3    set_property (CACHE _${_PYTHON_PREFIX}_COMPILER PROPERTY VALUE "${_PYTHON_PREFIX}_COMPILER-NOTFOUND")
846    return()
847  endif()
848
849  _python_get_launcher (launcher COMPILER)
850
851  # retrieve python environment version from compiler
852  set (working_dir "${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/PythonCompilerVersion.dir")
853  if (major_version)
854    # check only major version
855    file (WRITE "${working_dir}/version.py" "import sys; sys.stdout.write(str(sys.version_info[0]))")
856  else()
857    # compute number of components for version
858    string (REGEX REPLACE "[^.]" "" dots "${expected_version}")
859    # add one dot because there is one dot less than there are components
860    string (LENGTH "${dots}." count)
861    if (count GREATER 3)
862      set (count 3)
863    endif()
864    file (WRITE "${working_dir}/version.py" "import sys; sys.stdout.write('.'.join([str(x) for x in sys.version_info[:${count}]]))\n")
865  endif()
866  execute_process (COMMAND ${launcher} "${_${_PYTHON_PREFIX}_COMPILER}"
867                           ${_${_PYTHON_PREFIX}_IRON_PYTHON_COMPILER_ARCH_FLAGS}
868                           /target:exe /embed "${working_dir}/version.py"
869                   WORKING_DIRECTORY "${working_dir}"
870                   OUTPUT_QUIET
871                   ERROR_QUIET
872                   OUTPUT_STRIP_TRAILING_WHITESPACE)
873  get_filename_component (ir_dir "${_${_PYTHON_PREFIX}_COMPILER}" DIRECTORY)
874  execute_process (COMMAND "${CMAKE_COMMAND}" -E env "MONO_PATH=${ir_dir}"
875                                              ${${_PYTHON_PREFIX}_DOTNET_LAUNCHER} "${working_dir}/version.exe"
876                   WORKING_DIRECTORY "${working_dir}"
877                   RESULT_VARIABLE result
878                   OUTPUT_VARIABLE version
879                   ERROR_QUIET)
880  file (REMOVE_RECURSE "${working_dir}")
881  if (result)
882    # compiler is not usable
883    set (_${_PYTHON_PREFIX}_Compiler_REASON_FAILURE "Cannot use the compiler \"${_${_PYTHON_PREFIX}_COMPILER}\"" PARENT_SCOPE)
884#HM3    set_property (CACHE _${_PYTHON_PREFIX}_COMPILER PROPERTY VALUE "${_PYTHON_PREFIX}_COMPILER-NOTFOUND")
885  elseif ((_PVC_EXACT AND NOT version VERSION_EQUAL expected_version)
886          OR NOT version VERSION_GREATER_EQUAL expected_version)
887    # Compiler has wrong version
888    set (_${_PYTHON_PREFIX}_Compiler_REASON_FAILURE "Wrong version for the compiler \"${_${_PYTHON_PREFIX}_COMPILER}\"" PARENT_SCOPE)
889#HM3    set_property (CACHE _${_PYTHON_PREFIX}_COMPILER PROPERTY VALUE "${_PYTHON_PREFIX}_COMPILER-NOTFOUND")
890  endif()
891endfunction()
892
893
894function (_PYTHON_VALIDATE_LIBRARY)
895  if (NOT _${_PYTHON_PREFIX}_LIBRARY_RELEASE)
896    unset (_${_PYTHON_PREFIX}_LIBRARY_DEBUG)
897    return()
898  endif()
899
900  cmake_parse_arguments (PARSE_ARGV 0 _PVL "EXACT;CHECK_EXISTS" "" "")
901  if (_PVL_UNPARSED_ARGUMENTS)
902    set (expected_version ${_PVL_UNPARSED_ARGUMENTS})
903  else()
904    unset (expected_version)
905  endif()
906
907  if (_PVL_CHECK_EXISTS AND NOT EXISTS "${_${_PYTHON_PREFIX}_LIBRARY_RELEASE}")
908    # library does not exist anymore
909    set (_${_PYTHON_PREFIX}_Development_REASON_FAILURE "Cannot find the library \"${_${_PYTHON_PREFIX}_LIBRARY_RELEASE}\"" PARENT_SCOPE)
910#HM3    set_property (CACHE _${_PYTHON_PREFIX}_LIBRARY_RELEASE PROPERTY VALUE "${_PYTHON_PREFIX}_LIBRARY_RELEASE-NOTFOUND")
911    if (WIN32)
912#HM3      set_property (CACHE _${_PYTHON_PREFIX}_LIBRARY_DEBUG PROPERTY VALUE "${_PYTHON_PREFIX}_LIBRARY_DEBUG-NOTFOUND")
913    endif()
914#HM3    set_property (CACHE _${_PYTHON_PREFIX}_INCLUDE_DIR PROPERTY VALUE "${_PYTHON_PREFIX}_INCLUDE_DIR-NOTFOUND")
915    return()
916  endif()
917
918  # retrieve version and abi from library name
919  _python_get_version (LIBRARY PREFIX lib_)
920
921  if (DEFINED _${_PYTHON_PREFIX}_FIND_ABI AND NOT lib_ABI IN_LIST _${_PYTHON_PREFIX}_ABIFLAGS)
922    # incompatible ABI
923    set (_${_PYTHON_PREFIX}_Development_REASON_FAILURE "Wrong ABI for the library \"${_${_PYTHON_PREFIX}_LIBRARY_RELEASE}\"" PARENT_SCOPE)
924#HM3    set_property (CACHE _${_PYTHON_PREFIX}_LIBRARY_RELEASE PROPERTY VALUE "${_PYTHON_PREFIX}_LIBRARY_RELEASE-NOTFOUND")
925  else()
926    if (expected_version)
927      # library have only major.minor information
928      string (REGEX MATCH "[0-9](\\.[0-9]+)?" version "${expected_version}")
929      if ((_PVL_EXACT AND NOT lib_VERSION VERSION_EQUAL version) OR (lib_VERSION VERSION_LESS version))
930        # library has wrong version
931        set (_${_PYTHON_PREFIX}_Development_REASON_FAILURE "Wrong version for the library \"${_${_PYTHON_PREFIX}_LIBRARY_RELEASE}\"" PARENT_SCOPE)
932#HM3        set_property (CACHE _${_PYTHON_PREFIX}_LIBRARY_RELEASE PROPERTY VALUE "${_PYTHON_PREFIX}_LIBRARY_RELEASE-NOTFOUND")
933      endif()
934    else()
935      if (NOT lib_VERSION_MAJOR VERSION_EQUAL _${_PYTHON_PREFIX}_REQUIRED_VERSION_MAJOR)
936        # library has wrong major version
937        set (_${_PYTHON_PREFIX}_Development_REASON_FAILURE "Wrong major version for the library \"${_${_PYTHON_PREFIX}_LIBRARY_RELEASE}\"" PARENT_SCOPE)
938#HM3        set_property (CACHE _${_PYTHON_PREFIX}_LIBRARY_RELEASE PROPERTY VALUE "${_PYTHON_PREFIX}_LIBRARY_RELEASE-NOTFOUND")
939      endif()
940    endif()
941  endif()
942
943  if (NOT _${_PYTHON_PREFIX}_LIBRARY_RELEASE)
944    if (WIN32)
945#HM3      set_property (CACHE _${_PYTHON_PREFIX}_LIBRARY_DEBUG PROPERTY VALUE "${_PYTHON_PREFIX}_LIBRARY_DEBUG-NOTFOUND")
946    endif()
947    unset (_${_PYTHON_PREFIX}_RUNTIME_LIBRARY_RELEASE CACHE)
948    unset (_${_PYTHON_PREFIX}_RUNTIME_LIBRARY_DEBUG CACHE)
949#HM3    set_property (CACHE _${_PYTHON_PREFIX}_INCLUDE_DIR PROPERTY VALUE "${_PYTHON_PREFIX}_INCLUDE_DIR-NOTFOUND")
950  endif()
951endfunction()
952
953
954function (_PYTHON_VALIDATE_INCLUDE_DIR)
955  if (NOT _${_PYTHON_PREFIX}_INCLUDE_DIR)
956    return()
957  endif()
958
959  cmake_parse_arguments (PARSE_ARGV 0 _PVID "EXACT;CHECK_EXISTS" "" "")
960  if (_PVID_UNPARSED_ARGUMENTS)
961    set (expected_version ${_PVID_UNPARSED_ARGUMENTS})
962  else()
963    unset (expected_version)
964  endif()
965
966  if (_PVID_CHECK_EXISTS AND NOT EXISTS "${_${_PYTHON_PREFIX}_INCLUDE_DIR}")
967    # include file does not exist anymore
968    set (_${_PYTHON_PREFIX}_Development_REASON_FAILURE "Cannot find the directory \"${_${_PYTHON_PREFIX}_INCLUDE_DIR}\"" PARENT_SCOPE)
969#HM3    set_property (CACHE _${_PYTHON_PREFIX}_INCLUDE_DIR PROPERTY VALUE "${_PYTHON_PREFIX}_INCLUDE_DIR-NOTFOUND")
970    return()
971  endif()
972
973  # retrieve version from header file
974  _python_get_version (INCLUDE PREFIX inc_)
975
976  if (DEFINED _${_PYTHON_PREFIX}_FIND_ABI AND NOT inc_ABI IN_LIST _${_PYTHON_PREFIX}_ABIFLAGS)
977    # incompatible ABI
978    set (_${_PYTHON_PREFIX}_Development_REASON_FAILURE "Wrong ABI for the directory \"${_${_PYTHON_PREFIX}_INCLUDE_DIR}\"" PARENT_SCOPE)
979#HM3    set_property (CACHE _${_PYTHON_PREFIX}_INCLUDE_DIR PROPERTY VALUE "${_PYTHON_PREFIX}_INCLUDE_DIR-NOTFOUND")
980  else()
981    if (expected_version)
982      if ((_PVID_EXACT AND NOT inc_VERSION VERSION_EQUAL expected_version) OR (inc_VERSION VERSION_LESS expected_version))
983        # include dir has wrong version
984        set (_${_PYTHON_PREFIX}_Development_REASON_FAILURE "Wrong version for the directory \"${_${_PYTHON_PREFIX}_INCLUDE_DIR}\"" PARENT_SCOPE)
985#HM3        set_property (CACHE _${_PYTHON_PREFIX}_INCLUDE_DIR PROPERTY VALUE "${_PYTHON_PREFIX}_INCLUDE_DIR-NOTFOUND")
986      endif()
987    else()
988      if (NOT inc_VERSION_MAJOR VERSION_EQUAL _${_PYTHON_PREFIX}_REQUIRED_VERSION_MAJOR)
989        # include dir has wrong major version
990        set (_${_PYTHON_PREFIX}_Development_REASON_FAILURE "Wrong major version for the directory \"${_${_PYTHON_PREFIX}_INCLUDE_DIR}\"" PARENT_SCOPE)
991#HM3        set_property (CACHE _${_PYTHON_PREFIX}_INCLUDE_DIR PROPERTY VALUE "${_PYTHON_PREFIX}_INCLUDE_DIR-NOTFOUND")
992      endif()
993    endif()
994  endif()
995endfunction()
996
997
998function (_PYTHON_FIND_RUNTIME_LIBRARY _PYTHON_LIB)
999  string (REPLACE "_RUNTIME" "" _PYTHON_LIB "${_PYTHON_LIB}")
1000  # look at runtime part on systems supporting it
1001  if (CMAKE_SYSTEM_NAME STREQUAL "Windows" OR
1002      (CMAKE_SYSTEM_NAME MATCHES "MSYS|CYGWIN"
1003        AND ${_PYTHON_LIB} MATCHES "${CMAKE_IMPORT_LIBRARY_SUFFIX}$"))
1004    set (CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_SHARED_LIBRARY_SUFFIX})
1005    # MSYS has a special syntax for runtime libraries
1006    if (CMAKE_SYSTEM_NAME MATCHES "MSYS")
1007      list (APPEND CMAKE_FIND_LIBRARY_PREFIXES "msys-")
1008    endif()
1009    find_library (${ARGV})
1010  endif()
1011endfunction()
1012
1013
1014function (_PYTHON_SET_LIBRARY_DIRS _PYTHON_SLD_RESULT)
1015  unset (_PYTHON_DIRS)
1016  set (_PYTHON_LIBS ${ARGN})
1017  foreach (_PYTHON_LIB IN LISTS _PYTHON_LIBS)
1018    if (${_PYTHON_LIB})
1019      get_filename_component (_PYTHON_DIR "${${_PYTHON_LIB}}" DIRECTORY)
1020      list (APPEND _PYTHON_DIRS "${_PYTHON_DIR}")
1021    endif()
1022  endforeach()
1023  if (_PYTHON_DIRS)
1024  list (REMOVE_DUPLICATES _PYTHON_DIRS)
1025  endif()
1026  set (${_PYTHON_SLD_RESULT} ${_PYTHON_DIRS} PARENT_SCOPE)
1027endfunction()
1028
1029
1030function (_PYTHON_SET_DEVELOPMENT_MODULE_FOUND module)
1031  if ("Development.${module}" IN_LIST ${_PYTHON_PREFIX}_FIND_COMPONENTS)
1032    string(TOUPPER "${module}" id)
1033    set (module_found TRUE)
1034
1035    if ("LIBRARY" IN_LIST _${_PYTHON_PREFIX}_FIND_DEVELOPMENT_${id}_ARTIFACTS
1036        AND NOT _${_PYTHON_PREFIX}_LIBRARY_RELEASE)
1037      set (module_found FALSE)
1038    endif()
1039    if ("INCLUDE_DIR" IN_LIST _${_PYTHON_PREFIX}_FIND_DEVELOPMENT_${id}_ARTIFACTS
1040        AND NOT _${_PYTHON_PREFIX}_INCLUDE_DIR)
1041      set (module_found FALSE)
1042    endif()
1043
1044    set (${_PYTHON_PREFIX}_Development.${module}_FOUND ${module_found} PARENT_SCOPE)
1045  endif()
1046endfunction()
1047
1048
1049# If major version is specified, it must be the same as internal major version
1050if (DEFINED ${_PYTHON_PREFIX}_FIND_VERSION_MAJOR
1051    AND NOT ${_PYTHON_PREFIX}_FIND_VERSION_MAJOR VERSION_EQUAL _${_PYTHON_PREFIX}_REQUIRED_VERSION_MAJOR)
1052  _python_display_failure ("Could NOT find ${_PYTHON_PREFIX}: Wrong major version specified is \"${${_PYTHON_PREFIX}_FIND_VERSION_MAJOR}\", but expected major version is \"${_${_PYTHON_PREFIX}_REQUIRED_VERSION_MAJOR}\"")
1053endif()
1054
1055
1056# handle components
1057if (NOT ${_PYTHON_PREFIX}_FIND_COMPONENTS)
1058  set (${_PYTHON_PREFIX}_FIND_COMPONENTS Interpreter)
1059  set (${_PYTHON_PREFIX}_FIND_REQUIRED_Interpreter TRUE)
1060endif()
1061if ("NumPy" IN_LIST ${_PYTHON_PREFIX}_FIND_COMPONENTS)
1062  list (APPEND ${_PYTHON_PREFIX}_FIND_COMPONENTS "Interpreter" "Development.Module")
1063endif()
1064if ("Development" IN_LIST ${_PYTHON_PREFIX}_FIND_COMPONENTS)
1065  list (APPEND ${_PYTHON_PREFIX}_FIND_COMPONENTS "Development.Module" "Development.Embed")
1066endif()
1067list (REMOVE_DUPLICATES ${_PYTHON_PREFIX}_FIND_COMPONENTS)
1068foreach (_${_PYTHON_PREFIX}_COMPONENT IN ITEMS Interpreter Compiler Development Development.Module Development.Embed NumPy)
1069  set (${_PYTHON_PREFIX}_${_${_PYTHON_PREFIX}_COMPONENT}_FOUND FALSE)
1070endforeach()
1071if (${_PYTHON_PREFIX}_FIND_REQUIRED_Development)
1072  set (${_PYTHON_PREFIX}_FIND_REQUIRED_Development.Module TRUE)
1073  set (${_PYTHON_PREFIX}_FIND_REQUIRED_Development.Embed TRUE)
1074endif()
1075
1076unset (_${_PYTHON_PREFIX}_FIND_DEVELOPMENT_ARTIFACTS)
1077unset (_${_PYTHON_PREFIX}_FIND_DEVELOPMENT_MODULE_ARTIFACTS)
1078unset (_${_PYTHON_PREFIX}_FIND_DEVELOPMENT_EMBED_ARTIFACTS)
1079if ("Development.Module" IN_LIST ${_PYTHON_PREFIX}_FIND_COMPONENTS)
1080  if (CMAKE_SYSTEM_NAME MATCHES "^(Windows.*|CYGWIN|MSYS)$")
1081    list (APPEND _${_PYTHON_PREFIX}_FIND_DEVELOPMENT_MODULE_ARTIFACTS "LIBRARY")
1082  endif()
1083  list (APPEND _${_PYTHON_PREFIX}_FIND_DEVELOPMENT_MODULE_ARTIFACTS "INCLUDE_DIR")
1084endif()
1085if ("Development.Embed" IN_LIST ${_PYTHON_PREFIX}_FIND_COMPONENTS)
1086  list (APPEND _${_PYTHON_PREFIX}_FIND_DEVELOPMENT_EMBED_ARTIFACTS "LIBRARY" "INCLUDE_DIR")
1087endif()
1088set (_${_PYTHON_PREFIX}_FIND_DEVELOPMENT_ARTIFACTS ${_${_PYTHON_PREFIX}_FIND_DEVELOPMENT_MODULE_ARTIFACTS} ${_${_PYTHON_PREFIX}_FIND_DEVELOPMENT_EMBED_ARTIFACTS})
1089list (REMOVE_DUPLICATES _${_PYTHON_PREFIX}_FIND_DEVELOPMENT_ARTIFACTS)
1090
1091# Set versions to search
1092## default: search any version
1093set (_${_PYTHON_PREFIX}_FIND_VERSIONS ${_${_PYTHON_PREFIX}_VERSIONS})
1094unset (_${_PYTHON_PREFIX}_FIND_VERSION_EXACT)
1095
1096if (${_PYTHON_PREFIX}_FIND_VERSION_COUNT)
1097  if (${_PYTHON_PREFIX}_FIND_VERSION_EXACT)
1098    set (_${_PYTHON_PREFIX}_FIND_VERSION_EXACT "EXACT")
1099    set (_${_PYTHON_PREFIX}_FIND_VERSIONS ${${_PYTHON_PREFIX}_FIND_VERSION_MAJOR}.${${_PYTHON_PREFIX}_FIND_VERSION_MINOR})
1100  else()
1101    unset (_${_PYTHON_PREFIX}_FIND_VERSIONS)
1102    # add all compatible versions
1103    foreach (_${_PYTHON_PREFIX}_VERSION IN LISTS _${_PYTHON_PREFIX}_VERSIONS)
1104      if (_${_PYTHON_PREFIX}_VERSION VERSION_GREATER_EQUAL "${${_PYTHON_PREFIX}_FIND_VERSION_MAJOR}.${${_PYTHON_PREFIX}_FIND_VERSION_MINOR}")
1105        list (APPEND _${_PYTHON_PREFIX}_FIND_VERSIONS ${_${_PYTHON_PREFIX}_VERSION})
1106      endif()
1107    endforeach()
1108  endif()
1109endif()
1110
1111# Set ABIs to search
1112## default: search any ABI
1113if (_${_PYTHON_PREFIX}_REQUIRED_VERSION_MAJOR VERSION_LESS "3")
1114  # ABI not supported
1115  unset (_${_PYTHON_PREFIX}_FIND_ABI)
1116  set (_${_PYTHON_PREFIX}_ABIFLAGS "")
1117else()
1118  unset (_${_PYTHON_PREFIX}_FIND_ABI)
1119  unset (_${_PYTHON_PREFIX}_ABIFLAGS)
1120  if (DEFINED ${_PYTHON_PREFIX}_FIND_ABI)
1121    # normalization
1122    string (TOUPPER "${${_PYTHON_PREFIX}_FIND_ABI}" _${_PYTHON_PREFIX}_FIND_ABI)
1123#FIXME!
1124#HM3    list (TRANSFORM _${_PYTHON_PREFIX}_FIND_ABI REPLACE "^(TRUE|Y(ES)?|1)$" "ON")
1125#HM3    list (TRANSFORM _${_PYTHON_PREFIX}_FIND_ABI REPLACE "^(FALSE|N(O)?|0)$" "OFF")
1126    MESSAGE(STATUS ${_PYTHON_PREFIX}_FIND_ABI)
1127    set(temp "")
1128    foreach(f ${_PYTHON_PREFIX}_FIND_ABI )
1129        string(REGEX REPLACE "^(TRUE|Y(ES)?|1)$" "ON" zone ${f} )
1130        string(REGEX REPLACE "^(FALSE|N(O)?|0)$" "OFF" ztwo ${zone} )
1131        list(PREPEND temp "${ztwo}")
1132    endforeach()
1133    set(_${_PYTHON_PREFIX}_FIND_ABI "${temp}")
1134    MESSAGE(STATUS ${_PYTHON_PREFIX}_FIND_ABI)
1135    if (NOT _${_PYTHON_PREFIX}_FIND_ABI MATCHES "^(ON|OFF|ANY);(ON|OFF|ANY);(ON|OFF|ANY)$")
1136      message (AUTHOR_WARNING "Find${_PYTHON_PREFIX}: ${${_PYTHON_PREFIX}_FIND_ABI}: invalid value for '${_PYTHON_PREFIX}_FIND_ABI'. Ignore it")
1137      unset (_${_PYTHON_PREFIX}_FIND_ABI)
1138    endif()
1139    _python_get_abiflags (_${_PYTHON_PREFIX}_ABIFLAGS)
1140  endif()
1141endif()
1142unset (${_PYTHON_PREFIX}_SOABI)
1143
1144# Define lookup strategy
1145if (_${_PYTHON_PREFIX}_LOOKUP_POLICY STREQUAL "NEW")
1146  set (_${_PYTHON_PREFIX}_FIND_STRATEGY "LOCATION")
1147else()
1148  set (_${_PYTHON_PREFIX}_FIND_STRATEGY "VERSION")
1149endif()
1150if (DEFINED ${_PYTHON_PREFIX}_FIND_STRATEGY)
1151  if (NOT ${_PYTHON_PREFIX}_FIND_STRATEGY MATCHES "^(VERSION|LOCATION)$")
1152    message (AUTHOR_WARNING "Find${_PYTHON_PREFIX}: ${${_PYTHON_PREFIX}_FIND_STRATEGY}: invalid value for '${_PYTHON_PREFIX}_FIND_STRATEGY'. 'VERSION' or 'LOCATION' expected.")
1153    set (_${_PYTHON_PREFIX}_FIND_STRATEGY "VERSION")
1154  else()
1155    set (_${_PYTHON_PREFIX}_FIND_STRATEGY "${${_PYTHON_PREFIX}_FIND_STRATEGY}")
1156  endif()
1157endif()
1158
1159# Python and Anaconda distributions: define which architectures can be used
1160if (CMAKE_SIZEOF_VOID_P)
1161  # In this case, search only for 64bit or 32bit
1162  math (EXPR _${_PYTHON_PREFIX}_ARCH "${CMAKE_SIZEOF_VOID_P} * 8")
1163  set (_${_PYTHON_PREFIX}_ARCH2 ${_${_PYTHON_PREFIX}_ARCH})
1164else()
1165  # architecture unknown, search for both 64bit and 32bit
1166  set (_${_PYTHON_PREFIX}_ARCH 64)
1167  set (_${_PYTHON_PREFIX}_ARCH2 32)
1168endif()
1169
1170# IronPython support
1171unset (_${_PYTHON_PREFIX}_IRON_PYTHON_INTERPRETER_NAMES)
1172unset (_${_PYTHON_PREFIX}_IRON_PYTHON_COMPILER_NAMES)
1173unset (_${_PYTHON_PREFIX}_IRON_PYTHON_COMPILER_ARCH_FLAGS)
1174if (CMAKE_SIZEOF_VOID_P)
1175  if (_${_PYTHON_PREFIX}_ARCH EQUAL "32")
1176    set (_${_PYTHON_PREFIX}_IRON_PYTHON_COMPILER_ARCH_FLAGS "/platform:x86")
1177  else()
1178    set (_${_PYTHON_PREFIX}_IRON_PYTHON_COMPILER_ARCH_FLAGS "/platform:x64")
1179  endif()
1180endif()
1181if (NOT CMAKE_SYSTEM_NAME STREQUAL "Linux")
1182  # Do not use wrapper script on Linux because it is buggy: -c interpreter option cannot be used
1183  list (APPEND _${_PYTHON_PREFIX}_IRON_PYTHON_INTERPRETER_NAMES "ipy${_${_PYTHON_PREFIX}_REQUIRED_VERSION_MAJOR}" "ipy64" "ipy32" "ipy")
1184  list (APPEND _${_PYTHON_PREFIX}_IRON_PYTHON_COMPILER_NAMES "ipyc")
1185endif()
1186list (APPEND _${_PYTHON_PREFIX}_IRON_PYTHON_INTERPRETER_NAMES "ipy.exe")
1187list (APPEND _${_PYTHON_PREFIX}_IRON_PYTHON_COMPILER_NAMES "ipyc.exe")
1188set (_${_PYTHON_PREFIX}_IRON_PYTHON_PATH_SUFFIXES net45 net40 bin)
1189
1190# PyPy support
1191if (_${_PYTHON_PREFIX}_REQUIRED_VERSION_MAJOR EQUAL "3")
1192  set (_${_PYTHON_PREFIX}_PYPY_NAMES pypy3)
1193  set (_${_PYTHON_PREFIX}_PYPY_LIB_NAMES pypy3-c)
1194  if (WIN32)
1195    # special name for runtime part
1196    list (APPEND _${_PYTHON_PREFIX}_PYPY_LIB_NAMES libpypy3-c)
1197  endif()
1198  set (_${_PYTHON_PREFIX}_PYPY_INCLUDE_PATH_SUFFIXES lib/pypy3)
1199else()
1200  set (_${_PYTHON_PREFIX}_PYPY_NAMES pypy)
1201  set (_${_PYTHON_PREFIX}_PYPY_LIB_NAMES pypy-c)
1202  if (WIN32)
1203    # special name for runtime part
1204    list (APPEND _${_PYTHON_PREFIX}_PYPY_LIB_NAMES libpypy-c)
1205  endif()
1206  set (_${_PYTHON_PREFIX}_PYPY_INCLUDE_PATH_SUFFIXES lib/pypy)
1207endif()
1208set (_${_PYTHON_PREFIX}_PYPY_EXECUTABLE_PATH_SUFFIXES bin)
1209set (_${_PYTHON_PREFIX}_PYPY_LIBRARY_PATH_SUFFIXES lib libs bin)
1210list (APPEND _${_PYTHON_PREFIX}_PYPY_INCLUDE_PATH_SUFFIXES include)
1211
1212# Python Implementations handling
1213unset (_${_PYTHON_PREFIX}_FIND_IMPLEMENTATIONS)
1214if (DEFINED ${_PYTHON_PREFIX}_FIND_IMPLEMENTATIONS)
1215  foreach (_${_PYTHON_PREFIX}_IMPLEMENTATION IN LISTS ${_PYTHON_PREFIX}_FIND_IMPLEMENTATIONS)
1216    if (NOT _${_PYTHON_PREFIX}_IMPLEMENTATION MATCHES "^(CPython|IronPython|PyPy)$")
1217      message (AUTHOR_WARNING "Find${_PYTHON_PREFIX}: ${_${_PYTHON_PREFIX}_IMPLEMENTATION}: invalid value for '${_PYTHON_PREFIX}_FIND_IMPLEMENTATIONS'. 'CPython', 'IronPython' or 'PyPy' expected. Value will be ignored.")
1218    else()
1219      list (APPEND _${_PYTHON_PREFIX}_FIND_IMPLEMENTATIONS ${_${_PYTHON_PREFIX}_IMPLEMENTATION})
1220    endif()
1221  endforeach()
1222else()
1223  if (WIN32)
1224    set (_${_PYTHON_PREFIX}_FIND_IMPLEMENTATIONS CPython IronPython)
1225  else()
1226    set (_${_PYTHON_PREFIX}_FIND_IMPLEMENTATIONS CPython)
1227  endif()
1228endif()
1229
1230# compute list of names for header file
1231unset (_${_PYTHON_PREFIX}_INCLUDE_NAMES)
1232foreach (_${_PYTHON_PREFIX}_IMPLEMENTATION IN LISTS _${_PYTHON_PREFIX}_FIND_IMPLEMENTATIONS)
1233  if (_${_PYTHON_PREFIX}_IMPLEMENTATION STREQUAL "CPython")
1234    list (APPEND _${_PYTHON_PREFIX}_INCLUDE_NAMES "Python.h")
1235  elseif (_${_PYTHON_PREFIX}_IMPLEMENTATION STREQUAL "PyPy")
1236    list (APPEND _${_PYTHON_PREFIX}_INCLUDE_NAMES "PyPy.h")
1237  endif()
1238endforeach()
1239
1240
1241# Apple frameworks handling
1242_python_find_frameworks ()
1243
1244set (_${_PYTHON_PREFIX}_FIND_FRAMEWORK "FIRST")
1245
1246if (DEFINED ${_PYTHON_PREFIX}_FIND_FRAMEWORK)
1247  if (NOT ${_PYTHON_PREFIX}_FIND_FRAMEWORK MATCHES "^(FIRST|LAST|NEVER)$")
1248    message (AUTHOR_WARNING "Find${_PYTHON_PREFIX}: ${${_PYTHON_PREFIX}_FIND_FRAMEWORK}: invalid value for '${_PYTHON_PREFIX}_FIND_FRAMEWORK'. 'FIRST', 'LAST' or 'NEVER' expected. 'FIRST' will be used instead.")
1249  else()
1250    set (_${_PYTHON_PREFIX}_FIND_FRAMEWORK ${${_PYTHON_PREFIX}_FIND_FRAMEWORK})
1251  endif()
1252elseif (DEFINED CMAKE_FIND_FRAMEWORK)
1253  if (CMAKE_FIND_FRAMEWORK STREQUAL "ONLY")
1254    message (AUTHOR_WARNING "Find${_PYTHON_PREFIX}: CMAKE_FIND_FRAMEWORK: 'ONLY' value is not supported. 'FIRST' will be used instead.")
1255  elseif (NOT CMAKE_FIND_FRAMEWORK MATCHES "^(FIRST|LAST|NEVER)$")
1256    message (AUTHOR_WARNING "Find${_PYTHON_PREFIX}: ${CMAKE_FIND_FRAMEWORK}: invalid value for 'CMAKE_FIND_FRAMEWORK'. 'FIRST', 'LAST' or 'NEVER' expected. 'FIRST' will be used instead.")
1257  else()
1258    set (_${_PYTHON_PREFIX}_FIND_FRAMEWORK ${CMAKE_FIND_FRAMEWORK})
1259  endif()
1260endif()
1261
1262# Save CMAKE_FIND_APPBUNDLE
1263if (DEFINED CMAKE_FIND_APPBUNDLE)
1264  set (_${_PYTHON_PREFIX}_CMAKE_FIND_APPBUNDLE ${CMAKE_FIND_APPBUNDLE})
1265else()
1266  unset (_${_PYTHON_PREFIX}_CMAKE_FIND_APPBUNDLE)
1267endif()
1268# To avoid app bundle lookup
1269set (CMAKE_FIND_APPBUNDLE "NEVER")
1270
1271# Save CMAKE_FIND_FRAMEWORK
1272if (DEFINED CMAKE_FIND_FRAMEWORK)
1273  set (_${_PYTHON_PREFIX}_CMAKE_FIND_FRAMEWORK ${CMAKE_FIND_FRAMEWORK})
1274else()
1275  unset (_${_PYTHON_PREFIX}_CMAKE_FIND_FRAMEWORK)
1276endif()
1277# To avoid framework lookup
1278set (CMAKE_FIND_FRAMEWORK "NEVER")
1279
1280# Windows Registry handling
1281if (DEFINED ${_PYTHON_PREFIX}_FIND_REGISTRY)
1282  if (NOT ${_PYTHON_PREFIX}_FIND_REGISTRY MATCHES "^(FIRST|LAST|NEVER)$")
1283    message (AUTHOR_WARNING "Find${_PYTHON_PREFIX}: ${${_PYTHON_PREFIX}_FIND_REGISTRY}: invalid value for '${_PYTHON_PREFIX}_FIND_REGISTRY'. 'FIRST', 'LAST' or 'NEVER' expected. 'FIRST' will be used instead.")
1284    set (_${_PYTHON_PREFIX}_FIND_REGISTRY "FIRST")
1285  else()
1286    set (_${_PYTHON_PREFIX}_FIND_REGISTRY ${${_PYTHON_PREFIX}_FIND_REGISTRY})
1287  endif()
1288else()
1289  set (_${_PYTHON_PREFIX}_FIND_REGISTRY "FIRST")
1290endif()
1291
1292# virtual environments recognition
1293if (DEFINED ENV{VIRTUAL_ENV} OR DEFINED ENV{CONDA_PREFIX})
1294  if (DEFINED ${_PYTHON_PREFIX}_FIND_VIRTUALENV)
1295    if (NOT ${_PYTHON_PREFIX}_FIND_VIRTUALENV MATCHES "^(FIRST|ONLY|STANDARD)$")
1296      message (AUTHOR_WARNING "Find${_PYTHON_PREFIX}: ${${_PYTHON_PREFIX}_FIND_VIRTUALENV}: invalid value for '${_PYTHON_PREFIX}_FIND_VIRTUALENV'. 'FIRST', 'ONLY' or 'STANDARD' expected. 'FIRST' will be used instead.")
1297      set (_${_PYTHON_PREFIX}_FIND_VIRTUALENV "FIRST")
1298    else()
1299      set (_${_PYTHON_PREFIX}_FIND_VIRTUALENV ${${_PYTHON_PREFIX}_FIND_VIRTUALENV})
1300    endif()
1301  else()
1302    set (_${_PYTHON_PREFIX}_FIND_VIRTUALENV FIRST)
1303  endif()
1304else()
1305  set (_${_PYTHON_PREFIX}_FIND_VIRTUALENV STANDARD)
1306endif()
1307
1308
1309# Compute search signature
1310# This signature will be used to check validity of cached variables on new search
1311set (_${_PYTHON_PREFIX}_SIGNATURE "${${_PYTHON_PREFIX}_ROOT_DIR}:${_${_PYTHON_PREFIX}_FIND_IMPLEMENTATIONS}:${_${_PYTHON_PREFIX}_FIND_STRATEGY}:${${_PYTHON_PREFIX}_FIND_VIRTUALENV}")
1312if (NOT WIN32)
1313  string (APPEND _${_PYTHON_PREFIX}_SIGNATURE ":${${_PYTHON_PREFIX}_USE_STATIC_LIBS}:")
1314endif()
1315if (CMAKE_HOST_APPLE)
1316  string (APPEND _${_PYTHON_PREFIX}_SIGNATURE ":${_${_PYTHON_PREFIX}_FIND_FRAMEWORK}")
1317endif()
1318if (CMAKE_HOST_WIN32)
1319  string (APPEND _${_PYTHON_PREFIX}_SIGNATURE ":${_${_PYTHON_PREFIX}_FIND_REGISTRY}")
1320endif()
1321
1322function (_PYTHON_CHECK_DEVELOPMENT_SIGNATURE module)
1323  if ("Development.${module}" IN_LIST ${_PYTHON_PREFIX}_FIND_COMPONENTS)
1324    string (TOUPPER "${module}" id)
1325    set (signature "${_${_PYTHON_PREFIX}_SIGNATURE}:")
1326    if ("LIBRARY" IN_LIST _${_PYTHON_PREFIX}_FIND_DEVELOPMENT_${id}_ARTIFACTS)
1327      list (APPEND signature "${_${_PYTHON_PREFIX}_LIBRARY_RELEASE}:")
1328    endif()
1329    if ("INCLUDE_DIR" IN_LIST _${_PYTHON_PREFIX}_FIND_DEVELOPMENT_${id}_ARTIFACTS)
1330      list (APPEND signature "${_${_PYTHON_PREFIX}_INCLUDE_DIR}:")
1331    endif()
1332    string (MD5 signature "${signature}")
1333    if (signature STREQUAL _${_PYTHON_PREFIX}_DEVELOPMENT_${id}_SIGNATURE)
1334      if ("LIBRARY" IN_LIST _${_PYTHON_PREFIX}_FIND_DEVELOPMENT_${id}_ARTIFACTS)
1335        _python_validate_library (${${_PYTHON_PREFIX}_FIND_VERSION}
1336                                  ${_${_PYTHON_PREFIX}_FIND_VERSION_EXACT}
1337                                  CHECK_EXISTS)
1338      endif()
1339      if ("INCLUDE_DIR" IN_LIST _${_PYTHON_PREFIX}_FIND_DEVELOPMENT_${id}_ARTIFACTS)
1340        _python_validate_include_dir (${${_PYTHON_PREFIX}_FIND_VERSION}
1341                                      ${_${_PYTHON_PREFIX}_FIND_VERSION_EXACT}
1342                                      CHECK_EXISTS)
1343      endif()
1344    else()
1345      if ("LIBRARY" IN_LIST _${_PYTHON_PREFIX}_FIND_DEVELOPMENT_${id}_ARTIFACTS)
1346        unset (_${_PYTHON_PREFIX}_LIBRARY_RELEASE CACHE)
1347        unset (_${_PYTHON_PREFIX}_LIBRARY_DEBUG CACHE)
1348      endif()
1349      if ("INCLUDE_DIR" IN_LIST _${_PYTHON_PREFIX}_FIND_DEVELOPMENT_${id}_ARTIFACTS)
1350        unset (_${_PYTHON_PREFIX}_INCLUDE_DIR CACHE)
1351      endif()
1352    endif()
1353    if (("LIBRARY" IN_LIST _${_PYTHON_PREFIX}_FIND_DEVELOPMENT_${id}_ARTIFACTS
1354          AND NOT _${_PYTHON_PREFIX}_LIBRARY_RELEASE)
1355        OR ("INCLUDE_DIR" IN_LIST _${_PYTHON_PREFIX}_FIND_DEVELOPMENT_${id}_ARTIFACTS
1356          AND NOT _${_PYTHON_PREFIX}_INCLUDE_DIR))
1357      unset (_${_PYTHON_PREFIX}_CONFIG CACHE)
1358      unset (_${_PYTHON_PREFIX}_DEVELOPMENT_${id}_SIGNATURE CACHE)
1359    endif()
1360  endif()
1361endfunction()
1362
1363function (_PYTHON_COMPUTE_DEVELOPMENT_SIGNATURE module)
1364  string (TOUPPER "${module}" id)
1365  if (${_PYTHON_PREFIX}_Development.${module}_FOUND)
1366    set (signature "${_${_PYTHON_PREFIX}_SIGNATURE}:")
1367    if ("LIBRARY" IN_LIST _${_PYTHON_PREFIX}_FIND_DEVELOPMENT_${id}_ARTIFACTS)
1368      list (APPEND signature "${_${_PYTHON_PREFIX}_LIBRARY_RELEASE}:")
1369    endif()
1370    if ("INCLUDE_DIR" IN_LIST _${_PYTHON_PREFIX}_FIND_DEVELOPMENT_${id}_ARTIFACTS)
1371      list (APPEND signature "${_${_PYTHON_PREFIX}_INCLUDE_DIR}:")
1372    endif()
1373    string (MD5 signature "${signature}")
1374#HM3    set (_${_PYTHON_PREFIX}_DEVELOPMENT_${id}_SIGNATURE "${signature}" CACHE INTERNAL "")
1375  else()
1376    unset (_${_PYTHON_PREFIX}_DEVELOPMENT_${id}_SIGNATURE CACHE)
1377  endif()
1378endfunction()
1379
1380
1381unset (_${_PYTHON_PREFIX}_REQUIRED_VARS)
1382unset (_${_PYTHON_PREFIX}_CACHED_VARS)
1383unset (_${_PYTHON_PREFIX}_Interpreter_REASON_FAILURE)
1384unset (_${_PYTHON_PREFIX}_Compiler_REASON_FAILURE)
1385unset (_${_PYTHON_PREFIX}_Development_REASON_FAILURE)
1386unset (_${_PYTHON_PREFIX}_NumPy_REASON_FAILURE)
1387
1388
1389# preamble
1390## For IronPython on platforms other than Windows, search for the .Net interpreter
1391if ("IronPython" IN_LIST _${_PYTHON_PREFIX}_FIND_IMPLEMENTATIONS
1392    AND NOT WIN32)
1393  find_program (${_PYTHON_PREFIX}_DOTNET_LAUNCHER
1394                NAMES "mono")
1395endif()
1396
1397
1398# first step, search for the interpreter
1399if ("Interpreter" IN_LIST ${_PYTHON_PREFIX}_FIND_COMPONENTS)
1400#HM3  list (APPEND _${_PYTHON_PREFIX}_CACHED_VARS _${_PYTHON_PREFIX}_EXECUTABLE
1401#HM3                                              _${_PYTHON_PREFIX}_INTERPRETER_PROPERTIES)
1402  if (${_PYTHON_PREFIX}_FIND_REQUIRED_Interpreter)
1403    list (APPEND _${_PYTHON_PREFIX}_REQUIRED_VARS ${_PYTHON_PREFIX}_EXECUTABLE)
1404  endif()
1405
1406  if (DEFINED ${_PYTHON_PREFIX}_EXECUTABLE
1407      AND IS_ABSOLUTE "${${_PYTHON_PREFIX}_EXECUTABLE}")
1408    if (NOT ${_PYTHON_PREFIX}_EXECUTABLE STREQUAL _${_PYTHON_PREFIX}_EXECUTABLE)
1409      # invalidate cache properties
1410      unset (_${_PYTHON_PREFIX}_INTERPRETER_PROPERTIES CACHE)
1411    endif()
1412#HM3    set (_${_PYTHON_PREFIX}_EXECUTABLE "${${_PYTHON_PREFIX}_EXECUTABLE}" CACHE INTERNAL "")
1413  elseif (DEFINED _${_PYTHON_PREFIX}_EXECUTABLE)
1414    # compute interpreter signature and check validity of definition
1415    string (MD5 __${_PYTHON_PREFIX}_INTERPRETER_SIGNATURE "${_${_PYTHON_PREFIX}_SIGNATURE}:${_${_PYTHON_PREFIX}_EXECUTABLE}")
1416    if (__${_PYTHON_PREFIX}_INTERPRETER_SIGNATURE STREQUAL _${_PYTHON_PREFIX}_INTERPRETER_SIGNATURE)
1417      # check version validity
1418      if (${_PYTHON_PREFIX}_FIND_VERSION_EXACT)
1419        _python_validate_interpreter (${${_PYTHON_PREFIX}_FIND_VERSION} EXACT CHECK_EXISTS)
1420      else()
1421        _python_validate_interpreter (${${_PYTHON_PREFIX}_FIND_VERSION} CHECK_EXISTS)
1422      endif()
1423    else()
1424      unset (_${_PYTHON_PREFIX}_EXECUTABLE CACHE)
1425    endif()
1426    if (NOT _${_PYTHON_PREFIX}_EXECUTABLE)
1427      unset (_${_PYTHON_PREFIX}_INTERPRETER_SIGNATURE CACHE)
1428      unset (_${_PYTHON_PREFIX}_INTERPRETER_PROPERTIES CACHE)
1429    endif()
1430  endif()
1431
1432  if (NOT _${_PYTHON_PREFIX}_EXECUTABLE)
1433    set (_${_PYTHON_PREFIX}_HINTS "${${_PYTHON_PREFIX}_ROOT_DIR}" ENV ${_PYTHON_PREFIX}_ROOT_DIR)
1434
1435    if (_${_PYTHON_PREFIX}_FIND_STRATEGY STREQUAL "LOCATION")
1436      # build all executable names
1437      _python_get_names (_${_PYTHON_PREFIX}_NAMES VERSION ${_${_PYTHON_PREFIX}_FIND_VERSIONS} POSIX INTERPRETER)
1438      _python_get_path_suffixes (_${_PYTHON_PREFIX}_PATH_SUFFIXES VERSION ${_${_PYTHON_PREFIX}_FIND_VERSIONS} INTERPRETER)
1439
1440      # Framework Paths
1441      _python_get_frameworks (_${_PYTHON_PREFIX}_FRAMEWORK_PATHS VERSION ${_${_PYTHON_PREFIX}_FIND_VERSIONS})
1442      # Registry Paths
1443      _python_get_registries (_${_PYTHON_PREFIX}_REGISTRY_PATHS VERSION ${_${_PYTHON_PREFIX}_FIND_VERSIONS})
1444
1445      while (TRUE)
1446        # Virtual environments handling
1447        if (_${_PYTHON_PREFIX}_FIND_VIRTUALENV MATCHES "^(FIRST|ONLY)$")
1448          find_program (_${_PYTHON_PREFIX}_EXECUTABLE
1449                        NAMES ${_${_PYTHON_PREFIX}_NAMES}
1450                        NAMES_PER_DIR
1451                        HINTS ${_${_PYTHON_PREFIX}_HINTS}
1452                        PATHS ENV VIRTUAL_ENV ENV CONDA_PREFIX
1453                        PATH_SUFFIXES ${_${_PYTHON_PREFIX}_PATH_SUFFIXES}
1454                        NO_CMAKE_PATH
1455                        NO_CMAKE_ENVIRONMENT_PATH
1456                        NO_SYSTEM_ENVIRONMENT_PATH
1457                        NO_CMAKE_SYSTEM_PATH)
1458
1459          _python_validate_interpreter (${${_PYTHON_PREFIX}_FIND_VERSION} ${_${_PYTHON_PREFIX}_FIND_VERSION_EXACT})
1460          if (_${_PYTHON_PREFIX}_EXECUTABLE)
1461            break()
1462          endif()
1463          if (_${_PYTHON_PREFIX}_FIND_VIRTUALENV STREQUAL "ONLY")
1464            break()
1465          endif()
1466        endif()
1467
1468        # Apple frameworks handling
1469        if (CMAKE_HOST_APPLE AND _${_PYTHON_PREFIX}_FIND_FRAMEWORK STREQUAL "FIRST")
1470          find_program (_${_PYTHON_PREFIX}_EXECUTABLE
1471                        NAMES ${_${_PYTHON_PREFIX}_NAMES}
1472                        NAMES_PER_DIR
1473                        HINTS ${_${_PYTHON_PREFIX}_HINTS}
1474                        PATHS ${_${_PYTHON_PREFIX}_FRAMEWORK_PATHS}
1475                        PATH_SUFFIXES ${_${_PYTHON_PREFIX}_PATH_SUFFIXES}
1476                        NO_CMAKE_PATH
1477                        NO_CMAKE_ENVIRONMENT_PATH
1478                        NO_SYSTEM_ENVIRONMENT_PATH
1479                        NO_CMAKE_SYSTEM_PATH)
1480          _python_validate_interpreter (${${_PYTHON_PREFIX}_FIND_VERSION} ${_${_PYTHON_PREFIX}_FIND_VERSION_EXACT})
1481          if (_${_PYTHON_PREFIX}_EXECUTABLE)
1482            break()
1483          endif()
1484        endif()
1485        # Windows registry
1486        if (CMAKE_HOST_WIN32 AND _${_PYTHON_PREFIX}_FIND_REGISTRY STREQUAL "FIRST")
1487          find_program (_${_PYTHON_PREFIX}_EXECUTABLE
1488                        NAMES ${_${_PYTHON_PREFIX}_NAMES}
1489                        NAMES_PER_DIR
1490                        HINTS ${_${_PYTHON_PREFIX}_HINTS}
1491                        PATHS ${_${_PYTHON_PREFIX}_REGISTRY_PATHS}
1492                        PATH_SUFFIXES ${_${_PYTHON_PREFIX}_PATH_SUFFIXES}
1493                        NO_SYSTEM_ENVIRONMENT_PATH
1494                        NO_CMAKE_SYSTEM_PATH)
1495          _python_validate_interpreter (${${_PYTHON_PREFIX}_FIND_VERSION} ${_${_PYTHON_PREFIX}_FIND_VERSION_EXACT})
1496          if (_${_PYTHON_PREFIX}_EXECUTABLE)
1497            break()
1498          endif()
1499        endif()
1500
1501        # try using HINTS
1502        find_program (_${_PYTHON_PREFIX}_EXECUTABLE
1503                      NAMES ${_${_PYTHON_PREFIX}_NAMES}
1504                      NAMES_PER_DIR
1505                      HINTS ${_${_PYTHON_PREFIX}_HINTS}
1506                      PATH_SUFFIXES ${_${_PYTHON_PREFIX}_PATH_SUFFIXES}
1507                      NO_SYSTEM_ENVIRONMENT_PATH
1508                      NO_CMAKE_SYSTEM_PATH)
1509        _python_validate_interpreter (${${_PYTHON_PREFIX}_FIND_VERSION} ${_${_PYTHON_PREFIX}_FIND_VERSION_EXACT})
1510        if (_${_PYTHON_PREFIX}_EXECUTABLE)
1511          break()
1512        endif()
1513        # try using standard paths
1514        find_program (_${_PYTHON_PREFIX}_EXECUTABLE
1515                      NAMES ${_${_PYTHON_PREFIX}_NAMES}
1516                      NAMES_PER_DIR
1517                      PATH_SUFFIXES ${_${_PYTHON_PREFIX}_PATH_SUFFIXES})
1518        _python_validate_interpreter (${${_PYTHON_PREFIX}_FIND_VERSION} ${_${_PYTHON_PREFIX}_FIND_VERSION_EXACT})
1519        if (_${_PYTHON_PREFIX}_EXECUTABLE)
1520          break()
1521        endif()
1522
1523        # Apple frameworks handling
1524        if (CMAKE_HOST_APPLE AND _${_PYTHON_PREFIX}_FIND_FRAMEWORK STREQUAL "LAST")
1525          find_program (_${_PYTHON_PREFIX}_EXECUTABLE
1526                        NAMES ${_${_PYTHON_PREFIX}_NAMES}
1527                        NAMES_PER_DIR
1528                        PATHS ${_${_PYTHON_PREFIX}_FRAMEWORK_PATHS}
1529                        PATH_SUFFIXES ${_${_PYTHON_PREFIX}_PATH_SUFFIXES}
1530                        NO_DEFAULT_PATH)
1531          _python_validate_interpreter (${${_PYTHON_PREFIX}_FIND_VERSION} ${_${_PYTHON_PREFIX}_FIND_VERSION_EXACT})
1532          if (_${_PYTHON_PREFIX}_EXECUTABLE)
1533            break()
1534          endif()
1535        endif()
1536        # Windows registry
1537        if (CMAKE_HOST_WIN32 AND _${_PYTHON_PREFIX}_FIND_REGISTRY STREQUAL "LAST")
1538          find_program (_${_PYTHON_PREFIX}_EXECUTABLE
1539                        NAMES ${_${_PYTHON_PREFIX}_NAMES}
1540                        NAMES_PER_DIR
1541                        PATHS ${_${_PYTHON_PREFIX}_REGISTRY_PATHS}
1542                        PATH_SUFFIXES ${_${_PYTHON_PREFIX}_PATH_SUFFIXES}
1543                        NO_DEFAULT_PATH)
1544          _python_validate_interpreter (${${_PYTHON_PREFIX}_FIND_VERSION} ${_${_PYTHON_PREFIX}_FIND_VERSION_EXACT})
1545          if (_${_PYTHON_PREFIX}_EXECUTABLE)
1546            break()
1547          endif()
1548        endif()
1549
1550        break()
1551      endwhile()
1552    else()
1553      # look-up for various versions and locations
1554      foreach (_${_PYTHON_PREFIX}_VERSION IN LISTS _${_PYTHON_PREFIX}_FIND_VERSIONS)
1555        _python_get_names (_${_PYTHON_PREFIX}_NAMES VERSION ${_${_PYTHON_PREFIX}_VERSION} POSIX INTERPRETER)
1556        _python_get_path_suffixes (_${_PYTHON_PREFIX}_PATH_SUFFIXES VERSION ${_${_PYTHON_PREFIX}_VERSION} INTERPRETER)
1557
1558        _python_get_frameworks (_${_PYTHON_PREFIX}_FRAMEWORK_PATHS VERSION ${_${_PYTHON_PREFIX}_VERSION})
1559        _python_get_registries (_${_PYTHON_PREFIX}_REGISTRY_PATHS VERSION ${_${_PYTHON_PREFIX}_VERSION})
1560
1561        # Virtual environments handling
1562        if (_${_PYTHON_PREFIX}_FIND_VIRTUALENV MATCHES "^(FIRST|ONLY)$")
1563          find_program (_${_PYTHON_PREFIX}_EXECUTABLE
1564                        NAMES ${_${_PYTHON_PREFIX}_NAMES}
1565                        NAMES_PER_DIR
1566                        HINTS ${_${_PYTHON_PREFIX}_HINTS}
1567                        PATHS ENV VIRTUAL_ENV ENV CONDA_PREFIX
1568                        PATH_SUFFIXES ${_${_PYTHON_PREFIX}_PATH_SUFFIXES}
1569                        NO_CMAKE_PATH
1570                        NO_CMAKE_ENVIRONMENT_PATH
1571                        NO_SYSTEM_ENVIRONMENT_PATH
1572                        NO_CMAKE_SYSTEM_PATH)
1573          _python_validate_interpreter (${_${_PYTHON_PREFIX}_VERSION} EXACT)
1574          if (_${_PYTHON_PREFIX}_EXECUTABLE)
1575            break()
1576          endif()
1577          if (_${_PYTHON_PREFIX}_FIND_VIRTUALENV STREQUAL "ONLY")
1578            continue()
1579          endif()
1580        endif()
1581
1582        # Apple frameworks handling
1583        if (CMAKE_HOST_APPLE AND _${_PYTHON_PREFIX}_FIND_FRAMEWORK STREQUAL "FIRST")
1584          find_program (_${_PYTHON_PREFIX}_EXECUTABLE
1585                        NAMES ${_${_PYTHON_PREFIX}_NAMES}
1586                        NAMES_PER_DIR
1587                        HINTS ${_${_PYTHON_PREFIX}_HINTS}
1588                        PATHS ${_${_PYTHON_PREFIX}_FRAMEWORK_PATHS}
1589                        PATH_SUFFIXES ${_${_PYTHON_PREFIX}_PATH_SUFFIXES}
1590                        NO_CMAKE_PATH
1591                        NO_CMAKE_ENVIRONMENT_PATH
1592                        NO_SYSTEM_ENVIRONMENT_PATH
1593                        NO_CMAKE_SYSTEM_PATH)
1594        endif()
1595
1596        # Windows registry
1597        if (CMAKE_HOST_WIN32 AND _${_PYTHON_PREFIX}_FIND_REGISTRY STREQUAL "FIRST")
1598          find_program (_${_PYTHON_PREFIX}_EXECUTABLE
1599                        NAMES ${_${_PYTHON_PREFIX}_NAMES}
1600                        NAMES_PER_DIR
1601                        HINTS ${_${_PYTHON_PREFIX}_HINTS}
1602                        PATHS ${_${_PYTHON_PREFIX}_REGISTRY_PATHS}
1603                        PATH_SUFFIXES ${_${_PYTHON_PREFIX}_PATH_SUFFIXES}
1604                        NO_SYSTEM_ENVIRONMENT_PATH
1605                        NO_CMAKE_SYSTEM_PATH)
1606        endif()
1607
1608        _python_validate_interpreter (${_${_PYTHON_PREFIX}_VERSION} EXACT)
1609        if (_${_PYTHON_PREFIX}_EXECUTABLE)
1610          break()
1611        endif()
1612
1613        # try using HINTS
1614        find_program (_${_PYTHON_PREFIX}_EXECUTABLE
1615                      NAMES ${_${_PYTHON_PREFIX}_NAMES}
1616                      NAMES_PER_DIR
1617                      HINTS ${_${_PYTHON_PREFIX}_HINTS}
1618                      PATH_SUFFIXES ${_${_PYTHON_PREFIX}_PATH_SUFFIXES}
1619                      NO_SYSTEM_ENVIRONMENT_PATH
1620                      NO_CMAKE_SYSTEM_PATH)
1621        _python_validate_interpreter (${_${_PYTHON_PREFIX}_VERSION} EXACT)
1622        if (_${_PYTHON_PREFIX}_EXECUTABLE)
1623          break()
1624        endif()
1625        # try using standard paths.
1626        # NAMES_PER_DIR is not defined on purpose to have a chance to find
1627        # expected version.
1628        # For example, typical systems have 'python' for version 2.* and 'python3'
1629        # for version 3.*. So looking for names per dir will find, potentially,
1630        # systematically 'python' (i.e. version 2) even if version 3 is searched.
1631        find_program (_${_PYTHON_PREFIX}_EXECUTABLE
1632                      NAMES ${_${_PYTHON_PREFIX}_NAMES}
1633                      PATH_SUFFIXES ${_${_PYTHON_PREFIX}_PATH_SUFFIXES})
1634        _python_validate_interpreter (${_${_PYTHON_PREFIX}_VERSION} EXACT)
1635        if (_${_PYTHON_PREFIX}_EXECUTABLE)
1636          break()
1637        endif()
1638
1639        # Apple frameworks handling
1640        if (CMAKE_HOST_APPLE AND _${_PYTHON_PREFIX}_FIND_FRAMEWORK STREQUAL "LAST")
1641          find_program (_${_PYTHON_PREFIX}_EXECUTABLE
1642                        NAMES ${_${_PYTHON_PREFIX}_NAMES}
1643                        NAMES_PER_DIR
1644                        PATHS ${_${_PYTHON_PREFIX}_FRAMEWORK_PATHS}
1645                        PATH_SUFFIXES ${_${_PYTHON_PREFIX}_PATH_SUFFIXES}
1646                        NO_DEFAULT_PATH)
1647        endif()
1648
1649        # Windows registry
1650        if (CMAKE_HOST_WIN32 AND _${_PYTHON_PREFIX}_FIND_REGISTRY STREQUAL "LAST")
1651          find_program (_${_PYTHON_PREFIX}_EXECUTABLE
1652                        NAMES ${_${_PYTHON_PREFIX}_NAMES}
1653                        NAMES_PER_DIR
1654                        PATHS ${_${_PYTHON_PREFIX}_REGISTRY_PATHS}
1655                        PATH_SUFFIXES ${_${_PYTHON_PREFIX}_PATH_SUFFIXES}
1656                        NO_DEFAULT_PATH)
1657        endif()
1658
1659        _python_validate_interpreter (${_${_PYTHON_PREFIX}_VERSION} EXACT)
1660        if (_${_PYTHON_PREFIX}_EXECUTABLE)
1661          break()
1662        endif()
1663      endforeach()
1664
1665      if (NOT _${_PYTHON_PREFIX}_EXECUTABLE AND
1666          NOT _${_PYTHON_PREFIX}_FIND_VIRTUALENV STREQUAL "ONLY")
1667        # No specific version found. Retry with generic names and standard paths.
1668        # NAMES_PER_DIR is not defined on purpose to have a chance to find
1669        # expected version.
1670        # For example, typical systems have 'python' for version 2.* and 'python3'
1671        # for version 3.*. So looking for names per dir will find, potentially,
1672        # systematically 'python' (i.e. version 2) even if version 3 is searched.
1673        _python_get_names (_${_PYTHON_PREFIX}_NAMES POSIX INTERPRETER)
1674        find_program (_${_PYTHON_PREFIX}_EXECUTABLE
1675                      NAMES ${_${_PYTHON_PREFIX}_NAMES})
1676        _python_validate_interpreter ()
1677      endif()
1678    endif()
1679  endif()
1680
1681  set (${_PYTHON_PREFIX}_EXECUTABLE "${_${_PYTHON_PREFIX}_EXECUTABLE}")
1682  _python_get_launcher (_${_PYTHON_PREFIX}_INTERPRETER_LAUNCHER INTERPRETER)
1683
1684  # retrieve exact version of executable found
1685  if (_${_PYTHON_PREFIX}_EXECUTABLE)
1686    execute_process (COMMAND ${_${_PYTHON_PREFIX}_INTERPRETER_LAUNCHER} "${_${_PYTHON_PREFIX}_EXECUTABLE}" -c
1687                             "import sys; sys.stdout.write('.'.join([str(x) for x in sys.version_info[:3]]))"
1688                     RESULT_VARIABLE _${_PYTHON_PREFIX}_RESULT
1689                     OUTPUT_VARIABLE ${_PYTHON_PREFIX}_VERSION
1690                     ERROR_QUIET
1691                     OUTPUT_STRIP_TRAILING_WHITESPACE)
1692    if (NOT _${_PYTHON_PREFIX}_RESULT)
1693      set (_${_PYTHON_PREFIX}_EXECUTABLE_USABLE TRUE)
1694    else()
1695      # Interpreter is not usable
1696      set (_${_PYTHON_PREFIX}_EXECUTABLE_USABLE FALSE)
1697      unset (${_PYTHON_PREFIX}_VERSION)
1698      set (_${_PYTHON_PREFIX}_Interpreter_REASON_FAILURE "Cannot run the interpreter \"${_${_PYTHON_PREFIX}_EXECUTABLE}\"")
1699    endif()
1700  endif()
1701
1702  if (_${_PYTHON_PREFIX}_EXECUTABLE AND _${_PYTHON_PREFIX}_EXECUTABLE_USABLE)
1703    if (_${_PYTHON_PREFIX}_INTERPRETER_PROPERTIES)
1704      set (${_PYTHON_PREFIX}_Interpreter_FOUND TRUE)
1705
1706      list (GET _${_PYTHON_PREFIX}_INTERPRETER_PROPERTIES 0 ${_PYTHON_PREFIX}_INTERPRETER_ID)
1707
1708      list (GET _${_PYTHON_PREFIX}_INTERPRETER_PROPERTIES 1 ${_PYTHON_PREFIX}_VERSION_MAJOR)
1709      list (GET _${_PYTHON_PREFIX}_INTERPRETER_PROPERTIES 2 ${_PYTHON_PREFIX}_VERSION_MINOR)
1710      list (GET _${_PYTHON_PREFIX}_INTERPRETER_PROPERTIES 3 ${_PYTHON_PREFIX}_VERSION_PATCH)
1711
1712      list (GET _${_PYTHON_PREFIX}_INTERPRETER_PROPERTIES 4 _${_PYTHON_PREFIX}_ARCH)
1713      set (_${_PYTHON_PREFIX}_ARCH2 ${_${_PYTHON_PREFIX}_ARCH})
1714
1715      list (GET _${_PYTHON_PREFIX}_INTERPRETER_PROPERTIES 5 _${_PYTHON_PREFIX}_ABIFLAGS)
1716      list (GET _${_PYTHON_PREFIX}_INTERPRETER_PROPERTIES 6 ${_PYTHON_PREFIX}_SOABI)
1717
1718      list (GET _${_PYTHON_PREFIX}_INTERPRETER_PROPERTIES 7 ${_PYTHON_PREFIX}_STDLIB)
1719      list (GET _${_PYTHON_PREFIX}_INTERPRETER_PROPERTIES 8 ${_PYTHON_PREFIX}_STDARCH)
1720      list (GET _${_PYTHON_PREFIX}_INTERPRETER_PROPERTIES 9 ${_PYTHON_PREFIX}_SITELIB)
1721      list (GET _${_PYTHON_PREFIX}_INTERPRETER_PROPERTIES 10 ${_PYTHON_PREFIX}_SITEARCH)
1722    else()
1723      string (REGEX MATCHALL "[0-9]+" _${_PYTHON_PREFIX}_VERSIONS "${${_PYTHON_PREFIX}_VERSION}")
1724      list (GET _${_PYTHON_PREFIX}_VERSIONS 0 ${_PYTHON_PREFIX}_VERSION_MAJOR)
1725      list (GET _${_PYTHON_PREFIX}_VERSIONS 1 ${_PYTHON_PREFIX}_VERSION_MINOR)
1726      list (GET _${_PYTHON_PREFIX}_VERSIONS 2 ${_PYTHON_PREFIX}_VERSION_PATCH)
1727
1728      if (${_PYTHON_PREFIX}_VERSION_MAJOR VERSION_EQUAL _${_PYTHON_PREFIX}_REQUIRED_VERSION_MAJOR)
1729        set (${_PYTHON_PREFIX}_Interpreter_FOUND TRUE)
1730
1731        # Use interpreter version and ABI for future searches to ensure consistency
1732        set (_${_PYTHON_PREFIX}_FIND_VERSIONS ${${_PYTHON_PREFIX}_VERSION_MAJOR}.${${_PYTHON_PREFIX}_VERSION_MINOR})
1733        execute_process (COMMAND ${_${_PYTHON_PREFIX}_INTERPRETR_LAUNCHER} "${_${_PYTHON_PREFIX}_EXECUTABLE}" -c
1734                                 "import sys; sys.stdout.write(sys.abiflags)"
1735                         RESULT_VARIABLE _${_PYTHON_PREFIX}_RESULT
1736                         OUTPUT_VARIABLE _${_PYTHON_PREFIX}_ABIFLAGS
1737                         ERROR_QUIET
1738                         OUTPUT_STRIP_TRAILING_WHITESPACE)
1739        if (_${_PYTHON_PREFIX}_RESULT)
1740          # assunme ABI is not supported
1741          set (_${_PYTHON_PREFIX}_ABIFLAGS "")
1742        endif()
1743      endif()
1744
1745      if (${_PYTHON_PREFIX}_Interpreter_FOUND)
1746        unset (_${_PYTHON_PREFIX}_Interpreter_REASON_FAILURE)
1747
1748        # compute and save interpreter signature
1749        string (MD5 __${_PYTHON_PREFIX}_INTERPRETER_SIGNATURE "${_${_PYTHON_PREFIX}_SIGNATURE}:${_${_PYTHON_PREFIX}_EXECUTABLE}")
1750#HM3        set (_${_PYTHON_PREFIX}_INTERPRETER_SIGNATURE "${__${_PYTHON_PREFIX}_INTERPRETER_SIGNATURE}" CACHE INTERNAL "")
1751
1752        if (NOT CMAKE_SIZEOF_VOID_P)
1753          # determine interpreter architecture
1754          execute_process (COMMAND ${_${_PYTHON_PREFIX}_INTERPRETER_LAUNCHER} "${_${_PYTHON_PREFIX}_EXECUTABLE}" -c
1755                                   "import sys; sys.stdout.write(str(sys.maxsize > 2**32))"
1756                           RESULT_VARIABLE _${_PYTHON_PREFIX}_RESULT
1757                           OUTPUT_VARIABLE ${_PYTHON_PREFIX}_IS64BIT
1758                           ERROR_VARIABLE ${_PYTHON_PREFIX}_IS64BIT)
1759          if (NOT _${_PYTHON_PREFIX}_RESULT)
1760            if (${_PYTHON_PREFIX}_IS64BIT)
1761              set (_${_PYTHON_PREFIX}_ARCH 64)
1762              set (_${_PYTHON_PREFIX}_ARCH2 64)
1763            else()
1764              set (_${_PYTHON_PREFIX}_ARCH 32)
1765              set (_${_PYTHON_PREFIX}_ARCH2 32)
1766            endif()
1767          endif()
1768        endif()
1769
1770        # retrieve interpreter identity
1771        execute_process (COMMAND ${_${_PYTHON_PREFIX}_INTERPRETER_LAUNCHER} "${_${_PYTHON_PREFIX}_EXECUTABLE}" -V
1772                         RESULT_VARIABLE _${_PYTHON_PREFIX}_RESULT
1773                         OUTPUT_VARIABLE ${_PYTHON_PREFIX}_INTERPRETER_ID
1774                         ERROR_VARIABLE ${_PYTHON_PREFIX}_INTERPRETER_ID)
1775        if (NOT _${_PYTHON_PREFIX}_RESULT)
1776          if (${_PYTHON_PREFIX}_INTERPRETER_ID MATCHES "Anaconda")
1777            set (${_PYTHON_PREFIX}_INTERPRETER_ID "Anaconda")
1778          elseif (${_PYTHON_PREFIX}_INTERPRETER_ID MATCHES "Enthought")
1779            set (${_PYTHON_PREFIX}_INTERPRETER_ID "Canopy")
1780          elseif (${_PYTHON_PREFIX}_INTERPRETER_ID MATCHES "PyPy ([0-9.]+)")
1781            set (${_PYTHON_PREFIX}_INTERPRETER_ID "PyPy")
1782            set  (${_PYTHON_PREFIX}_PyPy_VERSION "${CMAKE_MATCH_1}")
1783          else()
1784            string (REGEX REPLACE "^([^ ]+).*" "\\1" ${_PYTHON_PREFIX}_INTERPRETER_ID "${${_PYTHON_PREFIX}_INTERPRETER_ID}")
1785            if (${_PYTHON_PREFIX}_INTERPRETER_ID STREQUAL "Python")
1786              # try to get a more precise ID
1787              execute_process (COMMAND ${_${_PYTHON_PREFIX}_INTERPRETER_LAUNCHER} "${_${_PYTHON_PREFIX}_EXECUTABLE}" -c
1788                                       "import sys; sys.stdout.write(sys.copyright)"
1789                               RESULT_VARIABLE _${_PYTHON_PREFIX}_RESULT
1790                               OUTPUT_VARIABLE ${_PYTHON_PREFIX}_COPYRIGHT
1791                               ERROR_QUIET)
1792              if (${_PYTHON_PREFIX}_COPYRIGHT MATCHES "ActiveState")
1793                set (${_PYTHON_PREFIX}_INTERPRETER_ID "ActivePython")
1794              endif()
1795            endif()
1796          endif()
1797        else()
1798          set (${_PYTHON_PREFIX}_INTERPRETER_ID Python)
1799        endif()
1800
1801        # retrieve various package installation directories
1802        execute_process (COMMAND ${_${_PYTHON_PREFIX}_INTERPRETER_LAUNCHER} "${_${_PYTHON_PREFIX}_EXECUTABLE}" -c
1803                                 "import sys\ntry:\n   from distutils import sysconfig\n   sys.stdout.write(';'.join([sysconfig.get_python_lib(plat_specific=False,standard_lib=True),sysconfig.get_python_lib(plat_specific=True,standard_lib=True),sysconfig.get_python_lib(plat_specific=False,standard_lib=False),sysconfig.get_python_lib(plat_specific=True,standard_lib=False)]))\nexcept Exception:\n   import sysconfig\n   sys.stdout.write(';'.join([sysconfig.get_path('stdlib'),sysconfig.get_path('platstdlib'),sysconfig.get_path('purelib'),sysconfig.get_path('platlib')]))"
1804                         RESULT_VARIABLE _${_PYTHON_PREFIX}_RESULT
1805                         OUTPUT_VARIABLE _${_PYTHON_PREFIX}_LIBPATHS
1806                         ERROR_QUIET)
1807        if (NOT _${_PYTHON_PREFIX}_RESULT)
1808          list (GET _${_PYTHON_PREFIX}_LIBPATHS 0 ${_PYTHON_PREFIX}_STDLIB)
1809          list (GET _${_PYTHON_PREFIX}_LIBPATHS 1 ${_PYTHON_PREFIX}_STDARCH)
1810          list (GET _${_PYTHON_PREFIX}_LIBPATHS 2 ${_PYTHON_PREFIX}_SITELIB)
1811          list (GET _${_PYTHON_PREFIX}_LIBPATHS 3 ${_PYTHON_PREFIX}_SITEARCH)
1812        else()
1813          unset (${_PYTHON_PREFIX}_STDLIB)
1814          unset (${_PYTHON_PREFIX}_STDARCH)
1815          unset (${_PYTHON_PREFIX}_SITELIB)
1816          unset (${_PYTHON_PREFIX}_SITEARCH)
1817        endif()
1818
1819        if (_${_PYTHON_PREFIX}_REQUIRED_VERSION_MAJOR VERSION_GREATER_EQUAL "3")
1820          _python_get_config_var (${_PYTHON_PREFIX}_SOABI SOABI)
1821        endif()
1822
1823        # store properties in the cache to speed-up future searches
1824#HM3        set (_${_PYTHON_PREFIX}_INTERPRETER_PROPERTIES
1825#HM3          "${${_PYTHON_PREFIX}_INTERPRETER_ID};${${_PYTHON_PREFIX}_VERSION_MAJOR};${${_PYTHON_PREFIX}_VERSION_MINOR};${${_PYTHON_PREFIX}_VERSION_PATCH};${_${_PYTHON_PREFIX}_ARCH};${_${_PYTHON_PREFIX}_ABIFLAGS};${${_PYTHON_PREFIX}_SOABI};${${_PYTHON_PREFIX}_STDLIB};${${_PYTHON_PREFIX}_STDARCH};${${_PYTHON_PREFIX}_SITELIB};${${_PYTHON_PREFIX}_SITEARCH}" CACHE INTERNAL "${_PYTHON_PREFIX} Properties")
1826      else()
1827        unset (_${_PYTHON_PREFIX}_INTERPRETER_SIGNATURE CACHE)
1828        unset (${_PYTHON_PREFIX}_INTERPRETER_ID)
1829      endif()
1830    endif()
1831  endif()
1832
1833  if (${_PYTHON_PREFIX}_ARTIFACTS_INTERACTIVE)
1834#HM3    set (${_PYTHON_PREFIX}_EXECUTABLE "${_${_PYTHON_PREFIX}_EXECUTABLE}" CACHE FILEPATH "${_PYTHON_PREFIX} Interpreter")
1835  endif()
1836
1837  _python_mark_as_internal (_${_PYTHON_PREFIX}_EXECUTABLE
1838                            _${_PYTHON_PREFIX}_INTERPRETER_PROPERTIES
1839                            _${_PYTHON_PREFIX}_INTERPRETER_SIGNATURE)
1840endif()
1841
1842
1843# second step, search for compiler (IronPython)
1844if ("Compiler" IN_LIST ${_PYTHON_PREFIX}_FIND_COMPONENTS)
1845#HM3  list (APPEND _${_PYTHON_PREFIX}_CACHED_VARS _${_PYTHON_PREFIX}_COMPILER)
1846  if (${_PYTHON_PREFIX}_FIND_REQUIRED_Compiler)
1847    list (APPEND _${_PYTHON_PREFIX}_REQUIRED_VARS ${_PYTHON_PREFIX}_COMPILER)
1848  endif()
1849
1850  if (NOT "IronPython" IN_LIST _${_PYTHON_PREFIX}_FIND_IMPLEMENTATIONS)
1851    unset (_${_PYTHON_PREFIX}_COMPILER CACHE)
1852    unset (_${_PYTHON_PREFIX}_COMPILER_SIGNATURE CACHE)
1853  elseif (DEFINED ${_PYTHON_PREFIX}_COMPILER
1854      AND IS_ABSOLUTE "${${_PYTHON_PREFIX}_COMPILER}")
1855#HM3    set (_${_PYTHON_PREFIX}_COMPILER "${${_PYTHON_PREFIX}_COMPILER}" CACHE INTERNAL "")
1856  elseif (DEFINED _${_PYTHON_PREFIX}_COMPILER)
1857    # compute compiler signature and check validity of definition
1858    string (MD5 __${_PYTHON_PREFIX}_COMPILER_SIGNATURE "${_${_PYTHON_PREFIX}_SIGNATURE}:${_${_PYTHON_PREFIX}_COMPILER}")
1859    if (__${_PYTHON_PREFIX}_COMPILER_SIGNATURE STREQUAL _${_PYTHON_PREFIX}_COMPILER_SIGNATURE)
1860      # check version validity
1861      if (${_PYTHON_PREFIX}_FIND_VERSION_EXACT)
1862        _python_validate_compiler (${${_PYTHON_PREFIX}_FIND_VERSION} EXACT CHECK_EXISTS)
1863      else()
1864        _python_validate_compiler (${${_PYTHON_PREFIX}_FIND_VERSION} CHECK_EXISTS)
1865      endif()
1866    else()
1867      unset (_${_PYTHON_PREFIX}_COMPILER CACHE)
1868      unset (_${_PYTHON_PREFIX}_COMPILER_SIGNATURE CACHE)
1869    endif()
1870  endif()
1871
1872  if ("IronPython" IN_LIST _${_PYTHON_PREFIX}_FIND_IMPLEMENTATIONS
1873      AND NOT _${_PYTHON_PREFIX}_COMPILER)
1874    # IronPython specific artifacts
1875    # If IronPython interpreter is found, use its path
1876    unset (_${_PYTHON_PREFIX}_IRON_ROOT)
1877    if (${_PYTHON_PREFIX}_Interpreter_FOUND AND ${_PYTHON_PREFIX}_INTERPRETER_ID STREQUAL "IronPython")
1878      get_filename_component (_${_PYTHON_PREFIX}_IRON_ROOT "${${_PYTHON_PREFIX}_EXECUTABLE}" DIRECTORY)
1879    endif()
1880
1881    if (_${_PYTHON_PREFIX}_FIND_STRATEGY STREQUAL "LOCATION")
1882      _python_get_names (_${_PYTHON_PREFIX}_COMPILER_NAMES
1883                         IMPLEMENTATIONS IronPython
1884                         VERSION ${_${_PYTHON_PREFIX}_FIND_VERSIONS}
1885                         COMPILER)
1886
1887      _python_get_path_suffixes (_${_PYTHON_PREFIX}_PATH_SUFFIXES
1888                                 IMPLEMENTATIONS IronPython
1889                                 VERSION ${_${_PYTHON_PREFIX}_FIND_VERSIONS}
1890                                 COMPILER)
1891
1892      _python_get_frameworks (_${_PYTHON_PREFIX}_FRAMEWORK_PATHS
1893                              IMPLEMENTATIONS IronPython
1894                              VERSION ${_${_PYTHON_PREFIX}_FIND_VERSIONS})
1895      _python_get_registries (_${_PYTHON_PREFIX}_REGISTRY_PATHS
1896                              IMPLEMENTATIONS IronPython
1897                              VERSION ${_${_PYTHON_PREFIX}_FIND_VERSIONS})
1898
1899      while (TRUE)
1900        # Apple frameworks handling
1901        if (CMAKE_HOST_APPLE AND _${_PYTHON_PREFIX}_FIND_FRAMEWORK STREQUAL "FIRST")
1902          find_program (_${_PYTHON_PREFIX}_COMPILER
1903                        NAMES ${_${_PYTHON_PREFIX}_COMPILER_NAMES}
1904                        NAMES_PER_DIR
1905                        HINTS ${_${_PYTHON_PREFIX}_IRON_ROOT} ${_${_PYTHON_PREFIX}_HINTS}
1906                        PATHS ${_${_PYTHON_PREFIX}_FRAMEWORK_PATHS}
1907                        PATH_SUFFIXES ${_${_PYTHON_PREFIX}_PATH_SUFFIXES}
1908                        NO_CMAKE_PATH
1909                        NO_CMAKE_ENVIRONMENT_PATH
1910                        NO_SYSTEM_ENVIRONMENT_PATH
1911                        NO_CMAKE_SYSTEM_PATH)
1912          _python_validate_compiler (${${_PYTHON_PREFIX}_FIND_VERSION} ${_${_PYTHON_PREFIX}_FIND_VERSION_EXACT})
1913          if (_${_PYTHON_PREFIX}_COMPILER)
1914            break()
1915          endif()
1916        endif()
1917        # Windows registry
1918        if (CMAKE_HOST_WIN32 AND _${_PYTHON_PREFIX}_FIND_REGISTRY STREQUAL "FIRST")
1919          find_program (_${_PYTHON_PREFIX}_COMPILER
1920                        NAMES ${_${_PYTHON_PREFIX}_COMPILER_NAMES}
1921                        NAMES_PER_DIR
1922                        HINTS ${_${_PYTHON_PREFIX}_IRON_ROOT} ${_${_PYTHON_PREFIX}_HINTS}
1923                        PATHS ${_${_PYTHON_PREFIX}_REGISTRY_PATHS}
1924                        PATH_SUFFIXES ${_${_PYTHON_PREFIX}_PATH_SUFFIXES}
1925                        NO_SYSTEM_ENVIRONMENT_PATH
1926                        NO_CMAKE_SYSTEM_PATH)
1927          _python_validate_compiler (${${_PYTHON_PREFIX}_FIND_VERSION} ${_${_PYTHON_PREFIX}_FIND_VERSION_EXACT})
1928          if (_${_PYTHON_PREFIX}_COMPILER)
1929            break()
1930          endif()
1931        endif()
1932
1933        # try using HINTS
1934        find_program (_${_PYTHON_PREFIX}_COMPILER
1935                      NAMES ${_${_PYTHON_PREFIX}_COMPILER_NAMES}
1936                      NAMES_PER_DIR
1937                      HINTS ${_${_PYTHON_PREFIX}_IRON_ROOT} ${_${_PYTHON_PREFIX}_HINTS}
1938                      PATH_SUFFIXES ${_${_PYTHON_PREFIX}_PATH_SUFFIXES}
1939                      NO_SYSTEM_ENVIRONMENT_PATH
1940                      NO_CMAKE_SYSTEM_PATH)
1941        _python_validate_compiler (${${_PYTHON_PREFIX}_FIND_VERSION} ${_${_PYTHON_PREFIX}_FIND_VERSION_EXACT})
1942        if (_${_PYTHON_PREFIX}_COMPILER)
1943          break()
1944        endif()
1945
1946        # try using standard paths
1947        find_program (_${_PYTHON_PREFIX}_COMPILER
1948                      NAMES ${_${_PYTHON_PREFIX}_COMPILER_NAMES}
1949                      NAMES_PER_DIR
1950                      PATH_SUFFIXES ${_${_PYTHON_PREFIX}_PATH_SUFFIXES})
1951        _python_validate_compiler (${${_PYTHON_PREFIX}_FIND_VERSION} ${_${_PYTHON_PREFIX}_FIND_VERSION_EXACT})
1952        if (_${_PYTHON_PREFIX}_COMPILER)
1953          break()
1954        endif()
1955
1956        # Apple frameworks handling
1957        if (CMAKE_HOST_APPLE AND _${_PYTHON_PREFIX}_FIND_FRAMEWORK STREQUAL "LAST")
1958          find_program (_${_PYTHON_PREFIX}_COMPILER
1959                        NAMES ${_${_PYTHON_PREFIX}_COMPILER_NAMES}
1960                        NAMES_PER_DIR
1961                        PATHS ${_${_PYTHON_PREFIX}_FRAMEWORK_PATHS}
1962                        PATH_SUFFIXES ${_${_PYTHON_PREFIX}_PATH_SUFFIXES}
1963                        NO_DEFAULT_PATH)
1964          _python_validate_compiler (${${_PYTHON_PREFIX}_FIND_VERSION} ${_${_PYTHON_PREFIX}_FIND_VERSION_EXACT})
1965          if (_${_PYTHON_PREFIX}_COMPILER)
1966            break()
1967          endif()
1968        endif()
1969        # Windows registry
1970        if (CMAKE_HOST_WIN32 AND _${_PYTHON_PREFIX}_FIND_REGISTRY STREQUAL "LAST")
1971          find_program (_${_PYTHON_PREFIX}_COMPILER
1972                        NAMES ${_${_PYTHON_PREFIX}_COMPILER_NAMES}
1973                        NAMES_PER_DIR
1974                        PATHS ${_${_PYTHON_PREFIX}_REGISTRY_PATHS}
1975                        PATH_SUFFIXES ${_${_PYTHON_PREFIX}_PATH_SUFFIXES}
1976                        NO_DEFAULT_PATH)
1977          if (_${_PYTHON_PREFIX}_COMPILER)
1978            break()
1979          endif()
1980        endif()
1981
1982        break()
1983      endwhile()
1984    else()
1985      # try using root dir and registry
1986      foreach (_${_PYTHON_PREFIX}_VERSION IN LISTS _${_PYTHON_PREFIX}_FIND_VERSIONS)
1987        _python_get_names (_${_PYTHON_PREFIX}_COMPILER_NAMES
1988                           IMPLEMENTATIONS IronPython
1989                           VERSION ${_${_PYTHON_PREFIX}_FIND_VERSIONS}
1990                           COMPILER)
1991
1992        _python_get_path_suffixes (_${_PYTHON_PREFIX}_PATH_SUFFIXES
1993                                   IMPLEMENTATIONS IronPython
1994                                   VERSION ${_${_PYTHON_PREFIX}_FIND_VERSION}
1995                                   COMPILER)
1996
1997        _python_get_frameworks (_${_PYTHON_PREFIX}_FRAMEWORK_PATHS
1998                                IMPLEMENTATIONS IronPython
1999                                VERSION ${_${_PYTHON_PREFIX}_VERSION})
2000        _python_get_registries (_${_PYTHON_PREFIX}_REGISTRY_PATHS
2001                                IMPLEMENTATIONS IronPython
2002                                VERSION ${_${_PYTHON_PREFIX}_VERSION})
2003
2004        # Apple frameworks handling
2005        if (CMAKE_HOST_APPLE AND _${_PYTHON_PREFIX}_FIND_FRAMEWORK STREQUAL "FIRST")
2006          find_program (_${_PYTHON_PREFIX}_COMPILER
2007                        NAMES ${_${_PYTHON_PREFIX}_COMPILER_NAMES}
2008                        NAMES_PER_DIR
2009                        HINTS ${_${_PYTHON_PREFIX}_IRON_ROOT} ${_${_PYTHON_PREFIX}_HINTS}
2010                        PATHS ${_${_PYTHON_PREFIX}_FRAMEWORK_PATHS}
2011                        PATH_SUFFIXES ${_${_PYTHON_PREFIX}_PATH_SUFFIXES}
2012                        NO_CMAKE_PATH
2013                        NO_CMAKE_ENVIRONMENT_PATH
2014                        NO_SYSTEM_ENVIRONMENT_PATH
2015                        NO_CMAKE_SYSTEM_PATH)
2016          _python_validate_compiler (${_${_PYTHON_PREFIX}_VERSION} EXACT)
2017          if (_${_PYTHON_PREFIX}_COMPILER)
2018            break()
2019          endif()
2020        endif()
2021        # Windows registry
2022        if (CMAKE_HOST_WIN32 AND _${_PYTHON_PREFIX}_FIND_REGISTRY STREQUAL "FIRST")
2023          find_program (_${_PYTHON_PREFIX}_COMPILER
2024                        NAMES ${_${_PYTHON_PREFIX}_COMPILER_NAMES}
2025                        NAMES_PER_DIR
2026                        HINTS ${_${_PYTHON_PREFIX}_IRON_ROOT} ${_${_PYTHON_PREFIX}_HINTS}
2027                        PATHS ${_${_PYTHON_PREFIX}_REGISTRY_PATHS}
2028                        PATH_SUFFIXES ${_${_PYTHON_PREFIX}_PATH_SUFFIXES}
2029                        NO_SYSTEM_ENVIRONMENT_PATH
2030                        NO_CMAKE_SYSTEM_PATH)
2031          _python_validate_compiler (${_${_PYTHON_PREFIX}_VERSION} EXACT)
2032          if (_${_PYTHON_PREFIX}_COMPILER)
2033            break()
2034          endif()
2035        endif()
2036
2037        # try using HINTS
2038        find_program (_${_PYTHON_PREFIX}_COMPILER
2039                      NAMES ${_${_PYTHON_PREFIX}_COMPILER_NAMES}
2040                      NAMES_PER_DIR
2041                      HINTS ${_${_PYTHON_PREFIX}_IRON_ROOT} ${_${_PYTHON_PREFIX}_HINTS}
2042                      PATH_SUFFIXES ${_${_PYTHON_PREFIX}_PATH_SUFFIXES}
2043                      NO_SYSTEM_ENVIRONMENT_PATH
2044                      NO_CMAKE_SYSTEM_PATH)
2045        _python_validate_compiler (${_${_PYTHON_PREFIX}_VERSION} EXACT)
2046        if (_${_PYTHON_PREFIX}_COMPILER)
2047          break()
2048        endif()
2049
2050        # Apple frameworks handling
2051        if (CMAKE_HOST_APPLE AND _${_PYTHON_PREFIX}_FIND_FRAMEWORK STREQUAL "LAST")
2052          find_program (_${_PYTHON_PREFIX}_COMPILER
2053                        NAMES ${_${_PYTHON_PREFIX}_COMPILER_NAMES}
2054                        NAMES_PER_DIR
2055                        PATHS ${_${_PYTHON_PREFIX}_FRAMEWORK_PATHS}
2056                        PATH_SUFFIXES ${_${_PYTHON_PREFIX}_PATH_SUFFIXES}
2057                        NO_DEFAULT_PATH)
2058          _python_validate_compiler (${_${_PYTHON_PREFIX}_VERSION} EXACT)
2059          if (_${_PYTHON_PREFIX}_COMPILER)
2060            break()
2061          endif()
2062        endif()
2063        # Windows registry
2064        if (CMAKE_HOST_WIN32 AND _${_PYTHON_PREFIX}_FIND_REGISTRY STREQUAL "LAST")
2065          find_program (_${_PYTHON_PREFIX}_COMPILER
2066                        NAMES ${_${_PYTHON_PREFIX}_COMPILER_NAMES}
2067                        NAMES_PER_DIR
2068                        PATHS ${_${_PYTHON_PREFIX}_REGISTRY_PATHS}
2069                        PATH_SUFFIXES ${_${_PYTHON_PREFIX}_PATH_SUFFIXES}
2070                        NO_DEFAULT_PATH)
2071          _python_validate_compiler (${_${_PYTHON_PREFIX}_VERSION} EXACT)
2072          if (_${_PYTHON_PREFIX}_COMPILER)
2073            break()
2074          endif()
2075        endif()
2076      endforeach()
2077
2078      # no specific version found, re-try in standard paths
2079      _python_get_names (_${_PYTHON_PREFIX}_COMPILER_NAMES
2080                         IMPLEMENTATIONS IronPython
2081                         VERSION ${_${_PYTHON_PREFIX}_FIND_VERSIONS}
2082                         COMPILER)
2083      _python_get_path_suffixes (_${_PYTHON_PREFIX}_PATH_SUFFIXES
2084                                 IMPLEMENTATIONS IronPython
2085                                 VERSION ${_${_PYTHON_PREFIX}_FIND_VERSIONS}
2086                                 COMPILER)
2087      find_program (_${_PYTHON_PREFIX}_COMPILER
2088                    NAMES ${_${_PYTHON_PREFIX}_COMPILER_NAMES}
2089                    HINTS ${_${_PYTHON_PREFIX}_IRON_ROOT} ${_${_PYTHON_PREFIX}_HINTS}
2090                    PATH_SUFFIXES ${_${_PYTHON_PREFIX}_PATH_SUFFIXES})
2091    endif()
2092  endif()
2093
2094  set (${_PYTHON_PREFIX}_COMPILER "${_${_PYTHON_PREFIX}_COMPILER}")
2095
2096  if (_${_PYTHON_PREFIX}_COMPILER)
2097    # retrieve python environment version from compiler
2098    _python_get_launcher (_${_PYTHON_PREFIX}_COMPILER_LAUNCHER COMPILER)
2099    set (_${_PYTHON_PREFIX}_VERSION_DIR "${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/PythonCompilerVersion.dir")
2100    file (WRITE "${_${_PYTHON_PREFIX}_VERSION_DIR}/version.py" "import sys; sys.stdout.write('.'.join([str(x) for x in sys.version_info[:3]]))\n")
2101    execute_process (COMMAND ${_${_PYTHON_PREFIX}_COMPILER_LAUNCHER} "${_${_PYTHON_PREFIX}_COMPILER}"
2102                             ${_${_PYTHON_PREFIX}_IRON_PYTHON_COMPILER_ARCH_FLAGS}
2103                             /target:exe /embed "${_${_PYTHON_PREFIX}_VERSION_DIR}/version.py"
2104                     WORKING_DIRECTORY "${_${_PYTHON_PREFIX}_VERSION_DIR}"
2105                     OUTPUT_QUIET
2106                     ERROR_QUIET)
2107    get_filename_component (_${_PYTHON_PREFIX}_IR_DIR "${_${_PYTHON_PREFIX}_COMPILER}" DIRECTORY)
2108    execute_process (COMMAND "${CMAKE_COMMAND}" -E env "MONO_PATH=${_${_PYTHON_PREFIX}_IR_DIR}"
2109                             ${${_PYTHON_PREFIX}_DOTNET_LAUNCHER} "${_${_PYTHON_PREFIX}_VERSION_DIR}/version.exe"
2110                     WORKING_DIRECTORY "${_${_PYTHON_PREFIX}_VERSION_DIR}"
2111                     RESULT_VARIABLE _${_PYTHON_PREFIX}_RESULT
2112                     OUTPUT_VARIABLE _${_PYTHON_PREFIX}_VERSION
2113                     ERROR_QUIET)
2114    if (NOT _${_PYTHON_PREFIX}_RESULT)
2115      set (_${_PYTHON_PREFIX}_COMPILER_USABLE TRUE)
2116      string (REGEX MATCHALL "[0-9]+" _${_PYTHON_PREFIX}_VERSIONS "${_${_PYTHON_PREFIX}_VERSION}")
2117      list (GET _${_PYTHON_PREFIX}_VERSIONS 0 _${_PYTHON_PREFIX}_VERSION_MAJOR)
2118      list (GET _${_PYTHON_PREFIX}_VERSIONS 1 _${_PYTHON_PREFIX}_VERSION_MINOR)
2119      list (GET _${_PYTHON_PREFIX}_VERSIONS 2 _${_PYTHON_PREFIX}_VERSION_PATCH)
2120
2121      if (NOT ${_PYTHON_PREFIX}_Interpreter_FOUND)
2122        # set public version information
2123        set (${_PYTHON_PREFIX}_VERSION ${_${_PYTHON_PREFIX}_VERSION})
2124        set (${_PYTHON_PREFIX}_VERSION_MAJOR ${_${_PYTHON_PREFIX}_VERSION_MAJOR})
2125        set (${_PYTHON_PREFIX}_VERSION_MINOR ${_${_PYTHON_PREFIX}_VERSION_MINOR})
2126        set (${_PYTHON_PREFIX}_VERSION_PATCH ${_${_PYTHON_PREFIX}_VERSION_PATCH})
2127      endif()
2128    else()
2129      # compiler not usable
2130      set (_${_PYTHON_PREFIX}_COMPILER_USABLE FALSE)
2131      set (_${_PYTHON_PREFIX}_Compiler_REASON_FAILURE "Cannot run the compiler \"${_${_PYTHON_PREFIX}_COMPILER}\"")
2132    endif()
2133    file (REMOVE_RECURSE "${_${_PYTHON_PREFIX}_VERSION_DIR}")
2134  endif()
2135
2136  if (_${_PYTHON_PREFIX}_COMPILER AND _${_PYTHON_PREFIX}_COMPILER_USABLE)
2137    if (${_PYTHON_PREFIX}_Interpreter_FOUND)
2138      # Compiler must be compatible with interpreter
2139      if ("${_${_PYTHON_PREFIX}_VERSION_MAJOR}.${_${_PYTHON_PREFIX}_VERSION_MINOR}" VERSION_EQUAL "${${_PYTHON_PREFIX}_VERSION_MAJOR}.${${_PYTHON_PREFIX}_VERSION_MINOR}")
2140        set (${_PYTHON_PREFIX}_Compiler_FOUND TRUE)
2141      endif()
2142    elseif (${_PYTHON_PREFIX}_VERSION_MAJOR VERSION_EQUAL _${_PYTHON_PREFIX}_REQUIRED_VERSION_MAJOR)
2143      set (${_PYTHON_PREFIX}_Compiler_FOUND TRUE)
2144      # Use compiler version for future searches to ensure consistency
2145      set (_${_PYTHON_PREFIX}_FIND_VERSIONS ${${_PYTHON_PREFIX}_VERSION_MAJOR}.${${_PYTHON_PREFIX}_VERSION_MINOR})
2146    endif()
2147  endif()
2148
2149  if (${_PYTHON_PREFIX}_Compiler_FOUND)
2150    unset (_${_PYTHON_PREFIX}_Compiler_REASON_FAILURE)
2151
2152    # compute and save compiler signature
2153    string (MD5 __${_PYTHON_PREFIX}_COMPILER_SIGNATURE "${_${_PYTHON_PREFIX}_SIGNATURE}:${_${_PYTHON_PREFIX}_COMPILER}")
2154#HM3    set (_${_PYTHON_PREFIX}_COMPILER_SIGNATURE "${__${_PYTHON_PREFIX}_COMPILER_SIGNATURE}" CACHE INTERNAL "")
2155
2156    set (${_PYTHON_PREFIX}_COMPILER_ID IronPython)
2157  else()
2158    unset (_${_PYTHON_PREFIX}_COMPILER_SIGNATURE CACHE)
2159    unset (${_PYTHON_PREFIX}_COMPILER_ID)
2160  endif()
2161
2162  if (${_PYTHON_PREFIX}_ARTIFACTS_INTERACTIVE)
2163#HM3    set (${_PYTHON_PREFIX}_COMPILER "${_${_PYTHON_PREFIX}_COMPILER}" CACHE FILEPATH "${_PYTHON_PREFIX} Compiler")
2164  endif()
2165
2166  _python_mark_as_internal (_${_PYTHON_PREFIX}_COMPILER
2167                            _${_PYTHON_PREFIX}_COMPILER_SIGNATURE)
2168endif()
2169
2170# third step, search for the development artifacts
2171if (${_PYTHON_PREFIX}_FIND_REQUIRED_Development.Module)
2172  if ("LIBRARY" IN_LIST _${_PYTHON_PREFIX}_FIND_DEVELOPMENT_MODULE_ARTIFACTS)
2173    list (APPEND _${_PYTHON_PREFIX}_REQUIRED_VARS ${_PYTHON_PREFIX}_LIBRARIES)
2174  endif()
2175  if ("INCLUDE_DIR" IN_LIST _${_PYTHON_PREFIX}_FIND_DEVELOPMENT_MODULE_ARTIFACTS)
2176    list (APPEND _${_PYTHON_PREFIX}_REQUIRED_VARS ${_PYTHON_PREFIX}_INCLUDE_DIRS)
2177  endif()
2178endif()
2179if (${_PYTHON_PREFIX}_FIND_REQUIRED_Development.Embed)
2180  if ("LIBRARY" IN_LIST _${_PYTHON_PREFIX}_FIND_DEVELOPMENT_EMBED_ARTIFACTS)
2181    list (APPEND _${_PYTHON_PREFIX}_REQUIRED_VARS ${_PYTHON_PREFIX}_LIBRARIES)
2182  endif()
2183  if ("INCLUDE_DIR" IN_LIST _${_PYTHON_PREFIX}_FIND_DEVELOPMENT_EMBED_ARTIFACTS)
2184    list (APPEND _${_PYTHON_PREFIX}_REQUIRED_VARS ${_PYTHON_PREFIX}_INCLUDE_DIRS)
2185  endif()
2186endif()
2187list (REMOVE_DUPLICATES _${_PYTHON_PREFIX}_REQUIRED_VARS)
2188## Development environment is not compatible with IronPython interpreter
2189if (("Development.Module" IN_LIST ${_PYTHON_PREFIX}_FIND_COMPONENTS
2190      OR "Development.Embed" IN_LIST ${_PYTHON_PREFIX}_FIND_COMPONENTS)
2191    AND ((${_PYTHON_PREFIX}_Interpreter_FOUND
2192        AND NOT ${_PYTHON_PREFIX}_INTERPRETER_ID STREQUAL "IronPython")
2193      OR NOT ${_PYTHON_PREFIX}_Interpreter_FOUND))
2194  if (${_PYTHON_PREFIX}_Interpreter_FOUND)
2195    # reduce possible implementations to the interpreter one
2196    if (${_PYTHON_PREFIX}_INTERPRETER_ID STREQUAL "PyPy")
2197      set (_${_PYTHON_PREFIX}_FIND_IMPLEMENTATIONS "PyPy")
2198    else()
2199      set (_${_PYTHON_PREFIX}_FIND_IMPLEMENTATIONS "CPython")
2200    endif()
2201  else()
2202    list (REMOVE_ITEM _${_PYTHON_PREFIX}_FIND_IMPLEMENTATIONS "IronPython")
2203  endif()
2204  if ("LIBRARY" IN_LIST _${_PYTHON_PREFIX}_FIND_DEVELOPMENT_ARTIFACTS)
2205#HM3  list (APPEND _${_PYTHON_PREFIX}_CACHED_VARS _${_PYTHON_PREFIX}_LIBRARY_RELEASE
2206#HM3                                              _${_PYTHON_PREFIX}_RUNTIME_LIBRARY_RELEASE
2207#HM3                                              _${_PYTHON_PREFIX}_LIBRARY_DEBUG
2208#HM3                                              _${_PYTHON_PREFIX}_RUNTIME_LIBRARY_DEBUG)
2209  endif()
2210  if ("INCLUDE_DIR" IN_LIST _${_PYTHON_PREFIX}_FIND_DEVELOPMENT_ARTIFACTS)
2211#HM3    list (APPEND _${_PYTHON_PREFIX}_CACHED_VARS _${_PYTHON_PREFIX}_INCLUDE_DIR)
2212  endif()
2213
2214  _python_check_development_signature (Module)
2215  _python_check_development_signature (Embed)
2216
2217  if (DEFINED ${_PYTHON_PREFIX}_LIBRARY
2218      AND IS_ABSOLUTE "${${_PYTHON_PREFIX}_LIBRARY}")
2219#HM3    set (_${_PYTHON_PREFIX}_LIBRARY_RELEASE "${${_PYTHON_PREFIX}_LIBRARY}" CACHE INTERNAL "")
2220    unset (_${_PYTHON_PREFIX}_LIBRARY_DEBUG CACHE)
2221    unset (_${_PYTHON_PREFIX}_INCLUDE_DIR CACHE)
2222  endif()
2223  if (DEFINED ${_PYTHON_PREFIX}_INCLUDE_DIR
2224      AND IS_ABSOLUTE "${${_PYTHON_PREFIX}_INCLUDE_DIR}")
2225#HM3    set (_${_PYTHON_PREFIX}_INCLUDE_DIR "${${_PYTHON_PREFIX}_INCLUDE_DIR}" CACHE INTERNAL "")
2226  endif()
2227
2228  # Support preference of static libs by adjusting CMAKE_FIND_LIBRARY_SUFFIXES
2229  unset (_${_PYTHON_PREFIX}_CMAKE_FIND_LIBRARY_SUFFIXES)
2230  if (DEFINED ${_PYTHON_PREFIX}_USE_STATIC_LIBS AND NOT WIN32)
2231    set(_${_PYTHON_PREFIX}_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
2232    if(${_PYTHON_PREFIX}_USE_STATIC_LIBS)
2233      set (CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_STATIC_LIBRARY_SUFFIX})
2234    else()
2235      list (REMOVE_ITEM CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_STATIC_LIBRARY_SUFFIX})
2236    endif()
2237  endif()
2238
2239  if (NOT _${_PYTHON_PREFIX}_LIBRARY_RELEASE OR NOT _${_PYTHON_PREFIX}_INCLUDE_DIR)
2240    # if python interpreter is found, use it to look-up for artifacts
2241    # to ensure consistency between interpreter and development environments.
2242    # If not, try to locate a compatible config tool
2243    if ((NOT ${_PYTHON_PREFIX}_Interpreter_FOUND OR CMAKE_CROSSCOMPILING)
2244        AND "CPython" IN_LIST _${_PYTHON_PREFIX}_FIND_IMPLEMENTATIONS)
2245      set (_${_PYTHON_PREFIX}_HINTS "${${_PYTHON_PREFIX}_ROOT_DIR}" ENV ${_PYTHON_PREFIX}_ROOT_DIR)
2246      unset (_${_PYTHON_PREFIX}_VIRTUALENV_PATHS)
2247      if (_${_PYTHON_PREFIX}_FIND_VIRTUALENV MATCHES "^(FIRST|ONLY)$")
2248        set (_${_PYTHON_PREFIX}_VIRTUALENV_PATHS ENV VIRTUAL_ENV ENV CONDA_PREFIX)
2249      endif()
2250
2251      if (_${_PYTHON_PREFIX}_FIND_STRATEGY STREQUAL "LOCATION")
2252        _python_get_names (_${_PYTHON_PREFIX}_CONFIG_NAMES VERSION ${_${_PYTHON_PREFIX}_FIND_VERSIONS} POSIX CONFIG)
2253          # Framework Paths
2254        _python_get_frameworks (_${_PYTHON_PREFIX}_FRAMEWORK_PATHS VERSION ${_${_PYTHON_PREFIX}_FIND_VERSIONS})
2255
2256        # Apple frameworks handling
2257        if (CMAKE_HOST_APPLE AND _${_PYTHON_PREFIX}_FIND_FRAMEWORK STREQUAL "FIRST")
2258          find_program (_${_PYTHON_PREFIX}_CONFIG
2259                        NAMES ${_${_PYTHON_PREFIX}_CONFIG_NAMES}
2260                        NAMES_PER_DIR
2261                        HINTS ${_${_PYTHON_PREFIX}_HINTS}
2262                        PATHS ${_${_PYTHON_PREFIX}_VIRTUALENV_PATHS}
2263                              ${_${_PYTHON_PREFIX}_FRAMEWORK_PATHS}
2264                        PATH_SUFFIXES bin
2265                        NO_CMAKE_PATH
2266                        NO_CMAKE_ENVIRONMENT_PATH
2267                        NO_SYSTEM_ENVIRONMENT_PATH
2268                        NO_CMAKE_SYSTEM_PATH)
2269        endif()
2270
2271        find_program (_${_PYTHON_PREFIX}_CONFIG
2272                      NAMES ${_${_PYTHON_PREFIX}_CONFIG_NAMES}
2273                      NAMES_PER_DIR
2274                      HINTS ${_${_PYTHON_PREFIX}_HINTS}
2275                      PATHS ${_${_PYTHON_PREFIX}_VIRTUALENV_PATHS}
2276                      PATH_SUFFIXES bin)
2277
2278        # Apple frameworks handling
2279        if (CMAKE_HOST_APPLE AND _${_PYTHON_PREFIX}_FIND_FRAMEWORK STREQUAL "LAST")
2280          find_program (_${_PYTHON_PREFIX}_CONFIG
2281                        NAMES ${_${_PYTHON_PREFIX}_CONFIG_NAMES}
2282                        NAMES_PER_DIR
2283                        PATHS ${_${_PYTHON_PREFIX}_FRAMEWORK_PATHS}
2284                        PATH_SUFFIXES bin
2285                        NO_DEFAULT_PATH)
2286        endif()
2287
2288        if (_${_PYTHON_PREFIX}_CONFIG)
2289          execute_process (COMMAND "${_${_PYTHON_PREFIX}_CONFIG}" --help
2290                           RESULT_VARIABLE _${_PYTHON_PREFIX}_RESULT
2291                           OUTPUT_VARIABLE __${_PYTHON_PREFIX}_HELP
2292                           ERROR_QUIET
2293                           OUTPUT_STRIP_TRAILING_WHITESPACE)
2294          if (_${_PYTHON_PREFIX}_RESULT)
2295            # assume config tool is not usable
2296            unset (_${_PYTHON_PREFIX}_CONFIG CACHE)
2297          endif()
2298        endif()
2299
2300        if (_${_PYTHON_PREFIX}_CONFIG)
2301          execute_process (COMMAND "${_${_PYTHON_PREFIX}_CONFIG}" --abiflags
2302                           RESULT_VARIABLE _${_PYTHON_PREFIX}_RESULT
2303                           OUTPUT_VARIABLE __${_PYTHON_PREFIX}_ABIFLAGS
2304                           ERROR_QUIET
2305                           OUTPUT_STRIP_TRAILING_WHITESPACE)
2306          if (_${_PYTHON_PREFIX}_RESULT)
2307            # assume ABI is not supported
2308            set (__${_PYTHON_PREFIX}_ABIFLAGS "")
2309          endif()
2310          if (DEFINED _${_PYTHON_PREFIX}_FIND_ABI AND NOT __${_PYTHON_PREFIX}_ABIFLAGS IN_LIST _${_PYTHON_PREFIX}_ABIFLAGS)
2311            # Wrong ABI
2312            unset (_${_PYTHON_PREFIX}_CONFIG CACHE)
2313          endif()
2314        endif()
2315
2316        if (_${_PYTHON_PREFIX}_CONFIG AND DEFINED CMAKE_LIBRARY_ARCHITECTURE)
2317          # check that config tool match library architecture
2318          execute_process (COMMAND "${_${_PYTHON_PREFIX}_CONFIG}" --configdir
2319                           RESULT_VARIABLE _${_PYTHON_PREFIX}_RESULT
2320                           OUTPUT_VARIABLE _${_PYTHON_PREFIX}_CONFIGDIR
2321                           ERROR_QUIET
2322                           OUTPUT_STRIP_TRAILING_WHITESPACE)
2323          if (_${_PYTHON_PREFIX}_RESULT)
2324            unset (_${_PYTHON_PREFIX}_CONFIG CACHE)
2325          else()
2326            string(FIND "${_${_PYTHON_PREFIX}_CONFIGDIR}" "${CMAKE_LIBRARY_ARCHITECTURE}" _${_PYTHON_PREFIX}_RESULT)
2327            if (_${_PYTHON_PREFIX}_RESULT EQUAL -1)
2328              unset (_${_PYTHON_PREFIX}_CONFIG CACHE)
2329            endif()
2330          endif()
2331        endif()
2332      else()
2333        foreach (_${_PYTHON_PREFIX}_VERSION IN LISTS _${_PYTHON_PREFIX}_FIND_VERSIONS)
2334          # try to use pythonX.Y-config tool
2335          _python_get_names (_${_PYTHON_PREFIX}_CONFIG_NAMES VERSION ${_${_PYTHON_PREFIX}_VERSION} POSIX CONFIG)
2336
2337          # Framework Paths
2338          _python_get_frameworks (_${_PYTHON_PREFIX}_FRAMEWORK_PATHS VERSION ${_${_PYTHON_PREFIX}_VERSION})
2339
2340          # Apple frameworks handling
2341          if (CMAKE_HOST_APPLE AND _${_PYTHON_PREFIX}_FIND_FRAMEWORK STREQUAL "FIRST")
2342            find_program (_${_PYTHON_PREFIX}_CONFIG
2343                          NAMES ${_${_PYTHON_PREFIX}_CONFIG_NAMES}
2344                          NAMES_PER_DIR
2345                          HINTS ${_${_PYTHON_PREFIX}_HINTS}
2346                          PATHS ${_${_PYTHON_PREFIX}_VIRTUALENV_PATHS}
2347                                ${_${_PYTHON_PREFIX}_FRAMEWORK_PATHS}
2348                          PATH_SUFFIXES bin
2349                          NO_CMAKE_PATH
2350                          NO_CMAKE_ENVIRONMENT_PATH
2351                          NO_SYSTEM_ENVIRONMENT_PATH
2352                          NO_CMAKE_SYSTEM_PATH)
2353          endif()
2354
2355          find_program (_${_PYTHON_PREFIX}_CONFIG
2356                        NAMES ${_${_PYTHON_PREFIX}_CONFIG_NAMES}
2357                        NAMES_PER_DIR
2358                        HINTS ${_${_PYTHON_PREFIX}_HINTS}
2359                        PATHS ${_${_PYTHON_PREFIX}_VIRTUALENV_PATHS}
2360                        PATH_SUFFIXES bin)
2361
2362          # Apple frameworks handling
2363          if (CMAKE_HOST_APPLE AND _${_PYTHON_PREFIX}_FIND_FRAMEWORK STREQUAL "LAST")
2364            find_program (_${_PYTHON_PREFIX}_CONFIG
2365                          NAMES ${_${_PYTHON_PREFIX}_CONFIG_NAMES}
2366                          NAMES_PER_DIR
2367                          PATHS ${_${_PYTHON_PREFIX}_FRAMEWORK_PATHS}
2368                          PATH_SUFFIXES bin
2369                          NO_DEFAULT_PATH)
2370          endif()
2371
2372          unset (_${_PYTHON_PREFIX}_CONFIG_NAMES)
2373
2374          if (_${_PYTHON_PREFIX}_CONFIG)
2375            execute_process (COMMAND "${_${_PYTHON_PREFIX}_CONFIG}" --help
2376                             RESULT_VARIABLE _${_PYTHON_PREFIX}_RESULT
2377                             OUTPUT_VARIABLE __${_PYTHON_PREFIX}_HELP
2378                             ERROR_QUIET
2379                             OUTPUT_STRIP_TRAILING_WHITESPACE)
2380            if (_${_PYTHON_PREFIX}_RESULT)
2381              # assume config tool is not usable
2382              unset (_${_PYTHON_PREFIX}_CONFIG CACHE)
2383            endif()
2384          endif()
2385
2386          if (NOT _${_PYTHON_PREFIX}_CONFIG)
2387            continue()
2388          endif()
2389
2390          execute_process (COMMAND "${_${_PYTHON_PREFIX}_CONFIG}" --abiflags
2391                           RESULT_VARIABLE _${_PYTHON_PREFIX}_RESULT
2392                           OUTPUT_VARIABLE __${_PYTHON_PREFIX}_ABIFLAGS
2393                           ERROR_QUIET
2394                           OUTPUT_STRIP_TRAILING_WHITESPACE)
2395          if (_${_PYTHON_PREFIX}_RESULT)
2396            # assume ABI is not supported
2397            set (__${_PYTHON_PREFIX}_ABIFLAGS "")
2398          endif()
2399          if (DEFINED _${_PYTHON_PREFIX}_FIND_ABI AND NOT __${_PYTHON_PREFIX}_ABIFLAGS IN_LIST _${_PYTHON_PREFIX}_ABIFLAGS)
2400            # Wrong ABI
2401            unset (_${_PYTHON_PREFIX}_CONFIG CACHE)
2402            continue()
2403          endif()
2404
2405          if (_${_PYTHON_PREFIX}_CONFIG AND DEFINED CMAKE_LIBRARY_ARCHITECTURE)
2406            # check that config tool match library architecture
2407            execute_process (COMMAND "${_${_PYTHON_PREFIX}_CONFIG}" --configdir
2408                             RESULT_VARIABLE _${_PYTHON_PREFIX}_RESULT
2409                             OUTPUT_VARIABLE _${_PYTHON_PREFIX}_CONFIGDIR
2410                             ERROR_QUIET
2411                             OUTPUT_STRIP_TRAILING_WHITESPACE)
2412            if (_${_PYTHON_PREFIX}_RESULT)
2413              unset (_${_PYTHON_PREFIX}_CONFIG CACHE)
2414              continue()
2415            endif()
2416            string (FIND "${_${_PYTHON_PREFIX}_CONFIGDIR}" "${CMAKE_LIBRARY_ARCHITECTURE}" _${_PYTHON_PREFIX}_RESULT)
2417            if (_${_PYTHON_PREFIX}_RESULT EQUAL -1)
2418              unset (_${_PYTHON_PREFIX}_CONFIG CACHE)
2419              continue()
2420            endif()
2421          endif()
2422
2423          if (_${_PYTHON_PREFIX}_CONFIG)
2424            break()
2425          endif()
2426        endforeach()
2427      endif()
2428    endif()
2429  endif()
2430
2431  if ("LIBRARY" IN_LIST _${_PYTHON_PREFIX}_FIND_DEVELOPMENT_ARTIFACTS)
2432    if (NOT _${_PYTHON_PREFIX}_LIBRARY_RELEASE)
2433      if ((${_PYTHON_PREFIX}_Interpreter_FOUND AND NOT CMAKE_CROSSCOMPILING) OR _${_PYTHON_PREFIX}_CONFIG)
2434        # retrieve root install directory
2435        _python_get_config_var (_${_PYTHON_PREFIX}_PREFIX PREFIX)
2436
2437        # enforce current ABI
2438        _python_get_config_var (_${_PYTHON_PREFIX}_ABIFLAGS ABIFLAGS)
2439
2440        set (_${_PYTHON_PREFIX}_HINTS "${_${_PYTHON_PREFIX}_PREFIX}")
2441
2442        # retrieve library
2443        ## compute some paths and artifact names
2444        if (_${_PYTHON_PREFIX}_CONFIG)
2445          string (REGEX REPLACE "^.+python([0-9.]+)[a-z]*-config" "\\1" _${_PYTHON_PREFIX}_VERSION "${_${_PYTHON_PREFIX}_CONFIG}")
2446        else()
2447          set (_${_PYTHON_PREFIX}_VERSION "${${_PYTHON_PREFIX}_VERSION_MAJOR}.${${_PYTHON_PREFIX}_VERSION_MINOR}")
2448        endif()
2449        _python_get_path_suffixes (_${_PYTHON_PREFIX}_PATH_SUFFIXES VERSION ${_${_PYTHON_PREFIX}_VERSION} LIBRARY)
2450        _python_get_names (_${_PYTHON_PREFIX}_LIB_NAMES VERSION ${_${_PYTHON_PREFIX}_VERSION} WIN32 POSIX LIBRARY)
2451
2452        _python_get_config_var (_${_PYTHON_PREFIX}_CONFIGDIR CONFIGDIR)
2453        list (APPEND _${_PYTHON_PREFIX}_HINTS "${_${_PYTHON_PREFIX}_CONFIGDIR}")
2454
2455        list (APPEND _${_PYTHON_PREFIX}_HINTS "${${_PYTHON_PREFIX}_ROOT_DIR}" ENV ${_PYTHON_PREFIX}_ROOT_DIR)
2456
2457        find_library (_${_PYTHON_PREFIX}_LIBRARY_RELEASE
2458                      NAMES ${_${_PYTHON_PREFIX}_LIB_NAMES}
2459                      NAMES_PER_DIR
2460                      HINTS ${_${_PYTHON_PREFIX}_HINTS}
2461                      PATH_SUFFIXES ${_${_PYTHON_PREFIX}_PATH_SUFFIXES}
2462                      NO_SYSTEM_ENVIRONMENT_PATH
2463                      NO_CMAKE_SYSTEM_PATH)
2464      endif()
2465
2466      # Rely on HINTS and standard paths if interpreter or config tool failed to locate artifacts
2467      if (NOT _${_PYTHON_PREFIX}_LIBRARY_RELEASE)
2468        set (_${_PYTHON_PREFIX}_HINTS "${${_PYTHON_PREFIX}_ROOT_DIR}" ENV ${_PYTHON_PREFIX}_ROOT_DIR)
2469
2470        unset (_${_PYTHON_PREFIX}_VIRTUALENV_PATHS)
2471        if (_${_PYTHON_PREFIX}_FIND_VIRTUALENV MATCHES "^(FIRST|ONLY)$")
2472          set (_${_PYTHON_PREFIX}_VIRTUALENV_PATHS ENV VIRTUAL_ENV ENV CONDA_PREFIX)
2473        endif()
2474
2475        if (_${_PYTHON_PREFIX}_FIND_STRATEGY STREQUAL "LOCATION")
2476          # library names
2477          _python_get_names (_${_PYTHON_PREFIX}_LIB_NAMES VERSION ${_${_PYTHON_PREFIX}_FIND_VERSIONS} WIN32 POSIX LIBRARY)
2478          _python_get_names (_${_PYTHON_PREFIX}_LIB_NAMES_DEBUG VERSION ${_${_PYTHON_PREFIX}_FIND_VERSIONS} WIN32 DEBUG)
2479          # Paths suffixes
2480          _python_get_path_suffixes (_${_PYTHON_PREFIX}_PATH_SUFFIXES VERSION ${_${_PYTHON_PREFIX}_FIND_VERSIONS} LIBRARY)
2481
2482          # Framework Paths
2483          _python_get_frameworks (_${_PYTHON_PREFIX}_FRAMEWORK_PATHS VERSION ${_${_PYTHON_PREFIX}_LIB_FIND_VERSIONS})
2484          # Registry Paths
2485          _python_get_registries (_${_PYTHON_PREFIX}_REGISTRY_PATHS VERSION ${_${_PYTHON_PREFIX}_FIND_VERSIONS} )
2486
2487          if (APPLE AND _${_PYTHON_PREFIX}_FIND_FRAMEWORK STREQUAL "FIRST")
2488            find_library (_${_PYTHON_PREFIX}_LIBRARY_RELEASE
2489                          NAMES ${_${_PYTHON_PREFIX}_LIB_NAMES}
2490                          NAMES_PER_DIR
2491                          HINTS ${_${_PYTHON_PREFIX}_HINTS}
2492                          PATHS ${_${_PYTHON_PREFIX}_VIRTUALENV_PATHS}
2493                                ${_${_PYTHON_PREFIX}_FRAMEWORK_PATHS}
2494                          PATH_SUFFIXES ${_${_PYTHON_PREFIX}_PATH_SUFFIXES}
2495                          NO_CMAKE_PATH
2496                          NO_CMAKE_ENVIRONMENT_PATH
2497                          NO_SYSTEM_ENVIRONMENT_PATH
2498                          NO_CMAKE_SYSTEM_PATH)
2499          endif()
2500
2501          if (WIN32 AND _${_PYTHON_PREFIX}_FIND_REGISTRY STREQUAL "FIRST")
2502            find_library (_${_PYTHON_PREFIX}_LIBRARY_RELEASE
2503                          NAMES ${_${_PYTHON_PREFIX}_LIB_NAMES}
2504                          NAMES_PER_DIR
2505                          HINTS ${_${_PYTHON_PREFIX}_HINTS}
2506                          PATHS ${_${_PYTHON_PREFIX}_VIRTUALENV_PATHS}
2507                                ${_${_PYTHON_PREFIX}_REGISTRY_PATHS}
2508                          PATH_SUFFIXES ${_${_PYTHON_PREFIX}_PATH_SUFFIXES}
2509                          NO_SYSTEM_ENVIRONMENT_PATH
2510                          NO_CMAKE_SYSTEM_PATH)
2511          endif()
2512
2513          # search in HINTS locations
2514          find_library (_${_PYTHON_PREFIX}_LIBRARY_RELEASE
2515                        NAMES ${_${_PYTHON_PREFIX}_LIB_NAMES}
2516                        NAMES_PER_DIR
2517                        HINTS ${_${_PYTHON_PREFIX}_HINTS}
2518                        PATHS ${_${_PYTHON_PREFIX}_VIRTUALENV_PATHS}
2519                        PATH_SUFFIXES ${_${_PYTHON_PREFIX}_PATH_SUFFIXES}
2520                        NO_SYSTEM_ENVIRONMENT_PATH
2521                        NO_CMAKE_SYSTEM_PATH)
2522
2523          if (APPLE AND _${_PYTHON_PREFIX}_FIND_FRAMEWORK STREQUAL "LAST")
2524            set (__${_PYTHON_PREFIX}_FRAMEWORK_PATHS ${_${_PYTHON_PREFIX}_FRAMEWORK_PATHS})
2525          else()
2526            unset (__${_PYTHON_PREFIX}_FRAMEWORK_PATHS)
2527          endif()
2528
2529          if (WIN32 AND _${_PYTHON_PREFIX}_FIND_REGISTRY STREQUAL "LAST")
2530            set (__${_PYTHON_PREFIX}_REGISTRY_PATHS ${_${_PYTHON_PREFIX}_REGISTRY_PATHS})
2531          else()
2532            unset (__${_PYTHON_PREFIX}_REGISTRY_PATHS)
2533          endif()
2534
2535          # search in all default paths
2536          find_library (_${_PYTHON_PREFIX}_LIBRARY_RELEASE
2537                        NAMES ${_${_PYTHON_PREFIX}_LIB_NAMES}
2538                        NAMES_PER_DIR
2539                        PATHS ${__${_PYTHON_PREFIX}_FRAMEWORK_PATHS}
2540                              ${__${_PYTHON_PREFIX}_REGISTRY_PATHS}
2541                        PATH_SUFFIXES ${_${_PYTHON_PREFIX}_PATH_SUFFIXES})
2542        else()
2543          foreach (_${_PYTHON_PREFIX}_LIB_VERSION IN LISTS _${_PYTHON_PREFIX}_FIND_VERSIONS)
2544            _python_get_names (_${_PYTHON_PREFIX}_LIB_NAMES VERSION ${_${_PYTHON_PREFIX}_LIB_VERSION} WIN32 POSIX LIBRARY)
2545            _python_get_names (_${_PYTHON_PREFIX}_LIB_NAMES_DEBUG VERSION ${_${_PYTHON_PREFIX}_LIB_VERSION} WIN32 DEBUG)
2546
2547            _python_get_frameworks (_${_PYTHON_PREFIX}_FRAMEWORK_PATHS VERSION ${_${_PYTHON_PREFIX}_LIB_VERSION})
2548            _python_get_registries (_${_PYTHON_PREFIX}_REGISTRY_PATHS VERSION ${_${_PYTHON_PREFIX}_LIB_VERSION})
2549
2550            _python_get_path_suffixes (_${_PYTHON_PREFIX}_PATH_SUFFIXES VERSION ${_${_PYTHON_PREFIX}_LIB_VERSION} LIBRARY)
2551
2552            if (APPLE AND _${_PYTHON_PREFIX}_FIND_FRAMEWORK STREQUAL "FIRST")
2553              find_library (_${_PYTHON_PREFIX}_LIBRARY_RELEASE
2554                            NAMES ${_${_PYTHON_PREFIX}_LIB_NAMES}
2555                            NAMES_PER_DIR
2556                            HINTS ${_${_PYTHON_PREFIX}_HINTS}
2557                            PATHS ${_${_PYTHON_PREFIX}_VIRTUALENV_PATHS}
2558                                  ${_${_PYTHON_PREFIX}_FRAMEWORK_PATHS}
2559                            PATH_SUFFIXES ${_${_PYTHON_PREFIX}_PATH_SUFFIXES}
2560                            NO_CMAKE_PATH
2561                            NO_CMAKE_ENVIRONMENT_PATH
2562                            NO_SYSTEM_ENVIRONMENT_PATH
2563                            NO_CMAKE_SYSTEM_PATH)
2564            endif()
2565
2566            if (WIN32 AND _${_PYTHON_PREFIX}_FIND_REGISTRY STREQUAL "FIRST")
2567              find_library (_${_PYTHON_PREFIX}_LIBRARY_RELEASE
2568                            NAMES ${_${_PYTHON_PREFIX}_LIB_NAMES}
2569                            NAMES_PER_DIR
2570                            HINTS ${_${_PYTHON_PREFIX}_HINTS}
2571                            PATHS ${_${_PYTHON_PREFIX}_VIRTUALENV_PATHS}
2572                                  ${_${_PYTHON_PREFIX}_REGISTRY_PATHS}
2573                            PATH_SUFFIXES ${_${_PYTHON_PREFIX}_PATH_SUFFIXES}
2574                            NO_SYSTEM_ENVIRONMENT_PATH
2575                            NO_CMAKE_SYSTEM_PATH)
2576            endif()
2577
2578            # search in HINTS locations
2579            find_library (_${_PYTHON_PREFIX}_LIBRARY_RELEASE
2580                          NAMES ${_${_PYTHON_PREFIX}_LIB_NAMES}
2581                          NAMES_PER_DIR
2582                          HINTS ${_${_PYTHON_PREFIX}_HINTS}
2583                          PATHS ${_${_PYTHON_PREFIX}_VIRTUALENV_PATHS}
2584                          PATH_SUFFIXES ${_${_PYTHON_PREFIX}_PATH_SUFFIXES}
2585                          NO_SYSTEM_ENVIRONMENT_PATH
2586                          NO_CMAKE_SYSTEM_PATH)
2587
2588            if (APPLE AND _${_PYTHON_PREFIX}_FIND_FRAMEWORK STREQUAL "LAST")
2589              set (__${_PYTHON_PREFIX}_FRAMEWORK_PATHS ${_${_PYTHON_PREFIX}_FRAMEWORK_PATHS})
2590            else()
2591              unset (__${_PYTHON_PREFIX}_FRAMEWORK_PATHS)
2592            endif()
2593
2594            if (WIN32 AND _${_PYTHON_PREFIX}_FIND_REGISTRY STREQUAL "LAST")
2595              set (__${_PYTHON_PREFIX}_REGISTRY_PATHS ${_${_PYTHON_PREFIX}_REGISTRY_PATHS})
2596            else()
2597              unset (__${_PYTHON_PREFIX}_REGISTRY_PATHS)
2598            endif()
2599
2600            # search in all default paths
2601            find_library (_${_PYTHON_PREFIX}_LIBRARY_RELEASE
2602                          NAMES ${_${_PYTHON_PREFIX}_LIB_NAMES}
2603                          NAMES_PER_DIR
2604                          PATHS ${__${_PYTHON_PREFIX}_FRAMEWORK_PATHS}
2605                                ${__${_PYTHON_PREFIX}_REGISTRY_PATHS}
2606                          PATH_SUFFIXES ${_${_PYTHON_PREFIX}_PATH_SUFFIXES})
2607
2608            if (_${_PYTHON_PREFIX}_LIBRARY_RELEASE)
2609              break()
2610            endif()
2611          endforeach()
2612        endif()
2613      endif()
2614    endif()
2615
2616    # finalize library version information
2617    _python_get_version (LIBRARY PREFIX _${_PYTHON_PREFIX}_)
2618    if (_${_PYTHON_PREFIX}_VERSION EQUAL "${_${_PYTHON_PREFIX}_REQUIRED_VERSION_MAJOR}")
2619      # not able to extract full version from library name
2620      if (${_PYTHON_PREFIX}_Interpreter_FOUND)
2621        # update from interpreter
2622        set (_${_PYTHON_PREFIX}_VERSION ${${_PYTHON_PREFIX}_VERSION})
2623        set (_${_PYTHON_PREFIX}_VERSION_MAJOR ${${_PYTHON_PREFIX}_VERSION_MAJOR})
2624        set (_${_PYTHON_PREFIX}_VERSION_MINOR ${${_PYTHON_PREFIX}_VERSION_MINOR})
2625        set (_${_PYTHON_PREFIX}_VERSION_PATCH ${${_PYTHON_PREFIX}_VERSION_PATCH})
2626      endif()
2627    endif()
2628
2629    set (${_PYTHON_PREFIX}_LIBRARY_RELEASE "${_${_PYTHON_PREFIX}_LIBRARY_RELEASE}")
2630
2631    if (_${_PYTHON_PREFIX}_LIBRARY_RELEASE AND NOT EXISTS "${_${_PYTHON_PREFIX}_LIBRARY_RELEASE}")
2632      set (_${_PYTHON_PREFIX}_Development_REASON_FAILURE "Cannot find the library \"${_${_PYTHON_PREFIX}_LIBRARY_RELEASE}\"")
2633#HM3      set_property (CACHE _${_PYTHON_PREFIX}_LIBRARY_RELEASE PROPERTY VALUE "${_PYTHON_PREFIX}_LIBRARY_RELEASE-NOTFOUND")
2634    endif()
2635
2636    set (_${_PYTHON_PREFIX}_HINTS "${${_PYTHON_PREFIX}_ROOT_DIR}" ENV ${_PYTHON_PREFIX}_ROOT_DIR)
2637
2638    if (WIN32 AND _${_PYTHON_PREFIX}_LIBRARY_RELEASE)
2639      # search for debug library
2640      # use release library location as a hint
2641      _python_get_names (_${_PYTHON_PREFIX}_LIB_NAMES_DEBUG VERSION ${_${_PYTHON_PREFIX}_VERSION} WIN32 DEBUG)
2642      get_filename_component (_${_PYTHON_PREFIX}_PATH "${_${_PYTHON_PREFIX}_LIBRARY_RELEASE}" DIRECTORY)
2643      find_library (_${_PYTHON_PREFIX}_LIBRARY_DEBUG
2644                    NAMES ${_${_PYTHON_PREFIX}_LIB_NAMES_DEBUG}
2645                    NAMES_PER_DIR
2646                    HINTS "${_${_PYTHON_PREFIX}_PATH}" ${_${_PYTHON_PREFIX}_HINTS}
2647                    NO_DEFAULT_PATH)
2648      # second try including CMAKE variables to catch-up non conventional layouts
2649      find_library (_${_PYTHON_PREFIX}_LIBRARY_DEBUG
2650                    NAMES ${_${_PYTHON_PREFIX}_LIB_NAMES_DEBUG}
2651                    NAMES_PER_DIR
2652                    NO_SYSTEM_ENVIRONMENT_PATH
2653                    NO_CMAKE_SYSTEM_PATH)
2654    endif()
2655
2656    # retrieve runtime libraries
2657    if (_${_PYTHON_PREFIX}_LIBRARY_RELEASE)
2658      _python_get_names (_${_PYTHON_PREFIX}_LIB_NAMES VERSION ${_${_PYTHON_PREFIX}_VERSION} WIN32 POSIX LIBRARY)
2659      get_filename_component (_${_PYTHON_PREFIX}_PATH "${_${_PYTHON_PREFIX}_LIBRARY_RELEASE}" DIRECTORY)
2660      get_filename_component (_${_PYTHON_PREFIX}_PATH2 "${_${_PYTHON_PREFIX}_PATH}" DIRECTORY)
2661      _python_find_runtime_library (_${_PYTHON_PREFIX}_RUNTIME_LIBRARY_RELEASE
2662                                    NAMES ${_${_PYTHON_PREFIX}_LIB_NAMES}
2663                                    NAMES_PER_DIR
2664                                    HINTS "${_${_PYTHON_PREFIX}_PATH}"
2665                                          "${_${_PYTHON_PREFIX}_PATH2}" ${_${_PYTHON_PREFIX}_HINTS}
2666                                    PATH_SUFFIXES bin)
2667    endif()
2668    if (_${_PYTHON_PREFIX}_LIBRARY_DEBUG)
2669      _python_get_names (_${_PYTHON_PREFIX}_LIB_NAMES_DEBUG VERSION ${_${_PYTHON_PREFIX}_VERSION} WIN32 DEBUG)
2670      get_filename_component (_${_PYTHON_PREFIX}_PATH "${_${_PYTHON_PREFIX}_LIBRARY_DEBUG}" DIRECTORY)
2671      get_filename_component (_${_PYTHON_PREFIX}_PATH2 "${_${_PYTHON_PREFIX}_PATH}" DIRECTORY)
2672      _python_find_runtime_library (_${_PYTHON_PREFIX}_RUNTIME_LIBRARY_DEBUG
2673                                    NAMES ${_${_PYTHON_PREFIX}_LIB_NAMES_DEBUG}
2674                                    NAMES_PER_DIR
2675                                    HINTS "${_${_PYTHON_PREFIX}_PATH}"
2676                                          "${_${_PYTHON_PREFIX}_PATH2}" ${_${_PYTHON_PREFIX}_HINTS}
2677                                    PATH_SUFFIXES bin)
2678    endif()
2679  endif()
2680
2681  if ("INCLUDE_DIR" IN_LIST _${_PYTHON_PREFIX}_FIND_DEVELOPMENT_ARTIFACTS)
2682    while (NOT _${_PYTHON_PREFIX}_INCLUDE_DIR)
2683      if ("LIBRARY" IN_LIST _${_PYTHON_PREFIX}_FIND_DEVELOPMENT_ARTIFACTS
2684          AND NOT _${_PYTHON_PREFIX}_LIBRARY_RELEASE)
2685        # Don't search for include dir if no library was founded
2686        break()
2687      endif()
2688
2689      if ((${_PYTHON_PREFIX}_Interpreter_FOUND AND NOT CMAKE_CROSSCOMPILING) OR _${_PYTHON_PREFIX}_CONFIG)
2690        _python_get_config_var (_${_PYTHON_PREFIX}_INCLUDE_DIRS INCLUDES)
2691
2692        find_path (_${_PYTHON_PREFIX}_INCLUDE_DIR
2693                   NAMES ${_${_PYTHON_PREFIX}_INCLUDE_NAMES}
2694                   HINTS ${_${_PYTHON_PREFIX}_INCLUDE_DIRS}
2695                   NO_SYSTEM_ENVIRONMENT_PATH
2696                   NO_CMAKE_SYSTEM_PATH)
2697      endif()
2698
2699      # Rely on HINTS and standard paths if interpreter or config tool failed to locate artifacts
2700      if (NOT _${_PYTHON_PREFIX}_INCLUDE_DIR)
2701        unset (_${_PYTHON_PREFIX}_VIRTUALENV_PATHS)
2702        if (_${_PYTHON_PREFIX}_FIND_VIRTUALENV MATCHES "^(FIRST|ONLY)$")
2703          set (_${_PYTHON_PREFIX}_VIRTUALENV_PATHS ENV VIRTUAL_ENV ENV CONDA_PREFIX)
2704        endif()
2705        unset (_${_PYTHON_PREFIX}_INCLUDE_HINTS)
2706
2707        if (_${_PYTHON_PREFIX}_LIBRARY_RELEASE)
2708          # Use the library's install prefix as a hint
2709          if (_${_PYTHON_PREFIX}_LIBRARY_RELEASE MATCHES "^(.+/Frameworks/Python.framework/Versions/[0-9.]+)")
2710            list (APPEND _${_PYTHON_PREFIX}_INCLUDE_HINTS "${CMAKE_MATCH_1}")
2711          elseif (_${_PYTHON_PREFIX}_LIBRARY_RELEASE MATCHES "^(.+)/lib(64|32)?/python[0-9.]+/config")
2712            list (APPEND _${_PYTHON_PREFIX}_INCLUDE_HINTS "${CMAKE_MATCH_1}")
2713          elseif (DEFINED CMAKE_LIBRARY_ARCHITECTURE AND ${_${_PYTHON_PREFIX}_LIBRARY_RELEASE} MATCHES "^(.+)/lib/${CMAKE_LIBRARY_ARCHITECTURE}")
2714            list (APPEND _${_PYTHON_PREFIX}_INCLUDE_HINTS "${CMAKE_MATCH_1}")
2715          else()
2716            # assume library is in a directory under root
2717            get_filename_component (_${_PYTHON_PREFIX}_PREFIX "${_${_PYTHON_PREFIX}_LIBRARY_RELEASE}" DIRECTORY)
2718            get_filename_component (_${_PYTHON_PREFIX}_PREFIX "${_${_PYTHON_PREFIX}_PREFIX}" DIRECTORY)
2719            list (APPEND _${_PYTHON_PREFIX}_INCLUDE_HINTS "${_${_PYTHON_PREFIX}_PREFIX}")
2720          endif()
2721        endif()
2722
2723        _python_get_frameworks (_${_PYTHON_PREFIX}_FRAMEWORK_PATHS VERSION ${_${_PYTHON_PREFIX}_VERSION})
2724        _python_get_registries (_${_PYTHON_PREFIX}_REGISTRY_PATHS VERSION ${_${_PYTHON_PREFIX}_VERSION})
2725        _python_get_path_suffixes (_${_PYTHON_PREFIX}_PATH_SUFFIXES VERSION ${_${_PYTHON_PREFIX}_VERSION} INCLUDE)
2726
2727        if (APPLE AND _${_PYTHON_PREFIX}_FIND_FRAMEWORK STREQUAL "FIRST")
2728          find_path (_${_PYTHON_PREFIX}_INCLUDE_DIR
2729                     NAMES ${_${_PYTHON_PREFIX}_INCLUDE_NAMES}
2730                     HINTS ${_${_PYTHON_PREFIX}_INCLUDE_HINTS} ${_${_PYTHON_PREFIX}_HINTS}
2731                     PATHS ${_${_PYTHON_PREFIX}_VIRTUALENV_PATHS}
2732                           ${_${_PYTHON_PREFIX}_FRAMEWORK_PATHS}
2733                     PATH_SUFFIXES ${_${_PYTHON_PREFIX}_PATH_SUFFIXES}
2734                     NO_CMAKE_PATH
2735                     NO_CMAKE_ENVIRONMENT_PATH
2736                     NO_SYSTEM_ENVIRONMENT_PATH
2737                     NO_CMAKE_SYSTEM_PATH)
2738        endif()
2739
2740        if (WIN32 AND _${_PYTHON_PREFIX}_FIND_REGISTRY STREQUAL "FIRST")
2741          find_path (_${_PYTHON_PREFIX}_INCLUDE_DIR
2742                     NAMES ${_${_PYTHON_PREFIX}_INCLUDE_NAMES}
2743                     HINTS ${_${_PYTHON_PREFIX}_INCLUDE_HINTS} ${_${_PYTHON_PREFIX}_HINTS}
2744                     PATHS ${_${_PYTHON_PREFIX}_VIRTUALENV_PATHS}
2745                           ${_${_PYTHON_PREFIX}_REGISTRY_PATHS}
2746                     PATH_SUFFIXES ${_${_PYTHON_PREFIX}_PATH_SUFFIXES}
2747                     NO_SYSTEM_ENVIRONMENT_PATH
2748                     NO_CMAKE_SYSTEM_PATH)
2749        endif()
2750
2751        if (APPLE AND _${_PYTHON_PREFIX}_FIND_FRAMEWORK STREQUAL "LAST")
2752          set (__${_PYTHON_PREFIX}_FRAMEWORK_PATHS ${_${_PYTHON_PREFIX}_FRAMEWORK_PATHS})
2753        else()
2754          unset (__${_PYTHON_PREFIX}_FRAMEWORK_PATHS)
2755        endif()
2756
2757        if (WIN32 AND _${_PYTHON_PREFIX}_FIND_REGISTRY STREQUAL "LAST")
2758          set (__${_PYTHON_PREFIX}_REGISTRY_PATHS ${_${_PYTHON_PREFIX}_REGISTRY_PATHS})
2759        else()
2760          unset (__${_PYTHON_PREFIX}_REGISTRY_PATHS)
2761        endif()
2762
2763        find_path (_${_PYTHON_PREFIX}_INCLUDE_DIR
2764                   NAMES ${_${_PYTHON_PREFIX}_INCLUDE_NAMES}
2765                   HINTS ${_${_PYTHON_PREFIX}_INCLUDE_HINTS} ${_${_PYTHON_PREFIX}_HINTS}
2766                   PATHS ${_${_PYTHON_PREFIX}_VIRTUALENV_PATHS}
2767                         ${__${_PYTHON_PREFIX}_FRAMEWORK_PATHS}
2768                         ${__${_PYTHON_PREFIX}_REGISTRY_PATHS}
2769                   PATH_SUFFIXES ${_${_PYTHON_PREFIX}_PATH_SUFFIXES}
2770                   NO_SYSTEM_ENVIRONMENT_PATH
2771                   NO_CMAKE_SYSTEM_PATH)
2772      endif()
2773
2774      # search header file in standard locations
2775      find_path (_${_PYTHON_PREFIX}_INCLUDE_DIR
2776                 NAMES ${_${_PYTHON_PREFIX}_INCLUDE_NAMES})
2777
2778      break()
2779    endwhile()
2780
2781    set (${_PYTHON_PREFIX}_INCLUDE_DIRS "${_${_PYTHON_PREFIX}_INCLUDE_DIR}")
2782
2783    if (_${_PYTHON_PREFIX}_INCLUDE_DIR AND NOT EXISTS "${_${_PYTHON_PREFIX}_INCLUDE_DIR}")
2784      set (_${_PYTHON_PREFIX}_Development_REASON_FAILURE "Cannot find the directory \"${_${_PYTHON_PREFIX}_INCLUDE_DIR}\"")
2785#HM3      set_property (CACHE _${_PYTHON_PREFIX}_INCLUDE_DIR PROPERTY VALUE "${_PYTHON_PREFIX}_INCLUDE_DIR-NOTFOUND")
2786    endif()
2787
2788    if (_${_PYTHON_PREFIX}_INCLUDE_DIR)
2789      # retrieve version from header file
2790      _python_get_version (INCLUDE PREFIX _${_PYTHON_PREFIX}_INC_)
2791
2792      if (_${_PYTHON_PREFIX}_LIBRARY_RELEASE)
2793        if ("${_${_PYTHON_PREFIX}_INC_VERSION_MAJOR}.${_${_PYTHON_PREFIX}_INC_VERSION_MINOR}"
2794            VERSION_EQUAL _${_PYTHON_PREFIX}_VERSION)
2795          # update versioning
2796          set (_${_PYTHON_PREFIX}_VERSION ${_${_PYTHON_PREFIX}_INC_VERSION})
2797          set (_${_PYTHON_PREFIX}_VERSION_PATCH ${_${_PYTHON_PREFIX}_INC_VERSION_PATCH})
2798        endif()
2799      else()
2800        set (_${_PYTHON_PREFIX}_VERSION ${_${_PYTHON_PREFIX}_INC_VERSION})
2801        set (_${_PYTHON_PREFIX}_VERSION_MAJOR ${_${_PYTHON_PREFIX}_INC_VERSION_MAJOR})
2802        set (_${_PYTHON_PREFIX}_VERSION_MINOR ${_${_PYTHON_PREFIX}_INC_VERSION_MINOR})
2803        set (_${_PYTHON_PREFIX}_VERSION_PATCH ${_${_PYTHON_PREFIX}_INC_VERSION_PATCH})
2804      endif()
2805    endif()
2806  endif()
2807
2808  if (NOT ${_PYTHON_PREFIX}_Interpreter_FOUND AND NOT ${_PYTHON_PREFIX}_Compiler_FOUND)
2809    # set public version information
2810    set (${_PYTHON_PREFIX}_VERSION ${_${_PYTHON_PREFIX}_VERSION})
2811    set (${_PYTHON_PREFIX}_VERSION_MAJOR ${_${_PYTHON_PREFIX}_VERSION_MAJOR})
2812    set (${_PYTHON_PREFIX}_VERSION_MINOR ${_${_PYTHON_PREFIX}_VERSION_MINOR})
2813    set (${_PYTHON_PREFIX}_VERSION_PATCH ${_${_PYTHON_PREFIX}_VERSION_PATCH})
2814  endif()
2815
2816  # define public variables
2817  if ("LIBRARY" IN_LIST _${_PYTHON_PREFIX}_FIND_DEVELOPMENT_ARTIFACTS)
2818    set (${_PYTHON_PREFIX}_LIBRARY_DEBUG "${_${_PYTHON_PREFIX}_LIBRARY_DEBUG}")
2819    _python_select_library_configurations (${_PYTHON_PREFIX})
2820
2821    set (${_PYTHON_PREFIX}_RUNTIME_LIBRARY_RELEASE "${_${_PYTHON_PREFIX}_RUNTIME_LIBRARY_RELEASE}")
2822    set (${_PYTHON_PREFIX}_RUNTIME_LIBRARY_DEBUG "${_${_PYTHON_PREFIX}_RUNTIME_LIBRARY_DEBUG}")
2823
2824    if (_${_PYTHON_PREFIX}_RUNTIME_LIBRARY_RELEASE)
2825      set (${_PYTHON_PREFIX}_RUNTIME_LIBRARY "${_${_PYTHON_PREFIX}_RUNTIME_LIBRARY_RELEASE}")
2826    elseif (_${_PYTHON_PREFIX}_RUNTIME_LIBRARY_DEBUG)
2827      set (${_PYTHON_PREFIX}_RUNTIME_LIBRARY "${_${_PYTHON_PREFIX}_RUNTIME_LIBRARY_DEBUG}")
2828    else()
2829      set (${_PYTHON_PREFIX}_RUNTIME_LIBRARY "${_PYTHON_PREFIX}_RUNTIME_LIBRARY-NOTFOUND")
2830    endif()
2831
2832    _python_set_library_dirs (${_PYTHON_PREFIX}_LIBRARY_DIRS
2833                              _${_PYTHON_PREFIX}_LIBRARY_RELEASE
2834                              _${_PYTHON_PREFIX}_LIBRARY_DEBUG)
2835    if (UNIX)
2836      if (_${_PYTHON_PREFIX}_LIBRARY_RELEASE MATCHES "${CMAKE_SHARED_LIBRARY_SUFFIX}$")
2837        set (${_PYTHON_PREFIX}_RUNTIME_LIBRARY_DIRS ${${_PYTHON_PREFIX}_LIBRARY_DIRS})
2838      endif()
2839    else()
2840      _python_set_library_dirs (${_PYTHON_PREFIX}_RUNTIME_LIBRARY_DIRS
2841                                _${_PYTHON_PREFIX}_RUNTIME_LIBRARY_RELEASE
2842                                _${_PYTHON_PREFIX}_RUNTIME_LIBRARY_DEBUG)
2843    endif()
2844  endif()
2845
2846  if (_${_PYTHON_PREFIX}_LIBRARY_RELEASE OR _${_PYTHON_PREFIX}_INCLUDE_DIR)
2847    if (${_PYTHON_PREFIX}_Interpreter_FOUND OR ${_PYTHON_PREFIX}_Compiler_FOUND)
2848      # development environment must be compatible with interpreter/compiler
2849      if ("${_${_PYTHON_PREFIX}_VERSION_MAJOR}.${_${_PYTHON_PREFIX}_VERSION_MINOR}" VERSION_EQUAL "${${_PYTHON_PREFIX}_VERSION_MAJOR}.${${_PYTHON_PREFIX}_VERSION_MINOR}"
2850          AND "${_${_PYTHON_PREFIX}_INC_VERSION_MAJOR}.${_${_PYTHON_PREFIX}_INC_VERSION_MINOR}" VERSION_EQUAL "${_${_PYTHON_PREFIX}_VERSION_MAJOR}.${_${_PYTHON_PREFIX}_VERSION_MINOR}")
2851        _python_set_development_module_found (Module)
2852        _python_set_development_module_found (Embed)
2853      endif()
2854    elseif (${_PYTHON_PREFIX}_VERSION_MAJOR VERSION_EQUAL _${_PYTHON_PREFIX}_REQUIRED_VERSION_MAJOR
2855        AND "${_${_PYTHON_PREFIX}_INC_VERSION_MAJOR}.${_${_PYTHON_PREFIX}_INC_VERSION_MINOR}" VERSION_EQUAL "${_${_PYTHON_PREFIX}_VERSION_MAJOR}.${_${_PYTHON_PREFIX}_VERSION_MINOR}")
2856      _python_set_development_module_found (Module)
2857      _python_set_development_module_found (Embed)
2858    endif()
2859    if (DEFINED _${_PYTHON_PREFIX}_FIND_ABI AND
2860        (NOT _${_PYTHON_PREFIX}_ABI IN_LIST _${_PYTHON_PREFIX}_ABIFLAGS
2861          OR NOT _${_PYTHON_PREFIX}_INC_ABI IN_LIST _${_PYTHON_PREFIX}_ABIFLAGS))
2862      set (${_PYTHON_PREFIX}_Development.Module_FOUND FALSE)
2863      set (${_PYTHON_PREFIX}_Development.Embed_FOUND FALSE)
2864    endif()
2865  endif()
2866
2867  if (( ${_PYTHON_PREFIX}_Development.Module_FOUND
2868        AND ${_PYTHON_PREFIX}_Development.Embed_FOUND)
2869      OR (NOT "Development.Module" IN_LIST ${_PYTHON_PREFIX}_FIND_COMPONENTS
2870        AND ${_PYTHON_PREFIX}_Development.Embed_FOUND)
2871      OR (NOT "Development.Embed" IN_LIST ${_PYTHON_PREFIX}_FIND_COMPONENTS
2872        AND ${_PYTHON_PREFIX}_Development.Module_FOUND))
2873    unset (_${_PYTHON_PREFIX}_Development_REASON_FAILURE)
2874  endif()
2875
2876  if ("Development" IN_LIST ${_PYTHON_PREFIX}_FIND_COMPONENTS
2877      AND ${_PYTHON_PREFIX}_Development.Module_FOUND
2878      AND ${_PYTHON_PREFIX}_Development.Embed_FOUND)
2879    set (${_PYTHON_PREFIX}_Development_FOUND TRUE)
2880  endif()
2881
2882  if ((${_PYTHON_PREFIX}_Development.Module_FOUND
2883      OR ${_PYTHON_PREFIX}_Development.Embed_FOUND)
2884    AND EXISTS "${_${_PYTHON_PREFIX}_INCLUDE_DIR}/PyPy.h")
2885  # retrieve PyPy version
2886  file (STRINGS "${_${_PYTHON_PREFIX}_INCLUDE_DIR}/patchlevel.h" ${_PYTHON_PREFIX}_PyPy_VERSION
2887                REGEX "^#define[ \t]+PYPY_VERSION[ \t]+\"[^\"]+\"")
2888  string (REGEX REPLACE "^#define[ \t]+PYPY_VERSION[ \t]+\"([^\"]+)\".*" "\\1"
2889                ${_PYTHON_PREFIX}_PyPy_VERSION "${${_PYTHON_PREFIX}_PyPy_VERSION}")
2890  endif()
2891
2892  if (_${_PYTHON_PREFIX}_REQUIRED_VERSION_MAJOR VERSION_GREATER_EQUAL "3"
2893      AND NOT DEFINED ${_PYTHON_PREFIX}_SOABI)
2894    _python_get_config_var (${_PYTHON_PREFIX}_SOABI SOABI)
2895  endif()
2896
2897  _python_compute_development_signature (Module)
2898  _python_compute_development_signature (Embed)
2899
2900  # Restore the original find library ordering
2901  if (DEFINED _${_PYTHON_PREFIX}_CMAKE_FIND_LIBRARY_SUFFIXES)
2902    set (CMAKE_FIND_LIBRARY_SUFFIXES ${_${_PYTHON_PREFIX}_CMAKE_FIND_LIBRARY_SUFFIXES})
2903  endif()
2904
2905  if (${_PYTHON_PREFIX}_ARTIFACTS_INTERACTIVE)
2906    if ("LIBRARY" IN_LIST _${_PYTHON_PREFIX}_FIND_DEVELOPMENT_ARTIFACTS)
2907#HM3      set (${_PYTHON_PREFIX}_LIBRARY "${_${_PYTHON_PREFIX}_LIBRARY_RELEASE}" CACHE FILEPATH "${_PYTHON_PREFIX} Library")
2908    endif()
2909    if ("INCLUDE_DIR" IN_LIST _${_PYTHON_PREFIX}_FIND_DEVELOPMENT_ARTIFACTS)
2910#HM3      set (${_PYTHON_PREFIX}_INCLUDE_DIR "${_${_PYTHON_PREFIX}_INCLUDE_DIR}" CACHE FILEPATH "${_PYTHON_PREFIX} Include Directory")
2911    endif()
2912  endif()
2913
2914  _python_mark_as_internal (_${_PYTHON_PREFIX}_LIBRARY_RELEASE
2915                            _${_PYTHON_PREFIX}_LIBRARY_DEBUG
2916                            _${_PYTHON_PREFIX}_RUNTIME_LIBRARY_RELEASE
2917                            _${_PYTHON_PREFIX}_RUNTIME_LIBRARY_DEBUG
2918                            _${_PYTHON_PREFIX}_INCLUDE_DIR
2919                            _${_PYTHON_PREFIX}_CONFIG
2920                            _${_PYTHON_PREFIX}_DEVELOPMENT_MODULE_SIGNATURE
2921                            _${_PYTHON_PREFIX}_DEVELOPMENT_EMBED_SIGNATURE)
2922endif()
2923
2924if (${_PYTHON_PREFIX}_FIND_REQUIRED_NumPy)
2925  list (APPEND _${_PYTHON_PREFIX}_REQUIRED_VARS ${_PYTHON_PREFIX}_NumPy_INCLUDE_DIRS)
2926endif()
2927if ("NumPy" IN_LIST ${_PYTHON_PREFIX}_FIND_COMPONENTS AND ${_PYTHON_PREFIX}_Interpreter_FOUND)
2928#HM3  list (APPEND _${_PYTHON_PREFIX}_CACHED_VARS _${_PYTHON_PREFIX}_NumPy_INCLUDE_DIR)
2929
2930  if (DEFINED ${_PYTHON_PREFIX}_NumPy_INCLUDE_DIR
2931      AND IS_ABSOLUTE "${${_PYTHON_PREFIX}_NumPy_INCLUDE_DIR}")
2932#HM3    set (_${_PYTHON_PREFIX}_NumPy_INCLUDE_DIR "${${_PYTHON_PREFIX}_NumPy_INCLUDE_DIR}" CACHE INTERNAL "")
2933  elseif (DEFINED _${_PYTHON_PREFIX}_NumPy_INCLUDE_DIR)
2934    # compute numpy signature. Depends on interpreter and development signatures
2935    string (MD5 __${_PYTHON_PREFIX}_NUMPY_SIGNATURE "${_${_PYTHON_PREFIX}_INTERPRETER_SIGNATURE}:${_${_PYTHON_PREFIX}_DEVELOPMENT_MODULE_SIGNATURE}:${_${_PYTHON_PREFIX}_NumPy_INCLUDE_DIR}")
2936    if (NOT __${_PYTHON_PREFIX}_NUMPY_SIGNATURE STREQUAL _${_PYTHON_PREFIX}_NUMPY_SIGNATURE
2937        OR NOT EXISTS "${_${_PYTHON_PREFIX}_NumPy_INCLUDE_DIR}")
2938      unset (_${_PYTHON_PREFIX}_NumPy_INCLUDE_DIR CACHE)
2939      unset (_${_PYTHON_PREFIX}_NUMPY_SIGNATURE CACHE)
2940    endif()
2941  endif()
2942
2943  if (NOT _${_PYTHON_PREFIX}_NumPy_INCLUDE_DIR)
2944    execute_process(COMMAND ${${_PYTHON_PREFIX}_INTERPRETER_LAUNCHER} "${_${_PYTHON_PREFIX}_EXECUTABLE}" -c
2945                            "import sys\ntry: import numpy; sys.stdout.write(numpy.get_include())\nexcept:pass\n"
2946                    RESULT_VARIABLE _${_PYTHON_PREFIX}_RESULT
2947                    OUTPUT_VARIABLE _${_PYTHON_PREFIX}_NumPy_PATH
2948                    ERROR_QUIET
2949                    OUTPUT_STRIP_TRAILING_WHITESPACE)
2950
2951    if (NOT _${_PYTHON_PREFIX}_RESULT)
2952      find_path (_${_PYTHON_PREFIX}_NumPy_INCLUDE_DIR
2953                 NAMES "numpy/arrayobject.h" "numpy/numpyconfig.h"
2954                 HINTS "${_${_PYTHON_PREFIX}_NumPy_PATH}"
2955                 NO_DEFAULT_PATH)
2956    endif()
2957  endif()
2958
2959  set (${_PYTHON_PREFIX}_NumPy_INCLUDE_DIRS "${_${_PYTHON_PREFIX}_NumPy_INCLUDE_DIR}")
2960
2961  if(_${_PYTHON_PREFIX}_NumPy_INCLUDE_DIR AND NOT EXISTS "${_${_PYTHON_PREFIX}_NumPy_INCLUDE_DIR}")
2962    set (_${_PYTHON_PREFIX}_NumPy_REASON_FAILURE "Cannot find the directory \"${_${_PYTHON_PREFIX}_NumPy_INCLUDE_DIR}\"")
2963#HM3    set_property (CACHE _${_PYTHON_PREFIX}_NumPy_INCLUDE_DIR PROPERTY VALUE "${_PYTHON_PREFIX}_NumPy_INCLUDE_DIR-NOTFOUND")
2964  endif()
2965
2966  if (_${_PYTHON_PREFIX}_NumPy_INCLUDE_DIR)
2967    execute_process (COMMAND ${${_PYTHON_PREFIX}_INTERPRETER_LAUNCHER} "${_${_PYTHON_PREFIX}_EXECUTABLE}" -c
2968                             "import sys\ntry: import numpy; sys.stdout.write(numpy.__version__)\nexcept:pass\n"
2969                     RESULT_VARIABLE _${_PYTHON_PREFIX}_RESULT
2970                     OUTPUT_VARIABLE _${_PYTHON_PREFIX}_NumPy_VERSION)
2971    if (NOT _${_PYTHON_PREFIX}_RESULT)
2972      set (${_PYTHON_PREFIX}_NumPy_VERSION "${_${_PYTHON_PREFIX}_NumPy_VERSION}")
2973    else()
2974      unset (${_PYTHON_PREFIX}_NumPy_VERSION)
2975    endif()
2976
2977    # final step: set NumPy founded only if Development.Module component is founded as well
2978    set(${_PYTHON_PREFIX}_NumPy_FOUND ${${_PYTHON_PREFIX}_Development.Module_FOUND})
2979  else()
2980    set (${_PYTHON_PREFIX}_NumPy_FOUND FALSE)
2981  endif()
2982
2983  if (${_PYTHON_PREFIX}_NumPy_FOUND)
2984    unset (_${_PYTHON_PREFIX}_NumPy_REASON_FAILURE)
2985
2986    # compute and save numpy signature
2987    string (MD5 __${_PYTHON_PREFIX}_NUMPY_SIGNATURE "${_${_PYTHON_PREFIX}_INTERPRETER_SIGNATURE}:${_${_PYTHON_PREFIX}_DEVELOPMENT_MODULE_SIGNATURE}:${${_PYTHON_PREFIX}_NumPyINCLUDE_DIR}")
2988#HM3    set (_${_PYTHON_PREFIX}_NUMPY_SIGNATURE "${__${_PYTHON_PREFIX}_NUMPY_SIGNATURE}" CACHE INTERNAL "")
2989  else()
2990    unset (_${_PYTHON_PREFIX}_NUMPY_SIGNATURE CACHE)
2991  endif()
2992
2993  if (${_PYTHON_PREFIX}_ARTIFACTS_INTERACTIVE)
2994#HM3    set (${_PYTHON_PREFIX}_NumPy_INCLUDE_DIR "${_${_PYTHON_PREFIX}_NumPy_INCLUDE_DIR}" CACHE FILEPATH "${_PYTHON_PREFIX} NumPy Include Directory")
2995  endif()
2996
2997  _python_mark_as_internal (_${_PYTHON_PREFIX}_NumPy_INCLUDE_DIR
2998                            _${_PYTHON_PREFIX}_NUMPY_SIGNATURE)
2999endif()
3000
3001# final validation
3002if (${_PYTHON_PREFIX}_VERSION_MAJOR AND
3003    NOT ${_PYTHON_PREFIX}_VERSION_MAJOR VERSION_EQUAL _${_PYTHON_PREFIX}_REQUIRED_VERSION_MAJOR)
3004  _python_display_failure ("Could NOT find ${_PYTHON_PREFIX}: Found unsuitable major version \"${${_PYTHON_PREFIX}_VERSION_MAJOR}\", but required major version is exact version \"${_${_PYTHON_PREFIX}_REQUIRED_VERSION_MAJOR}\"")
3005endif()
3006
3007unset (_${_PYTHON_PREFIX}_REASON_FAILURE)
3008foreach (_${_PYTHON_PREFIX}_COMPONENT IN ITEMS Interpreter Compiler Development NumPy)
3009  if (_${_PYTHON_PREFIX}_${_${_PYTHON_PREFIX}_COMPONENT}_REASON_FAILURE)
3010    string (APPEND _${_PYTHON_PREFIX}_REASON_FAILURE "\n        ${_${_PYTHON_PREFIX}_COMPONENT}: ${_${_PYTHON_PREFIX}_${_${_PYTHON_PREFIX}_COMPONENT}_REASON_FAILURE}")
3011    unset (_${_PYTHON_PREFIX}_${_${_PYTHON_PREFIX}_COMPONENT}_REASON_FAILURE)
3012  endif()
3013endforeach()
3014
3015include (${CMAKE_CURRENT_LIST_DIR}/../FindPackageHandleStandardArgs.cmake)
3016find_package_handle_standard_args (${_PYTHON_PREFIX}
3017                                   REQUIRED_VARS ${_${_PYTHON_PREFIX}_REQUIRED_VARS}
3018                                   VERSION_VAR ${_PYTHON_PREFIX}_VERSION
3019                                   HANDLE_COMPONENTS
3020                                   REASON_FAILURE_MESSAGE "${_${_PYTHON_PREFIX}_REASON_FAILURE}")
3021
3022# Create imported targets and helper functions
3023if(_${_PYTHON_PREFIX}_CMAKE_ROLE STREQUAL "PROJECT")
3024  if ("Interpreter" IN_LIST ${_PYTHON_PREFIX}_FIND_COMPONENTS
3025      AND ${_PYTHON_PREFIX}_Interpreter_FOUND
3026      AND NOT TARGET ${_PYTHON_PREFIX}::Interpreter)
3027    add_executable (${_PYTHON_PREFIX}::Interpreter IMPORTED)
3028    set_property (TARGET ${_PYTHON_PREFIX}::Interpreter
3029                  PROPERTY IMPORTED_LOCATION "${${_PYTHON_PREFIX}_EXECUTABLE}")
3030  endif()
3031
3032  if ("Compiler" IN_LIST ${_PYTHON_PREFIX}_FIND_COMPONENTS
3033      AND ${_PYTHON_PREFIX}_Compiler_FOUND
3034      AND NOT TARGET ${_PYTHON_PREFIX}::Compiler)
3035    add_executable (${_PYTHON_PREFIX}::Compiler IMPORTED)
3036    set_property (TARGET ${_PYTHON_PREFIX}::Compiler
3037                  PROPERTY IMPORTED_LOCATION "${${_PYTHON_PREFIX}_COMPILER}")
3038  endif()
3039
3040  if (("Development.Module" IN_LIST ${_PYTHON_PREFIX}_FIND_COMPONENTS
3041        AND ${_PYTHON_PREFIX}_Development.Module_FOUND)
3042      OR ("Development.Embed" IN_LIST ${_PYTHON_PREFIX}_FIND_COMPONENTS
3043        AND ${_PYTHON_PREFIX}_Development.Embed_FOUND))
3044
3045    macro (__PYTHON_IMPORT_LIBRARY __name)
3046      if (${_PYTHON_PREFIX}_LIBRARY_RELEASE MATCHES "${CMAKE_SHARED_LIBRARY_SUFFIX}$"
3047          OR ${_PYTHON_PREFIX}_RUNTIME_LIBRARY_RELEASE)
3048        set (_${_PYTHON_PREFIX}_LIBRARY_TYPE SHARED)
3049      else()
3050        set (_${_PYTHON_PREFIX}_LIBRARY_TYPE STATIC)
3051      endif()
3052
3053      if (NOT TARGET ${__name})
3054        add_library (${__name} ${_${_PYTHON_PREFIX}_LIBRARY_TYPE} IMPORTED)
3055      endif()
3056
3057      set_property (TARGET ${__name}
3058                    PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${${_PYTHON_PREFIX}_INCLUDE_DIRS}")
3059
3060      if (${_PYTHON_PREFIX}_LIBRARY_RELEASE AND ${_PYTHON_PREFIX}_RUNTIME_LIBRARY_RELEASE)
3061        # System manage shared libraries in two parts: import and runtime
3062        if (${_PYTHON_PREFIX}_LIBRARY_RELEASE AND ${_PYTHON_PREFIX}_LIBRARY_DEBUG)
3063          set_property (TARGET ${__name} PROPERTY IMPORTED_CONFIGURATIONS RELEASE DEBUG)
3064          set_target_properties (${__name}
3065                                 PROPERTIES IMPORTED_LINK_INTERFACE_LANGUAGES_RELEASE "C"
3066                                            IMPORTED_IMPLIB_RELEASE "${${_PYTHON_PREFIX}_LIBRARY_RELEASE}"
3067                                            IMPORTED_LOCATION_RELEASE "${${_PYTHON_PREFIX}_RUNTIME_LIBRARY_RELEASE}")
3068          set_target_properties (${__name}
3069                                 PROPERTIES IMPORTED_LINK_INTERFACE_LANGUAGES_DEBUG "C"
3070                                            IMPORTED_IMPLIB_DEBUG "${${_PYTHON_PREFIX}_LIBRARY_DEBUG}"
3071                                            IMPORTED_LOCATION_DEBUG "${${_PYTHON_PREFIX}_RUNTIME_LIBRARY_DEBUG}")
3072        else()
3073          set_target_properties (${__name}
3074                                 PROPERTIES IMPORTED_LINK_INTERFACE_LANGUAGES "C"
3075                                            IMPORTED_IMPLIB "${${_PYTHON_PREFIX}_LIBRARIES}"
3076                                            IMPORTED_LOCATION "${${_PYTHON_PREFIX}_RUNTIME_LIBRARY_RELEASE}")
3077        endif()
3078      else()
3079        if (${_PYTHON_PREFIX}_LIBRARY_RELEASE AND ${_PYTHON_PREFIX}_LIBRARY_DEBUG)
3080          set_property (TARGET ${__name} PROPERTY IMPORTED_CONFIGURATIONS RELEASE DEBUG)
3081          set_target_properties (${__name}
3082                                 PROPERTIES IMPORTED_LINK_INTERFACE_LANGUAGES_RELEASE "C"
3083                                            IMPORTED_LOCATION_RELEASE "${${_PYTHON_PREFIX}_LIBRARY_RELEASE}")
3084          set_target_properties (${__name}
3085                                 PROPERTIES IMPORTED_LINK_INTERFACE_LANGUAGES_DEBUG "C"
3086                                            IMPORTED_LOCATION_DEBUG "${${_PYTHON_PREFIX}_LIBRARY_DEBUG}")
3087        else()
3088          set_target_properties (${__name}
3089                                 PROPERTIES IMPORTED_LINK_INTERFACE_LANGUAGES "C"
3090                                            IMPORTED_LOCATION "${${_PYTHON_PREFIX}_LIBRARY_RELEASE}")
3091        endif()
3092      endif()
3093
3094      if (_${_PYTHON_PREFIX}_LIBRARY_TYPE STREQUAL "STATIC")
3095        # extend link information with dependent libraries
3096        _python_get_config_var (_${_PYTHON_PREFIX}_LINK_LIBRARIES LIBS)
3097        if (_${_PYTHON_PREFIX}_LINK_LIBRARIES)
3098          set_property (TARGET ${__name}
3099                        PROPERTY INTERFACE_LINK_LIBRARIES ${_${_PYTHON_PREFIX}_LINK_LIBRARIES})
3100        endif()
3101      endif()
3102    endmacro()
3103
3104    if (${_PYTHON_PREFIX}_Development.Embed_FOUND)
3105      __python_import_library (${_PYTHON_PREFIX}::Python)
3106    endif()
3107
3108    if (${_PYTHON_PREFIX}_Development.Module_FOUND)
3109      if ("LIBRARY" IN_LIST _${_PYTHON_PREFIX}_FIND_DEVELOPMENT_MODULE_ARTIFACTS)
3110        # On Windows/CYGWIN/MSYS, Python::Module is the same as Python::Python
3111        # but ALIAS cannot be used because the imported library is not GLOBAL.
3112        __python_import_library (${_PYTHON_PREFIX}::Module)
3113      else()
3114        if (NOT TARGET ${_PYTHON_PREFIX}::Module)
3115          add_library (${_PYTHON_PREFIX}::Module INTERFACE IMPORTED)
3116        endif()
3117        set_property (TARGET ${_PYTHON_PREFIX}::Module
3118                      PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${${_PYTHON_PREFIX}_INCLUDE_DIRS}")
3119
3120        # When available, enforce shared library generation with undefined symbols
3121        if (APPLE)
3122          set_property (TARGET ${_PYTHON_PREFIX}::Module
3123                        PROPERTY INTERFACE_LINK_OPTIONS "LINKER:-undefined,dynamic_lookup")
3124        endif()
3125        if (CMAKE_SYSTEM_NAME STREQUAL "SunOS")
3126          set_property (TARGET ${_PYTHON_PREFIX}::Module
3127                        PROPERTY INTERFACE_LINK_OPTIONS "LINKER:-z,nodefs")
3128        endif()
3129        if (CMAKE_SYSTEM_NAME STREQUAL "AIX")
3130          set_property (TARGET ${_PYTHON_PREFIX}::Module
3131                        PROPERTY INTERFACE_LINK_OPTIONS "LINKER:-b,erok")
3132        endif()
3133      endif()
3134    endif()
3135
3136    #
3137    # PYTHON_ADD_LIBRARY (<name> [STATIC|SHARED|MODULE] src1 src2 ... srcN)
3138    # It is used to build modules for python.
3139    #
3140    function (__${_PYTHON_PREFIX}_ADD_LIBRARY prefix name)
3141      cmake_parse_arguments (PARSE_ARGV 2 PYTHON_ADD_LIBRARY "STATIC;SHARED;MODULE;WITH_SOABI" "" "")
3142
3143      if (prefix STREQUAL "Python2" AND PYTHON_ADD_LIBRARY_WITH_SOABI)
3144        message (AUTHOR_WARNING "FindPython2: Option `WITH_SOABI` is not supported for Python2 and will be ignored.")
3145        unset (PYTHON_ADD_LIBRARY_WITH_SOABI)
3146      endif()
3147
3148      if (PYTHON_ADD_LIBRARY_STATIC)
3149        set (type STATIC)
3150      elseif (PYTHON_ADD_LIBRARY_SHARED)
3151        set (type SHARED)
3152      else()
3153        set (type MODULE)
3154      endif()
3155
3156      if (type STREQUAL "MODULE" AND NOT TARGET ${prefix}::Module)
3157        message (SEND_ERROR "${prefix}_ADD_LIBRARY: dependent target '${prefix}::Module' is not defined.\n   Did you miss to request COMPONENT 'Development.Module'?")
3158        return()
3159      endif()
3160      if (NOT type STREQUAL "MODULE" AND NOT TARGET ${prefix}::Python)
3161        message (SEND_ERROR "${prefix}_ADD_LIBRARY: dependent target '${prefix}::Python' is not defined.\n   Did you miss to request COMPONENT 'Development.Embed'?")
3162        return()
3163      endif()
3164
3165      add_library (${name} ${type} ${PYTHON_ADD_LIBRARY_UNPARSED_ARGUMENTS})
3166
3167      get_property (type TARGET ${name} PROPERTY TYPE)
3168
3169      if (type STREQUAL "MODULE_LIBRARY")
3170        target_link_libraries (${name} PRIVATE ${prefix}::Module)
3171        # customize library name to follow module name rules
3172        set_property (TARGET ${name} PROPERTY PREFIX "")
3173        if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
3174          set_property (TARGET ${name} PROPERTY SUFFIX ".pyd")
3175        endif()
3176
3177        if (PYTHON_ADD_LIBRARY_WITH_SOABI AND ${prefix}_SOABI)
3178          get_property (suffix TARGET ${name} PROPERTY SUFFIX)
3179          if (NOT suffix)
3180            set (suffix "${CMAKE_SHARED_MODULE_SUFFIX}")
3181          endif()
3182          set_property (TARGET ${name} PROPERTY SUFFIX ".${${prefix}_SOABI}${suffix}")
3183        endif()
3184      else()
3185        if (PYTHON_ADD_LIBRARY_WITH_SOABI)
3186          message (AUTHOR_WARNING "Find${prefix}: Option `WITH_SOABI` is only supported for `MODULE` library type.")
3187        endif()
3188        target_link_libraries (${name} PRIVATE ${prefix}::Python)
3189      endif()
3190    endfunction()
3191  endif()
3192
3193  if ("NumPy" IN_LIST ${_PYTHON_PREFIX}_FIND_COMPONENTS AND ${_PYTHON_PREFIX}_NumPy_FOUND
3194      AND NOT TARGET ${_PYTHON_PREFIX}::NumPy AND TARGET ${_PYTHON_PREFIX}::Module)
3195    add_library (${_PYTHON_PREFIX}::NumPy INTERFACE IMPORTED)
3196    set_property (TARGET ${_PYTHON_PREFIX}::NumPy
3197                  PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${${_PYTHON_PREFIX}_NumPy_INCLUDE_DIRS}")
3198    target_link_libraries (${_PYTHON_PREFIX}::NumPy INTERFACE ${_PYTHON_PREFIX}::Module)
3199  endif()
3200endif()
3201
3202# final clean-up
3203
3204# Restore CMAKE_FIND_APPBUNDLE
3205if (DEFINED _${_PYTHON_PREFIX}_CMAKE_FIND_APPBUNDLE)
3206  set (CMAKE_FIND_APPBUNDLE ${_${_PYTHON_PREFIX}_CMAKE_FIND_APPBUNDLE})
3207  unset (_${_PYTHON_PREFIX}_CMAKE_FIND_APPBUNDLE)
3208else()
3209  unset (CMAKE_FIND_APPBUNDLE)
3210endif()
3211# Restore CMAKE_FIND_FRAMEWORK
3212if (DEFINED _${_PYTHON_PREFIX}_CMAKE_FIND_FRAMEWORK)
3213  set (CMAKE_FIND_FRAMEWORK ${_${_PYTHON_PREFIX}_CMAKE_FIND_FRAMEWORK})
3214  unset (_${_PYTHON_PREFIX}_CMAKE_FIND_FRAMEWORK)
3215else()
3216  unset (CMAKE_FIND_FRAMEWORK)
3217endif()
3218