/home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-composable-kernel/checkouts/develop/include/ck/filesystem.hpp Source File

/home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-composable-kernel/checkouts/develop/include/ck/filesystem.hpp Source File#

Composable Kernel: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-composable-kernel/checkouts/develop/include/ck/filesystem.hpp Source File
filesystem.hpp
Go to the documentation of this file.
1 // SPDX-License-Identifier: MIT
2 // Copyright (c) 2018-2024, Advanced Micro Devices, Inc. All rights reserved.
3 
4 #ifndef GUARD_CK_FILESYSTEM_HPP_
5 #define GUARD_CK_FILESYSTEM_HPP_
6 
7 #include <string>
8 #include <string_view>
9 
10 // clang-format off
11 #if defined(CPPCHECK)
12  #define CK_HAS_FILESYSTEM 1
13  #define CK_HAS_FILESYSTEM_TS 1
14 #elif defined(_WIN32)
15  #if _MSC_VER >= 1920
16  #define CK_HAS_FILESYSTEM 1
17  #define CK_HAS_FILESYSTEM_TS 0
18  #elif _MSC_VER >= 1900
19  #define CK_HAS_FILESYSTEM 0
20  #define CK_HAS_FILESYSTEM_TS 1
21  #else
22  #define CK_HAS_FILESYSTEM 0
23  #define CK_HAS_FILESYSTEM_TS 0
24  #endif
25 #elif defined(__has_include)
26  #if __has_include(<filesystem>) && __cplusplus >= 201703L
27  #define CK_HAS_FILESYSTEM 1
28  #else
29  #define CK_HAS_FILESYSTEM 0
30  #endif
31  #if __has_include(<experimental/filesystem>) && __cplusplus >= 201103L
32  #define CK_HAS_FILESYSTEM_TS 1
33  #else
34  #define CK_HAS_FILESYSTEM_TS 0
35  #endif
36 #else
37  #define CK_HAS_FILESYSTEM 0
38  #define CK_HAS_FILESYSTEM_TS 0
39 #endif
40 // clang-format on
41 
42 #if CK_HAS_FILESYSTEM
43 #include <filesystem>
44 #elif CK_HAS_FILESYSTEM_TS
45 #include <experimental/filesystem>
46 #else
47 #error "No filesystem include available"
48 #endif
49 
50 namespace CK {
51 
52 #if CK_HAS_FILESYSTEM
53 namespace fs = ::std::filesystem;
54 #elif CK_HAS_FILESYSTEM_TS
55 namespace fs = ::std::experimental::filesystem;
56 #endif
57 
58 } // namespace CK
59 
60 inline std::string operator+(const std::string_view s, const CK::fs::path& path)
61 {
62  return path.string().insert(0, s);
63 }
64 
65 inline std::string operator+(const CK::fs::path& path, const std::string_view s)
66 {
67  return path.string().append(s);
68 }
69 
70 #define FS_ENUM_PERMS_ALL fs::perms::all
71 
72 #if CK_HAS_FILESYSTEM_TS
73 #ifdef __linux__
74 #include <linux/limits.h>
75 namespace CK {
76 inline fs::path weakly_canonical(const fs::path& path)
77 {
78  std::string result(PATH_MAX, '\0');
79  std::string p{path.is_relative() ? (fs::current_path() / path).string() : path.string()};
80  char* retval = realpath(p.c_str(), &result[0]);
81  return (retval == nullptr) ? path : fs::path{result};
82 }
83 } // namespace CK
84 #else
85 #error "Not implmeneted!"
86 #endif
87 #else
88 namespace CK {
89 inline fs::path weakly_canonical(const fs::path& path) { return fs::weakly_canonical(path); }
90 } // namespace CK
91 #endif
92 
93 namespace CK {
94 
95 #ifdef _WIN32
96 constexpr std::string_view executable_postfix{".exe"};
97 constexpr std::string_view library_prefix{""};
98 constexpr std::string_view dynamic_library_postfix{".dll"};
99 constexpr std::string_view static_library_postfix{".lib"};
100 constexpr std::string_view object_file_postfix{".obj"};
101 #else
102 constexpr std::string_view executable_postfix{""};
103 constexpr std::string_view library_prefix{"lib"};
104 constexpr std::string_view dynamic_library_postfix{".so"};
105 constexpr std::string_view static_library_postfix{".a"};
106 constexpr std::string_view object_file_postfix{".o"};
107 #endif
108 
109 inline fs::path make_executable_name(const fs::path& path)
110 {
111  return path.parent_path() / (path.filename() + executable_postfix);
112 }
113 
114 inline fs::path make_dynamic_library_name(const fs::path& path)
115 {
116  return path.parent_path() / (library_prefix + path.filename() + dynamic_library_postfix);
117 }
118 
119 inline fs::path make_object_file_name(const fs::path& path)
120 {
121  return path.parent_path() / (path.filename() + object_file_postfix);
122 }
123 
124 inline fs::path make_static_library_name(const fs::path& path)
125 {
126  return path.parent_path() / (library_prefix + path.filename() + static_library_postfix);
127 }
128 
130 {
131  std::size_t operator()(const fs::path& path) const { return fs::hash_value(path); }
132 };
133 } // namespace CK
134 
135 #endif // GUARD_CK_FILESYSTEM_HPP_
std::string operator+(const std::string_view s, const CK::fs::path &path)
Definition: filesystem.hpp:60
Definition: filesystem.hpp:50
constexpr std::string_view library_prefix
Definition: filesystem.hpp:103
fs::path make_object_file_name(const fs::path &path)
Definition: filesystem.hpp:119
constexpr std::string_view static_library_postfix
Definition: filesystem.hpp:105
fs::path weakly_canonical(const fs::path &path)
Definition: filesystem.hpp:89
constexpr std::string_view executable_postfix
Definition: filesystem.hpp:102
constexpr std::string_view object_file_postfix
Definition: filesystem.hpp:106
fs::path make_dynamic_library_name(const fs::path &path)
Definition: filesystem.hpp:114
constexpr std::string_view dynamic_library_postfix
Definition: filesystem.hpp:104
fs::path make_static_library_name(const fs::path &path)
Definition: filesystem.hpp:124
fs::path make_executable_name(const fs::path &path)
Definition: filesystem.hpp:109
Definition: filesystem.hpp:130
std::size_t operator()(const fs::path &path) const
Definition: filesystem.hpp:131