# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4

PortSystem          1.0
PortGroup           crossgcc 1.0

# keep in sync with i686-w64-mingw32-gcc
set mingw_name      w64-mingw32
set mingw_arch      x86_64
set mingw_target    ${mingw_arch}-${mingw_name}

crossgcc.setup      ${mingw_target} 15.2.0
crossgcc.languages  {c c++ fortran objc obj-c++}
dist_subdir         gcc[lindex [split ${version} .] 0]
revision            0

maintainers         {@Gcenx gmail.com:gcenx83} openmaintainer

# these are build + runtime dependencies
# (binutils are already added by the portgroup)
depends_lib-append  port:${mingw_target}-headers
# not sure if useful:
#                   port:cloog
#                   port:ppl

# Fixes libgomp compile
patchfiles-append   patch-libgomp-Don-t-hard-code-MS-printf-attributes.diff

configure.args-replace \
                    --enable-multilib \
                    --disable-multilib

configure.args-append \
                    --enable-shared \
                    --with-dwarf \
                    --enable-fully-dynamic-string \
                    --enable-libssp \
                    --enable-lto

# not sure if useful:
#   --with-cloog=${prefix}
#   --with-ppl=${prefix}

# TODO:
#   --enable-version-specific-runtime-libs
#       Installs libgcc/libstdc++ and other target support libraries in such a
#       way that multiple GCC installs can coexist simultaneously.
#       (add once you have the default working as it needs some extra tweaking)

# First bootstrapping phase
subport ${mingw_target}-gcc-bootstrap {
    build.target    all-gcc
    destroot.target install-gcc
}

# Second bootstrapping phase
subport ${mingw_target}-gcc-nothreads {
    # these two are not needed since we are not yet requesting posix threads
    # build.target          all-target-libgcc
    # destroot.target       install-target-libgcc

    depends_lib-append      port:${mingw_target}-crt
    depends_build-append    bin:${mingw_target}-gcc:${mingw_target}-gcc-bootstrap
}

# Final phase
if {${subport} eq ${name}} {
    depends_lib-append      port:${mingw_target}-crt \
                            port:${mingw_target}-winpthreads

    depends_build-append    path:${mingw_target}/lib/libgcc_s.a:${mingw_target}-gcc-nothreads

    configure.args-append   --enable-threads=posix \
                            --enable-libgomp

    # TODO: and add notes how to fix wine in regedit
} else {
    livecheck.type  none
}

if {${subport} ne "${name}-bootstrap"} {
    post-destroot {
        move {*}[glob ${destroot}${prefix}/${mingw_target}/lib/*.dll] \
            ${destroot}${prefix}/${mingw_target}/bin
    }
    pre-activate {
        foreach flavour {gcc-bootstrap gcc-nothreads gcc} {
            set gcc ${mingw_target}-${flavour}
            if {![catch {set installed [lindex [registry_active ${gcc}] 0]}]} {
                registry_deactivate_composite ${gcc} "" [list ports_nodepcheck 1]
            }
        }
    }
}
