# -*- 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                fpc
version             3.2.2
categories          lang
platforms           darwin
license             GPL-2 LGPL-2
maintainers         {@kamischi web.de:karl-michael.schindler} openmaintainer
description         Free Pascal, an open source Pascal and Object Pascal compiler.
long_description    Free Pascal is a 32, 64 and 16 bit professional Pascal compiler. \
                    It can target many processor architectures: Intel x86 (including 8086), \
                    AMD64/x86-64, PowerPC, PowerPC64, SPARC, ARM, AArch64, MIPS and the JVM. \
                    Supported operating systems include Linux, FreeBSD, Mac OS X/iOS/iPhoneSimulator/Darwin, \
                    Win32, Win64, WinCE and Android.
homepage            http://www.freepascal.org
master_sites        sourceforge:freepascal:main \
                    ftp://ftp.freepascal.org/pub/fpc/dist/3.0.4/bootstrap/:bootstrap

set src             ${name}build-${version}.tar.gz
# the 3.0.4 bootstrap compiler
set pp              x86_64-macosx-10.9-ppcx64.tar.bz2
distfiles           ${src}:main ${pp}:bootstrap
checksums           ${src} \
                    rmd160  3106b4aff1adc4cd08dfdd39cc3ef4e800888255 \
                    sha256  85ef993043bb83f999e2212f1bca766eb71f6f973d362e2290475dbaaf50161f \
                    size    84195619 \
                    ${pp} \
                    rmd160  7c5327aa6f4fd8616d78bc4f708463b9fb3e8844 \
                    sha256  0515ad4251c51fa7e5c0ff9b1d90ea50f41530b56276cc72b73798fae437b3b4 \
                    size    1556308
supported_archs     x86_64 arm64

# doesn't install shared libraries
installs_libs       no

# parallel building is unreliable
use_parallel_build  no

extract.only        ${src}

use_configure       no
universal_variant   no

set fpcbasepath     ${prefix}/libexec/${name}

subport "chmcmd-${name}" {
    revision        0

    description     The FPC Pascal htmlhelp (CHM) compiler
    long_description \
                    chmcmd is an cross-platform utility to generate compressed HTML (.chm) \
                    documentation, written in Free Pascal

    depends_build-append \
                    port:${name}
    extract.post_args-append \
                    ${name}build-${version}/fpcsrc/packages/chm \
                    ${name}build-${version}/fpcsrc/packages/fcl-xml \
                    ${name}build-${version}/fpcsrc/packages/fcl-base \
                    ${name}build-${version}/fpcsrc/packages/fcl-process \
                    ${name}build-${version}/fpcsrc/packages/fpmkunit \
                    ${name}build-${version}/fpcsrc/packages/hash \
                    ${name}build-${version}/fpcsrc/packages/iconvenc \
                    ${name}build-${version}/fpcsrc/packages/libtar \
                    ${name}build-${version}/fpcsrc/packages/paszlib \
                    ${name}build-${version}/fpcsrc/packages/univint \
                    ${name}build-${version}/fpcsrc/rtl
    worksrcdir      ${name}build-${version}/fpcsrc/packages/chm
    use_configure   yes
    configure.cmd   ${fpcbasepath}/bin/fpcmake
    configure.pre_args
    configure.args  -r -v Makefile.fpc.fpcmake
    configure.post_args
    build.env       PREFIX=${prefix}
    build.target
    build.post_args-append \
                    V=1 VERBOSE=1
    destroot {
        xinstall -m 0755 ${build.dir}/chmcmd ${destroot}${prefix}/bin
    }
}

subport "${name}-cross" {
    revision        0

    description     FreePascal common cross-compiler binaries
    long_description \
                    Crosscompilers serving as starting points \
                    for specific operating system targets.    \
                    OS runtime libraries are not yet ready.
    depends_build-append \
                    port:${name}

    worksrcdir      ${name}build-${version}/fpcsrc/compiler
    use_configure   no
    build.args      OPT="-ap -v0"
    build.target    aarch64 arm avr i386 i8086 jvm m68k mips mipsel powerpc powerpc64 sparc x86_64
    switch ${build_arch} {
        x86_64 {
            build.target-delete x86_64
        }
        arm64 {
            build.target-delete aarch64
        }
    }
    destroot {
      # delete unwanted files
        file delete -force ${worksrcpath}/ppcgen
        file delete        {*}[glob ${worksrcpath}/ppc*.*]

      # create dirs
        xinstall -m 0755 -d ${destroot}${prefix}/bin
        xinstall -m 0755 -d ${destroot}${prefix}/lib/fpc/${version}

      # loop over installing all cross-compilers
        foreach PPCCROSS [glob -tails -directory ${worksrcpath} ppc*] {
            xinstall -m 0755 ${worksrcpath}/$PPCCROSS ${destroot}${prefix}/lib/fpc/${version}
            ln -sf ../lib/fpc/${version}/$PPCCROSS ${destroot}${prefix}/bin/$PPCCROSS
        }
    }
}

