From 708367f299846c0779f0f256da34eff21971243e Mon Sep 17 00:00:00 2001 From: emankov Date: Wed, 26 Dec 2018 16:30:41 +0300 Subject: [PATCH] [HIPIFY] Introduce option -o-dir Option -o-dir for output directory: + if not specified source file(s) directory is used; + creates the directory if the directory doesn't exist (only one level in a tree); + if -o and -o-dir both are specified the hipified file is placed to "-o-dir" + "-o"; + reports an error in case of a wrong directory specified, in case of necessity of creating a tree of subfolders, or in case of a filename specified. [ROCm/hip commit: c8d720081f58d976b87686e5d2a3d4cc6178d1f1] --- projects/hip/hipify-clang/src/ArgParse.cpp | 5 ++ projects/hip/hipify-clang/src/ArgParse.h | 1 + projects/hip/hipify-clang/src/main.cpp | 75 +++++++++++++++------- 3 files changed, 57 insertions(+), 24 deletions(-) diff --git a/projects/hip/hipify-clang/src/ArgParse.cpp b/projects/hip/hipify-clang/src/ArgParse.cpp index 87aa8b6755..e5915aedb9 100644 --- a/projects/hip/hipify-clang/src/ArgParse.cpp +++ b/projects/hip/hipify-clang/src/ArgParse.cpp @@ -29,6 +29,11 @@ cl::opt OutputFilename("o", cl::value_desc("filename"), cl::cat(ToolTemplateCategory)); +cl::opt OutputDir("o-dir", + cl::desc("Output direcory"), + cl::value_desc("directory"), + cl::cat(ToolTemplateCategory)); + cl::opt TemporaryDir("temp-dir", cl::desc("Temporary direcory"), cl::value_desc("directory"), diff --git a/projects/hip/hipify-clang/src/ArgParse.h b/projects/hip/hipify-clang/src/ArgParse.h index efffe9e5de..70a6a9b28d 100644 --- a/projects/hip/hipify-clang/src/ArgParse.h +++ b/projects/hip/hipify-clang/src/ArgParse.h @@ -30,6 +30,7 @@ namespace ct = clang::tooling; extern cl::OptionCategory ToolTemplateCategory; extern cl::opt OutputFilename; +extern cl::opt OutputDir; extern cl::opt TemporaryDir; extern cl::opt Inplace; extern cl::opt SaveTemps; diff --git a/projects/hip/hipify-clang/src/main.cpp b/projects/hip/hipify-clang/src/main.cpp index 5417dda783..7af894e16c 100644 --- a/projects/hip/hipify-clang/src/main.cpp +++ b/projects/hip/hipify-clang/src/main.cpp @@ -36,13 +36,47 @@ THE SOFTWARE. #define DEBUG_TYPE "cuda2hip" +std::string sHipify = "[HIPIFY] ", sConflict = "conflict: ", sError = "error: "; namespace ct = clang::tooling; +std::string getAbsoluteDirectory(const std::string& sDir, std::error_code& EC, + const std::string& sDirType = "temporary", + bool bCreateDir = true) { + if (sDir.empty()) { + return sDir; + } + SmallString<256> dirAbsPath; + EC = sys::fs::real_path(sDir, dirAbsPath, true); + if (!EC && sys::fs::is_regular_file(dirAbsPath)) { + llvm::errs() << "\n" << sHipify << sError << sDir << " is not a directory\n"; + EC = std::error_code(static_cast(std::errc::not_a_directory), std::generic_category()); + return ""; + } + if (EC && bCreateDir) { + EC = sys::fs::create_directory(sDir); + if (EC) { + llvm::errs() << "\n" << sHipify << sError << EC.message() << ": " << sDirType << " directory: " << sDir << "\n"; + return ""; + } + EC = sys::fs::real_path(sDir, dirAbsPath, true); + if (EC) { + llvm::errs() << "\n" << sHipify << sError << EC.message() << ": " << sDirType << " directory: " << sDir << "\n"; + return ""; + } + } + return dirAbsPath.c_str(); +} + int main(int argc, const char **argv) { llcompat::PrintStackTraceOnErrorSignal(); ct::CommonOptionsParser OptionsParser(argc, argv, ToolTemplateCategory, llvm::cl::OneOrMore); std::vector fileSources = OptionsParser.getSourcePathList(); - std::string dst = OutputFilename, sHipify = "[HIPIFY] ", sConflict = "conflict: ", sError = "error: "; + std::string dst = OutputFilename, dstDir = OutputDir; + std::error_code EC; + std::string sOutputDirAbsPath = getAbsoluteDirectory(OutputDir, EC, "output"); + if (EC) { + return 1; + } if (!dst.empty()) { if (fileSources.size() > 1) { llvm::errs() << sHipify << sConflict << "-o and multiple source files are specified.\n"; @@ -56,39 +90,29 @@ int main(int argc, const char **argv) { llvm::errs() << sHipify << sConflict << "both -no-output and -o options are specified.\n"; return 1; } + if (!dstDir.empty()) { + dst = sOutputDirAbsPath + "/" + dst; + } } if (NoOutput && Inplace) { llvm::errs() << sHipify << sConflict << "both -no-output and -inplace options are specified.\n"; return 1; } + if (!dstDir.empty() && Inplace) { + llvm::errs() << sHipify << sConflict << "both -o-dir and -inplace options are specified.\n"; + return 1; + } if (Examine) { NoOutput = PrintStats = true; } int Result = 0; - std::error_code EC; SmallString<128> tmpFile; - SmallString<256> sourceAbsPath, tmpDirAbsPath; - StringRef sourceFileName, sourceDir, ext = "hip"; - std::string sTmpDirAbsParh, sTmpFileName; - if (!TemporaryDir.empty()) { - EC = sys::fs::real_path(TemporaryDir, tmpDirAbsPath, true); - if (!EC && sys::fs::is_regular_file(tmpDirAbsPath)) { - llvm::errs() << "\n" << sHipify << sError << TemporaryDir << " is not a directory\n"; - return 1; - } - if (EC) { - EC = sys::fs::create_directory(TemporaryDir); - if (EC) { - llvm::errs() << "\n" << sHipify << sError << EC.message() << ": temporary directory: " << TemporaryDir << "\n"; - return 1; - } - EC = sys::fs::real_path(TemporaryDir, tmpDirAbsPath, true); - if (EC) { - llvm::errs() << "\n" << sHipify << sError << EC.message() << ": temporary directory: " << TemporaryDir << "\n"; - return 1; - } - } - sTmpDirAbsParh = tmpDirAbsPath.c_str(); + SmallString<256> sourceAbsPath; + StringRef sourceFileName, ext = "hip"; + std::string sTmpFileName; + std::string sTmpDirAbsParh = getAbsoluteDirectory(TemporaryDir, EC); + if (EC) { + return 1; } // Arguments for the Statistics print routines. std::unique_ptr csv = nullptr; @@ -106,6 +130,9 @@ int main(int argc, const char **argv) { dst = src; } else { dst = src + "." + ext.str(); + if (!dstDir.empty()) { + dst = sOutputDirAbsPath + "/" + dst; + } } } // Create a copy of the file to work on. When we're done, we'll move this onto the