global_exe_path.cpp 784 B

12345678910111213141516171819202122232425262728
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "testing/base/global_exe_path.h"
  5. #include <string>
  6. #include "common/check.h"
  7. #include "common/exe_path.h"
  8. static constinit std::optional<std::string> exe_path = {};
  9. namespace Carbon::Testing {
  10. auto GetExePath() -> llvm::StringRef {
  11. CARBON_CHECK(
  12. exe_path,
  13. "Must not query the executable path until after it has been set!");
  14. return *exe_path;
  15. }
  16. auto SetExePath(const char* argv_zero) -> void {
  17. CARBON_CHECK(!exe_path, "Must not call `SetExePath` more than once!");
  18. exe_path.emplace(Carbon::FindExecutablePath(argv_zero));
  19. }
  20. } // namespace Carbon::Testing