# -*- 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           cmake 1.1
PortGroup           qt6 1.0

name                qt6-qtcreator
categories          devel aqua
platforms           {macosx >= 18}
# from https://blog.qt.io/blog/2016/01/13/new-agreement-with-the-kde-free-qt-foundation/
# The GPL license comes with two exceptions that ensure that there are no license restrictions on generated code, and that bridging to 3rd party code is still possible.
license             GPL-3
maintainers         {gmail.com:golubchikov.mihail @rue-ryuzaki} \
                    openmaintainer
description         Cross-platform integrated development environment (IDE) tailored to the needs of Qt developers.
long_description    Qt Creator is a cross-platform integrated development environment (IDE) tailored to the needs of Qt developers.

compiler.cxx_standard \
                    2020

if {([vercmp ${xcodeversion} 15] >= 0)} {
    # qtcreator since version 19.0.0 requires xcode version 15
    version                 19.0.1
    revision                0

    checksums               rmd160  0eb18e523e6741abc102bd3c3b4b506850bf51c7 \
                            sha256  38051c52b8673cc152e07c2c6faed0b3cd6f4f9ce39b7fbb8041396db47f831d \
                            size    51831338

    # Do not update all versions to clang 18+ until https://trac.macports.org/ticket/72064 is resolved
    if {${os.major} >= 24} {
        set llvm_version    20
    } else {
        set llvm_version    17
    }

    depends_build-append    port:python314
    configure.args-append   -DPython3_EXECUTABLE=${prefix}/bin/python3.14

    # qtcreator since version 18.0.0 requires go
    depends_build-append    port:go
} else {
    if {${os.major} >= 21} {
        # qtcreator since version 16.0.0 requires qt 6.5.3
        # last build for xcode version up to 15
        version             18.0.2
        # rebuild with macports-clang-17
        revision            1

        checksums           rmd160  a07ece48ad3035d08489825aa70bde917df45fd3 \
                            sha256  e73799b526dbf041487dc5023dd80840d3e722ad476d6995f98cf9a894f6698f \
                            size    68461824

        # qtcreator since version 18.0.0 requires go
        depends_build-append \
                            port:go
    } else {
        # last version for qt 6.4
        version             15.0.1
        revision            0

        checksums           rmd160  237837f7c84ddb4ff688d4215496c9b0c0832351 \
                            sha256  f2e4aed76b2a66140ca25ba86eb9ca0e9b0f69245572c374a905ff5293cc5713 \
                            size    64570173
    }

    set llvm_version        17

    depends_build-append    port:python314
    configure.args-append   -DPython3_EXECUTABLE=${prefix}/bin/python3.14

    # remove -no_warn_duplicated_libraries linker option (for xcode up to 15)
    post-configure {
        foreach f [exec find "${worksrcpath}/../build/" -name link.txt] {
            reinplace -E {s/-no_warn_duplicate_libraries//} ${f}
        }
    }
}

set llvm_dir            ${prefix}/libexec/llvm-${llvm_version}

depends_build-append    port:llvm-${llvm_version}
depends_lib-append      port:clang-${llvm_version}

cmake.install_rpath-append \
                        ${llvm_dir}/lib

configure.compiler      macports-clang-${llvm_version}
configure.args-append   -DLLVM_DIR=${llvm_dir}/lib/cmake/llvm \
                        -DClang_DIR=${llvm_dir}/lib/cmake/clang

homepage                http://qt-project.org/wiki/Category:Tools::QtCreator
distname                qt-creator-opensource-src-${version}
master_sites            https://download.qt.io/official_releases/qtcreator/[join [lrange [split ${version} .] 0 1] .]/${version}/

if { ${subport} eq ${name} } {
    # xcode-select: error: tool 'actool' requires Xcode, but active developer directory '/Library/Developer/CommandLineTools' is a command line tools instance
    use_xcode               yes

    qt6.depends_lib         qtdeclarative qttools qtsvg qt5compat

    depends_lib-append      port:qbs

    # do not opportunistically use QtWebengine
    configure.args-append   -DBUILD_HELPVIEWERBACKEND_QTWEBENGINE=NO

    # qtwebengine does not have a universal build
    variant qtwebengine conflicts universal description {build with QtWebengine support} {
        configure.args-delete \
                            -DBUILD_HELPVIEWERBACKEND_QTWEBENGINE=NO
        qt6.depends_lib     qtwebengine qtwebchannel
    }

    # PortGroup qt6 doesn't set qt_apps_dir
    set qt_apps_dir         ${applications_dir}/Qt6

    # PortGroup qt6 doesn't set qt_qmake_cmd
    set qt_qmake_cmd        ${qt6.dir}/bin/qmake

    destroot {
        xinstall -m 0755 -d ${destroot}${qt_apps_dir}
        file copy "${worksrcpath}/../build/Qt Creator.app" ${destroot}${qt_apps_dir}
    }

    # put following in post-destroot so it works with universal
    post-destroot {
        # sdktool is a tool located in "Qt Creator.app/Contents/Resources"
        #
        # two necessary files that sdktool creates are:
        #    "${qt_apps_dir}/Qt Creator.app/Contents/Resources/QtProject/qtcreator/qtversion.xml"
        #    "${qt_apps_dir}/Qt Creator.app/Contents/Resources/QtProject/qtcreator/profiles.xml"
        #
        # from the installer.dat file, it seems the following are run by the binary installer of Qt Creator version 5.11.0
        # see https://code.qt.io/cgit/qtsdk/qtsdk.git/tree/packaging-tools/configurations/pkg_templates/pkg_qt5_5110/qt.qt5.5110.clang_64/meta/installscript.qs
        #    sdktool addQt --id qt.59.clang_64 --name "Qt %{Qt:Version} clang 64bit" --type Qt4ProjectManager.QtVersion.Desktop --qmake qmakeBinary
        #    sdktool addKit --id qt.59.clang_64_kit --name "Desktop Qt %{Qt:Version} clang 64bit" --Ctoolchain x86-macos-generic-mach_o-64bit --Cxxtoolchain x86-macos-generic-mach_o-64bit --qt qt.59.clang_64 --debuggerengine 256 --devicetype Desktop
        #
        # modify for our purposes
        #
        set sdktool ${destroot}${qt_apps_dir}/Qt\ Creator.app/Contents/Resources/libexec/sdktool

        system "\"${sdktool}\" addQt --id qt.6.macports --name \"Qt %{Qt:Version} from MacPorts\" --qmake \"${qt_qmake_cmd}\" --type Qt4ProjectManager.QtVersion.Desktop"

        # set same as in qt5 >= 5.9, from qt5-1.0.tcl
        # see http://web.archive.org/web/20170621174843/http://doc.qt.io/qt-5/osx.html
        set qt_qmake_spec_arch macx-clang

        if {[variant_exists universal] && [variant_isset universal]} {
            foreach arch ${configure.universal_archs} {
                if { ${arch} eq "i386" } {
                    set toolchain_bit 32
                } else {
                    set toolchain_bit 64
                }

                system "\"${sdktool}\" addKit --id qt.6.macports_${arch}_kit --name \"Desktop Qt %{Qt:Version} from MacPorts ${arch}\" --Ctoolchain x86-macos-generic-mach_o-${toolchain_bit}bit --Cxxtoolchain x86-macos-generic-mach_o-${toolchain_bit}bit --qt qt.6.macports --debuggerengine 256 --devicetype Desktop --mkspec ${qt_qmake_spec_arch}"
            }
        } else {
            if { ${build_arch} eq "i386" } {
                set toolchain_bit 32
            } else {
                set toolchain_bit 64
            }

            system "\"${sdktool}\" addKit --id qt.6.macports_${build_arch}_kit --name \"Desktop Qt %{Qt:Version} from MacPorts ${build_arch}\" --Ctoolchain x86-macos-generic-mach_o-${toolchain_bit}bit --Cxxtoolchain x86-macos-generic-mach_o-${toolchain_bit}bit --qt qt.6.macports --debuggerengine 256 --devicetype Desktop --mkspec ${qt_qmake_spec_arch}"
        }

        # N.B. that at some point in the future, QtCreator.ini may need to be created
        #    to include more search locations for documentation and examples
    }
}

subport ${name}-docs {
    platforms               any
    supported_archs         noarch
    installs_libs           no

    # PortGroup qt6 doesn't set qt_docs_dir
    set qt_docs_dir         ${qt6.dir}/doc

    configure.args-append   -DWITH_DOCS=ON
    qt6.depends_build       sqlite-plugin
    depends_run-append      port:${name}
    build.target            docs

    destroot {
        xinstall -m 755 -d ${destroot}${qt_docs_dir}

        foreach dir "qtcreator" {
            xinstall -m 0644 "${worksrcpath}/../build/Qt Creator.app/Contents/Resources/doc/${dir}.qch" ${destroot}${qt_docs_dir}
            copy ${worksrcpath}/../build/doc/html/${dir} ${destroot}${qt_docs_dir}
        }
    }
}

livecheck.url       https://download.qt.io/official_releases/qtcreator/[join [lrange [split ${version} .] 0 1] .]/
livecheck.regex     href="(\[a-z0-9.\]+)/"
