# -*- 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       muniversal 1.0

name            gdb
version         16.3
revision        0
categories      devel
license         GPL-3+
maintainers     nomaintainer
description     GDB: The GNU Project Debugger

long_description \
GDB, the GNU Project debugger, allows you to see what is going on 'inside' \
another program while it executes -- or what another program was doing at the \
moment it crashed.  GDB can do four main kinds of things (plus other things \
in support of these) to help you catch bugs in the act: \
    a) start your program, specifying anything that might affect its behavior, \
    b) make your program stop on specified conditions, \
    c) examine what has happened, when your program has stopped, \
    d) change things in your program, so you can experiment with correcting \
       the effects of one bug and go on to learn about another. \
The program being debugged can be written in Ada, C, C++, Objective-C, \
Pascal (and many other languages). Those programs might be executing on \
the same machine as GDB (native) or on another machine (remote). GDB \
can run on most popular UNIX and Microsoft Windows variants.

homepage        https://www.gnu.org/software/gdb/
supported_archs x86_64 i386

master_sites    gnu

checksums       rmd160  8c259c736d846878e82efc69b168ec277f7b5801 \
                sha256  86015081217dc82c5485db9ce864dcc0b703759d2bdbe4441b095c5f900a6a37 \
                size    41599902

# Ventura+ do not have makeinfo in the system
depends_build   bin:makeinfo:texinfo

# these dependencies are listed under depends_lib rather than depends_build
# because gdb will link with libraries they provide if installed.
# there may be more. See variable host_libs in configure.ac.
# guile should also be a dependency, but currently does not build universally. (#48767)

depends_lib     port:boehmgc \
                port:expat \
                port:gmp \
                port:gettext-runtime \
                port:libiconv \
                port:mpfr \
                port:ncurses \
                port:zlib

# Older Apple clang versions segfault, c.f., https://trac.macports.org/ticket/56883
compiler.blacklist      {clang < 601}
compiler.thread_local_storage \
                        yes

# Needs C++11; halfway redundant due to the blacklist above, but make sure selected
# compiler supports the standard - getting rid of old Apple GCC versions and the like?
compiler.cxx_standard   2011

configure.dir ${workpath}/build
configure.cmd ${worksrcpath}/configure
configure.args \
    --with-docdir=${prefix}/share/doc \
    --with-system-zlib \
    --without-guile \
    --without-python \
    --program-prefix=g \
    --disable-werror

platform darwin arm {
    # build an x86_64 binary as gdb arm binaries are not yet supported.
    configure.args-append --build=aarch64-apple-${os.platform}${os.major} \
                          --host=x86_64-apple-${os.platform}${os.major}

    # prevent make[2]: x86_64-apple-darwin22-ar: No such file or directory
    configure.env-append  AR=ar
}

pre-configure {
    file mkdir ${configure.dir}
}

build.dir ${configure.dir}

post-destroot {
    if {${os.platform} eq "darwin" && ${os.major} < 12} {
        system "chgrp procmod ${destroot}${prefix}/bin/ggdb*"
        system "chmod g+s ${destroot}${prefix}/bin/ggdb*"
    }

    foreach info [glob -tails -directory ${destroot}${prefix}/share/info g*] {
        move ${destroot}${prefix}/share/info/${info} ${destroot}${prefix}/share/info/g${info}
    }

    xinstall -m 755 -d ${destroot}${prefix}/libexec/gnubin
    foreach binary [glob -tails -directory ${destroot}${prefix}/bin g*] {
        ln -s ${prefix}/bin/${binary} ${destroot}${prefix}/libexec/gnubin/[string range $binary 1 end]
    }
}

set pythons_suffixes {27 310 311 312 313 314}

set pythons_ports {}
foreach s ${pythons_suffixes} {
    lappend pythons_ports python${s}
}

foreach s ${pythons_suffixes} {
    set p python${s}
    set v [string index ${s} 0].[string range ${s} 1 end]
    set i [lsearch -exact ${pythons_ports} ${p}]
    set c [lreplace ${pythons_ports} ${i} ${i}]
    variant ${p} description "Build GDB with Python ${v} Scripting" conflicts {*}${c} "
        depends_lib-append      port:${p}
        configure.args-delete   --without-python
        configure.args-append   --with-python=${prefix}/bin/python${v}
        patchfiles-append       patch-python-config.py.diff
    "
}

variant multiarch description {Support all target architectures} {
    patchfiles-append        patch-elf-bfd.h.diff
    configure.args-append    --enable-targets=all
}

if {${os.platform} eq "darwin" && ${os.major} >= 12} {
    notes "
You will need to codesign ${prefix}/bin/ggdb

See https://sourceware.org/gdb/wiki/BuildingOnDarwin#Giving_gdb_permission_to_control_other_processes
for more information.
"
}


livecheck.type  regex
livecheck.url   https://ftp.gnu.org/gnu/gdb/
livecheck.regex gdb-(\\d+(?:\\.\\d+)+)\\.tar
