Various build system fixes

Matt Jadud spotted this link order problem: libtvm uses fegetround and various
other maths-y functions, so -lm needs to come after it.