global_exe_path.cpp 781 B

123456789101112131415161718192021222324252627
  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(exe_path)
  12. << "Must not query the executable path until after it has been set!";
  13. return *exe_path;
  14. }
  15. auto SetExePath(const char* argv_zero) -> void {
  16. CARBON_CHECK(!exe_path) << "Must not call `SetExePath` more than once!";
  17. exe_path.emplace(Carbon::FindExecutablePath(argv_zero));
  18. }
  19. } // namespace Carbon::Testing