CIRCT 23.0.0git
Loading...
Searching...
No Matches
Path.cpp
Go to the documentation of this file.
1//===- Path.cpp - Path Utilities --------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Utilities for file system path handling, supplementing the ones from
10// llvm::sys::path.
11//
12//===----------------------------------------------------------------------===//
13
14#include "circt/Support/Path.h"
15#include "mlir/IR/Diagnostics.h"
16#include "mlir/Support/FileUtilities.h"
17#include "llvm/Support/FileSystem.h"
18#include "llvm/Support/Path.h"
19
20using namespace circt;
21
22/// Append a path to an existing path, replacing it if the other path is
23/// absolute. This mimicks the behaviour of `foo/bar` and `/foo/bar` being used
24/// in a working directory `/home`, resulting in `/home/foo/bar` and `/foo/bar`,
25/// respectively.
26void circt::appendPossiblyAbsolutePath(llvm::SmallVectorImpl<char> &base,
27 const llvm::Twine &suffix) {
28 if (llvm::sys::path::is_absolute(suffix)) {
29 base.clear();
30 suffix.toVector(base);
31 } else {
32 llvm::sys::path::append(base, suffix);
33 }
34}
35
36void circt::makeCommonDirectoryPrefix(llvm::SmallVectorImpl<char> &a,
37 StringRef b) {
38 // Truncate `a` to the common character prefix of `a` and `b`.
39 StringRef aRef(a.data(), a.size());
40 size_t e = std::min(aRef.size(), b.size());
41 size_t i = 0;
42 for (; i < e; ++i)
43 if (aRef[i] != b[i])
44 break;
45 a.truncate(i);
46
47 // Truncate `a` so it ends on a directory separator.
48 auto sep = llvm::sys::path::get_separator();
49 StringRef sepRef(sep);
50 while (!a.empty() && !StringRef(a.data(), a.size()).ends_with(sepRef))
51 a.pop_back();
52}
53
54std::unique_ptr<llvm::ToolOutputFile>
55circt::createOutputFile(StringRef filename, StringRef dirname,
56 function_ref<InFlightDiagnostic()> emitError) {
57 // Determine the output path from the output directory and filename.
58 SmallString<128> outputFilename(dirname);
59 appendPossiblyAbsolutePath(outputFilename, filename);
60 auto outputDir = llvm::sys::path::parent_path(outputFilename);
61
62 // Create the output directory if needed.
63 std::error_code error = llvm::sys::fs::create_directories(outputDir);
64 if (error) {
65 emitError() << "cannot create output directory \"" << outputDir
66 << "\": " << error.message();
67 return {};
68 }
69
70 // Open the output file.
71 std::string errorMessage;
72 auto output = mlir::openOutputFile(outputFilename, &errorMessage);
73 if (!output)
74 emitError() << errorMessage;
75 return output;
76}
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
void makeCommonDirectoryPrefix(llvm::SmallVectorImpl< char > &a, StringRef b)
Truncate a in place to the longest common directory prefix of a and b, ensuring that the result ends ...
Definition Path.cpp:36
std::unique_ptr< llvm::ToolOutputFile > createOutputFile(StringRef filename, StringRef dirname, function_ref< InFlightDiagnostic()> emitError)
Creates an output file with the given filename in the specified directory.
Definition Path.cpp:55
void appendPossiblyAbsolutePath(llvm::SmallVectorImpl< char > &base, const llvm::Twine &suffix)
Append a path to an existing path, replacing it if the other path is absolute.
Definition Path.cpp:26