{% set version = gcc_version %}
{% set build_num = 19 %}

{% if gcc_maj_ver is not defined %}
{% set gcc_maj_ver = 15 %}
{% endif %}

# libgcc-devel is a noarch: generic package that is built for
# cross-compilers as well. Instead of skipping for cross-compilers,
# let's prioritize the native compilers package
{% if target_platform == cross_target_platform %}
{% set libgcc_devel_build_num = build_num + 100 %}
{% else %}
{% set libgcc_devel_build_num = build_num %}
{% endif %}

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

package:
  name: gcc_compilers
  version: {{ version }}

source:
  - url: 
      #- https://ftp.gnu.org/gnu/gcc/gcc-{{ version }}/gcc-{{ version }}.tar.gz
      - https://mirrors.ocf.berkeley.edu/gnu/gcc/gcc-{{ version }}/gcc-{{ version }}.tar.gz
    sha256: 7294d65cc1a0558cb815af0ca8c7763d86f7a31199794ede3f630c0d1b0a5723   # [gcc_version == "15.2.0"]
    sha256: ace8b8b0dbfe6abfc22f821cb093e195aa5498b7ccf7cd23e4424b9f14afed22   # [gcc_version == "14.3.0"]
    sha256: bf0baf3e570c9c74c17c8201f0196c6924b4bd98c90e69d6b2ac0cd823f33bbc   # [gcc_version == "13.4.0"]
    patches:
      - patches/0001-allow-commands-in-main-specfile.patch
      - patches/0002-patch-zoneinfo_dir_override-to-point-to-our-tzdata.patch       # [gcc_maj_ver != 13]
      {% if cross_target_platform.startswith("linux-") %}
      - patches/0003-add-ldl-to-libstdc___la_LDFLAGS.patch                          # [gcc_maj_ver != 13]
      - patches/0004-Hardcode-HAVE_ALIGNED_ALLOC-1-in-libstdc-v3-configur.patch
      {% elif cross_target_platform.startswith("win-") %}
      # for GCC 15: https://github.com/msys2/MINGW-packages/tree/f59921184b35858d4ceb91679578de0d62475cbf/mingw-w64-gcc
      # for GCC 14: https://github.com/msys2/MINGW-packages/tree/331bf945d21af562d228ed46bda21c8272d1e76e/mingw-w64-gcc
      # for GCC 13: https://github.com/msys2/MINGW-packages/tree/4f1262b4e1072632eccf0958764f90d890b832ac/mingw-w64-gcc
      - patches/mingw/{{ gcc_maj_ver }}/0002-Relocate-libintl.patch                                             # [gcc_maj_ver == 13]
      - patches/mingw/{{ gcc_maj_ver }}/0003-Windows-Follow-Posix-dir-exists-semantics-more-close.patch
      - patches/mingw/{{ gcc_maj_ver }}/0005-Windows-Don-t-ignore-native-system-header-dir.patch
      - patches/mingw/{{ gcc_maj_ver }}/0006-Windows-New-feature-to-allow-overriding.patch                      # [gcc_maj_ver == 13]
      - patches/mingw/{{ gcc_maj_ver }}/0007-Build-EXTRA_GNATTOOLS-for-Ada.patch
      - patches/mingw/{{ gcc_maj_ver }}/0008-Prettify-linking-no-undefined.patch
      - patches/mingw/{{ gcc_maj_ver }}/0011-Enable-shared-gnat-implib.patch
      - patches/mingw/{{ gcc_maj_ver }}/0012-Handle-spaces-in-path-for-default-manifest.patch
      - patches/mingw/{{ gcc_maj_ver }}/0014-gcc-9-branch-clone_function_name_1-Retain-any-stdcall-suffix.patch
      - patches/mingw/{{ gcc_maj_ver }}/0020-libgomp-Don-t-hard-code-MS-printf-attributes.patch
      - patches/mingw/{{ gcc_maj_ver }}/0021-PR14940-Allow-a-PCH-to-be-mapped-to-a-different-addr.patch         # [gcc_maj_ver != 15]
      - patches/mingw/{{ gcc_maj_ver }}/0140-gcc-diagnostic-color.patch
      - patches/mingw/{{ gcc_maj_ver }}/0200-add-m-no-align-vector-insn-option-for-i386.patch
      - patches/mingw/{{ gcc_maj_ver }}/0300-override-builtin-printf-format.patch                               # [gcc_maj_ver == 13]
      - patches/mingw/{{ gcc_maj_ver }}/2000-enable-rust.patch                                                  # [gcc_maj_ver == 13]
      - patches/mingw/{{ gcc_maj_ver }}/2001-fix-building-rust-on-mingw-w64.patch
      - patches/mingw/{{ gcc_maj_ver }}/2f7e7bfa3c6327793cdcdcb5c770b93cecd49bd0.patch                          # [gcc_maj_ver == 13]
      - patches/mingw/{{ gcc_maj_ver }}/3eeb4801d6f45f6250fc77a6d3ab4e0115f8cfdd.patch                          # [gcc_maj_ver == 13]
      - patches/mingw/{{ gcc_maj_ver }}/9002-native-tls.patch                                                   # [gcc_maj_ver == 15]
      {% elif cross_target_platform.startswith("osx-") %}
      # all patches from https://github.com/Homebrew/formula-patches/tree/698885df7f624d0ce15bceb79a4d9760a473b502/gcc
      # and edited with `s/-Wc,-nodefaultrpaths/-nodefaultrpaths`.
      # in case of several patches per version, use newest one
      - patches/macos/gcc-{{ version }}.diff
      # from https://github.com/JuliaPackaging/Yggdrasil/blob/master/0_RootFS/GCCBootstrap%4014/bundled/patches/gcc/gcc1420-Increase-fallback-min-macOS-version-for-libgcc-to-10.8.patch
      - patches/macos/0001-GCC-{{ gcc_maj_ver }}-Increase-fallback-min-macOS-version-for-libgc.patch   # [cross_target_platform == "osx-64"]
      # Make -stdlib=libcxx the default
      - patches/macos/0002-Make-stdlib-libc-the-default-for-macos-platforms.patch
      # call xcrun to get sdkroot
      - patches/macos/0003-Find-macos-sysroot-by-calling-xcrun.patch
      - patches/macos/fix-malloc-error.diff
      {% endif %}
  # Need an up-to-date zlib for recent clang
  - url: https://github.com/madler/zlib/releases/download/v1.3.1/zlib-1.3.1.tar.gz
    sha256: 9a93b2b7dfdac77ceba5a558a580e74667dd6fede4585b91eefb60f03b72df23
    folder: zlib2