subport "${name}-cross-arm-wince" {
    revision                0
    worksrcdir              fpcbuild-${version}/fpcsrc
    use_parallel_build      no
    use_configure           no
    depends_build-append    port:${name} port:${name}-cross
#    build.target            rtl packages
#    destroot.target         rtl_install packages_install
    build.target            rtl
    destroot.target         rtl_install

# target specifics
    description     FPC cross-compiler for arm-wince
    long_description \
        This Pascal crosscompiler produces arm executables, \
        which run natively on arm-wince systems. \n \
        Get help with: \n \
        fpc -h \n \
        Compile and link a Pascal file with: \n \
        \n \
        fpc -Parm -Twince FILENAME

    build.args      PP=ppcarm CPU_TARGET=arm OS_TARGET=wince OPT="-ap -v0"
    destroot.args   CPU_TARGET=arm OS_TARGET=wince CROSSINSTALL=1 INSTALL_PREFIX=${destroot}${prefix}

# remove duplicate doc and bin files
    post-destroot   {
        file delete -force ${destroot}${prefix}/share
        file delete -force ${destroot}${prefix}/bin
    }
}

foreach ostarget {nativent win32 wince} {
    subport "${name}-cross-i386-$ostarget" {
        revision                0
        worksrcdir              fpcbuild-${version}/fpcsrc
        use_parallel_build      no
        use_configure           no
        depends_build-append    port:${name} port:${name}-cross
#        build.target            rtl packages
#        destroot.target         rtl_install packages_install
        build.target            rtl
        destroot.target         rtl_install

# target specifics
        description     FPC cross-compiler for i386-$ostarget
        long_description \
            This Pascal crosscompiler produces i386 executables, \
            which run natively on i386-$ostarget systems. \n \
            Get help with: \n \
            fpc -h \n \
            Compile and link a Pascal file with: \n \
            \n \
            fpc -Pi386 -T$ostarget FILENAME

        build.args      PP=ppc386 CPU_TARGET=i386 OS_TARGET=$ostarget OPT="-ap -v0"
        destroot.args   CPU_TARGET=i386 OS_TARGET=$ostarget CROSSINSTALL=1 INSTALL_PREFIX=${destroot}${prefix}

# remove duplicate doc and bin files
        post-destroot   {
            file delete -force ${destroot}${prefix}/share
            file delete -force ${destroot}${prefix}/bin
        }
    }
}

subport "${name}-cross-x86-64-win64" {
    revision                0
    worksrcdir              fpcbuild-${version}/fpcsrc
    use_parallel_build      no
    use_configure           no
    depends_build-append    port:${name} port:${name}-cross
#    build.target            rtl packages
#    destroot.target         rtl_install packages_install
    build.target            rtl
    destroot.target         rtl_install

# target specifics
    description     FPC cross-compiler for x86_64-win64
    long_description \
        This Pascal crosscompiler produces x86_64 executables, \
        which run natively on x86_64-win64 systems. \n \
        Get help with: \n \
        fpc -h \n \
        Compile and link a Pascal file with: \n \
        \n \
        fpc -Px86_64 -Twin64 FILENAME

    build.args      PP=ppcx64 CPU_TARGET=x86_64 OS_TARGET=win64 OPT="-ap -v0"
    destroot.args   CPU_TARGET=x86_64 OS_TARGET=win64 CROSSINSTALL=1 INSTALL_PREFIX=${destroot}${prefix}

# remove duplicate doc and bin files
    post-destroot   {
        file delete -force ${destroot}${prefix}/share
        file delete -force ${destroot}${prefix}/bin
    }
}

