diff --git a/composer.json b/composer.json
index 061ed97b8f5318578f72cf146abaf85505139130..f223fe40278cd2bf8acd30401c2fdc52de750d47 100644
--- a/composer.json
+++ b/composer.json
@@ -48,5 +48,13 @@
             "type": "vcs",
             "url": "https://github.com/phacility/xhprof"
         }
-    ]
+    ],
+    "scripts": {
+        "post-update-cmd": [
+            "misc/composer/build-xhprof.sh"
+        ],
+        "post-install-cmd": [
+            "misc/composer/build-xhprof.sh"
+        ]
+    }
 }
diff --git a/misc/composer/build-xhprof.sh b/misc/composer/build-xhprof.sh
new file mode 100644
index 0000000000000000000000000000000000000000..2d961e0889d7e0db3b8b5f900bf4c3dafff3add3
--- /dev/null
+++ b/misc/composer/build-xhprof.sh
@@ -0,0 +1,23 @@
+#!/bin/bash
+
+cd vendor/facebook/xhprof/extension
+
+echo "Building xhprof..."
+
+git fetch origin pull/33/head:33_pull_request
+git merge 33_pull_request
+
+if ! phpize; then
+    echo "Fatal error: phpize failed!"
+    exit 1
+fi
+
+if ! ./configure; then
+    echo "Fatal error: configure script failed!"
+    exit 2
+fi
+
+if ! make; then
+    echo "Fatal error: could not build extension (make failed)!"
+    exit 3
+fi