# -*- 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
# $Id: Portfile 87272 2011-11-15 17:45:11Z ryandesign@macports.org $

PortSystem          1.0

# use the qt4 group; set 'building_qt4' so that the portgroup
# does not include certain parts
set building_qt4    1
PortGroup           qt4 1.0
PortGroup           xcodeversion 1.0

name                qt4-mac
conflicts           qt3 qt3-mac qt4-mac-devel
version             4.7.4
revision            1
categories          aqua
platforms           macosx
maintainers         michaelld
license             {LGPL-2.1 GPL-3}

homepage            http://qt.nokia.com/
description         Qt Tool Kit
long_description    Qt Tool Kit: A cross-platform framework \
                    (headers, data, and libraries) for writing \
                    cross-platform GUI-based applications.

master_sites        trolltech
distname            qt-everywhere-opensource-src-${version}

# 4.7.4 was stealth-updated; see #31252
dist_subdir         ${name}/${version}_1

checksums           md5     9831cf1dfa8d0689a06c2c54c5c65aaf \
                    sha1    af9016aa924a577f7b06ffd28c9773b56d74c939 \
                    rmd160  3e47b09663a89973b1c8628506c995dfc2a5387c

minimum_xcodeversions   {8 2.5}

depends_lib-append  port:zlib port:dbus port:openssl port:sqlite3 \
                    port:tiff port:libpng port:libmng port:jpeg

# (0) Fix the testing script to properly handle both +universal as
# well as the native arch for both CXX and CC compiling; also make
# sure it does not create a Mac app bundle.
patchfiles-append   patch-config.tests_unix_compile.test.diff

# (1) Test for Precompiled-headers support requires a C++ compiler,
# but configure calls it, by default, with a C compiler.
patchfiles-append   patch-precomp.test.diff

# (2) Tweak various scripts to allow easy replacement of
# MACOSX_DEPLOYMENT_TARGET: build for just the user's current OS.
patchfiles-append   patch-macosx-deployment-target.diff

# (3) Tweak the configure script to allow easy replacement of arch
# type(s).  The location of this patch is dependent on patch (2), but
# 'patch' should be able to figure it out.
patchfiles-append   patch-configure-arch.diff

# (4) Tweak Qt spec files:
# (4a) Place the -I path for local moc-created headers -before-
# all other -I paths.
patchfiles-append   patch-mkspecs-features-moc.prf.diff

# (4b) Remove Qt's LIBDIR and INCDIR from various Qmake internal
# variables, since they are already included in the user-supplied
# LIBS and INCLUDEPATH.
patchfiles-append   patch-mkspecs-features-qt.prf.diff

# (4c) Change placement of Qt's LIBDIR and INCDIR, to always be after
# those supplied by the user's QMake (.pro) files.  By the time QMake
# gets to the patched function, it is creating the Makefile parts to
# find Qt's headers and libraries, so moving these paths to this
# location should always work.
patchfiles-append   patch-mkspecs-features-qt_functions.prf.diff

# (4d) Add "absolute_library_soname" to the default CONFIG variables,
# so that when a DYLIB is created, if "target.path" is set for it then
# that path is prepended to its "install_name".
patchfiles-append   patch-mkspecs-macx-g++-qmake.prf.diff

# (4e) allow use of 'ccache' via "qmake && make CCACHE=ccache", in mac
# only; this patch requires that (2) already be in place.
patchfiles-append   patch-mkspecs_common_mac.conf.diff

# (5) fix the corewlan qmake file to use non-standard Developer SDK
# location under 10.6, e.g., for some developers doing both iOS and
# OSX.  This patch does not change behavior for 10.4 or 10.5.
# Also include fix to use the correct SDK.
patchfiles-append   patch-src-plugins-bearer-corewlan-corewlan.pro.diff

# (6) fix use of CARBON, found on PPC64, but will work on any system
patchfiles-append   patch-PluginView-no-carbon.diff

# (7) Allow compiling under OS X 10.7
patchfiles-append   patch-src-corelib-global-qglobal.h.diff

# (8) fix #includes for OS X 10.7 in Bearer CoreWLAN
patchfiles-append   patch-src-plugins-bearer-corewlan-qcorewlanengine.mm.diff

# (9) from Qt, blocklist bad digistor certs
# < http://labs.qt.nokia.com/2011/09/07/ >
patchfiles-append   patch-blacklist-diginotar-certs.diff