if {${subport} eq "${name}"} {
    revision            0

    installs_libs       yes

    post-extract {
        system -W ${workpath} "bzip2 -dc ${distpath}/${pp} | tar xf -"
    }

    post-patch {
        # adjust the path "codfilepath" for plex from /usr/local to MacPort's prefix.
        reinplace "s|/usr/local|${prefix}|g" \
            ${worksrcpath}/utils/tply/pyacc.y \
            ${worksrcpath}/utils/tply/pyacc.pas \
            ${worksrcpath}/utils/tply/plex.pas
    }

    switch ${build_arch} {
        x86_64 {
            set bootstrapCompiler ppcx64
            set cpuTarget x86_64
            set compiler ppcx64
        }
        arm64 {
            set bootstrapCompiler ppcx64
            set cpuTarget aarch64
            set compiler ppca64
        }
        default {
            set bootstrapCompiler unsupported
            set cpuTarget unsupported
            set compiler unsupported
        }
    }

    # os.major = 20 is macOS = 11 (BigSur)
    if {${os.major} >= 20} {
        set linkerOptions -XR${configure.sdkroot}
    } else {
        set linkerOptions ""
    }

    worksrcdir          ${name}build-${version}/fpcsrc
    build.env           PP=${workpath}/${bootstrapCompiler} \
                        PREFIX=${destroot}${fpcbasepath}
    build.args          OPT="-ap -v0 ${linkerOptions}" CPU_TARGET=${cpuTarget} UTILS=1

    destroot.args       {*}${build.args}
    destroot.env        {*}${build.env}

    # build the compiler utilities msgdif and msg2inc
    post-build {
        system -W ${worksrcpath}/compiler/utils \
               "../${compiler} -ap -v0 -Fu../../rtl/units/${cpuTarget}-darwin \
                  -WM${macosx_deployment_target} ${linkerOptions} msgdif.pp && \
                ../${compiler} -ap -v0 -Fu../../rtl/units/${cpuTarget}-darwin \
                  -WM${macosx_deployment_target} ${linkerOptions} msg2inc.pp"
    }

    post-destroot {
        # create a symlink to the architecture dependent executable
        ln -s ${fpcbasepath}/lib/${name}/${version}/${compiler} ${destroot}${fpcbasepath}/bin
        # target arm64 needs make utils_install explicitly
        if {${build_arch} == "arm64"} {
            system -W ${worksrcpath} \
                   "make utils_install FPC=${worksrcpath}/compiler/${compiler} \
                       PREFIX=${destroot}${fpcbasepath} \
                       OPT=\"-Furtl/units/${cpuTarget}-darwin -ap -v0 ${linkerOptions}\""
        }
        # generate a configuration file
        xinstall -d ${destroot}${fpcbasepath}/etc
        system -W ${destroot}${fpcbasepath} \
               "bin/fpcmkcfg -d basepath=${fpcbasepath}/lib/${name}/${version} -o etc/fpc.cfg"
        ln -s ${fpcbasepath}/etc/fpc.cfg ${destroot}${prefix}/etc
        system "patch ${destroot}${fpcbasepath}/etc/fpc.cfg ${filespath}/fpc.cfg.patch"

        # install man
        xinstall -d ${destroot}${fpcbasepath}/man
        foreach d {1 5} {
            file copy ${workpath}/${name}build-${version}/install/man/man${d} ${destroot}${fpcbasepath}/man
            foreach f [glob ${destroot}${fpcbasepath}/man/man${d}/*.${d}] {
                system "/usr/bin/gzip ${f}"
            }
        }
        ln -s ${fpcbasepath}/man/man1/fpc.1.gz ${destroot}${prefix}/share/man/man1
        ln -s ${fpcbasepath}/man/man5/fpc.cfg.5.gz ${destroot}${prefix}/share/man/man5

        # chmcmd will be installed by the chmcmd-fpc subport
        file delete -force ${destroot}${fpcbasepath}/bin/chmcmd
        foreach b [glob -nocomplain ${destroot}${fpcbasepath}/bin/*] {
            set n [file tail ${b}]
            ln -s ${fpcbasepath}/bin/${n} ${destroot}${prefix}/bin
        }

        # install the compiler utilities msgdif and msg2inc
        xinstall -m 0755 -W ${worksrcpath}/compiler/utils msgdif msg2inc ${destroot}${prefix}/bin
    }

    notes "
        The compiler fpc looks for the fpc.cfg file in the following places:
        - The current directory.
        - Home directory, looks for .fpc.cfg
        - The directory specified in the environment variable PPC_CONFIG_PATH,\
          and if it's not set under compilerdir/../etc.
        - If it is not yet found: in /etc.

        All the files of fpc are installed in ${fpcbasepath} and\
        the executables are installed in ${fpcbasepath}/bin.
        The fpc.cfg is installed in ${fpcbasepath}/etc to make fpc find fpc.cfg.
        To customize, use ~/.fpc.cfg or /etc/fpc.cfg and call ${fpcbasepath}/bin/fpc directly.
    "

    livecheck.distname  ${name}
} else {
    livecheck.type      none
}
