Use a secure randomized build directory when possible

Fixes #1964
Fixes #1935
Fixes #676