{% set build_num = 14 %}

{% set gcc_major = 13 if gcc_version is undefined else gcc_version.split(".")[0] %}
# generally, the runtime version of libstdcxx needs to be at least as high
# as the compiler; however, wherever libstdcxx changes the default ABI version
# of a symbol, we need to ensure at least that version at runtime also for
# packages built with older compilers (because that build environment will
# pull in the newest libstdcxx, and then depend on that new symbol version).
# C.f. https://johannst.github.io/notes/development/symbolver.html resp.
# check if some symbol changed ABI (and thus got reversioned) at the bottom of
# https://gcc.gnu.org/onlinedocs/libstdc++/manual/api.html
# In this case, libstdcxx last changed the default version of an ELF symbol
# in v12, therefore this is the lowest we can go for GCC <=12, see #97 or
# https://github.com/gcc-mirror/gcc/commit/9e18a25331fa25c3907249fede65a02c6817b06e
{% set last_symbol_bump = 12 %}
{% set min_runtime_version = gcc_major if gcc_major|int > last_symbol_bump else last_symbol_bump %}

{% if cross_target_platform is not defined %}
{% set cross_target_platform = "linux-64" %}
{% endif %}

{% if target_platform is not defined %}
{% set target_platform = "linux-64" %}
{% endif %}

{% if target_platform.startswith("win-") %}
{% set library_prefix = "Library/" %}
{% else %}
{% set library_prefix = "" %}
{% endif %}

package:
  name: ctng-compiler-activation
  version: {{ gcc_version }}

source:
  path: .

build:
  number: {{ build_num }}
  # Missing binutils and gcc on Windows.
  skip: True  # [not linux]
  {% if (target_platform == "win-64" and cross_target_platform != "win-64") or (cross_target_platform == "win-64" and gcc_major|int < 13) or cross_target_platform == "linux-s390x" or s390x %}
  skip: true
  {% endif %}

requirements:
  build:
    - shellcheck  # [not (aarch64 or ppc64le)]

