{% set version = "3.3.1" %}

package:
  name: openssl_split
  version: {{ version }}

source:
  url: http://www.openssl.org/source/openssl-{{ version }}.tar.gz
  sha256: 777cd596284c883375a2a7a11bf5d2786fc5413255efab20c50d6ffe6d020b7e
  patches:
    # backport https://github.com/openssl/openssl/pull/24687 (2 commits)
    - patches/0001-Give-util-mkinstallvars.pl-more-fine-grained-control.patch
    - patches/0002-Adapt-all-the-exporter-files-to-the-new-vars-from-ut.patch

build:
  number: 1

requirements:
  build:
    - {{ compiler('c') }}
    - {{ stdlib('c') }}
    - nasm               # [win]
    - make               # [unix]
    - perl *
  # Empty host section to ensure that this is identified as cb3
  host:

outputs:
  - name: openssl
    script: install_openssl.sh  # [unix]
    script: install_openssl.bat  # [win]
    build:
      no_link: lib/libcrypto.so.3.0  # [linux]
      no_link: lib/libcrypto.3.0.dylib  # [osx]
      run_exports:
        # openssl's versioning used to be X.Y.Z(rev), but is MAJOR.MINOR.PATCH since 3.0.0;
        # see https://www.openssl.org/policies/releasestrat.html
        - {{ pin_subpackage('openssl', max_pin='x') }}
    requirements:
      build:
        - {{ compiler('c') }}
        - {{ stdlib('c') }}
        - make
        - perl *
      # Empty host section to ensure that this is identified as cb3
      # FIXME: this doesn't seem to be enough. We need a compiler in build
      # with `compiler` jinja used to not use legacy cb2 build==host
      host:
      run:
        - ca-certificates
        - ucrt             # [win]
      run_constrained:
        # ensure the solver doesn't run into a situation where it picks old pyopenssl and breaks, see
        # https://github.com/conda-forge/pyopenssl-feedstock/issues/30; the symbol OpenSSL_add_all_algorithms
        # is deprecated in OpenSSL and not available by default, it was present in pyopenssl until
        # https://github.com/pyca/pyopenssl/commit/382e5e04410b8f07383b5fc5244a2d93b07b0baf
        - pyopenssl >=22.1
    test:
      requires:
        - pkg-config
        - ripgrep
      commands:
        - copy NUL checksum.txt        # [win]
        - touch checksum.txt           # [unix]
        - $PREFIX/bin/openssl sha256 checksum.txt       # [unix]
        - '%LIBRARY_BIN%\openssl sha256 checksum.txt'   # [win]
        - if not exist "%SSL_CERT_FILE%" exit 1           # [win]

        # test pkgconfig metadata
        - pkg-config --print-errors --exact-version "{{ version }}" openssl
        # test prefix is set as well (see #155)
        - if [[ "$(pkg-config --variable=prefix openssl)" == "" ]]; then exit 1; else exit 0; fi  # [unix]

        # make it possible to check !ERRORLEVEL!
        - setlocal EnableDelayedExpansion   # [win]
        # ensure (de)activation scripts for bash-on-win do not contain crlf line endings;
        # ripgrep will return exit code 1 if no match is found, which is what we want after
        # filtering to the carriage-return character that shouldn't be there.
        {% for task in ["activate", "deactivate"] %}
        - type %CONDA_PREFIX%\etc\conda\{{ task }}.d\openssl_{{ task }}-win.sh | rg \r & if !ERRORLEVEL! EQU 1 (exit 0) else (exit 1)  # [win]
        {% endfor %}

  - name: libopenssl-static
    script: install_libopenssl-static.sh  # [unix]
    script: install_libopenssl-static.bat  # [win]
    requirements:
      build:
        - {{ compiler('c') }}
        - {{ stdlib('c') }}
      run:
        - {{ pin_subpackage('openssl', exact=True) }}
    test:
      commands:
        - test -f ${PREFIX}/lib/libcrypto.a                   # [unix]
        - test -f ${PREFIX}/lib/libssl.a                      # [unix]
        - if exist %LIBRARY_PREFIX%\lib\libcrypto_static.lib (exit 0) else (exit 1)  # [win]
        - if exist %LIBRARY_PREFIX%\lib\libssl_static.lib (exit 0) else (exit 1)     # [win]

about:
  home: http://www.openssl.org/
  license_file: LICENSE.txt
  license: Apache-2.0
  license_family: Apache
  summary: OpenSSL is an open-source implementation of the SSL and TLS protocols
  dev_url: https://github.com/openssl/openssl
  doc_url: https://www.openssl.org/docs/man3.0/

extra:
  recipe-maintainers:
    - jakirkham
    - msarahan
    - ocefpaf
    - pelson
    - h-vetinari
  feedstock-name: openssl
