{% set version = "3.7.16" %}
{% set ver2 = '.'.join(version.split('.')[0:2]) %}
{% set ver2nd = ''.join(version.split('.')[0:2]) %}
{% set ver3nd = ''.join(version.split('.')[0:3]) %}
{% set linkage_nature = os.environ.get('PY_INTERP_LINKAGE_NATURE', '') %}
{% if linkage_nature != '' %}
  {% set linkage_nature = "_" ~ linkage_nature %}
{% endif %}
{% set dbg_abi = '' %}
{% set debug = os.environ.get('PY_INTERP_DEBUG', '') %}
{% if debug != '' %}
  {% set debug = "_dbg" %}
  {% set dbg_abi = "d" %}  # [win]
{% endif %}

# for some reason, this is not able to be read from the cbc.yaml with jinja2>=3.0.0?
{% set channel_targets = 'defaults' %}

package:
  name: python
  version: {{ version }}

source:
  - url: https://www.python.org/ftp/python/{{ version }}/Python-{{ version }}.tar.xz
    sha256: 8338f0c2222d847e904c955369155dc1beeeed806e8d5ef04b00ef4787238bfd
    patches:
      - patches/0001-Fix-off-by-one-error-in-_winapi_WaitForMultipleObjec.patch
      # Unfinished, still runs the whole thing.
      # - patches/0002-Adds-test.support.PGO-and-skips-tests-that-are-not-u.patch
{% if 'conda-forge' not in channel_targets %}
      - patches/0003-Add-Anaconda-Distribution-version-logic.patch
{% endif %}
      - patches/0004-Darwin-Add-arch-x86_64-to-UnicCCompiler-ld_args.patch
      - patches/0005-Win32-Change-FD_SETSIZE-from-512-to-2048.patch
      - patches/0006-Win32-distutils-Add-support-to-cygwinccompiler-for-V.patch
      - patches/0007-Do-not-pass-g-to-GCC-when-not-Py_DEBUG.patch
      - patches/0008-Support-cross-compiling-byte-code.patch
      - patches/0009-Win32-Fixes-for-Windows-GCC-interop-needed-by-RPy2-a.patch
      - patches/0010-Darwin-Look-in-sysroot-usr-lib-include-if-sysroot-is.patch
      - patches/0011-runtime_library_dir_option-Use-1st-word-of-CC-as-com.patch
      - patches/0012-Win32-Do-not-download-externals.patch
      - patches/0013-Add-support-for-_CONDA_PYTHON_SYSCONFIGDATA_NAME-if-.patch
      - patches/0014-Fix-find_library-so-that-it-looks-in-sys.prefix-lib-.patch
      - patches/0015-Disable-new-dtags-in-unixccompiler.py.patch
      - patches/0016-Fix-cross-compilation-on-Debian-based-distros.patch
      - patches/0017-Disable-registry-lookup-unless-CONDA_PY_ALLOW_REG_PA.patch
      - patches/0018-Unvendor-openssl.patch
      - patches/0019-Unvendor-sqlite3.patch
      # https://bugs.python.org/issue35644
      - patches/0020-venv-Revert-a-change-from-https-github.com-python-cp.patch
      - patches/0021-Add-CondaEcosystemModifyDllSearchPath.patch
      - patches/0022-Use-ranlib-from-env-if-env-variable-is-set.patch
      # MailCap CVE (CVE-2015-20107) was fixed in 3.7.16
      # - patches/0024-CVE-2015-20107.patch

  # TODO :: Depend on our own packages for these:
  - url: https://github.com/python/cpython-source-deps/archive/xz-5.2.2.zip          # [win]
    folder: externals/xz-5.2.2                                                       # [win]
    sha256: 02b6d6f1e0369940e7e1f0d324992e069d09f5f3d32d86e2d22cd8b82ac3fb3f         # [win]
  # Not used as of 3.7b3 (tcltk from cpython-bin- is), but kept for archival and license reasons.
  - url: https://github.com/python/cpython-source-deps/archive/tcl-core-8.6.9.0.zip  # [win]
    folder: externals/tcl-core-8.6.9.0                                               # [win]
    sha256: ea9e724536ec13b476ebbd47948303b92fdd90a33aa94f30fcc2f5c3a899f69e         # [win]
  # Not used as of 3.7b3 (tcltk from cpython-bin- is), but kept for archival and license reasons.
  - url: https://github.com/python/cpython-source-deps/archive/tk-8.6.9.0.zip        # [win]
    folder: externals/tk-8.6.9.0                                                     # [win]
    sha256: f3112b6bbc3bd163c877030187e73ce9a113ce03eff559ec5b9df697e3a08fad         # [win]
  - url: https://github.com/python/cpython-bin-deps/archive/tcltk-8.6.9.0.zip        # [win]
    folder: externals/tcltk-8.6.9.0                                                  # [win]
    sha256: 37cd629a75d2a34974a3d771ccab5c9e3caf3d138cea81040f9cdba1aded1cf2         # [win]
  - url: https://github.com/python/cpython-source-deps/archive/tix-8.4.3.6.zip       # [win]
    folder: externals/tix-8.4.3.6                                                    # [win]
    sha256: e558e3dc5e67ac0942f8fceafce00ca46b177da9ebeaf38ec7fafd9b9913ac56         # [win]
  - url: https://github.com/python/cpython-source-deps/archive/bzip2-1.0.8.zip       # [win]
    folder: externals/bzip2-1.0.8                                                    # [win]
    sha256: 12c17d15f99e27235529574a722fb484a4e8fdf2427cef53b1b68bdf07e404a9         # [win]
  - url: https://github.com/python/cpython-source-deps/archive/zlib-1.2.12.zip      # [win]
    folder: externals/zlib-1.2.12                                                    # [win]
    sha256: 92197502951ac8414ece9f2d9fa2cc010c51d610e6b2ed9833ba4a35d65f222b         # [win]
  - url: https://github.com/python/cpython-bin-deps/archive/nasm-2.11.06.zip         # [win]
    folder: externals/nasm-2.11.06                                                   # [win]
    sha256: de3c87b26a80e789986d8e6950c6304175d3829afe9c6c7211eb7257266ab0ac         # [win]

