Fix a number of CMake-related issues

(Note that since zaphoyd/0.3.x-cmake is behind experimental I had to merge as well, but there were no conflicts with the merge.)

This adds support for building as a shared library and fixes clang detection on OSX, as well as some other fixes. It also changes the name of SOURCE_FILES to something else as SOURCE_FILES can conflict as a keyword in CMake.

There are some other things I'd recommend changing and fixing as well but I don't have time to do an exhaustive fixup, rather than the parts that are affecting me.