Skip to content

Commit b6ec178

Browse files
Add quotes to options in cmdline printed by ocloc
When ocloc prints the command it was called with, enclose the -options and -internal_options with quotes. This allows easier copy-paste of the cmdline. Related-To: NEO-6002 Signed-off-by: Dominik Dabek <[email protected]>
1 parent f4c151c commit b6ec178

File tree

4 files changed

+69
-2
lines changed

4 files changed

+69
-2
lines changed

opencl/test/unit_test/offline_compiler/ocloc_api_tests.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "gtest/gtest.h"
1616
#include "hw_cmds.h"
1717

18+
#include <algorithm>
1819
#include <string>
1920

2021
extern Environment *gEnvironment;
@@ -181,6 +182,58 @@ TEST(OclocApiTests, WhenArgsWithMissingFileAreGivenThenErrorMessageIsProduced) {
181182
EXPECT_NE(std::string::npos, output.find("Command was: ocloc -q -file test_files/IDoNotExist.cl -device "s + argv[5]));
182183
}
183184

185+
TEST(OfflineCompilerTest, givenInputOptionsAndInternalOptionsWhenCmdlineIsPrintedThenBothAreInQuotes) {
186+
const char *argv[] = {
187+
"ocloc",
188+
"-q",
189+
"-file",
190+
"test_files/IDoNotExist.cl",
191+
"-device",
192+
gEnvironment->devicePrefix.c_str(),
193+
"-options", "-D DEBUG -cl-kernel-arg-info", "-internal_options", "-internalOption1 -internal-option-2"};
194+
unsigned int argc = sizeof(argv) / sizeof(const char *);
195+
196+
testing::internal::CaptureStdout();
197+
int retVal = oclocInvoke(argc, argv,
198+
0, nullptr, nullptr, nullptr,
199+
0, nullptr, nullptr, nullptr,
200+
nullptr, nullptr, nullptr, nullptr);
201+
std::string output = testing::internal::GetCapturedStdout();
202+
EXPECT_NE(retVal, NEO::OfflineCompiler::ErrorCode::SUCCESS);
203+
EXPECT_TRUE(output.find("Command was: ocloc -q -file test_files/IDoNotExist.cl -device "s +
204+
gEnvironment->devicePrefix.c_str() +
205+
" -options \"-D DEBUG -cl-kernel-arg-info\" -internal_options \"-internalOption1 -internal-option-2\"") != std::string::npos);
206+
207+
size_t quotesCount = std::count(output.begin(), output.end(), '\"');
208+
EXPECT_EQ(quotesCount, 4u);
209+
}
210+
211+
TEST(OfflineCompilerTest, givenInputOptionsCalledOptionsWhenCmdlineIsPrintedThenQuotesAreCorrect) {
212+
const char *argv[] = {
213+
"ocloc",
214+
"-q",
215+
"-file",
216+
"test_files/IDoNotExist.cl",
217+
"-device",
218+
gEnvironment->devicePrefix.c_str(),
219+
"-options", "-options", "-internal_options", "-internalOption"};
220+
unsigned int argc = sizeof(argv) / sizeof(const char *);
221+
222+
testing::internal::CaptureStdout();
223+
int retVal = oclocInvoke(argc, argv,
224+
0, nullptr, nullptr, nullptr,
225+
0, nullptr, nullptr, nullptr,
226+
nullptr, nullptr, nullptr, nullptr);
227+
std::string output = testing::internal::GetCapturedStdout();
228+
EXPECT_NE(retVal, NEO::OfflineCompiler::ErrorCode::SUCCESS);
229+
EXPECT_TRUE(output.find("Command was: ocloc -q -file test_files/IDoNotExist.cl -device "s +
230+
gEnvironment->devicePrefix.c_str() +
231+
" -options \"-options\" -internal_options \"-internalOption\"") != std::string::npos);
232+
233+
size_t quotesCount = std::count(output.begin(), output.end(), '\"');
234+
EXPECT_EQ(quotesCount, 4u);
235+
}
236+
184237
TEST(OclocApiTests, GivenIncludeHeadersWhenCompilingThenPassesToFclHeadersPackedAsElf) {
185238
auto prevFclDebugVars = NEO::getFclDebugVars();
186239
auto debugVars = prevFclDebugVars;

shared/offline_compiler/source/ocloc_api.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,17 @@ Default command (when none provided) is 'compile'.
6464
extern "C" {
6565
void printOclocCmdLine(unsigned int numArgs, const char *argv[], std::unique_ptr<OclocArgHelper> &helper) {
6666
printf("Command was:");
67-
for (auto i = 0u; i < numArgs; ++i)
68-
printf(" %s", argv[i]);
67+
bool useQuotes = false;
68+
for (auto i = 0u; i < numArgs; ++i) {
69+
const char *currArg = argv[i];
70+
if (useQuotes) {
71+
printf(" \"%s\"", currArg);
72+
useQuotes = false;
73+
} else {
74+
printf(" %s", currArg);
75+
useQuotes = helper->areQuotesRequired(currArg);
76+
}
77+
}
6978
printf("\n");
7079
}
7180

shared/offline_compiler/source/ocloc_arg_helper.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,10 @@ unsigned int OclocArgHelper::returnIGFXforGen(const std::string &device) {
346346
return it->second;
347347
}
348348

349+
bool OclocArgHelper::areQuotesRequired(const std::string_view &argName) {
350+
return argName == "-options" || argName == "-internal_options";
351+
}
352+
349353
PRODUCT_CONFIG OclocArgHelper::findConfigMatch(const std::string &device, bool firstAppearance) {
350354
auto numeration = getMajorMinorRevision(device);
351355
if (numeration.empty()) {

shared/offline_compiler/source/ocloc_arg_helper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,4 +147,5 @@ class OclocArgHelper {
147147
std::string returnProductNameForDevice(unsigned short deviceId);
148148
bool isGen(const std::string &device);
149149
unsigned int returnIGFXforGen(const std::string &device);
150+
bool areQuotesRequired(const std::string_view &argName);
150151
};

0 commit comments

Comments
 (0)