build:
  number: 0
  # Windows has issues updating python if conda is using files itself.
  # Copy rather than link.
  no_link:
    - DLLs/_ctypes.pyd  # [win]
  ignore_run_exports:  # [unix]
    # We delete the shared libraries here.
    - bzip2  # [unix]
    # C++ only installed so CXX is defined for distutils/sysconfig.
    - libcxx  # [osx]
    - libcxxabi  # [osx]
    - libstdcxx-ng  # [linux]
  detect_binary_files_with_prefix: False
  binary_has_prefix_files:
    - lib/libpython{{ ver2 }}.*.1.0
    - bin/python{{ ver2 }}  # [linux]
    - lib/libpython{{ ver2 }}.a  # [linux]
    - lib/libpython{{ ver2 }}.nolto.a  # [linux]
    # - lib/libpython3.so  # [linux]
    # - lib/python{{ ver2 }}/lib-dynload/_hashlib.cpython-{{ ver2nd }}-x86_64-linux-gnu.so  # [linux]
    - lib/libpython3.dylib  # [osx]
  # match python.org compiler standard
  skip: True  # [win and int(float(vc)) < 14 or (osx and arm64)]
{% if 'conda-forge' in channel_targets %}
  string: h{{ PKG_HASH }}_{{ PKG_BUILDNUM }}{{ linkage_nature }}_cpython{{ debug }}
{% else %}
  string: h{{ PKG_HASH }}_{{ PKG_BUILDNUM }}{{ linkage_nature }}{{ debug }}
{% endif %}
{% if 'conda-forge' in channel_targets %}
  run_exports:
    noarch:
      - python
    weak:
      - python_abi {{ ver2 }}.* *_cp{{ ver2nd }}{{ dbg_abi }}
{% else %}
  # run_exports:
  #   noarch:
  #     - python
  #   weak:
  #     - python
{% endif %}
  script_env:
   - PY_INTERP_LINKAGE_NATURE
   - PY_INTERP_DEBUG
   # Putting these here means they get emitted to build_env_setup.{sh,bat} meaning we can launch IDEs
   # after sourcing or calling that script without examine the contents of conda_build.{sh,bat} for
   # important env. vars.
   - 'OPENSSL_DIR=%PREFIX%\Library'  # [win]
   - 'SQLITE3_DIR=%PREFIX%\Library'  # [win]
# Not until: https://github.com/mingwandroid/conda-build/commit/c68a7d100866df7a3e9c0e3177fc7ef0ff76def9
# {% if 'conda-forge' in channel_targets %}
#      - CONDA_FORGE=yes
# {% else %}
#      - CONDA_FORGE=no
# {% endif %}
  missing_dso_whitelist:
  - '**/MSVCR71.dll'      # [win]
  - '**/MSVCR80.dll'      # [win]
  - '**libcrypt.so.1'     # [aarch64]
  # -=- libs that come from the sysroot.
  - '$RPATH/libc.so.6'        # [linux64]
  - '$RPATH/libcrypt.so.1'    # [linux64]
  - '$RPATH/libdl.so.2'       # [linux64]
  - '$RPATH/libm.so.6'        # [linux64]
  - '$RPATH/libnsl.so.1'      # [linux64]
  - '$RPATH/libpthread.so.0'  # [linux64]
  - '$RPATH/libutil.so.1'     # [linux64]

