--- /opt/local/var/macports/sources/svn.macports.org/trunk/dports/_resources/port1.0/group/cmake-1.0.tcl	2016-01-08 21:50:10.000000000 +0100
+++ /opt/local/site-ports/_resources/port1.0/group/cmake-1.0.tcl	2016-01-08 22:49:54.000000000 +0100
@@ -67,7 +67,7 @@
 
 configure.args      -DCMAKE_VERBOSE_MAKEFILE=ON \
                     -DCMAKE_COLOR_MAKEFILE=ON \
-                    -DCMAKE_BUILD_TYPE=Release \
+                    -DCMAKE_BUILD_TYPE=MacPorts \
                     -DCMAKE_BUILD_WITH_INSTALL_RPATH=ON \
                     -DCMAKE_INSTALL_RPATH=${prefix}/lib \
                     -DCMAKE_INSTALL_NAME_DIR=${prefix}/lib \
@@ -113,15 +113,68 @@
     # from the concerned Release build type so that configure.optflags
     # gets honored (Debug used by the +debug variant does not set
     # optimization flags by default).
-    if {${configure.optflags} ne ""} {
-        configure.args-append -DCMAKE_C_FLAGS_RELEASE="-DNDEBUG" \
-                              -DCMAKE_CXX_FLAGS_RELEASE="-DNDEBUG"
+    # NB: more recent CMake versions (>=3?) no longer take the env. variables into
+    # account, and thus require explicit use of ${configure.c*flags} below:
+#     if {${configure.optflags} ne ""} {
+#         configure.args-append   -DCMAKE_C_FLAGS="-DNDEBUG ${configure.cflags}" \
+#                                 -DCMAKE_CXX_FLAGS="-DNDEBUG ${configure.cxxflags}"
+#     }
+    # Using a custom BUILD_TYPE we can simply append to the env. variables,
+    # but why do we set -DNDEBUG?
+    configure.cflags-append     -DNDEBUG
+    configure.cxxflags-append   -DNDEBUG
+
+    # process ${configure.cppflags} to extract include directives and other options
+    if {${configure.cppflags} ne ""} {
+        set cppflags [split ${configure.cppflags}]
+        # reset configure.cppflags; we don't want options in double in CPPFLAGS and CFLAGS/CXXFLAGS
+        configure.cppflags-delete   ${configure.cppflags}
+        set next_is_path 0
+        foreach flag ${cppflags} {
+            if {${next_is_path}} {
+                # previous option was a lone -I
+                configure.cppflags-append       -I${flag}
+                set next_is_path 0
+            } else {
+                if {[string match "-I" ${flag}]} {
+                    # lone -I, store the next argument as a path
+                    # (or ignore if this is the last argument)
+                    set next_is_path 1
+                } elseif {[string match "-I*" ${flag}]} {
+                    # a -Ipath option
+                    configure.cppflags-append   ${flag}
+                } else {
+                    # everything else must go into CFLAGS and CXXFLAGS
+                    configure.cflags-append     ${flag}
+                    configure.cxxflags-append   ${flag}
+                    # append to the ObjC flags too, even if CMake ignores them:
+                    configure.objcflags-append  ${flag}
+                    configure.objcxxflags-append   ${flag}
+                }
+            }
+        }
+        ui_debug "-DINCLUDE_DIRECTORIES=${configure.cppflags}"
+        ui_debug "CFLAGS=\"${configure.cflags}\" CXXFLAGS=\"${configure.cxxflags}\""
+        configure.args-append   -DINCLUDE_DIRECTORIES:PATH="${configure.cppflags}"
     }
 }
 
-platform darwin {
-    set cmake._archflag_vars {cc_archflags cxx_archflags ld_archflags objc_archflags objcxx_archflags universal_cflags universal_cxxflags universal_ldflags universal_objcflags universal_objcxxflags}
+post-configure {
+    if {![catch {set fd [open "${workpath}/.macports.${subport}.configure.cmd" "w"]} err]} {
+        foreach var [array names ::env] {
+            puts ${fd} "${var}=$::env(${var})"
+        }
+        puts ${fd} "[join [lrange [split ${configure.env} " "] 0 end] "\n"]\n"
+        puts ${fd} "cd ${worksrcpath}"
+        puts ${fd} "${configure.cmd} ${configure.pre_args} ${configure.args} ${configure.post_args}"
+        close ${fd}
+        unset fd
+    }
+}
 
+platform darwin {
+    set cmake._archflag_vars {cc_archflags cxx_archflags ld_archflags objc_archflags objcxx_archflags \
+        universal_cflags universal_cxxflags universal_ldflags universal_objcflags universal_objcxxflags}
     pre-configure {
         # cmake will add the correct -arch flag(s) based on the value of CMAKE_OSX_ARCHITECTURES.
         if {[variant_exists universal] && [variant_isset universal]} {
@@ -162,7 +215,6 @@
             configure.args-append -DCMAKE_OSX_SYSROOT="/"
         }
     }
-
     post-configure {
         # Although cmake wants us not to set -arch flags ourselves when we run cmake,
         # ports might have need to access these variables at other times.
