Title: Browsing php's source code with woboq's code browser
Date: 2016-12-30 17:25

Php's source code being as clean as php itself, it is often non-trivial to find
what you're looking for (since the developer documentation is
[spartiate](https://secure.php.net/manual/en/internals2.php)), ctags chokes on
it, and their [lxr instance](http://lxr.php.net/) seems to be down for
[various reasons](https://bugs.php.net/bug.php?id=72396&edit=1).

So I decided to give a try to [woboq's code
browser](https://woboq.com/codebrowser.html), since it seems that it's a
[bit better](https://woboq.com/blog/codebrowser-introduction.html) than
[lxr](https://en.wikipedia.org/wiki/LXR_Cross_Referencer): it
works by inspecting the AST of the source code, using clang.

Unfortunately, it relies on the *[json compilation database
format](http://clang.llvm.org/docs/jsoncompilationdatabase.html)-magic-fu* that
is only supported by [cmake](http://suckless.org/sucks/) for now.
The php build system may have a lot of tares, but it's doesn't use cmake.

*Fortunately*, one can simply instrument `make` itself, with a sweet mix of
C++ and Python called [Bear](https://github.com/rizsotto/Bear) that uses
`LD_PRELOAD` internally, to generate the precious `compile_commands.json` file.


Here is the listing of commands that you can copy/paste if you're lazy.

```bash
git clone git@github.com:rizsotto/Bear.git
cd Bear
mkdir build && cd build
cmake ..
make all -j `cat /proc/cpuinfo | grep processor | wc -l`
make install
cd -

git clone git@github.com:php/php-src.git
cd php-src
./buildconf
./configure
bear make -j `cat /proc/cpuinfo | grep processor | wc -l`  # this takes ages
cd -

git clone git@github.com:woboq/woboq_codebrowser.git
cd wobog_codebrowser
cmake . -DLLVM_CONFIG_EXECUTABLE=/opt/llvm/bin/llvm-config -DCMAKE_BUILD_TYPE=Release
make -j `cat /proc/cpuinfo | grep processor | wc -l`

./generator/codebrowser_generator -b ../php-src -a -o /tmp/www_data -p php-src:../php-src:7.0
./indexgenerator/codebrowser_indexgenerator ../php-src
cp -rv ./data /tmp/data

x-www-browser /tmp/www_data
```

If everything went according to plan, you should now have a 
<abbr title="Browsing PHP source code isn't something super-sweet actually.">sweet</abbr>
browsable (static) web display for php's source code like this one:

[![screenshot of woboq's CB with php stuff]({static}/images/woboq_php.png)]({static}/images/woboq_php.png)