# (10) from Qt bugs (#18346), fix shader effect item code.
patchfiles-append   patch-src-imports-shaders-shadereffectitem.cpp.diff

# find a way to specify the OS MINOR version.
global MINOR
set MINOR ""

# hopefully the macosx_deployment_target exists and is set by now.  if
# not, last resort (which is not desirable) is to use the os.version.
if {${macosx_deployment_target} != ""} {
    set MINOR [lindex [split ${macosx_deployment_target} "."] 1]
} else {
    set MINOR [expr [lindex [split ${os.version} "."] 0] - 4]
}

post-patch {
    # set ARCHES in configure (per the third patchfile above), for
    # building QMake.  join any 2 or more arch entries with the GCC
    # arch flag (join does not effect a single entry).  first "-arch"
    # is already in place in the 'configure' script (since there has
    # to be at least 1 arch).
    set ARCHES ""
    if {[variant_exists universal] && [variant_isset universal]} {
        set ARCHES [join ${universal_archs} " -arch "]
    } else {
        set ARCHES ${build_arch}
    }
    reinplace "s|@ARCHES@|${ARCHES}|g" ${worksrcpath}/configure

    global MINOR

    # set MACOSX_DEPLOYMENT_TARGET version in various places.  These
    # were all patched in (2) above, and can be easily changed or
    # overridden by the user in a project-local qmake .pro script.
    set TARGET "10.${MINOR}"
    foreach fixfile {configure mkspecs/common/mac-g++.conf \
                     mkspecs/common/mac.conf qmake/qmake.pri \
                     src/tools/bootstrap/bootstrap.pro } {
        reinplace "s|@MACOSX_DEPLOYMENT_TARGET@|${TARGET}|g" \
            ${worksrcpath}/${fixfile}
    }

    # Fix OS version on Bearer CoreWLAN QMake file
    reinplace "s|@MACOSX_VERSION_MINOR@|${MINOR}|g" \
        ${worksrcpath}/src/plugins/bearer/corewlan/corewlan.pro

    # Ensure that correct C/C++ compilers are used.
    reinplace "s| g++\$| ${configure.cxx}|" \
        ${worksrcpath}/mkspecs/common/g++.conf
    reinplace "s| gcc\$| ${configure.cc}|"  \
        ${worksrcpath}/mkspecs/common/g++.conf
    reinplace "s| g++\$| ${configure.cxx}|" \
        ${worksrcpath}/mkspecs/macx-g++/qmake.conf
    reinplace "s| gcc\$| ${configure.cc}|"  \
        ${worksrcpath}/mkspecs/macx-g++/qmake.conf

    # fix up tests to work with ccache, if used
    foreach fixfile [exec find ${worksrcpath}/config.tests \
                         -name "*.test"] {
        reinplace "s@\\\"\\\$COMPILER\\\"@\$COMPILER@g" ${fixfile}
    }

    # fix 'configure' script to find the correct QMAKE CXX compiler by
    # removing possibilities that include the character "$".  Yes,
    # that's a lot of "\"s: each "\\\\" -> a single "\" in the patched
    # file, and need "\\\\$" (with the "s) there.
    reinplace "/QMAKE_CONF_COMPILER/ \
             s@tail@grep -v \"\\\\\\\\\\\\\\\\\$\" | tail@" \
        ${worksrcpath}/configure

    # fix the corewlan .pro file to use the correct developer dir
    reinplace "s,@DEVELOPER_DIR@,${developer_dir},g" \
        ${worksrcpath}/src/plugins/bearer/corewlan/corewlan.pro
}

# The build process uses -fconstant-cfstrings.
# configure.compiler should therefore not be one of the MacPorts compilers.

# does not work with clang, see #30271 and #31638 .
if {${configure.compiler} == "clang"} {
    configure.compiler llvm-gcc-4.2
}

# --prefix is not recognized.
configure.pre_args-delete       --prefix=${prefix}

# --disable-dependency-tracking is not recognized.
configure.universal_args-delete --disable-dependency-tracking

################################################################################
# We do not want flags of any kind -- QT is smart enough to set up its own.    #
################################################################################

configure.cflags
configure.cppflags
configure.cxxflags
configure.ldflags

configure.universal_cflags
configure.universal_cppflags
configure.universal_cxxflags
configure.universal_ldflags

configure.cc_archflags
configure.cxx_archflags
if {[info exists configure.ld_archflags]} {
    configure.ld_archflags
}

