Properly support out-of-tree builds (based on patch by Paul Millar).