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

name                qt5-qtcreator

version             6.0.2
revision            1
categories          devel aqua
# last version that runs on Lion is 3.6.1
# see https://trac.macports.org/ticket/58504
platforms           {darwin >= 16}
# 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         {mcalhoun @MarcusCalhoun-Lopez} 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  76496c85cadec45c4b52e0320d9a780e22394ba2 \
                    sha256  2ce657057b68613f242dfb190165efe4684a9ecf5b3e9c16a978c2d8a4c28fea \
                    size    57106963

compiler.cxx_standard \
                    2014

# see README.md
qt5.min_version     5.14

# avoid make[4]: write error
#use_parallel_build  no

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

    qt5.depends_component  qtscript qtdeclarative qttools qtmacextras qtquickcontrols qtsvg

    depends_lib-append     port:qbs
    configure.args-append  "QBS_INSTALL_DIR=${prefix}"

    depends_lib-append     port:llvm-12 \
                           port:clang-12
    configure.args-append  "LLVM_INSTALL_DIR=${prefix}/libexec/llvm-12"

    depends_build-append port:python39
    set python_framework ${frameworks_dir}/Python.framework/Versions/3.9
    configure.env-append PATH=${python_framework}/bin:$env(PATH)
    build.env-append     PATH=${python_framework}/bin:$env(PATH)

    # fixed in v10.0.2
    # see https://trac.macports.org/ticket/71319
    patchfiles-append      patch-clang16_Wenum_constexpr_conversion.diff

    # do not opportunistically use QtWebengine
    patchfiles-append      patch-no_qtwebengine.diff

    # qtwebengine does not have a universal build
    variant qtwebengine conflicts universal description {build with QtWebengine support} {
        patchfiles-delete  patch-no_qtwebengine.diff
        qt5.depends_component qtwebengine qtwebchannel
    }

    destroot {
        xinstall -m 0755 -d ${destroot}${qt_apps_dir}
        file copy "${worksrcpath}/bin/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.5.macports --name \"Qt %{Qt:Version} from MacPorts\" --qmake \"${qt_qmake_cmd}\" --type Qt4ProjectManager.QtVersion.Desktop"

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

                system "\"${sdktool}\" addKit --id qt.5.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.5.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.5.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.5.macports --debuggerengine 256 --devicetype Desktop --mkspec ${qt_qmake_spec}"
        }

        # as of version 4.11, sdktool seems to install the configuation files in a place that the application cannot find
        move \
            ${destroot}${qt_apps_dir}/Qt\ Creator.app/Contents/Resources/libexec/QtProject \
            ${destroot}${qt_apps_dir}/Qt\ Creator.app/Contents/Resources/

        # 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
    qt5.debug_variant no

    qt5.depends_build_component sqlite-plugin
    depends_run-append   port:${name}
    build.target       docs

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

        foreach dir "qtcreator qtcreator-dev" {
            xinstall -m 0644 "${worksrcpath}/bin/Qt Creator.app/Contents/Resources/doc/${dir}.qch" ${destroot}${qt_docs_dir}
            copy ${worksrcpath}/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.\]+)/"
