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

version             18.0.2
revision            0
categories          devel aqua
# qt creator 16.0.0 requires qt 6.5.3
# from qt6 PortGroup:
# qt 6.8 - darwin >= 23
# qt 6.7 - darwin >= 21
# qt 6.4 - darwin >= 18
platforms           {darwin >= 21}
# 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.

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}/

checksums           rmd160  a07ece48ad3035d08489825aa70bde917df45fd3 \
                    sha256  e73799b526dbf041487dc5023dd80840d3e722ad476d6995f98cf9a894f6698f \
                    size    68461824

compiler.cxx_standard \
                    2020

if {([vercmp ${xcodeversion} 15] >= 0)} {
    # cmake 3.29~3.31 adds -no_warn_duplicated_libraries to linker, works with xcode 15 or later
    # 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
    }

    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
} else {
    configure.compiler      clang

    # fix operator <=> return type deduction
    patchfiles-append       patch-fix_operator_return_type_deduction.diff
}

depends_build-append    port:python313 \
                        port:go

configure.args-append   -DPython3_EXECUTABLE=${prefix}/bin/python3.13

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.\]+)/"