#  use compiler.cpath and compiler.library_path, as per
#  http://trac.macports.org/ticket/25321 for all -I and -L entries,
#  so-as to avoid conflicts with already-installed headers and
#  libraries (giving preference to internal-to-this-port first)
#
#  CPATH is used instead of -I to avoid conflicts with other ports.
#  See http://trac.macports.org/ticket/15219 and
#  http://trac.macports.org/ticket/18723.
#
#  set the default paths to include those for SQLITE3 and DBUS-1

set header_path [ list ${prefix}/include/dbus-1.0 \
                       ${prefix}/lib/dbus-1.0/include \
                       ${prefix}/include ]

set library_path [ list ${prefix}/lib ]

# -system-sqlite ensures the use SQLite provided by MacPorts instead of Qt.
#
# -dbus-linked prevends qt4 from trying to dynamically load libdbus-1,
#     which it is not able to find in ${prefix}.
#
# -openssl-linked ensures that the MacPorts openssl is used.
#
# -no-framework ensures that this port is installed as separate
#     libraries and headers, with guaranteed locations and names.
#
# -no-phonon ensures that Qt does not install the 3rd-parth Phonon
#     port or its backend to QuickTime 7; Phonon is now installed via
#     the 'phonon' port, which is more up to date than that provided
#     by Qt.

configure.args                                            \
    -v                                                    \
    -confirm-license                                      \
    -opensource                                           \
    -prefix          ${qt_dir}                            \
    -bindir          ${qt_bins_dir}                       \
    -libdir          ${qt_libs_dir}                       \
    -docdir          ${qt_docs_dir}                       \
    -headerdir       ${qt_includes_dir}                   \
    -plugindir       ${qt_plugins_dir}                    \
    -importdir       ${qt_imports_dir}                    \
    -datadir         ${qt_data_dir}                       \
    -translationdir  ${qt_translations_dir}               \
    -sysconfdir      ${qt_sysconf_dir}                    \
    -examplesdir     ${qt_examples_dir}                   \
    -demosdir        ${qt_demos_dir}                      \
    -system-sqlite                                        \
    -openssl-linked                                       \
    -dbus-linked                                          \
    -fast                                                 \
    -optimized-qmake                                      \
    -no-framework                                         \
    -no-phonon                                            \
    -no-phonon-backend

# Stop configure script from searching for SQL Drivers
#    not available from MacPorts.
foreach driver {db2 ibase oci tds} {
    configure.args-append -no-sql-${driver}
}

# By default, disable most SQL Drivers.
# There must be at least one enabled (http://trac.macports.org/ticket/15627).
# SQLite is chosen to be always enabled because a copy of it is distributed
#    with Qt, so it is reasonable for the user to expect it.
foreach driver {mysql odbc psql sqlite2} {
    configure.args-append -no-sql-${driver}
}

pre-configure {
    # use the corrected CPATH and LIBRARY_PATH for build
    compiler.cpath [join ${header_path} :]
    compiler.library_path [join ${library_path} :]
}

platform darwin 8 {
    # build as Carbon only, not cocoa.
    configure.args-append -carbon

    # required linking flags?
    configure.ldflags-append -lcrypto -ldbus-1 -ljpeg -llcms -lsqlite3 -lssl -lz

    # Under 10.4 only: patch QMake build files using just "-lQtHelp"
    # to do "-lQtHelp -lQtCLucene -lQtNetwork -lQtWebKit" instead.
    # For some reason 10.5+ "knows" that how to do this correctly.
    patchfiles-append patch-QtHelp_10.4_only.diff
}