requirements:
  build:
    - {{ compiler('c') }}
    - {{ compiler('cxx') }}
    - {{ cdt('xorg-x11-proto-devel') }}  # [linux]
    - {{ cdt('libx11-devel') }}  # [linux]
    - {{ cdt('libuuid-devel') }}  # [linux and not (ppc64le or aarch64)]
    - make  # [not win]
    - pkg-config  # [not win]
    # configure script looks for llvm-ar for lto
{% if 'conda-forge' in channel_targets %}
    - llvm-tools  # [osx]
    - ld_impl_{{target_platform}}  # [linux]
    - binutils_impl_{{target_platform}} >=2.33.1  # [linux]
{% endif %}
    - patch  # [not win]
    - m2-patch  # [win]
    - m2-gcc-libs  # [win]
  host:
    - bzip2  # [not win]
    - sqlite
    - xz  # [not win]
    - zlib  # [not win]
    - openssl
    - readline  # [not win]
    - tk # [not win]
    - ncurses  # [not win]
    - libffi 3.4  # [not win]
    - ld_impl_{{target_platform}}  # [linux]
  run:
    - libffi >=3.4,<3.5  # [not win]
    - ld_impl_{{target_platform}}  # [linux]
{% if 'conda-forge' in channel_targets %}
  run_constrained:
    - python_abi {{ ver2 }}.* *_cp{{ ver2nd }}{{ dbg_abi }}
{% endif %}

test:
  requires:
    - ripgrep
    - cmake
    - ninja
    - cython
    - setuptools
    - {{ compiler('c') }}
    # Tried to use enable_language(C) to avoid needing this. It does not work.
    - {{ compiler('cxx') }}
  files:
    - tests/distutils/*
    - tests/distutils.cext/*
    - tests/cmake/*
    - tests/cython/*
    - tests/prefix-replacement/*
    - tests/processpoolexecutor-max_workers-61/*
  commands:
    - python -V
    - python3 -V            # [not win]
    - 2to3 -h
    - pydoc -h
    - python3-config --help  # [not win]
    - python -m venv %%TEMP%%\venv  # [win]
    - python -c "import sysconfig; print(sysconfig.get_config_var('CC'))"  # [not win]
    # diabled as it would fail for os update cos6->cos7
    # -  _CONDA_PYTHON_SYSCONFIGDATA_NAME=_sysconfigdata_x86_64_conda_cos6_linux_gnu python -c "import sysconfig; print(sysconfig.get_config_var('CC'))"  # [linux64]
    # check for unreplaced @ symbols in sysconfig files
    - for f in ${CONDA_PREFIX}/lib/python*/_sysconfig*.py; do echo "Checking $f:"; if [[ `rg @ $f` ]]; then echo "FAILED ON $f"; cat $f; exit 1; fi; done  # [linux64 or osx]
    - pushd tests
    -   pushd distutils
    -     python setup.py install -v -v
    -     python -c "import foobar"
    -   popd
    -   pushd distutils.cext
    # --single-version-externally-managed because .egg files with compiled code do not work on Windows (Python 3.8) when Py_DEBUG (this should be investigated)
    # .. an egg might be possible in debug if we use "build -g install" instead of "install" when debugging.
    -     python setup.py install -v -v  # [(not win) or environ.get('PY_INTERP_DEBUG') == '']
    -     python setup.py install --single-version-externally-managed --record=record.txt -v -v  # [win and environ.get('PY_INTERP_DEBUG') != '']
    -     python -v -v -v -c "import greet"
    -     python -v -v -v -c "import greet; greet.greet('Python user')" | rg "Hello Python"
    -   popd
    -   pushd prefix-replacement  # [unix]
    -     bash build-and-test.sh  # [unix and not (linux and aarch64)]
    -   popd  # [unix]
    # This used to fail on Windows.
    -   pushd processpoolexecutor-max_workers-61
    -     python ppe.py
    -   popd
    -   pushd cmake
    -     cmake -GNinja -DPY_VER={{ version }} -DPython_ROOT_DIR=%PREFIX% --trace --debug-output --debug-trycompile .  # [win]
    -   popd
    - popd

about:
  home: https://www.python.org/
  license: PSF-2.0
  license_family: PSF
  license_file: LICENSE
  summary: General purpose programming language
  description: |
    Python is a widely used high-level, general-purpose, interpreted, dynamic
    programming language. Its design philosophy emphasizes code
    readability, and its syntax allows programmers to express concepts in
    fewer lines of code than would be possible in languages such as C++ or
    Java. The language provides constructs intended to enable clear programs
    on both a small and large scale.
  doc_url: https://www.python.org/doc/versions/
  dev_url: https://devguide.python.org/

extra:
  recipe-maintainers:
    - isuruf
    - jakirkham
    - jjhelmus
    - mingwandroid
    - msarahan
    - pelson
    - ocefpaf
    - scopatz
