From 437f4d0a48f158cc9cf99e8ca375dcaec1bdbd1f Mon Sep 17 00:00:00 2001 From: dfukalov Date: Mon, 7 Mar 2016 21:29:23 +0300 Subject: [PATCH] added -inplace option --- src/Cuda2Hip.cpp | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/src/Cuda2Hip.cpp b/src/Cuda2Hip.cpp index a9cb162849..cfc7c02446 100644 --- a/src/Cuda2Hip.cpp +++ b/src/Cuda2Hip.cpp @@ -629,9 +629,14 @@ class Cuda2HipCallback : public MatchFinder::MatchCallback { // Set up the command line options static cl::OptionCategory ToolTemplateCategory("CUDA to HIP source translator options"); static cl::extrahelp MoreHelp( " specify the path of source file\n\n" ); + static cl::opt OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"), cl::cat(ToolTemplateCategory)); +static cl::opt +Inplace("inplace", cl::desc("Modify input file inplace, replacing input with hipified output, save backup in .prehip file. " + "If .prehip file exists, use that as input to hip."), cl::value_desc("inplace"), cl::cat(ToolTemplateCategory)); + int main(int argc, const char **argv) { llvm::sys::PrintStackTraceOnErrorSignal(); @@ -643,18 +648,24 @@ int main(int argc, const char **argv) { std::vector fileSources = OptionsParser.getSourcePathList(); if (dst.empty()) { dst = fileSources[0]; - size_t pos = dst.rfind(".cu"); - if (pos != std::string::npos) { - dst = dst.substr(0, pos) + ".hip.cu"; - } else { - llvm::errs() << "Input .cu file was not specified.\n"; - return 1; + if (!Inplace) { + size_t pos = dst.rfind(".cu"); + if (pos != std::string::npos) { + dst = dst.substr(0, pos) + ".hip.cu"; + } else { + llvm::errs() << "Input .cu file was not specified.\n"; + return 1; + } } } else { + if (Inplace) { + llvm::errs() << "Conflict: both -o and -inplace options are specified."; + } dst += ".cu"; } + std::ifstream source(fileSources[0], std::ios::binary); - std::ofstream dest(dst, std::ios::binary); + std::ofstream dest(Inplace ? dst+".prehip" : dst, std::ios::binary); dest << source.rdbuf(); source.close(); dest.close(); @@ -715,10 +726,12 @@ int main(int argc, const char **argv) { Result = Rewrite.overwriteChangedFiles(); - size_t pos = dst.rfind(".cu"); - if (pos != std::string::npos) - { - rename(dst.c_str(), dst.substr(0, pos).c_str()); + if (!Inplace) { + size_t pos = dst.rfind(".cu"); + if (pos != std::string::npos) + { + rename(dst.c_str(), dst.substr(0, pos).c_str()); + } } return Result; }