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

name                lazarus
version             4.8-0
categories          devel pascal
license             GPL-2 LGPL-2
maintainers         {@kamischi web.de:karl-michael.schindler} openmaintainer
homepage            https://www.lazarus-ide.org
master_sites        sourceforge:lazarus

checksums           ${name}-${version}.tar.gz \
                    rmd160  98df7ee74a518b85641f7d6f240b85d03762873f \
                    sha256  6b4c9ec94fe79fe4e58027dd7bf10d9b6c35c9cb2f91db5930b7580b4a201a99 \
                    size    92434220

subport qt5pas {
    revision        0
    PortGroup       qmake5 1.0

    description     Pascal wrapper for Qt5

    long_description \
                    The Free Pascal Qt5 binding allows Free Pascal to \
                    interface with the C++ Library Qt. \
                    \
                    This binding may not cover the whole Qt5 framework but \
                    only the classes needed by the Cross Platform Lazarus \
                    IDE to use Qt as a Widget set.

    homepage        https://wiki.freepascal.org/Qt5_Interface
    dist_subdir     lazarus
    worksrcdir      lazarus/lcl/interfaces/qt5/cbindings

    # Workaround xcrun: error: SDK "macosx12" cannot be located
    configure.sdk_version

    use_xcode      yes

    post-build { system -W ${worksrcpath} "install_name_tool \
        -id ${prefix}/libexec/qt5/lib/Qt5Pas.framework/Versions/1/Qt5Pas \
        Qt5Pas.framework/Qt5Pas"
    }
    post-destroot {
        xinstall -m 755 -d ${destroot}${prefix}/share/doc/qt5pas
        xinstall -m 644 {*}[glob ${worksrcpath}/*.TXT] ${destroot}${prefix}/share/doc/qt5pas
    }
}

subport qt6pas {
    revision        0
    PortGroup       qt6 1.0

    description     Pascal wrapper for Qt6

    long_description \
                    The Free Pascal Qt6 binding allows Free Pascal to \
                    interface with the C++ Library Qt. \
                    \
                    This binding may not cover the whole Qt6 framework but \
                    only the classes needed by the Cross Platform Lazarus \
                    IDE to use Qt as a Widget set.

    homepage        https://wiki.freepascal.org/Qt6_Interface
    worksrcdir      lazarus/lcl/interfaces/qt6/cbindings

    # Workaround xcrun: error: SDK "macosx12" cannot be located
    configure.sdk_version

    configure.pre_args-replace      --prefix=${prefix} "PREFIX=${prefix}"
    configure.universal_args-delete --disable-dependency-tracking
    configure.cmd  ${qt6.dir}/bin/qmake6

    destroot.destdir "INSTALL_ROOT=${destroot}"

    use_xcode      yes

    post-build { system -W ${worksrcpath} "install_name_tool \
        -id ${prefix}/libexec/qt6/lib/Qt6Pas.framework/Versions/6/Qt6Pas \
        Qt6Pas.framework/Qt6Pas"
    }
    post-destroot {
        xinstall -m 755 -d ${destroot}${prefix}/share/doc/qt6pas
        xinstall -m 644 {*}[glob ${worksrcpath}/*.TXT] ${destroot}${prefix}/share/doc/qt6pas
    }
}

if {${subport} eq "${name}"} {
    revision            0
    description         Free Pascal IDE.
    long_description \
        Lazarus is an open-source development system that builds \
        on the Free Pascal compiler by adding an integrated \
        development environment (IDE).  It includes a \
        syntax-highlighting code editor and a visual form \
        designer, as well as a component library that is highly \
        compatible with Delphi's Visual Component Library \
        (VCL).  The Lazarus Component Library (LCL) includes \
        equivalents for many of the familiar VCL controls such \
        as forms, buttons, text boxes and so on, which are used \
        to create applications with a graphical user interface \
        (GUI).

    depends_lib         port:fpc port:fpc-sources
    supported_archs     ppc x86_64 arm64

    use_configure       no
    worksrcdir          lazarus

    patchfiles          noGUI-Printer.patch

    post-patch {
    # add the MacPorts directory tree. This might need a check in a new version

        set patchtarget ${worksrcpath}/ide/packages/ideconfig/include/unix/lazbaseconf.inc

        reinplace "s|1\.\.16|1\.\.17|g" ${patchtarget}
        reinplace "s|'/usr/share/fpcsrc'|'/usr/share/fpcsrc',\\
        '${prefix}/share/fpcsrc'|g"      ${patchtarget}
        reinplace "s|1\.\.8|1\.\.9|g" ${patchtarget}
        reinplace "s|'/usr/share/lazarus',|'/usr/share/lazarus',\\
        '${prefix}/share/lazarus',|g" ${patchtarget}

    # change the preferences directory (actually, not yet)
    #   reinplace "s|'\.lazarus'|'Library/Preferences/lazarus'|g" ${patchtarget}
        reinplace "s|'/etc/lazarus'|'${prefix}/etc/lazarus'|g"    ${patchtarget}

    # adjust the default paths to MacPorts paths

        set patchtarget ${worksrcpath}/tools/install/macosx/environmentoptions.xml

        switch ${build_arch} {
            ppc {
                set defaultCompiler ppcppc
            }
            x86_64 {
                set defaultCompiler ppcx64
            }
            arm64 {
                set defaultCompiler ppca64
            }
            default {
                set defaultCompiler unsupported
            }
        }

        reinplace "s|/usr/local/bin/_PPCARCH_|${prefix}/bin/${defaultCompiler}|g" ${patchtarget}
        reinplace "s|/usr/local/share|${prefix}/share|g"              ${patchtarget}
        reinplace "s|/Developer/lazarus|${prefix}/share/lazarus|g"    ${patchtarget}

        reinplace "s|<DebuggerFilename Value=\"/usr/bin/lldb\"/>|<DebuggerFilename Value=\"/usr/bin/lldb\">\\
            <History Count=\"3\">\\
              <Item1 Value=\"/usr/bin/lldb\"/>\\
              <Item2 Value=\"/usr/bin/gdb\"/>\\
              <Item3 Value=\"${prefix}/bin/gdb\"/>\\
            </History>\\
          </DebuggerFilename>|g" ${patchtarget}

    # some more fixes for nogui taking cocoa as a template
    # if cocoa or nogui get updated, the files in ${filespath} need to be updated manually

        copy ${filespath}/nogui ${worksrcpath}/components/lclextensions/include/

        copy ${worksrcpath}/components/virtualtreeview/units/cocoa         ${worksrcpath}/components/virtualtreeview/units/nogui
        copy ${worksrcpath}/components/virtualtreeview/include/intf/cocoa/ ${worksrcpath}/components/virtualtreeview/include/intf/nogui

        if {[variant_isset gtk2]} {
            reinplace "s|/sw/lib;/sw/lib/pango-ft219|${prefix}|g" ${worksrcpath}/lcl/interfaces/lcl.lpk
            reinplace "s|/sw/lib|${prefix}/lib|g"                 ${worksrcpath}/lcl/interfaces/lcl.lpk
        }

    # macOS 10.13 (High Sierra) and lower has no separate framework UserNotifications
        if {${os.major} <= 17} {
            reinplace "s| -k-weak_framework -kUserNotifications||g" ide/Makefile
            reinplace "s| -k-weak_framework -kUserNotifications||g" ide/Makefile.fpc
            reinplace "s| -k-weak_framework -kUserNotifications||g" components/chmhelp/lhelp/Makefile
            reinplace "s| -k-weak_framework -kUserNotifications||g" components/chmhelp/lhelp/Makefile.fpc
            reinplace "s| -weak_framework UserNotifications||g" lcl/interfaces/lcl.lpk
        }

    # ld segfaults on macOS 15.4 and later, fall back to classic linker
    # See https://gitlab.com/freepascal.org/lazarus/lazarus/-/issues/41570
        if {${os.major} >= 24} {
            reinplace "s|_OPT+=-k-|_OPT+=-k-ld_classic -k-|" \
                ide/Makefile ide/Makefile.fpc components/chmhelp/lhelp/Makefile
        }
    }

    build.target       bigide

    use_parallel_build no

    variant cocoa conflicts gtk2 qt4 qt5 qt6 description "Build with Cocoa backend" {
        build.post_args    OPT=-gl LCL_PLATFORM=cocoa
    }

    variant gtk2 conflicts cocoa qt4 qt5 qt6 description "Build with GTK backend" {
        depends_lib-append path:lib/pkgconfig/gtk+-2.0.pc:gtk2
        build.post_args    OPT="-gl -Fl${prefix}/lib -dHASX" LCL_PLATFORM=gtk2
    }

    variant qt4 conflicts cocoa gtk2 qt5 qt6 description "Build with Qt4 backend" {
        PortGroup          qt4 1.0

        depends_lib-append port:qt4pas
        build.post_args    OPT="-gl -Ff${prefix}/libexec/qt4/lib" LCL_PLATFORM=qt
        notes-append       "Add -Ff${prefix}/libexec/qt4/lib to project options"
    }

    variant qt5 conflicts cocoa gtk2 qt4 qt6 description "Build with Qt5 backend" {
        PortGroup          qt5 1.0

        depends_lib-append port:qt5pas
        build.post_args    OPT="-gl -Ff${prefix}/libexec/qt5/lib" LCL_PLATFORM=qt5
        notes-append       "Add -Ff${prefix}/libexec/qt5/lib to project options"
    }

    variant qt6 conflicts cocoa gtk2 qt4 qt5 description "Build with Qt6 backend" {
        depends_lib-append port:qt6pas
        build.post_args    OPT=-"gl -Ff${prefix}/libexec/qt6/lib" LCL_PLATFORM=qt6
        notes-append       "Add -Ff${prefix}/libexec/qt6/lib to project options"
    }

    # It obviously makes no sense to use cocoa outside of macOS.
    # On powerpc it is broken, as well as carbon:
    # https://trac.macports.org/ticket/70815
    # FIXME: gtk and qt backends build, but on old MacOS seem to work
    # only in command-line: https://trac.macports.org/ticket/70817
    # https://gitlab.com/freepascal.org/lazarus/lazarus/-/issues/41153
    if {![variant_isset cocoa] && ![variant_isset gtk2] && ![variant_isset qt4] && ![variant_isset qt5] && ![variant_isset qt6]} {
        if {${os.platform} eq "darwin" && ${os.arch} ne "powerpc"} {
            default_variants-append +cocoa
        } else {
            default_variants-append +qt4
        }
    }

    # current version of python4delphi as of 26-11-2025
    if {[variant_isset python4delphi]} {
        set python4delphi_git_commit d02e0fbecc65c104837ae1f103148044ef6e6f61
    }

    variant python4delphi description "Build Lazarus IDE with Python4Delphi package included" {
        master_sites-append https://github.com/lmbelo/python4delphi/archive/:python4delphi
        distfiles-append    ${python4delphi_git_commit}.tar.gz:python4delphi

        checksums-append    ${python4delphi_git_commit}.tar.gz \
            rmd160  920dc07d5531270bef5caf8216de8258b4405f5a \
            sha256  7ac38d5090d9c4d3931ca3688d159669c42fd7b17e3cda6b08d55831aea6cf32 \
            size    7702618

        depends_lib-append  port:python314

        post-extract {
            move ${workpath}/python4delphi-${python4delphi_git_commit} \
                 ${worksrcpath}/components/python4delphi
        }

        post-build {
            # build the package using lazbuild
            system "${worksrcpath}/lazbuild \
                --lazarusdir=${worksrcpath} \
                --add-package-link \
                ${worksrcpath}/components/python4delphi/Packages/FPC/p4dlaz.lpk"
            # rebuild the ide with the package
            system "make -C ${worksrcpath} bigide \
                [join ${build.post_args} " "]"
        }
    }

    destroot.post_args INSTALL_PREFIX=${destroot}${prefix}

    post-destroot {
        xinstall -m 755 -d ${destroot}${prefix}/etc/lazarus
        xinstall -m 644 ${worksrcpath}/tools/install/macosx/environmentoptions.xml ${destroot}${prefix}/etc/lazarus

        ln -s ${prefix}/share/lazarus/lazarus.app ${destroot}${applications_dir}

    # Install documentation files
        xinstall -m 755 -d ${destroot}${prefix}/share/doc/lazarus
        xinstall -m 644 {*}[glob ${worksrcpath}/docs/*.txt] ${destroot}${prefix}/share/doc/lazarus
        xinstall -m 644 {*}[glob ${worksrcpath}/docs/*.pdf] ${destroot}${prefix}/share/doc/lazarus

        if {[variant_isset python4delphi]} {
            set p4d_dest ${destroot}${prefix}/share/lazarus/components/python4delphi
            xinstall -m 755 -d ${p4d_dest}
            foreach directory {Source Packages Modules} {
                copy ${worksrcpath}/components/python4delphi/${directory} \
                     ${p4d_dest}/${directory}
            }
        }
    }

    notes "
    Release Notes:
      https://wiki.freepascal.org/Lazarus_4.0_release_notes
    "
}

livecheck.regex "lazarus-(\\d+(?:\\.\\d+)*-\\d)"
