[cmake] fix setting debug flags, reduce code duplication