Loading [MathJax]/extensions/tex2jax.js
CIRCT 22.0.0git
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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
36std::unique_ptr<llvm::ToolOutputFile>
37circt::createOutputFile(StringRef filename, StringRef dirname,
38 function_ref<InFlightDiagnostic()> emitError) {
39 // Determine the output path from the output directory and filename.
40 SmallString<128> outputFilename(dirname);
41 appendPossiblyAbsolutePath(outputFilename, filename);
42 auto outputDir = llvm::sys::path::parent_path(outputFilename);
43
44 // Create the output directory if needed.
45 std::error_code error = llvm::sys::fs::create_directories(outputDir);
46 if (error) {
47 emitError() << "cannot create output directory \"" << outputDir
48 << "\": " << error.message();
49 return {};
50 }
51
52 // Open the output file.
53 std::string errorMessage;
54 auto output = mlir::openOutputFile(outputFilename, &errorMessage);
55 if (!output)
56 emitError() << errorMessage;
57 return output;
58}
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
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:37
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