Enable compilation on Cygwin

This is a WIP branch, mainly for cherry-picking commits inside.