platform darwin {
    pre-configure {
        # retrieve the arch type(s) to use, in Qt-speak
        configure.args-append -arch \"${qt_arch_types}\"

        # cannot use pre-compiled headers when compiling with more than 1
        # arch type (typically +universal) or for X11
        #if {[llength ${qt_arch_types}] != 1 || [variant_isset x11]}
        if {[llength ${qt_arch_types}] != 1} {
            configure.args-append -no-pch
        } else {
            configure.args-append -pch
        }

        # check to see if not compiling for the native machine hardware
        # (e.g., compiling for just i386 but CPU is x86_64); optimization
        # of QMake does not work in this circumstance.
        if {[lsearch -exact ${qt_arch_types} ${os.arch}] == -1} {
            # not building for native arch: disable optimizing qmake
            configure.args-delete -optimized-qmake
        } elseif {${MINOR} == "6" &&
                  [llength ${qt_arch_types}] == 1 &&
                  [lsearch -exact ${qt_arch_types} x86] == 0} {
            # disable qmake optimization on 10.6 32-bit
            configure.args-delete -optimized-qmake
        }

        # find a way to specify the SDK to use; Qt will use the 10.4u
        # by default which won't work on 10.6 since it is not
        # supported.  Allow the user to specify the SDK if desired.
        set SDK ${configure.sdkroot}
        if {${SDK} == ""} {
            # set SDK version depending on OS version
            global MINOR
            set sdk_version ""
            if {${MINOR} == "4"} {
                # OSX 10.4 requires an additional 'u'
                set sdk_version "10.4u"
            } else {
                set sdk_version "10.${MINOR}"
            }
            set SDK ${developer_dir}/SDKs/MacOSX${sdk_version}.sdk
        }
        if {[file exists ${SDK}]} {
            configure.args-append -sdk ${SDK}
        } else {
            ui_msg \
"No SDK found in ${developer_dir}/SDKs for ${sdk_version} ; \
configure will probably fail. \n \
Please install Apple's Developer Tools again."
        }
    }
}

# Remove any '-I' and '-L' string from the *FLAGS for SQLITE, DBUS,
# ODBC, and OPENSSL, so that what remains are just non-I/L flags;
# these paths are in compiler.cpath and compiler.library_path.  The
# header and library paths for all the other variants are not used by
# 'configure', and are already included in the compiler.cpath and
# compiler.library_path . during the 'build' stage, 'make' will
# rebuild the Makefiles that depend on any of these changed files.
# These patches are sufficient to make sure local (to this build)
# paths are searched first, and then system paths last (via the
# compiler.X settings).  Ideally, 'qmake' -- either via its mkspecs
# files or the project's build files -- would distinguish between
# "local" and "system" includes & libraries.
post-configure {
    # SQLITE
    reinplace "/SQLITE/s@-I${prefix}/\[^ \]*include\[^ \]*@@g" \
        ${worksrcpath}/.qmake.cache
    reinplace "/SQLITE/s@-L${prefix}/\[^ \]*lib\[^ \]*@@g" \
        ${worksrcpath}/.qmake.cache
    # DBUS
    reinplace "/DBUS/s@-I${prefix}/\[^ \]*include\[^ \]*@@g" \
        ${worksrcpath}/.qmake.cache
    reinplace "/DBUS/s@-L${prefix}/\[^ \]*lib\[^ \]*@@g" \
        ${worksrcpath}/.qmake.cache
    # OPENSSL
    reinplace "/OPENSSL/s@-I${prefix}/\[^ \]*include\[^ \]*@@g" \
        ${worksrcpath}/.qmake.cache
    reinplace "/OPENSSL/s@-L${prefix}/\[^ \]*lib\[^ \]*@@g" \
        ${worksrcpath}/.qmake.cache
    # ODBC
    reinplace "/ODBC/s@-I${prefix}/\[^ \]*include\[^ \]*@@g" \
        ${worksrcpath}/.qmake.cache
    reinplace "/ODBC/s@-L${prefix}/\[^ \]*lib\[^ \]*@@g" \
        ${worksrcpath}/.qmake.cache
}

pre-build {
    # use the corrected CPATH and LIBRARY_PATH for build
    compiler.cpath [join ${header_path} :]
    compiler.library_path [join ${library_path} :]
}

build.target all

