Skip to content

Commit a699897

Browse files
authored
[Windows] addkernels: fix operations on path for Windows (#2657)
1 parent 72f5f8b commit a699897

File tree

4 files changed

+41
-83
lines changed

4 files changed

+41
-83
lines changed

addkernels/addkernels.cpp

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
*******************************************************************************/
2626
#include "include_inliner.hpp"
2727
#include <algorithm>
28+
#include <filesystem>
2829
#include <fstream>
2930
#include <iomanip>
3031
#include <iostream>
@@ -124,52 +125,41 @@ void PrintHelp()
124125
WrongUsage(ss.str());
125126
}
126127

127-
void Process(const std::string& sourcePath,
128+
void Process(const std::filesystem::path& sourcePath,
128129
std::ostream& target,
129130
size_t bufferSize,
130131
size_t lineSize,
131132
bool recurse,
132133
bool as_extern,
133134
bool mark_includes)
134135
{
135-
std::string fileName(sourcePath);
136-
std::string extension, root;
137-
std::stringstream inlinerTemp;
138-
auto extPos = fileName.rfind('.');
139-
auto slashPos = fileName.rfind('/');
140-
141-
if(extPos != std::string::npos)
142-
{
143-
extension = fileName.substr(extPos + 1);
144-
fileName = fileName.substr(0, extPos);
145-
}
146-
147-
if(slashPos != std::string::npos)
136+
if(!std::filesystem::exists(sourcePath))
148137
{
149-
root = fileName.substr(0, slashPos + 1);
150-
fileName = fileName.substr(slashPos + 1);
138+
std::cerr << "File not found: " << sourcePath << std::endl;
139+
// NOLINTNEXTLINE (concurrency-mt-unsafe)
140+
std::exit(1);
151141
}
152142

153-
std::string variable(fileName);
154-
std::ifstream sourceFile(sourcePath, std::ios::in | std::ios::binary);
143+
std::filesystem::path root{sourcePath.has_parent_path() ? sourcePath.parent_path() : ""};
144+
std::ifstream sourceFile{sourcePath, std::ios::in | std::ios::binary};
155145
std::istream* source = &sourceFile;
156146

157-
if(!sourceFile.good())
147+
if(!sourceFile.is_open())
158148
{
159-
std::cerr << "File not found: " << sourcePath << std::endl;
149+
std::cerr << "Error opening file: " << sourcePath << std::endl;
160150
// NOLINTNEXTLINE (concurrency-mt-unsafe)
161151
std::exit(1);
162152
}
163153

164-
const auto is_asm = extension == "s";
165-
const auto is_cl = extension == "cl";
166-
const auto is_hip = extension == "cpp";
167-
const auto is_header = extension == "hpp";
154+
const auto is_asm = sourcePath.extension() == ".s";
155+
const auto is_cl = sourcePath.extension() == ".cl";
156+
const auto is_hip = sourcePath.extension() == ".cpp";
157+
const auto is_header = sourcePath.extension() == ".hpp";
168158

159+
std::stringstream inlinerTemp;
169160
if(is_asm || is_cl || is_hip || is_header)
170161
{
171162
IncludeInliner inliner;
172-
173163
try
174164
{
175165
if(is_asm)
@@ -190,15 +180,14 @@ void Process(const std::string& sourcePath,
190180
}
191181
catch(const InlineException& ex)
192182
{
193-
std::cerr << ex.What() << std::endl;
194-
std::cerr << ex.GetTrace() << std::endl;
183+
std::cerr << ex.What() << '\n' << ex.GetTrace() << std::endl;
195184
// NOLINTNEXTLINE (concurrency-mt-unsafe)
196185
std::exit(1);
197186
}
198-
199187
source = &inlinerTemp;
200188
}
201189

190+
auto variable{sourcePath.stem().string()};
202191
std::transform(variable.begin(), variable.end(), variable.begin(), ::toupper);
203192

204193
if(mark_includes)

addkernels/include_inliner.cpp

Lines changed: 9 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -24,48 +24,11 @@
2424
*
2525
*******************************************************************************/
2626
#include <algorithm>
27-
#include <exception>
2827
#include <fstream>
2928
#include <sstream>
3029

31-
#ifdef _WIN32
32-
#include <windows.h>
33-
#endif
34-
#ifdef __linux__
35-
#include <linux/limits.h>
36-
#include <cstdlib>
37-
#endif // !WIN32
38-
3930
#include "include_inliner.hpp"
4031

41-
namespace {
42-
int GetMaxPath()
43-
{
44-
#ifdef _WIN32
45-
return MAX_PATH;
46-
#else
47-
return PATH_MAX;
48-
#endif
49-
}
50-
51-
std::string GetAbsolutePath(const std::string& path)
52-
{
53-
std::string result(GetMaxPath(), ' ');
54-
#ifdef _WIN32
55-
const auto retval = GetFullPathName(path.c_str(), result.size(), &result[0], nullptr);
56-
57-
if(retval == 0)
58-
return "";
59-
#else
60-
auto* const retval = realpath(path.c_str(), &result[0]);
61-
62-
if(retval == nullptr)
63-
return "";
64-
#endif
65-
return result;
66-
}
67-
} // namespace
68-
6932
std::string IncludeFileExceptionBase::What() const
7033
{
7134
std::ostringstream ss;
@@ -76,8 +39,8 @@ std::string IncludeFileExceptionBase::What() const
7639

7740
void IncludeInliner::Process(std::istream& input,
7841
std::ostream& output,
79-
const std::string& root,
80-
const std::string& file_name,
42+
const std::filesystem::path& root,
43+
const std::filesystem::path& file_name,
8144
const std::string& directive,
8245
bool allow_angle_brackets,
8346
bool recurse)
@@ -87,8 +50,8 @@ void IncludeInliner::Process(std::istream& input,
8750

8851
void IncludeInliner::ProcessCore(std::istream& input,
8952
std::ostream& output,
90-
const std::string& root,
91-
const std::string& file_name,
53+
const std::filesystem::path& root,
54+
const std::filesystem::path& file_name,
9255
int line_number,
9356
const std::string& directive,
9457
bool allow_angle_brackets,
@@ -151,10 +114,11 @@ void IncludeInliner::ProcessCore(std::istream& input,
151114

152115
const std::string include_file_path =
153116
line.substr(first_quote_pos + 1, second_quote_pos - first_quote_pos - 1);
154-
const std::string abs_include_file_path(
155-
GetAbsolutePath(root + "/" + include_file_path)); // NOLINT
156117

157-
if(abs_include_file_path.empty())
118+
const auto abs_include_file_path{
119+
std::filesystem::weakly_canonical(root / include_file_path)};
120+
121+
if(!std::filesystem::exists(abs_include_file_path))
158122
{
159123
if(include_optional)
160124
continue;
@@ -163,7 +127,7 @@ void IncludeInliner::ProcessCore(std::istream& input,
163127
}
164128
std::ifstream include_file(abs_include_file_path, std::ios::in);
165129

166-
if(!include_file.good())
130+
if(!include_file.is_open())
167131
{
168132
throw IncludeCantBeOpenedException(include_file_path,
169133
GetIncludeStackTrace(current_line));

addkernels/include_inliner.hpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@
2424
*
2525
*******************************************************************************/
2626
#ifndef SOURCE_INLINER_HPP
27-
2827
#define SOURCE_INLINER_HPP
28+
2929
#include "source_file_desc.hpp"
30-
#include <ostream>
30+
#include <exception>
31+
#include <filesystem>
3132
#include <memory>
33+
#include <ostream>
3234
#include <stack>
3335

3436
class InlineException : public std::exception
@@ -118,8 +120,8 @@ class IncludeInliner
118120

119121
void Process(std::istream& input,
120122
std::ostream& output,
121-
const std::string& root,
122-
const std::string& file_name,
123+
const std::filesystem::path& root,
124+
const std::filesystem::path& file_name,
123125
const std::string& directive,
124126
bool allow_angle_brackets,
125127
bool recurse);
@@ -131,8 +133,8 @@ class IncludeInliner
131133

132134
void ProcessCore(std::istream& input,
133135
std::ostream& output,
134-
const std::string& root,
135-
const std::string& file_name,
136+
const std::filesystem::path& root,
137+
const std::filesystem::path& file_name,
136138
int line_number,
137139
const std::string& directive,
138140
bool allow_angle_brackets,

addkernels/source_file_desc.hpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,22 @@
2424
*
2525
*******************************************************************************/
2626
#ifndef SOURCE_FILE_DESC_HPP
27-
2827
#define SOURCE_FILE_DESC_HPP
29-
#include <string>
28+
29+
#include <filesystem>
3030
#include <memory>
31+
#include <string>
3132

3233
class SourceFileDesc
3334
{
3435
public:
35-
std::string path;
36+
std::filesystem::path path;
3637
int included_line;
3738
std::shared_ptr<SourceFileDesc> included_from;
3839

39-
SourceFileDesc(const std::string& path_, std::shared_ptr<SourceFileDesc> from, int line)
40+
SourceFileDesc(const std::filesystem::path& path_,
41+
std::shared_ptr<SourceFileDesc> from,
42+
int line)
4043
: path(path_), included_line(line), included_from(from)
4144
{
4245
}

0 commit comments

Comments
 (0)