Hoist build function to top level of Cakefile

See #2733. As it was written, so it has been done.