Support building on win32 and some other fixes

Two commits add support for building on win32. The other two commits fix CMakeLists.txt and an incomplete sync with upstream.