From 0b211478dd62f65af929077a3ab462ef24c2f110 Mon Sep 17 00:00:00 2001 From: Jatin Chaudhary <51944368+cjatin@users.noreply.github.com> Date: Fri, 10 Jan 2020 13:47:54 +0530 Subject: [PATCH] Remove filesystem dependency in hipRTC (#1749) Removing dependency on filesystem, so libstdc++fs is no longer required to link --- hipamd/CMakeLists.txt | 1 - hipamd/src/hiprtc.cpp | 61 +++++++++++++++++++++++++++++-------------- 2 files changed, 41 insertions(+), 21 deletions(-) diff --git a/hipamd/CMakeLists.txt b/hipamd/CMakeLists.txt index 53f1060621..294f755906 100644 --- a/hipamd/CMakeLists.txt +++ b/hipamd/CMakeLists.txt @@ -304,7 +304,6 @@ if(HIP_PLATFORM STREQUAL "hcc") target_include_directories( hiprtc SYSTEM PRIVATE ${PROJECT_SOURCE_DIR}/include ${HSA_PATH}/include) - target_link_libraries(hiprtc PUBLIC stdc++fs) endif() set_target_properties(hip_hcc PROPERTIES CXX_VISIBILITY_PRESET hidden) set_target_properties(hip_hcc PROPERTIES VISIBILITY_INLINES_HIDDEN 1) diff --git a/hipamd/src/hiprtc.cpp b/hipamd/src/hiprtc.cpp index f3288a3bcb..5198bf0cbb 100644 --- a/hipamd/src/hiprtc.cpp +++ b/hipamd/src/hiprtc.cpp @@ -35,7 +35,6 @@ THE SOFTWARE. #include #include #include -#include #include #include #include @@ -43,11 +42,13 @@ THE SOFTWARE. #include #include #include +#include #include #include #include #include +#include const char* hiprtcGetErrorString(hiprtcResult x) { @@ -80,6 +81,20 @@ const char* hiprtcGetErrorString(hiprtcResult x) }; } +namespace hip_impl { +inline bool create_directory(const std::string& path) { + mode_t mode = 0755; + int ret = mkdir(path.c_str(), mode); + if (ret == 0) return true; + return false; +} + +inline bool fileExists (const std::string& name) { + struct stat buffer; + return (stat (name.c_str(), &buffer) == 0); +} +} // namespace hip_impl + namespace { struct Symbol { @@ -196,8 +211,7 @@ struct _hiprtcProgram { } // MANIPULATORS - bool compile(const std::vector& args, - const std::experimental::filesystem::path& program_folder) + bool compile(const std::vector& args) { using namespace ELFIO; using namespace redi; @@ -288,9 +302,19 @@ struct _hiprtcProgram { return true; } + void replaceExtension(std::string& fileName, const std::string &ext) const { + auto res = fileName.rfind('.'); + auto sloc = fileName.rfind('/'); // slash location + if (res != std::string::npos && (res > sloc || sloc == std::string::npos)) { + fileName.replace(fileName.begin() + res, fileName.end(), ext); + } else { + fileName += ext; + } + } + // ACCESSORS - std::experimental::filesystem::path writeTemporaryFiles( - const std::experimental::filesystem::path& programFolder) const + std::string writeTemporaryFiles( + const std::string& programFolder) const { using namespace std; @@ -298,12 +322,13 @@ struct _hiprtcProgram { transform(headers.cbegin(), headers.cend(), begin(fut), [&](const pair& x) { return async([&]() { - ofstream h{programFolder / x.first}; + ofstream h{programFolder + '/' + x.first}; h.write(x.second.data(), x.second.size()); }); }); - auto tmp{(programFolder / name).replace_extension(".cpp")}; + auto tmp{(programFolder + '/' + name)}; + replaceExtension(tmp, ".cpp"); ofstream{tmp}.write(source.data(), source.size()); return tmp; @@ -356,27 +381,23 @@ namespace { class Unique_temporary_path { // DATA - std::experimental::filesystem::path path_{}; + std::string path_{}; public: // CREATORS Unique_temporary_path() : path_{std::tmpnam(nullptr)} { - while (std::experimental::filesystem::exists(path_)) { + while (hip_impl::fileExists(path_)) { path_ = std::tmpnam(nullptr); } } - Unique_temporary_path(const std::string& extension) - : Unique_temporary_path{} - { - path_.replace_extension(extension); - } Unique_temporary_path(const Unique_temporary_path&) = default; Unique_temporary_path(Unique_temporary_path&&) = default; ~Unique_temporary_path() noexcept { - std::experimental::filesystem::remove_all(path_); + std::string s("rm -r " + path_); + system(s.c_str()); } // MANIPULATORS @@ -385,7 +406,7 @@ namespace Unique_temporary_path& operator=(Unique_temporary_path&&) = default; // ACCESSORS - const std::experimental::filesystem::path& path() const noexcept + const std::string& path() const noexcept { return path_; } @@ -483,12 +504,12 @@ hiprtcResult hiprtcCompileProgram(hiprtcProgram p, int n, const char** o) getenv("HIP_PATH") ? (getenv("HIP_PATH") + string{"/bin/hipcc"}) : "/opt/rocm/bin/hipcc"}; - if (!experimental::filesystem::exists(hipcc)) { + if (!hip_impl::fileExists(hipcc)) { return HIPRTC_ERROR_INTERNAL_ERROR; } Unique_temporary_path tmp{}; - experimental::filesystem::create_directory(tmp.path()); + hip_impl::create_directory(tmp.path()); const auto src{p->writeTemporaryFiles(tmp.path())}; @@ -499,9 +520,9 @@ hiprtcResult hiprtcCompileProgram(hiprtcProgram p, int n, const char** o) args.emplace_back(src); args.emplace_back("-o"); - args.emplace_back(tmp.path() / "hiprtc.out"); + args.emplace_back(tmp.path() + '/' + "hiprtc.out"); - if (!p->compile(args, tmp.path())) return HIPRTC_ERROR_INTERNAL_ERROR; + if (!p->compile(args)) return HIPRTC_ERROR_INTERNAL_ERROR; if (!p->readLoweredNames()) return HIPRTC_ERROR_INTERNAL_ERROR; p->compiled = true;