outputs:
  - name: gcc_{{ cross_target_platform }}
    build:
      skip: true  # [not (linux or win)]
      run_exports:
        strong:
          - libgcc >={{ min_runtime_version }}
          # Need ucrt for windows<10
          - ucrt >=10.0.20348.0  # [cross_target_platform == "win-64"]
    script: install-gcc.sh
    requirements:
      run:
        - gcc_impl_{{ target_platform }} {{ gcc_version }}.*
        - gcc_impl_{{ cross_target_platform }} {{ gcc_version }}.*
        # for activation of binutils env vars
        - binutils_{{ cross_target_platform }}
        - {{ c_stdlib }}_{{ target_platform }}   # [linux]
        - {{ cross_stdlib }}_{{ cross_target_platform }}
        - gendef   # [cross_target_platform == "win-64"]
    test:
      requires:
        - {{ cross_stdlib }}_{{ cross_target_platform }} {{ cross_stdlib_version }}
      files:
        - tests
      commands:
        - ${CC} ${CFLAGS} -Wall tests/aligned_alloc.c -c -o c_aligned.o -v
        - ${CC} ${LDFLAGS} c_aligned.o -o c_aligned     # [target_platform == cross_target_platform]
        - ./c_aligned                                   # [target_platform == cross_target_platform]
        # CONDA_BUILD_SYSROOT is defined for clang++ to find correct C++ headers, see issue 8
        - test -z "${CONDA_BUILD_SYSROOT+x}" && echo "CONDA_BUILD_SYSROOT is not set" && exit 1
        - test -d ${CONDA_BUILD_SYSROOT} || exit 1
    about:
      summary: GNU C Compiler (activation scripts)
      home: https://github.com/conda-forge/ctng-compiler-activation-feedstock
      license: BSD-3-Clause
      license_file: LICENSE

  - name: clang_impl_{{ cross_target_platform }}
    version: {{ clang_version }}
    script: install-clang_impl.sh
    build:
      # only for linux; osx builds are done on clang-compiler-activation-feedstock
      skip: true  # [win or s390x or cross_target_platform not in ("linux-64", "linux-aarch64", "linux-ppc64le")]
    requirements:
      run:
        - libgcc-devel_{{ cross_target_platform }} {{ gcc_version }}.*
        - {{ cross_stdlib }}_{{ cross_target_platform }}
        - binutils_impl_{{ cross_target_platform }}
        - compiler-rt_{{ cross_target_platform }}
        - compiler-rt =={{ clang_version }}.*
        - clang =={{ clang_version }}.*
    test:
      commands:
        - {{ triplet }}-clang --version
        - {{ triplet }}-clang-cpp --version
    about:
      summary: Clang Compiler (activation scripts)
      home: https://github.com/conda-forge/ctng-compiler-activation-feedstock
      license: BSD-3-Clause
      license_file: LICENSE

  - name: clang_{{ cross_target_platform }}
    version: {{ clang_version }}
    script: install-clang.sh
    build:
      skip: true  # [win or s390x or cross_target_platform in ("linux-s390x", "win-64")]
      run_exports:
        strong:
          - libgcc >={{ min_runtime_version }}
    requirements:
      run:
        # for activation of binutils env vars
        - binutils_{{ cross_target_platform }}
        - clang_impl_{{ cross_target_platform }} =={{ clang_version }}.*
    test:
      requires:
        - {{ cross_stdlib }}_{{ cross_target_platform }} {{ cross_stdlib_version }}
      files:
        - tests
      commands:
        - ${CC} ${CFLAGS} -Wall tests/aligned_alloc.c -c -o c_aligned.o -v
        - ${CC} ${LDFLAGS} c_aligned.o -o c_aligned     # [target_platform == cross_target_platform]
        - ./c_aligned                                   # [target_platform == cross_target_platform]
        # CONDA_BUILD_SYSROOT is defined for clang++ to find correct C++ headers, see issue 8
        - test -z "${CONDA_BUILD_SYSROOT+x}" && echo "CONDA_BUILD_SYSROOT is not set" && exit 1
        - test -d ${CONDA_BUILD_SYSROOT} || exit 1
    about:
      summary: Clang Compiler (activation scripts)
      home: https://github.com/conda-forge/ctng-compiler-activation-feedstock
      license: BSD-3-Clause
      license_file: LICENSE

  - name: gxx_{{ cross_target_platform }}
    script: install-g++.sh
    build:
      skip: true  # [not (linux or win)]
      run_exports:
        strong:
          # This should be a transitive dependency, but conda-build doesn't support those
          - libstdcxx >={{ min_runtime_version }}
          # Because transitive run_exports do not work:
          - libgcc >={{ min_runtime_version }}
          # Need ucrt for windows<10
          - ucrt >=10.0.20348.0  # [cross_target_platform == "win-64"]
    requirements:
      run:
        - gxx_impl_{{ target_platform }} {{ gcc_version }}.*
        - gxx_impl_{{ cross_target_platform }} {{ gcc_version }}.*
        # for activation of gcc env vars
        - {{ pin_subpackage("gcc_" ~ cross_target_platform, exact=True) }}
        # for activation of binutils env vars
        - binutils_{{ cross_target_platform }}
        - {{ cross_stdlib }}_{{ cross_target_platform }}
    test:
      requires:
        - {{ cross_stdlib }}_{{ cross_target_platform }} {{ cross_stdlib_version }}
      files:
        - tests
      commands:
        - ${CXX} ${CXXFLAGS} -Wall tests/aligned_alloc.cpp -c -o cpp_aligned.o
        - ${CXX} ${LDFLAGS} cpp_aligned.o -o cpp_aligned    # [target_platform == cross_target_platform]
        - ./cpp_aligned                                     # [target_platform == cross_target_platform]
        - test -z "${CONDA_BUILD_SYSROOT+x}" && echo "CONDA_BUILD_SYSROOT is not set" && exit 1
        - test -d ${CONDA_BUILD_SYSROOT} || exit 1
    about:
      summary: GNU C++ Compiler (activation scripts)
      home: https://github.com/conda-forge/ctng-compiler-activation-feedstock
      license: BSD-3-Clause
      license_file: LICENSE

  - name: clangxx_impl_{{ cross_target_platform }}
    version: {{ clang_version }}
    script: install-clangxx_impl.sh
    build:
      # only for linux; osx builds are done on clang-compiler-activation-feedstock
      skip: true  # [win or s390x or cross_target_platform not in ("linux-64", "linux-aarch64", "linux-ppc64le")]
    requirements:
      run:
        - {{ pin_subpackage("clang_impl_" ~ cross_target_platform, exact=True) }}
        # C++ compiler on linux needs a stdlib
        - libstdcxx-devel_{{ cross_target_platform }} =={{ gcc_version }}.*
        - clangxx =={{ clang_version }}.*
    test:
      commands:
        - x86_64-conda-linux-gnu-clang++ --version          # [linux64]
        - aarch64-conda-linux-gnu-clang++ --version         # [aarch64]
        - powerpc64le-conda-linux-gnu-clang++ --version     # [ppc64le]
    about:
      summary: Clang C++ Compiler (activation scripts)
      home: https://github.com/conda-forge/ctng-compiler-activation-feedstock
      license: BSD-3-Clause
      license_file: LICENSE

  - name: clangxx_{{ cross_target_platform }}
    version: {{ clang_version }}
    script: install-clang++.sh
    build:
      skip: true  # [win or s390x or cross_target_platform in ("linux-s390x", "win-64")]
      run_exports:
        strong:
          # This should be a transitive dependency, but conda-build doesn't support those
          - libstdcxx >={{ min_runtime_version }}
          # Because transitive run_exports do not work:
          - libgcc >={{ min_runtime_version }}
    requirements:
      run:
        # for activation of gcc env vars
        - {{ pin_subpackage("clang_" ~ cross_target_platform, exact=True) }}
        - clangxx_impl_{{ cross_target_platform }} =={{ clang_version }}.*
        # the dependency below is redundant, but we need to ensure different hashes
        # since the clang_{impl_,}_<target> outputs depend on a specific gcc version
        - libstdcxx-devel_{{ cross_target_platform }} =={{ gcc_version }}.*
    test:
      requires:
        - {{ cross_stdlib }}_{{ cross_target_platform }} {{ cross_stdlib_version }}
      files:
        - tests
      commands:
        - ${CXX} ${CXXFLAGS} -Wall tests/aligned_alloc.cpp -c -o cpp_aligned.o
        - ${CXX} ${LDFLAGS} cpp_aligned.o -o cpp_aligned    # [target_platform == cross_target_platform]
        - ./cpp_aligned                                     # [target_platform == cross_target_platform]
        - test -z "${CONDA_BUILD_SYSROOT+x}" && echo "CONDA_BUILD_SYSROOT is not set" && exit 1
        - test -d ${CONDA_BUILD_SYSROOT} || exit 1
    about:
      summary: Clang C++ Compiler (activation scripts)
      home: https://github.com/conda-forge/ctng-compiler-activation-feedstock
      license: BSD-3-Clause
      license_file: LICENSE

  - name: gfortran_{{ cross_target_platform }}
    script: install-gfortran.sh
    build:
      skip: true  # [not (linux or win)]
      run_exports:
        strong:
          - libgfortran{{ libgfortran_soname }} >={{ gcc_version }}
          - libgfortran
          # Because transitive run_exports do not work:
          - libgcc >={{ min_runtime_version }}
          # Need ucrt for windows<10
          - ucrt >=10.0.20348.0  # [cross_target_platform == "win-64"]
    requirements:
      run:
        - gfortran_impl_{{ cross_target_platform }} {{ gcc_version }}.*
        - gfortran_impl_{{ target_platform }} {{ gcc_version }}.*
        # for activation of gcc env vars:
        - {{ pin_subpackage("gcc_" ~ cross_target_platform, exact=True) }}
        # for activation of binutils env vars:
        - binutils_{{ cross_target_platform }}
        - {{ cross_stdlib }}_{{ cross_target_platform }}
    test:
      requires:
        - cmake >=3.11  # [x86_64 or aarch64 or ppc64le]
        - make          # [x86_64 or aarch64 or ppc64le]
        - {{ cross_stdlib }}_{{ cross_target_platform }} {{ cross_stdlib_version }}
      commands:
        - ${FC} --version
        - pushd tests/fortomp
        - sh test_fort.sh       # [target_platform == cross_target_platform and (x86_64 or aarch64 or ppc64le)]
      files:
        - tests/fortomp/*
    about:
      summary: GNU Fortran Compiler (activation scripts)
      home: https://github.com/conda-forge/ctng-compiler-activation-feedstock
      license: BSD-3-Clause
      license_file: LICENSE

  - name: gcc_bootstrap_{{ cross_target_platform }}
    version: "{{ gcc_version }}"
    build:
      skip: true  # [not (linux or win)]
      binary_relocation: false
      detect_binary_files_with_prefix: false
      always_include_files:
        - {{ library_prefix }}bin/
        - {{ library_prefix }}etc/
        - {{ library_prefix }}lib64/
        - {{ library_prefix }}lib/
        - {{ library_prefix }}libexec/
        - {{ library_prefix }}share/
        - {{ library_prefix }}x86_64-conda-linux-gnu/
        - {{ library_prefix }}powerpc64le-conda-linux-gnu/
        - {{ library_prefix }}aarch64-conda-linux-gnu/
        - {{ library_prefix }}s390x-conda-linux-gnu/
        - {{ library_prefix }}x86_64-w64-mingw32/
      missing_dso_whitelist:  # [linux]
        - /lib64/*            # [linux]
      overlinking_ignore_patterns:  # [linux]
        # This is specifically the 'not a valid tag' issue and the '*'
        # is because there's no indication which file is an issue and
        # this package is a repack of the artifacts built in
        # ctng-compilers-feedstock.
        - '*'                       # [linux]
    requirements:
      host:
        - binutils_impl_{{ cross_target_platform }} {{ binutils_version }}.*
        - gfortran_impl_{{ cross_target_platform }} {{ gcc_version }}.*
        - gxx_impl_{{ cross_target_platform }} {{ gcc_version }}.*
        - gcc_impl_{{ cross_target_platform }} {{ gcc_version }}.*
        - binutils_impl_{{ target_platform }} {{ binutils_version }}.*
        - gfortran_impl_{{ target_platform }} {{ gcc_version }}.*
        - gxx_impl_{{ target_platform }} {{ gcc_version }}.*
        - gcc_impl_{{ target_platform }} {{ gcc_version }}.*
    test:
      commands:
        - test -f $PREFIX/bin/{{ triplet }}-cc                 # [target_platform != "win-64"]
        - test -f $PREFIX/Library/bin/{{ triplet }}-cc.exe     # [target_platform == "win-64"]
    about:
      summary: GCC bootstrap compilers for building deps
      home: https://github.com/conda-forge/ctng-compiler-activation-feedstock
      license: GPL
      license_file: GPL_LICENSE

about:
  summary: Activation scripts for gcc, g++, gfortran and clang
  home: https://github.com/conda-forge/ctng-compiler-activation-feedstock
  license: BSD-3-Clause
  license_file: LICENSE

extra:
  recipe-maintainers:
    - isuruf
    - beckermr
