Migrate to cmake, and fix mingw compile errors.