global_exe_path.cpp 804 B

1234567891011121314151617181920212223242526272829
  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 <optional>
  6. #include <string>
  7. #include "common/check.h"
  8. #include "common/exe_path.h"
  9. static constinit std::optional<std::string> exe_path = {};
  10. namespace Carbon::Testing {
  11. auto GetExePath() -> llvm::StringRef {
  12. CARBON_CHECK(
  13. exe_path,
  14. "Must not query the executable path until after it has been set!");
  15. return *exe_path;
  16. }
  17. auto SetExePath(const char* argv_zero) -> void {
  18. CARBON_CHECK(!exe_path, "Must not call `SetExePath` more than once!");
  19. exe_path.emplace(Carbon::FindExecutablePath(argv_zero));
  20. }
  21. } // namespace Carbon::Testing