cmake: more fixes and optimization