post-destroot {
    set destroot_qt ${destroot}${qt_dir}

    # Fix .pc and .prl files by changing ${worksrcpath}\${prefix} to
    # ${qt_dir}\${prefix} ("\" means 'take away').  Cannot use
    # "find -E" since it is not necessarily portable.
    regsub ${prefix} ${worksrcpath} "" from_dir
    regsub ${prefix} ${qt_dir} "" to_dir
    foreach fixfile [exec find ${destroot_qt} -name "*.pc"] {
        reinplace "s|${from_dir}|${to_dir}|g" ${fixfile}
    }
    foreach fixfile [exec find ${destroot_qt} -name "*.prl"] {
        reinplace "s|${from_dir}|${to_dir}|g" ${fixfile}
    }

    # Install documentation.
    xinstall -m 755 -d ${worksrcpath}/${qt_docs_dir}
    xinstall -m 644 -W ${worksrcpath} \
        INSTALL LGPL_EXCEPTION.txt LICENSE.FDL \
        LICENSE.GPL3 LICENSE.LGPL README \
        ${destroot}${qt_docs_dir}

    # Move .apps into the applications_dir
    xinstall -m 755 -d ${destroot}${qt_apps_dir}
    foreach app [glob ${destroot}/${qt_bins_dir}/*.app] {
        move ${app} ${destroot}${qt_apps_dir}
    }

    # install qt_menu.nib, used by any applications making use of Qt's
    # application class.
    xinstall -m 755 -d ${destroot_qt}/lib/Resources
    copy ${worksrcpath}/src/gui/mac/qt_menu.nib \
        ${destroot_qt}/lib/Resources
}

pre-activate {
    # make sure 'none' is selected by qt4_select, so that there is no
    # conflict between it and this port.
    if {[file exists ${prefix}/bin/qt4_select]} {
        if {[exec ${prefix}/bin/qt4_select -s] != "none"} {
            ui_msg "Disabling 'qt4_select' selection."
            system "exec ${prefix}/bin/qt4_select none"
        }
    }
}

variant framework description {Build as Frameworks} {
    configure.args-delete -no-framework
    configure.args-append -framework
}

variant mysql description {Enable MySQL SQL Driver} {
    depends_lib-append path:lib/mysql5:mysql5
    lunshift header_path ${prefix}/include/mysql5/mysql
    lunshift library_path ${prefix}/lib/mysql5/mysql
    configure.args-delete -no-sql-mysql
    configure.args-append -plugin-sql-mysql
}

variant odbc description {Enable iODBC SQL Driver} {
    depends_lib-append port:libiodbc
    configure.args-delete -no-sql-odbc
    configure.args-append -plugin-sql-odbc
}

variant psql83 conflicts psql84 psql90 psql91 \
description {Enable Postgre SQL Driver version 8.3} {}

variant psql84 conflicts psql83 psql90 psql91 \
description {Enable Postgre SQL Driver version 8.4} {}

variant psql90 conflicts psql83 psql84 psql91 \
description {Enable Postgre SQL Driver version 9.0} {}

variant psql91 conflicts psql83 psql84 psql90 \
description {Enable Postgre SQL Driver version 9.1} {}

set psql_version ""
if {[variant_isset psql83]} {
    set psql_version "83"
} elseif {[variant_isset psql84]} {
    set psql_version "84"
} elseif {[variant_isset psql90]} {
    set psql_version "90"
} elseif {[variant_isset psql91]} {
    set psql_version "91"
}

if {${psql_version} != ""} {
    depends_lib-append port:postgresql${psql_version}
    lunshift header_path ${prefix}/include/postgresql${psql_version}
    lunshift library_path ${prefix}/lib/postgresql${psql_version}
    configure.args-delete -no-sql-psql
    configure.args-append -plugin-sql-psql
}

variant sqlite2 description {Enable SQLite version 2 SQL Driver} {
    depends_lib-append port:sqlite2
    configure.args-delete -no-sql-sqlite2
    configure.args-append -plugin-sql-sqlite2
}

variant raster description {Use raster graphics system by default} {
    configure.args-append -graphicssystem raster
}

variant demos description {Build demos} {}

if {![variant_isset demos]} {
   configure.args-append -nomake demos
}

variant examples description {Build examples} {}

if {![variant_isset examples]} {
   configure.args-append -nomake examples
}

variant debug description {Build both release and debug library} {}

if {[variant_isset debug]} {
    configure.args-append -debug-and-release
} else {
    configure.args-append -release
}

#variant quartz conflicts x11
variant quartz \
description {Build for Native OSX Quartz GUI, not X11 (default)} {}

#variant x11 conflicts quartz \
#description {Build for X11 GUI, not Native OSX Quartz} {}

# if neither +x11 or +quartz were specified, default to the latter
#if {![variant_isset quartz] && ![variant_isset x11]}
    default_variants +quartz
#}

# check to see if user specified just "-quartz", which can't work.
#if {![variant_isset quartz] && ![variant_isset x11]} {
#    return -code error \
#"\n\nThe variant -quartz will not work alone.
#Please select one of +quartz or +x11 as a variant.\n"
#}

#if {[variant_isset x11]} {
#    return -code error "\n\nVariant +x11 is not yet functional; \
#please use +quartz for now.\n"
#}

livecheck.type      regex
livecheck.url       http://get.qt.nokia.com/qt/source/
livecheck.regex     "qt-everywhere-opensource-src-(\[0-9a-z.-\]+)${extract.suffix}"
