--- archivers/zlib/Portfile	2017-01-21 15:36:00.585667259 +0100
+++ /opt/local/site-ports/archivers/zlib/Portfile	2017-06-19 12:01:17.836918583 +0200
@@ -32,7 +32,10 @@
 
 test.run                yes
 
-destroot.destdir        prefix=${destroot}${prefix}
+if {![variant_isset ng]} {
+    # don't muck with this setting for the +ng variant (or it will destroot directly into ${prefix})
+    destroot.destdir    prefix=${destroot}${prefix}
+}
 
 post-destroot {
     platform darwin {
@@ -43,16 +46,127 @@
             }
         }
     }
-    
+
     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}
+    }
+}
+
+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
+    patch.pre_args      -Np1
+    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
+        }
+    }
+}
+
+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!} {}
+if {[variant_isset cloudflare]} {
+    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
+    revision                1
+    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
+    patch.pre_args          -Np1
+    patchfiles              cloudflare/patch-configure-clang.diff \
+                            cloudflare/patch-restore-uLong-API.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
+    }
 }
 
 livecheck.type          regex
 livecheck.url           ${homepage}
 livecheck.regex         ${name}-(\[0-9.\]+)\\.tar
+
+# 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;
