--- /opt/local/var/macports/sources/git.macports.org/dports/archivers/zlib/Portfile	2017-01-21 15:36:00.585667259 +0100
+++ archivers/zlib/Portfile	2017-05-23 16:58:35.682159112 +0200
@@ -1,4 +1,6 @@
 # -*- 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
+# kate: backspace-indents true; indent-pasted-text true; indent-width 4; keep-extra-spaces true; remove-trailing-spaces modified; replace-tabs true; replace-tabs-save true; syntax Tcl/Tk; tab-indents true; tab-width 4;
+# $Id: Portfile 107458 2013-06-29 21:08:49Z jeremyhu@macports.org $
 
 PortSystem              1.0
 PortGroup               xcodeversion 1.0
@@ -32,7 +34,144 @@
 
 test.run                yes
 
-destroot.destdir        prefix=${destroot}${prefix}
+patch.pre_args         -p1
+
+variant ng conflicts cloudflare description {provides the "Next Generation" zlib-ng project} {}
+if {[variant_isset ng]} {
+    PortGroup           github 1.0
+    PortGroup           cmake 1.1
+    PortGroup           muniversal 1.0
+    long_description-append This variant provides the "Next Generation" zlib-ng library.
+    git.branch          75e76eebeb08dccea44a1d9933699f7f9a0a97ea
+    github.setup        Dead2 zlib-ng ${git.branch}
+    fetch.type          git
+    name                zlib
+    version             1.2.11
+    supported_archs     i386 x86_64
+    distname            zlib-ng
+    worksrcdir          ${distname}
+    depends_build       port:cpuid
+    patchfiles          ng/patch-cmakelists.diff \
+                        ng/patch-zlib-compat.diff
+    configure.args-append \
+                        -DZLIB_COMPAT:BOOL=ON \
+                        -DWITH_GZFILEOP:BOOL=ON \
+                        -DWITH_NATIVE_INSTRUCTIONS:BOOL=ON
+    pre-configure {
+        if {[catch {system "${prefix}/bin/cpuid | fgrep -i sse4.2"} has_sse4]} {
+            ui_warn "Your CPU doesn't have the SSE4.2 instruction set: ${has_sse4}"
+            configure.optflags-append \
+                        -mno-sse4
+        }
+        if {[catch {system "${prefix}/bin/cpuid | fgrep -i pclmul"} has_pclmul]} {
+            ui_warn "Your CPU or build architecture doesn't support the PCLMUL code"
+            configure.optflags-append \
+                        -mno-pclmul
+        }
+    }
+
+    # 1.6Ghz Intel Celeron N3150, linux 4.9.8, -O3 -march=native, gcc-6.2, 262Mb source tarball as test
+    # 13.739 user_cpu 0.133 kernel_cpu 0:13.05 total_time 106.2%CPU {2928M 18F 212R 535265I 0O 1w 12999c}
+    # 13.744 user_cpu 0.145 kernel_cpu 0:13.07 total_time 106.1%CPU {2928M 0F 219R 535265I 0O 0w 13046c}
+    # 13.804 user_cpu 0.150 kernel_cpu 0:13.13 total_time 106.2%CPU {2928M 0F 224R 535265I 0O 1w 13087c}
+    # idem, zlib 1.2.11, -O3, without -march=native:
+    # 20.514 user_cpu 0.142 kernel_cpu 0:19.43 total_time 106.2%CPU {2928M 0F 215R 535263I 0O 1w 19373c}
+    # 21.246 user_cpu 0.160 kernel_cpu 0:20.19 total_time 105.9%CPU {2928M 0F 215R 535263I 0O 1w 20116c}
+    # 20.485 user_cpu 0.147 kernel_cpu 0:19.41 total_time 106.2%CPU {2928M 0F 222R 535263I 0O 1w 19342c}
+    # 2.7 Ghz i7, OS X 10.9, -O3 -march=native, clang-4.0, 262Mb source tarball as test
+    # 4.641 user_cpu 0.083 kernel_cpu 0:04.72 total_time 100.0%CPU {978944M 0F 275R 0I 0O 0k 0w 134c}
+    # 4.644 user_cpu 0.083 kernel_cpu 0:04.73 total_time 99.7%CPU {978944M 0F 275R 0I 0O 0k 0w 130c}
+    # 4.671 user_cpu 0.084 kernel_cpu 0:04.75 total_time 100.0%CPU {978944M 0F 275R 0I 0O 0k 0w 125c}
+    # stock 1.2.11 idem but with -flto:
+    # 7.704 user_cpu 0.089 kernel_cpu 0:07.80 total_time 99.7%CPU {966656M 0F 272R 0I 0O 0k 0w 245c}
+    # 7.676 user_cpu 0.089 kernel_cpu 0:07.77 total_time 99.7%CPU {958464M 0F 270R 0I 0O 0k 0w 241c}
+    # 7.889 user_cpu 0.089 kernel_cpu 0:07.98 total_time 99.7%CPU {958464M 0F 270R 0I 0O 0k 0w 209c}
+} else {
+    # don't muck with this setting for the +ng variant (or it will destroot directly into ${prefix})
+    destroot.destdir    prefix=${destroot}${prefix}
+}
+
+global cloudflare-commit
+variant cloudflare conflicts ng description {Applies CloudFlare's patches to use SSE for accelerated compression. \
+                            Note it is not 100% certain the zlib license continues to apply to this variant!} {
+    PortGroup               github 1.0
+    PortGroup               muniversal 1.0
+    long_description-append This variant has the CloudFlare patches for SSE accelerated compression
+    # This version contains a GPL'ed assembly file which may be incorporated. Just add GPL2+ to the license info.
+    license-append          GPL-2+
+    github.setup            cloudflare zlib 836eb111a5c5df7db3f2469a867a6f7c1b2e7bdb
+    fetch.type              git
+    version                 1.2.8.21
+    supported_archs         i386 x86_64
+    distname                zlib-cloudflare
+    worksrcdir              ${distname}
+    depends_build           port:cpuid
+    if {[catch {system "${prefix}/bin/cpuid | fgrep -i sse4.2"} has_sse4]} {
+        set has_sse4 no
+    } else {
+        set has_sse4 yes
+        configure.optflags-append \
+                            -msse4
+    }
+    pre-fetch {
+        if {!${has_sse4}} {
+            ui_error "Error checking for the SSE4.2 instruction set: ${has_sse4}"
+            return -code error "Your CPU doesn't seem to have the SSE4.2 instruction set: please install the regular zlib port"
+        }
+    }
+    global cloudflare-commit
+    set cloudflare-commit   836eb111a5c5df7db3f2469a867a6f7c1b2e7bdb
+    patchfiles              cloudflare/patch-configure-clang.diff
+    if {${os.arch} eq "i386" || [catch {system "${prefix}/bin/cpuid | fgrep -i pclmul"} has_pclmul]} {
+        set has_pclmul no
+        configure.optflags-append \
+                            -mno-pclmul
+    } else {
+        set has_pclmul yes
+        configure.optflags-append \
+                            -mnpclmul
+    }
+    pre-patch {
+        if {!${has_pclmul}} {
+            ui_warn "Your CPU or build architecture doesn't support the PCLMUL code"
+            patchfiles-append \
+                            cloudflare/patch-disable-pclmul.diff
+            post-patch {
+                ui_info "removing GPL'ed file crc32-pclmul_asm.S we won't be using anyway"
+                delete ${worksrcpath}/contrib/amd64/crc32-pclmul_asm.S
+            }
+        } else {
+            platform darwin {
+                patchfiles-append \
+                            cloudflare/patch-crc32pclmul-apple.diff
+            }
+        }
+    }
+
+    post-destroot {
+        set docdir ${prefix}/share/doc/${name}
+        xinstall -d ${destroot}${docdir}
+        xinstall -m 0644 -W ${filespath} cloudflare/CloudFlare.txt ${destroot}${docdir}
+        reinplace "s|@@HOST@@|[exec hostname]|g" ${destroot}${docdir}/CloudFlare.txt
+        reinplace "s|@@COMMIT@@|${cloudflare-commit}|g" ${destroot}${docdir}/CloudFlare.txt
+    }
+    # 1.6Ghz Intel Celeron N3150, linux 4.9.8, -O3 -march=native, gcc-6.2, 262Mb source tarball as test
+    # 12.149 user_cpu 0.144 kernel_cpu 0:11.56 total_time 106.2%CPU {2928M 0F 213R 535263I 0O 1w 11536c}
+    # 12.124 user_cpu 0.157 kernel_cpu 0:11.56 total_time 106.1%CPU {2928M 0F 212R 535263I 0O 1w 11525c}
+    # 12.156 user_cpu 0.109 kernel_cpu 0:11.54 total_time 106.1%CPU {2928M 0F 212R 535263I 0O 1w 11490c}
+    # idem, zlib 1.2.11, -O3, without -march=native:
+    # 20.514 user_cpu 0.142 kernel_cpu 0:19.43 total_time 106.2%CPU {2928M 0F 215R 535263I 0O 1w 19373c}
+    # 21.246 user_cpu 0.160 kernel_cpu 0:20.19 total_time 105.9%CPU {2928M 0F 215R 535263I 0O 1w 20116c}
+    # 20.485 user_cpu 0.147 kernel_cpu 0:19.41 total_time 106.2%CPU {2928M 0F 222R 535263I 0O 1w 19342c}
+    # 2.7 Ghz i7, OS X 10.9, -O3 -march=native, clang-4.0, 262Mb source tarball as test
+    # 4.665 user_cpu 0.086 kernel_cpu 0:04.76 total_time 99.5%CPU {958464M 0F 270R 0I 0O 0k 0w 215c}
+    # 4.520 user_cpu 0.081 kernel_cpu 0:04.60 total_time 100.0%CPU {958464M 0F 270R 0I 0O 0k 0w 117c}
+    # 4.640 user_cpu 0.084 kernel_cpu 0:04.72 total_time 100.0%CPU {958464M 0F 270R 0I 0O 0k 0w 134c}
+    # stock 1.2.11 idem but with -flto:
+    # 7.704 user_cpu 0.089 kernel_cpu 0:07.80 total_time 99.7%CPU {966656M 0F 272R 0I 0O 0k 0w 245c}
+    # 7.676 user_cpu 0.089 kernel_cpu 0:07.77 total_time 99.7%CPU {958464M 0F 270R 0I 0O 0k 0w 241c}
+    # 7.889 user_cpu 0.089 kernel_cpu 0:07.98 total_time 99.7%CPU {958464M 0F 270R 0I 0O 0k 0w 209c}
+}
 
 post-destroot {
     platform darwin {
@@ -43,14 +182,17 @@
             }
         }
     }
-    
+
     set docdir ${prefix}/share/doc/${name}
     xinstall -d ${destroot}${docdir}
-    xinstall -m 0644 -W ${worksrcpath} ChangeLog FAQ README zlib.3.pdf ${destroot}${docdir}
-    
-    set examplesdir ${prefix}/share/examples/${name}
-    xinstall -d ${destroot}${examplesdir}
-    xinstall -m 0644 {*}[glob ${worksrcpath}/example{.c,s/*}] ${destroot}${examplesdir}
+    if {[variant_isset ng]} {
+        xinstall -m 0644 -W ${worksrcpath} ChangeLog.zlib README.md README.zlib zlib.3.pdf ${destroot}${docdir}
+    } else {
+        xinstall -m 0644 -W ${worksrcpath} ChangeLog FAQ README zlib.3.pdf ${destroot}${docdir}
+        set examplesdir ${prefix}/share/examples/${name}
+        xinstall -d ${destroot}${examplesdir}
+        xinstall -m 0644 {*}[glob ${worksrcpath}/example{.c,s/*}] ${destroot}${examplesdir}
+    }
 }
 
 livecheck.type          regex
