Backport of core PR15461.
This PR is known to cause 2 issues:
- The build (native) on ARM might be broken:
https://github.com/bitcoin/bitcoin/issues/17010
- The error messages are not helpful:
https://github.com/bitcoin/bitcoin/issues/16803
None of these issues are critical.
I kept (adapted) the patch to work around the first issue. It doesn't
provide a very solid guarantee but it can't hurt.
The second issue will be fixed in a future backport.
Note that due to boost cmake update, it is now required to set
`Boost_USE_STATIC_RUNTIME` to `ON`.