{% set pkg_name = "lz4-c" %}
{% set name = "lz4" %}
{% set version = "1.9.4" %}

package:
  name: {{ pkg_name }}-split
  version: {{ version }}

source:
  url: https://github.com/{{ name }}/{{ name }}/archive/v{{ version }}.tar.gz
  sha256: 0b0e3aa07c8c063ddf40b082bdf7e37a1562bda40a0ff5272957f3e987e0e54b
  patches:
    - patches/windows-lz4-c-output-name.patch  # [win]

build:
  number: 5

requirements:
  build:
    - {{ stdlib('c') }}
    - {{ compiler('c') }}
    - {{ compiler('cxx') }}  # [not win]
    - make  # [not win]
    - cmake-no-system  # [win]
    - ninja-base  # [win]
  host:
  run:

outputs:
  - name: lz4-c
    build:
      # https://abi-laboratory.pro/index.php?view=timeline&l=lz4
      run_exports:
        - {{ pin_subpackage(pkg_name, max_pin='x.x') }}
    requirements:
      build:
        - {{ stdlib('c') }}
        - {{ compiler('c') }}
        - {{ compiler('cxx') }}  # [not win]
        - make  # [not win]
        - cmake-no-system  # [win]
        - ninja-base  # [win]
    # Multi-output recipes disable conda-build's default "bundle everything in
    # PREFIX" behavior; each output needs explicit files: and/or a per-output
    # script.  Here, recipe-level bld.bat/build.sh install shared artifacts
    # into PREFIX once before outputs run.  The lz4-c output has no script, so
    # these globs are what populate the package.
    files:
      - bin/**                          # [unix]
      - include/**                      # [unix]
      - lib/liblz4*{{ SHLIB_EXT }}*     # [unix]
      - lib/pkgconfig/**                # [unix]
      - share/man/man1/**               # [unix]
      - Library/bin/liblz4.dll          # [win]
      - Library/bin/lz4.exe             # [win]
      - Library/include/**              # [win]
      - Library/lib/liblz4.lib          # [win]
      - Library/lib/pkgconfig/**        # [win]
      - Library/lib/cmake/lz4/**        # [win]
      - Library/share/man/**            # [win]
    test:
      requires:
        - pkg-config  # [unix]

      commands:
        - lz4 -h
        - lz4c -h    # [unix]
        - lz4cat -h  # [unix]
        - unlz4 -h   # [unix]

        - test -f ${PREFIX}/include/lz4.h       # [unix]
        - test -f ${PREFIX}/include/lz4hc.h     # [unix]
        - test -f ${PREFIX}/include/lz4frame.h  # [unix]

        - if not exist %LIBRARY_INC%\\lz4.h exit 1       # [win]
        - if not exist %LIBRARY_INC%\\lz4hc.h exit 1     # [win]
        - if not exist %LIBRARY_INC%\\lz4frame.h exit 1  # [win]

        - test ! -f ${PREFIX}/lib/liblz4.a      # [unix]
        - test -f ${PREFIX}/lib/liblz4.dylib  # [osx]
        - test -f ${PREFIX}/lib/liblz4.so     # [linux]

        - if not exist %LIBRARY_BIN%\\liblz4.dll exit 1         # [win]
        - if not exist %LIBRARY_LIB%\\liblz4.lib exit 1         # [win]
        - if exist %LIBRARY_LIB%\\liblz4_static.lib exit 1  # [win]
        - if exist %LIBRARY_BIN%\\lz4.dll exit 1         # [win]
        - if exist %LIBRARY_LIB%\\lz4.lib exit 1         # [win]
        - powershell -NoProfile -Command "$t=[Text.Encoding]::ASCII.GetString([IO.File]::ReadAllBytes($env:LIBRARY_LIB + '\liblz4.lib')); if ($t -notlike '*liblz4.dll*') { exit 1 }; if ($t.Replace('liblz4.dll','') -like '*lz4.dll*') { exit 1 }"  # [win]
        - powershell -NoProfile -Command "$t=[Text.Encoding]::ASCII.GetString([IO.File]::ReadAllBytes($env:LIBRARY_BIN + '\liblz4.dll')); if ($t -notlike '*liblz4.dll*') { exit 1 }; if ($t.Replace('liblz4.dll','') -like '*lz4.dll*') { exit 1 }"  # [win]

        - test -f ${PREFIX}/lib/pkgconfig/liblz4.pc  # [unix]
        - pkg-config --cflags --libs liblz4          # [unix]
        - if not exist %LIBRARY_LIB%\\pkgconfig\\liblz4.pc exit 1  # [win]
        - findstr /C:"-lliblz4" "%LIBRARY_LIB%\\pkgconfig\\liblz4.pc"  # [win]
        - if not exist %LIBRARY_LIB%\\cmake\\lz4\\lz4Targets.cmake exit 1  # [win]
        - findstr /C:"liblz4.lib" "%LIBRARY_LIB%\\cmake\\lz4\\lz4Targets-release.cmake"  # [win]
        - findstr /C:"liblz4.dll" "%LIBRARY_LIB%\\cmake\\lz4\\lz4Targets-release.cmake"  # [win]

  - name: lz4-c-static
    script: build_static.sh  # [unix]
    script: bld_static.bat  # [win]
    test:
      commands:
        - test -f ${PREFIX}/lib/liblz4.a      # [unix]
        - if not exist %LIBRARY_LIB%\\liblz4_static.lib exit 1  # [win]

about:
  home: https://lz4.org/
  license: BSD-2-Clause
  license_family: BSD
  license_file: lib/LICENSE
  summary: Extremely Fast Compression algorithm
  description: |
    LZ4 is lossless compression algorithm, providing compression speed at 400
    MB/s per core (0.16 Bytes/cycle). It features an extremely fast decoder,
    with speed in multiple GB/s per core (0.71 Bytes/cycle). A high compression
    derivative, called LZ4_HC, is available, trading customizable CPU time for
    compression ratio. LZ4 library is provided as open source software using a
    BSD license.
  dev_url: https://github.com/lz4/lz4
  doc_url: https://github.com/lz4/lz4/blob/dev/README.md

extra:
  recipe-maintainers:
    - mingwandroid
    - rmax
    - wesm
    - xhochy