build:
  number: {{ build_num }}
  skip: true  # [(win and cross_target_platform != "win-64") or ((cross_target_platform in ("osx-64", "osx-arm64") or osx) and gcc_maj_ver < 15)]
  detect_binary_files_with_prefix: false
  ignore_run_exports_from:
    - {{ cross_target_stdlib }}_{{ cross_target_platform }} {{ cross_target_stdlib_version }}

requirements:
  build:
    # Build dependencies are installed in setup_compilers.sh due to
    # conda-build bugs
  host:
    - {{ cross_target_stdlib }}_{{ cross_target_platform }} {{ cross_target_stdlib_version }}  # [cross_target_stdlib != "None"]

outputs:
  - name: libgcc-devel_{{ cross_target_platform }}
    script: install-libgcc-devel.sh
    build:
      noarch: generic
      number: {{ libgcc_devel_build_num }}
      detect_binary_files_with_prefix: false
      missing_dso_whitelist:
        - "*"
      ignore_run_exports_from:
        - {{ cross_target_stdlib }}_{{ cross_target_platform }} {{ cross_target_stdlib_version }}
    requirements:
      build:
      host:
        - {{ cross_target_stdlib }}_{{ cross_target_platform }} {{ cross_target_stdlib_version }}  # [cross_target_stdlib != "None"]
      run:
        - __unix   # [unix]
        - m2-conda-epoch    # [win]
    test:
      commands:
        - echo {{ cross_target_stdlib_version }}
        - test -f ${PREFIX}/lib/gcc/{{ TARGET }}/{{ gcc_version }}/crtbegin.o    # [cross_target_platform not in ("osx-64", "osx-arm64")]
        - test -f ${PREFIX}/lib/gcc/{{ TARGET }}/{{ gcc_version }}/crtfastmath.o    # [cross_target_platform in ("osx-64", "osx-arm64")]
        - test -f ${PREFIX}/lib/gcc/{{ TARGET }}/{{ gcc_version }}/libgcc_eh.a
        - test -f ${PREFIX}/lib/gcc/{{ TARGET }}/{{ gcc_version }}/libgcc.a
        {% if cross_target_platform.startswith("linux-") %}
        - test -f ${PREFIX}/lib/gcc/{{ TARGET }}/{{ gcc_version }}/libgcc_s.so
        {% elif cross_target_platform == "osx-64" %}
        - test -f ${PREFIX}/lib/gcc/{{ TARGET }}/{{ gcc_version }}/libgcc_s.1.dylib
        - test -f ${PREFIX}/lib/gcc/{{ TARGET }}/{{ gcc_version }}/libgcc_s.1.1.dylib
        {% elif cross_target_platform == "osx-arm64" %}
        - test -f ${PREFIX}/lib/gcc/{{ TARGET }}/{{ gcc_version }}/libgcc_s.1.1.dylib
        {% else %}
        - test -f ${PREFIX}/lib/gcc/{{ TARGET }}/{{ gcc_version }}/libgcc_s.a
        {% endif %}
    about:
      summary: The GNU C development libraries and object files
      home: https://gcc.gnu.org/
      license: GPL-3.0-only WITH GCC-exception-3.1

  - name: libstdcxx-devel_{{ cross_target_platform }}
    script: install-libstdc++-devel.sh
    build:
      noarch: generic
      number: {{ libgcc_devel_build_num }}
      skip: true  # [cross_target_cxx_stdlib != "libstdcxx"]
      detect_binary_files_with_prefix: false
      missing_dso_whitelist:
        - "*"
      ignore_run_exports_from:
        - {{ cross_target_stdlib }}_{{ cross_target_platform }}
    requirements:
      build:
      host:
        - {{ cross_target_stdlib }}_{{ cross_target_platform }} {{ cross_target_stdlib_version }}  # [cross_target_stdlib != "None"]
      run:
        - __unix   # [unix]
        - m2-conda-epoch    # [win]
    test:
      commands:
        - test -f ${PREFIX}/lib/gcc/{{ TARGET }}/{{ gcc_version }}/libstdc++.a
        - test -f ${PREFIX}/lib/gcc/{{ TARGET }}/{{ gcc_version }}/libstdc++fs.a
        - test -f ${PREFIX}/lib/gcc/{{ TARGET }}/{{ gcc_version }}/libsupc++.a
        - test -f ${PREFIX}/lib/gcc/{{ TARGET }}/{{ gcc_version }}/include/c++/cstdio
        {% if cross_target_platform.startswith("linux-") %}
        - test -f ${PREFIX}/lib/gcc/{{ TARGET }}/{{ gcc_version }}/libstdc++.so
        {% else %}
        - test -f ${PREFIX}/lib/gcc/{{ TARGET }}/{{ gcc_version }}/libstdc++.dll.a
        {% endif %}
    about:
      summary: The GNU C++ headers and development libraries
      home: https://gcc.gnu.org/
      license: GPL-3.0-only WITH GCC-exception-3.1

  - name: gcc_impl_{{ cross_target_platform }}
    script: install-gcc.sh
    build:
      number: {{ build_num }}
      detect_binary_files_with_prefix: false
      missing_dso_whitelist:
        - '*'
      ignore_run_exports_from:
        - {{ cross_target_stdlib }}_{{ cross_target_platform }}
    requirements:
      build:
      host:
        - {{ cross_target_stdlib }}_{{ cross_target_platform }} {{ cross_target_stdlib_version }}  # [cross_target_stdlib != "None"]
        - {{ pin_subpackage("libgomp", exact=True) }}               # [target_platform == cross_target_platform and cross_target_platform not in ("osx-64", "osx-arm64")]
        - {{ pin_subpackage("libstdcxx", exact=True) }}             # [target_platform == cross_target_platform and cross_target_platform not in ("osx-64", "osx-arm64")]
        - {{ pin_subpackage("libgcc", exact=True) }}                # [target_platform == cross_target_platform]
        - {{ pin_subpackage("libgfortran" ~ libgfortran_soname) }}  # [target_platform == cross_target_platform]
        - {{ pin_subpackage("libsanitizer", exact=True) }}          # [target_platform == cross_target_platform and not (win or osx)]
        - {{ pin_subpackage("libgcc-devel_" ~ cross_target_platform, exact=True) }}
      run:
        {% if cross_target_platform.startswith("osx-") %}
        # For the linker
        - ld64_{{ cross_target_platform }}
        # For the assembler which calls the integrated assembler in clang
        - cctools_{{ cross_target_platform }}
        - clang
        {% else %}
        - binutils_impl_{{ cross_target_platform }} >={{ binutils_version }}
        - {{ cross_target_stdlib }}_{{ cross_target_platform }}    # [cross_target_stdlib != "None"]
        # libstdcxx is a runtime dep of gcc because LTO requires it.
        - {{ pin_subpackage("libstdcxx", max_pin=None) }}       # [target_platform == cross_target_platform]
        - {{ pin_subpackage("libgomp", max_pin=None) }}         # [target_platform == cross_target_platform]
        - {{ pin_subpackage("libstdcxx-devel_" ~ cross_target_platform, exact=True) }}
        {% endif %}
        - {{ pin_subpackage("libgcc-devel_" ~ cross_target_platform, exact=True) }}
        - {{ pin_subpackage("libsanitizer", exact=True) }}      # [target_platform == cross_target_platform and not (win or osx)]
        - {{ pin_subpackage("libgcc", max_pin=None) }}          # [target_platform == cross_target_platform]
    run_exports:
      # impose this requirement across the build/host boundary
      strong:
        - libgcc >={{ gcc_version }}
    test:
      requires:
        - {{ cross_target_stdlib }}_{{ cross_target_platform }} {{ cross_target_stdlib_version }}  # [cross_target_stdlib != "None"]
      files:
        - tests
      commands:
        - echo {{ c_stdlib }}
        - echo {{ c_stdlib_version }}
        - test -f ${PREFIX}/lib/gcc/{{ TARGET }}/{{ gcc_version }}/libgomp.so   # [target_platform == cross_target_platform and cross_target_platform not in ("osx-64", "osx-arm64")]
        - test `readlink ${PREFIX}/lib/gcc/{{ TARGET }}/{{ gcc_version }}/libgomp.so` == "../../../libgomp.so"  # [target_platform == cross_target_platform and cross_target_platform not in ("osx-64", "osx-arm64")]
        - test -f ${PREFIX}/bin/{{ TARGET }}-gcc
        - test -f ${PREFIX}/bin/{{ TARGET }}-cpp
        - test ! -f ${PREFIX}/bin/gcc   # [cross_target_platform not in ("osx-64", "osx-arm64")]
        - test ! -f ${PREFIX}/bin/cpp   # [cross_target_platform not in ("osx-64", "osx-arm64")]
        - CC=$(${PREFIX}/bin/*-gcc -dumpmachine)-gcc
        - ${CC} -Wall tests/aligned_alloc.c -c -o c_aligned.o -v -Wl,-v -march=native  # [target_platform == cross_target_platform and (x86_64 or s390x)]
        # This test does not work well with QEMU
        - ${CC} -Wall tests/aligned_alloc.c -c -o c_aligned.o -v -Wl,-v -mcpu=native   # [target_platform == cross_target_platform and (not x86_64 and not s390x and not ppc64le)]
        - ${CC} -Wall tests/aligned_alloc.c -c -o c_aligned.o -v -fsanitize=address  # [cross_target_platform != "win-64"]
        - ${CC} -Wall tests/aligned_alloc.c -c -o c_aligned.o -v                     # [cross_target_platform != "win-64"]
        - ${CC} -Wall c_aligned.o -o c_aligned -v && ./c_aligned   # [cross_target_platform == target_platform and cross_target_platform != "win-64"]
        - ${CC} -Wall c_aligned.o -o c_aligned -Wl,-rpath,/foo && {{ TARGET }}-readelf -d c_aligned | grep RPATH | grep "/foo:${PREFIX}/lib"  # [cross_target_platform == target_platform and linux]
        - ${CC} -Wall tests/hello_world.c -c -o hello_world.o -v
        - ${CC} -Wall hello_world.o -o hello_world -v
        {% if cross_target_platform.startswith("linux-") %}
        - test -f ${PREFIX}/lib/gcc/{{ TARGET }}/{{ gcc_version }}/libgcc_s.so
        - test -f ${PREFIX}/lib/gcc/{{ TARGET }}/{{ gcc_version }}/libsanitizer.spec
        - test -f ${PREFIX}/lib/gcc/{{ TARGET }}/{{ gcc_version }}/libasan_preinit.o
        {% elif cross_target_platform.startswith("osx-") %}
        - test -f ${PREFIX}/lib/gcc/{{ TARGET }}/{{ gcc_version }}/libgcc_s.1.1.dylib
        - find ${PREFIX}/{{ TARGET }} || true
        - test ! -d ${PREFIX}/{{ TARGET }}
        {% else %}
        - test -f ${PREFIX}/lib/gcc/{{ TARGET }}/{{ gcc_version }}/libgcc_s.a
        - test -f ${PREFIX}/lib/gcc/{{ TARGET }}/{{ gcc_version }}/libquadmath.a
        - test -f ${PREFIX}/lib/gcc/{{ TARGET }}/{{ gcc_version }}/libgomp.a
        {% endif %}
        - test -f ${PREFIX}/lib/gcc/{{ TARGET }}/{{ gcc_version }}/libgomp.spec
    about:
      summary: GNU C Compiler
      home: https://gcc.gnu.org/
      license: GPL-3.0-only WITH GCC-exception-3.1

  - name: gcc
    script: install-symlinks.sh
    build:
      skip: true  # [target_platform != cross_target_platform]
      track_features:            # [not with_conda_specs]
        - gcc_no_conda_specs     # [not with_conda_specs]
    requirements:
      host:
        - {{ pin_subpackage("gcc_impl_" ~ cross_target_platform, exact=True) }}
        - binutils_impl_{{ cross_target_platform }} {{ binutils_version }}.* bootstrap_*   # [cross_target_platform not in ("osx-arm64", "osx-64")]
      run:
        - {{ pin_subpackage("gcc_impl_" ~ cross_target_platform, exact=True) }}
        - conda-gcc-specs        # [with_conda_specs]
    test:
      commands:
        - echo "int main (void) {return 0;}" > conftest.c
        - ${PREFIX}/bin/gcc -v
        - ${PREFIX}/bin/gcc conftest.c -c -v
        - ${PREFIX}/bin/gcov -v
    about:
      summary: GNU C native compiler (symlinks)
      home: https://github.com/conda-forge/ctng-compilers-feedstock
      license: BSD-3-Clause
      license_file: LICENSE

  - name: gcc-no-conda-specs
    build:
      skip: true   # [with_conda_specs or target_platform != cross_target_platform]
    requirements:
      run:
        - {{ pin_subpackage("gcc", exact=True) }}
      run_constrained:
        - conda-gcc-specs <0.0a0
    test:
      commands:
        - gcc --version

  - name: conda-gcc-specs
    script: install-conda-specs.sh
    build:
      number: {{ build_num }}
      skip: true                # [cross_target_platform != target_platform]
      detect_binary_files_with_prefix: false
    requirements:
      build:
      run:
        - {{ pin_subpackage("gcc_impl_" ~ cross_target_platform, max_pin='x.x.x.x') }}
    test:
      files:
        - tests
      commands:
        - specdir=$PREFIX/lib/gcc/{{ TARGET }}/{{ gcc_version }}
        - test -f $specdir/conda.specs
        - CC=$(${PREFIX}/bin/*-gcc -dumpmachine)-gcc
        - echo | ${CC} -E -v -x c - |& grep '^Reading specs from' | awk '{print $NF}' | xargs readlink -e | awk -v ORS= '{print $1":"}' | grep "${specdir}/specs:${specdir}/conda.specs:"  # [linux]
        - cp tests/libhowdy.h $PREFIX/include/
        - ${CC} -shared -fpic -o $PREFIX/lib/libhowdy.so tests/libhowdy.c
        - ${CC} -o howdy-dso tests/howdy-dso.c -lhowdy
        - ./howdy-dso
        -    grep RPATH   <({{ TARGET }}-readelf -d howdy-dso)    # [linux]
        - "! grep RUNPATH <({{ TARGET }}-readelf -d howdy-dso)"   # [linux]
        - ${CC} -Wl,-enable-new-dtags -o howdy-dso-runpath tests/howdy-dso.c -lhowdy  # [linux]
        - ./howdy-dso-runpath                                                         # [linux]
        - "! grep RPATH   <({{ TARGET }}-readelf -d howdy-dso-runpath)"              # [linux]
        -    grep RUNPATH <({{ TARGET }}-readelf -d howdy-dso-runpath)               # [linux]
        - echo | ${CC} -E -Wp,-v -x c - |& awk '/include <\.\.\.> search starts/,/^End of search/ {print}' | tail -n2 | head -n1 | grep "$PREFIX/include"  # [linux]
        - echo | ${CC} -isystem "$PREFIX/include" -E -Wp,-v -x c - |& awk '/include <\.\.\.> search starts/, /^End of search/ {print}' | head -n2 | tail -n1 | grep "$PREFIX/include"  # [linux]

    about:
      summary: conda-specific specfile for GNU C/C++ Compiler
      description: |
          When installed, this optional package provides a specfile that
          directs gcc (and g++ or gfortran) to automatically:
            * search for includes in $PREFIX/include
            * link libraries in $PREFIX/lib
            * set RPATH to $PREFIX/lib
            * use RPATH instead of the newer RUNPATH
          This package is intended to aid usability of the compiler
          toolchain as a replacement for system-installed compilers.
          It should not be used in recipes.  Use the 'compiler(<lang>)'
          jinja function as described on
          https://conda-forge.org/docs/maintainer/knowledge_base.html#dep-compilers
      home: https://gcc.gnu.org/
      license: GPL-3.0-only WITH GCC-exception-3.1
      doc_url: https://gcc.gnu.org/onlinedocs/gcc/Spec-Files.html

  - name: gxx_impl_{{ cross_target_platform }}
    script: install-g++.sh
    build:
      number: {{ build_num }}
      detect_binary_files_with_prefix: false
      missing_dso_whitelist:
        - "*"
      ignore_run_exports_from:
        - {{ cross_target_stdlib }}_{{ cross_target_platform }}
    requirements:
      build:
      host:
        - {{ cross_target_stdlib }}_{{ cross_target_platform }} {{ cross_target_stdlib_version }}  # [cross_target_stdlib != "None"]
        # For cpp and crt{i,n}.o
        - {{ pin_subpackage("gcc_impl_" ~ cross_target_platform, exact=True) }}
        - binutils_impl_{{ cross_target_platform }} {{ binutils_version }}.* bootstrap_*   # [cross_target_platform not in ("osx-arm64", "osx-64")]
        - {{ pin_subpackage("libstdcxx-devel_" ~ cross_target_platform, exact=True) }}   # [cross_target_cxx_stdlib == "libstdcxx"]
      run:
        # For cpp and crt{i,n}.o
        - {{ pin_subpackage("gcc_impl_" ~ cross_target_platform, exact=True) }}
        # not needed due to pinning above but marks this build as using the new sysroots
        - {{ cross_target_stdlib }}_{{ cross_target_platform }}                          # [cross_target_stdlib != "None"]
        - {{ pin_subpackage("libstdcxx-devel_" ~ cross_target_platform, exact=True) }}   # [cross_target_cxx_stdlib == "libstdcxx"]
        - libcxx-devel      # [cross_target_cxx_stdlib == "libcxx" and target_platform == cross_target_platform]
        - libcxx-headers    # [cross_target_cxx_stdlib == "libcxx" and target_platform != cross_target_platform]
        # for C++20 chrono support
        - tzdata
    run_exports:
      # impose this requirement across the build/host boundary
      strong:
        - libstdcxx >={{ gcc_version }}                             # [cross_target_cxx_stdlib == "libstdcxx"]
        - libcxx                                                    # [cross_target_cxx_stdlib == "libcxx"]
    test:
      requires:
        - {{ cross_target_stdlib }}_{{ cross_target_platform }} {{ cross_target_stdlib_version }}  # [cross_target_stdlib != "None"]
      files:
        - tests
      commands:
        - test -f ${PREFIX}/bin/{{ TARGET }}-g++
        - CXX=$(${PREFIX}/bin/*-gcc -dumpmachine)-g++
        - ${CXX} -Wall tests/aligned_alloc.cpp -c -o cpp_aligned.o --std=c++17    # [cross_target_platform != "win-64"]
        - ${CXX} -Wall cpp_aligned.o -o cpp_aligned --std=c++17 && ./cpp_aligned  # [cross_target_platform == target_platform and cross_target_platform != "win-64"]
        - ${CXX} -Wall tests/hello_world.cpp -c -o hello_world.o --std=c++17 -v
        - ${CXX} -Wall hello_world.o -o hello_world --std=c++17 -v

        # test tzdb integration
      {% if (gcc_maj_ver|int >= 14) and cross_target_cxx_stdlib == "libstdcxx" %}
        - ${CXX} -isystem ${PREFIX}/include --std=c++20 -Wall tests/tzdb-override.cpp -c -o tzdb-override.o -v
        - ${CXX} -isystem ${PREFIX}/include --std=c++20 -Wall tests/tzdb.cpp -c -o tzdb.o -v
        {% if target_platform == cross_target_platform %}
        - ${CXX} tzdb-override.o -o tzdb-override -v && ./tzdb-override
        - ${CXX} tzdb.o -o tzdb -v && ./tzdb
        - strace ./tzdb 2>&1 | grep ${CONDA_PREFIX}/lib/../share/zoneinfo/tzdata.zi
        # also test without any environment activation
        - unset PREFIX
        - unset CONDA_PREFIX
        - ./tzdb
        {% endif %}
      {% endif %}
    about:
      summary: GNU C++ Compiler
      home: https://gcc.gnu.org/
      license: GPL-3.0-only WITH GCC-exception-3.1

  - name: gxx
    script: install-symlinks.sh
    build:
      skip: true  # [target_platform != cross_target_platform]
    requirements:
      host:
        - {{ pin_subpackage("gxx_impl_" ~ cross_target_platform, exact=True) }}
        - binutils_impl_{{ cross_target_platform }} {{ binutils_version }}.* bootstrap_*   # [cross_target_platform not in ("osx-arm64", "osx-64")]
        - {{ pin_subpackage("gcc", exact=True) }}
      run:
        - {{ pin_subpackage("gxx_impl_" ~ cross_target_platform, exact=True) }}
        - {{ pin_subpackage("gcc", exact=True) }}
    test:
      commands:
        - ${PREFIX}/bin/g++ -v
        - ${PREFIX}/bin/gcc -v
    about:
      summary: GNU C++ native compiler (symlinks)
      home: https://github.com/conda-forge/ctng-compilers-feedstock
      license: BSD-3-Clause
      license_file: LICENSE

  - name: gfortran_impl_{{ cross_target_platform }}
    script: install-gfortran.sh
    build:
      number: {{ build_num }}
      detect_binary_files_with_prefix: false
      missing_dso_whitelist:
        - "*"
      ignore_run_exports_from:
        - {{ cross_target_stdlib }}_{{ cross_target_platform }}
    requirements:
      build:
      host:
        # For cpp and crt{i,n}.o
        - {{ pin_subpackage("gcc_impl_" ~ cross_target_platform, exact=True) }}
        - binutils_impl_{{ cross_target_platform }} {{ binutils_version }}.* bootstrap_*   # [cross_target_platform not in ("osx-arm64", "osx-64")]
        # not needed due to pinning above but marks this build as using the new sysroots
        - {{ cross_target_stdlib }}_{{ cross_target_platform }} {{ cross_target_stdlib_version }}  # [cross_target_stdlib != "None"]
      run:
        # For cpp and crt{i,n}.o
        - gcc_impl_{{ cross_target_platform }} >={{ gcc_version }}
        - {{ pin_subpackage("libgfortran" ~ libgfortran_soname, max_pin=None) }}  # [target_platform == cross_target_platform]
        - {{ pin_subpackage("libgcc", max_pin=None) }}                            # [target_platform == cross_target_platform]
        - {{ pin_subpackage("libstdcxx", max_pin=None) }}                         # [target_platform == cross_target_platform and cross_target_cxx_stdlib == "libstdcxx"]
        - {{ cross_target_stdlib }}_{{ cross_target_platform }}                   # [cross_target_stdlib != "None"]
    run_exports:
      # impose this requirement across the build/host boundary
      strong:
        - libgfortran{{ libgfortran_soname }} {{ gcc_version }}.*
        - libgcc >={{ gcc_version }}
    test:
      requires:
        - cmake >=3.11,<4  # [x86_64 or aarch64 or ppc64le]
        - {{ cross_target_stdlib }}_{{ cross_target_platform }} {{ cross_target_stdlib_version }}  # [cross_target_stdlib != "None"]
      commands:
        {% if cross_target_platform.startswith("linux-") %}
        - test -f ${PREFIX}/lib/gcc/{{ TARGET }}/{{ gcc_version }}/libgfortran.so
        {% elif cross_target_platform.startswith("osx-") %}
        - test -f ${PREFIX}/lib/gcc/{{ TARGET }}/{{ gcc_version }}/libgfortran.dylib
        {% else %}
        - test -f ${PREFIX}/lib/gcc/{{ TARGET }}/{{ gcc_version }}/libgfortran.dll.a
        {% endif %}
        - test -f ${PREFIX}/lib/gcc/{{ TARGET }}/{{ gcc_version }}/libgfortran.a
        - test -f ${PREFIX}/bin/{{ TARGET }}-gfortran
        - find $PREFIX/lib -iname omp_lib.mod | grep '.'
        - find $PREFIX/lib -iname omp_lib.h | grep '.'
        - find $PREFIX/lib -iname ISO_Fortran_binding.h | grep '.'
        - echo {{ gcc_maj_ver }}
        - 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
      home: https://gcc.gnu.org/
      license: GPL-3.0-only WITH GCC-exception-3.1

  - name: gfortran
    script: install-symlinks.sh
    build:
      skip: true  # [target_platform != cross_target_platform]
    requirements:
      host:
        - {{ pin_subpackage("gfortran_impl_" ~ cross_target_platform, exact=True) }}
        - {{ pin_subpackage("gcc_impl_" ~ cross_target_platform, exact=True) }}
        - binutils_impl_{{ cross_target_platform }} {{ binutils_version }}.* bootstrap_*   # [cross_target_platform not in ("osx-arm64", "osx-64")]
        - {{ pin_subpackage("gcc", exact=True) }}
      run:
        - {{ pin_subpackage("gfortran_impl_" ~ cross_target_platform, exact=True) }}
        - {{ pin_subpackage("gcc_impl_" ~ cross_target_platform, exact=True) }}
        - {{ pin_subpackage("gcc", exact=True) }}
    test:
      commands:
        - ${PREFIX}/bin/gfortran -v
        - ${PREFIX}/bin/gcc -v
    about:
      summary: GNU Fortran native compiler (symlinks)
      home: https://github.com/conda-forge/ctng-compilers-feedstock
      license: BSD-3-Clause
      license_file: LICENSE

  - name: libstdcxx
    target: {{ cross_target_platform }}
    script: install-libstdc++.sh
    build:
      number: {{ build_num }}
      skip: true                # [target_platform != cross_target_platform or cross_target_cxx_stdlib != "libstdcxx"]
      detect_binary_files_with_prefix: false
      missing_dso_whitelist:
        - "*"
    requirements:
      build:
      host:
        - {{ cross_target_stdlib }}_{{ cross_target_platform }} {{ cross_target_stdlib_version }}  # [cross_target_stdlib != "None"]
        - {{ pin_subpackage("libgcc", exact=True) }}
      run:
        - {{ pin_subpackage("libgcc", exact=True) }}
      run_constrained:
        # avoid installing incompatible version of old name for this output
        - libstdcxx-ng =={{ version }}=*_{{ build_num }}
    test:
      commands:
        - test -f ${PREFIX}/lib/libstdc++.so
    about:
      summary: The GNU C++ Runtime Library
      home: https://gcc.gnu.org/
      license: GPL-3.0-only WITH GCC-exception-3.1

  - name: libsanitizer
    target: {{ cross_target_platform }}
    script: install-libsanitizer.sh
    build:
      number: {{ build_num }}
      skip: true                # [target_platform != cross_target_platform or win or osx]
      detect_binary_files_with_prefix: false
      missing_dso_whitelist:
        - "*"
      run_exports:
        - libsanitizer {{ gcc_version }}
    requirements:
      build:
      host:
        - {{ cross_target_stdlib }}_{{ cross_target_platform }}  {{ cross_target_stdlib_version }}  # [cross_target_stdlib != "None"]
      run:
        - libgcc >={{ gcc_version }}
        - libstdcxx >={{ gcc_version }}
    test:
      requires:
        - {{ cross_target_stdlib }}_{{ cross_target_platform }}  # [cross_target_stdlib != "None"]
        - gcc_impl_{{ cross_target_platform }}
        - binutils_impl_{{ cross_target_platform }} {{ binutils_version }}.* bootstrap_*   # [cross_target_platform not in ("osx-arm64", "osx-64")]
      commands:
        - test -f ${PREFIX}/lib/libasan.so
        - test -f ${PREFIX}/lib/libhwasan.so   # [x86_64 or aarch64]
        - file ${PREFIX}/bin/{{ TARGET }}-gcc
        - echo 'void main(){}' | {{ TARGET }}-gcc -fsanitize=address -Wl,--fatal-warnings -x c -
    about:
      summary: The GCC runtime libraries for sanitizers
      home: https://gcc.gnu.org/onlinedocs/gccint/Libgcc.html
      license: GPL-3.0-only WITH GCC-exception-3.1

  - name: libgomp
    target: {{ cross_target_platform }}
    script: install-libgomp.sh
    build:
      number: {{ build_num }}
      skip: true                # [target_platform != cross_target_platform or osx]
      detect_binary_files_with_prefix: false
      missing_dso_whitelist:
        - "*"
      run_exports:
        strong:
          - {{ pin_subpackage("_openmp_mutex", max_pin=None) }}
          {% if cross_target_platform.startswith("win-") %}
          - {{ pin_subpackage("libgomp", max_pin=None) }}
          {% endif %}
    requirements:
      build:
      host:
        - {{ cross_target_stdlib }}_{{ cross_target_platform }}  {{ cross_target_stdlib_version }}  # [cross_target_stdlib != "None"]
      run_constrained:
        - msys2-conda-epoch <0.0a0    # [win]
    test:
      requires:
        - {{ cross_target_stdlib }}_{{ cross_target_platform }}  # [cross_target_stdlib != "None"]
      commands:
        - test -f ${PREFIX}/lib/libgomp.so.{{ libgomp_ver }}
        - test ! -f ${PREFIX}/lib/libgomp.so.{{ libgomp_ver[0:1] }}
    about:
      summary: The GCC OpenMP implementation.
      home: https://gcc.gnu.org/onlinedocs/gccint/Libgcc.html
      license: GPL-3.0-only WITH GCC-exception-3.1

  - name: libgcc
    target: {{ cross_target_platform }}
    script: install-libgcc-no-gomp.sh
    build:
      number: {{ build_num }}
      skip: true                # [target_platform != cross_target_platform]
      detect_binary_files_with_prefix: false
      missing_dso_whitelist:
        - "*"
      rpaths_patcher: patchelf
    requirements:
      build:
      host:
        - {{ pin_subpackage("libgomp", exact=True) }}                                               # [not osx]
        - {{ pin_subpackage('_openmp_mutex', exact=True) }}                                         # [not osx]
        - {{ cross_target_stdlib }}_{{ cross_target_platform }}  {{ cross_target_stdlib_version }}  # [cross_target_stdlib != "None"]
      run:
        - {{ pin_subpackage("_openmp_mutex", max_pin=None) }}                                       # [not osx]
        - _openmp_mutex                                                                             # [osx]
      run_constrained:
        - {{ pin_subpackage("libgomp", exact=True) }}
        - msys2-conda-epoch <0.0a0    # [win]
        # avoid installing incompatible version of old name for this output
        - libgcc-ng =={{ version }}=*_{{ build_num }}
    test:
      requires:
        - {{ cross_target_stdlib }}_{{ cross_target_platform }}  # [cross_target_stdlib != "None"]
      commands:
        - test -f ${PREFIX}/lib/libgcc_s.so                                                         # [linux]
        - test -f ${PREFIX}/lib/libgcc_s.1.dylib                                                    # [osx]
        - test -f ${PREFIX}/lib/libgomp.so.{{ libgomp_ver[0:1] }}                                   # [linux]
        - test `readlink ${PREFIX}/lib/libgomp.so.{{ libgomp_ver[0:1] }}` == "libgomp.so.{{ libgomp_ver }}"   # [linux]
        - test `readlink ${PREFIX}/lib/libgomp.so.{{ libgomp_ver[0:1] }}` == "libgomp.so.{{ libgomp_ver }}"   # [linux]
    about:
      summary: The GCC low-level runtime library
      home: https://gcc.gnu.org/onlinedocs/gccint/Libgcc.html
      license: GPL-3.0-only WITH GCC-exception-3.1

  - name: _openmp_mutex
    script: install-openmp_impl.sh
    version: {{ openmp_ver }}
    build:
      # have higher priority than llvm
      string: 20_gnu
      number: 20
      skip: true    # [target_platform != cross_target_platform or osx]
      run_exports:
        strong:
          - {{ pin_subpackage("_openmp_mutex", max_pin=None) }}
    requirements:
      build:
      host:
        - {{ pin_subpackage('libgomp', exact=True) }}
        - {{ cross_target_stdlib }}_{{ cross_target_platform }}  {{ cross_target_stdlib_version }}  # [cross_target_stdlib != "None"]
      run:
        - libgomp >=7.5.0
      run_constrained:
        # conflict with previous name
        - openmp_impl <0.0a0
        - msys2-conda-epoch <0.0a0    # [win]
    test:
      commands:
        - test -f ${PREFIX}/lib/libgomp.so.{{ libgomp_ver[0:1] }}
        - test `readlink ${PREFIX}/lib/libgomp.so.{{ libgomp_ver[0:1] }}` == "libgomp.so.{{ libgomp_ver }}"
    about:
      summary: OpenMP Implementation Mutex
      license: BSD-3-Clause
      license_file: LICENSE
      home: https://github.com/conda-forge/ctng-compilers-feedstock

  - name: libgfortran{{ libgfortran_soname }}
    target: {{ cross_target_platform }}
    script: install-libgfortran.sh
    build:
      number: {{ build_num }}
      skip: true                # [target_platform != cross_target_platform]
      detect_binary_files_with_prefix: false
      missing_dso_whitelist:
        - "*"
    requirements:
      build:
      host:
        - {{ cross_target_stdlib }}_{{ cross_target_platform }} {{ cross_target_stdlib_version }}  # [cross_target_stdlib != "None"]
      run:
        - libgcc >={{ gcc_version }}
      run_constrained:
        - libgfortran {{ gcc_version }}
    test:
      commands:
        - test -f ${PREFIX}/lib/libgfortran${SHLIB_EXT}
    about:
      summary: The GNU Fortran Runtime Library
      home: https://gcc.gnu.org/
      license: GPL-3.0-only WITH GCC-exception-3.1

  - name: libgfortran
    target: {{ cross_target_platform }}
    build:
      skip: true   # [target_platform != cross_target_platform]
      number: {{ build_num }}
    requirements:
      run:
        - {{ pin_subpackage('libgfortran' ~ libgfortran_soname, exact=True) }}
      run_constrained:
        # avoid installing incompatible version of old name for this output
        - libgfortran-ng =={{ version }}=*_{{ build_num }}
    test:
      commands:
        - test -f ${PREFIX}/lib/libgfortran${SHLIB_EXT}
    about:
      summary: The GNU Fortran Runtime Library
      home: https://gcc.gnu.org/
      license: GPL-3.0-only WITH GCC-exception-3.1

  # compatibility outputs for previous naming of the runtime libraries
  # with "-ng" suffix; for windows the suffix had never been introduced
  {% if cross_target_platform.startswith("linux-") %}
  - name: libgcc-ng
    target: {{ cross_target_platform }}
    build:
      number: {{ build_num }}
      skip: true    # [target_platform != cross_target_platform]
      run_exports:
        strong:
          - libgcc
    requirements:
      host:
        - {{ pin_subpackage("libgcc", exact=True) }}
      run:
        - {{ pin_subpackage("libgcc", exact=True) }}
    test:
      commands:
        - echo "empty wrapper for compatibility with previous naming"
    about:
      summary: The GCC low-level runtime library
      home: https://gcc.gnu.org/onlinedocs/gccint/Libgcc.html
      license: GPL-3.0-only WITH GCC-exception-3.1

  - name: libstdcxx-ng
    target: {{ cross_target_platform }}
    script: install-libstdc++.sh
    build:
      number: {{ build_num }}
      skip: true   # [target_platform != cross_target_platform or cross_target_cxx_stdlib != "libstdcxx"]
      run_exports:
        strong:
          - libstdcxx
    requirements:
      host:
        - {{ pin_subpackage("libstdcxx", exact=True) }}
      run:
        - {{ pin_subpackage("libstdcxx", exact=True) }}
    test:
      commands:
        - echo "empty wrapper for compatibility with previous naming"
    about:
      summary: The GNU C++ Runtime Library
      home: https://gcc.gnu.org/
      license: GPL-3.0-only WITH GCC-exception-3.1

  - name: libgfortran-ng
    target: {{ cross_target_platform }}
    build:
      number: {{ build_num }}
      skip: true   # [target_platform != cross_target_platform]
      run_exports:
        strong:
          - libgfortran
    requirements:
      host:
        - {{ pin_subpackage('libgfortran', exact=True) }}
      run:
        - {{ pin_subpackage('libgfortran', exact=True) }}
    test:
      commands:
        - echo "empty wrapper for compatibility with previous naming"
    about:
      summary: The GNU Fortran Runtime Library
      home: https://gcc.gnu.org/
      license: GPL-3.0-only WITH GCC-exception-3.1
  {% endif %}

about:
  summary: GNU Compiler Collection
  home: https://gcc.gnu.org/
  license: GPL-3.0-only WITH GCC-exception-3.1
  license_file:
    - COPYING
    - COPYING.LIB
    - COPYING3
    - COPYING3.LIB

extra:
  feedstock-name: ctng-compilers-feedstock
  recipe-maintainers:
    - timsnyder
    - xhochy
    - isuruf
    - beckermr
