Refactor TLS setup and add compile flag automatically.