Refractor cmake files between tests and headless so as to reuse common code