bazel_working_dir.h 922 B

12345678910111213141516171819202122232425262728293031
  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. #ifndef CARBON_COMMON_BAZEL_WORKING_DIR_H_
  5. #define CARBON_COMMON_BAZEL_WORKING_DIR_H_
  6. #include "llvm/Support/FileSystem.h"
  7. namespace Carbon {
  8. // Behave as if the working directory is where `bazel run` was invoked.
  9. // This should only be used in development binaries, not release.
  10. inline auto SetWorkingDirForBazel() -> bool {
  11. char* build_working_dir = getenv("BUILD_WORKING_DIRECTORY");
  12. if (build_working_dir == nullptr) {
  13. return true;
  14. }
  15. if (std::error_code err =
  16. llvm::sys::fs::set_current_path(build_working_dir)) {
  17. llvm::errs() << "Failed to set working directory: " << err.message();
  18. return false;
  19. }
  20. return true;
  21. }
  22. } // namespace Carbon
  23. #endif // CARBON_COMMON_BAZEL_WORKING_DIR_H_