Skip to content

Commit

Permalink
refactor: temp directories use output path
Browse files Browse the repository at this point in the history
  • Loading branch information
alandefreitas committed Oct 18, 2024
1 parent 691ce1d commit 1aca304
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
22 changes: 22 additions & 0 deletions src/lib/Support/Path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,28 @@ ScopedTempDirectory(
}
}

ScopedTempDirectory::
ScopedTempDirectory(
llvm::StringRef root,
llvm::StringRef dir)
{
llvm::SmallString<128> tempPath(root);
llvm::sys::path::append(tempPath, dir);
bool const exists = llvm::sys::fs::exists(tempPath);
if (exists)
{
ok_ = !llvm::sys::fs::remove_directories(tempPath);
if (!ok_)
{
return;
}
}
ok_ = !llvm::sys::fs::create_directory(tempPath);
if (ok_)
{
path_ = tempPath;
}
}

} // mrdocs
} // clang
22 changes: 21 additions & 1 deletion src/lib/Support/Path.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,37 @@ class ScopedTempDirectory
<tempdir>/<prefix><random>
@endcode
For instance, if the prefix is "mrdocs" and the operating system
is Unix, the directory might be created as: "/tmp/mrdocs-1234".
On Windows, the directory might be created as:
"C:\Users\user\AppData\Local\Temp\mrdocs-1234".
@param prefix The prefix for the temporary directory name.
*/
ScopedTempDirectory(llvm::StringRef prefix);

/** Constructor with a specific path
Creates a temporary directory with the given path.
The directory is deleted when this object goes out of scope.
@param root The root directory for the temporary directory.
@param dir The name of the temporary directory.
*/
ScopedTempDirectory(llvm::StringRef root, llvm::StringRef dir);

/** Returns `true` if the directory was created successfully.
*/
operator bool() const { return ok_; }

/** Returns the path to the temporary directory.
*/
llvm::StringRef path() const { return path_; }
std::string_view path() const { return static_cast<llvm::StringRef>(path_); }

/** Convert temp directory to a std::string_view
*/
operator std::string_view() const { return path(); }
};

} // mrdocs
Expand Down
7 changes: 4 additions & 3 deletions src/tool/GenerateAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,16 @@ DoGenerateAction(
if (compilationDatabasePath.empty())
{
std::string c = files::appendPath(settings.sourceRoot, "CMakeLists.txt");
if (files::exists(c)) {
if (files::exists(c))
{
compilationDatabasePath = c;
}
}
MRDOCS_CHECK(
compilationDatabasePath,
"The compilation database path argument is missing");
ScopedTempDirectory tempDir("mrdocs");
std::string buildPath = files::appendPath(tempDir.path(), "build");
ScopedTempDirectory tempDir(config->settings().output, ".temp");
std::string buildPath = files::appendPath(tempDir, "build");
Expected<std::string> const compileCommandsPathExp =
generateCompileCommandsFile(
compilationDatabasePath, settings.cmake, buildPath);
Expand Down

0 comments on commit 1aca304

Please sign in to comment.