Add reading internal options from file to cloc

- if no options are passed try to read options
and internal options from text files.
- refactor code for reading from file.

Change-Id: I608c70f3afe77a4e4845fe1c96cc9d31464c6122
This commit is contained in:
Hoppe, Mateusz 2018-03-27 16:41:18 +02:00 committed by sys_ocldev
parent a02c3cb781
commit 320f456816
6 changed files with 132 additions and 25 deletions

View File

@ -290,30 +290,23 @@ int OfflineCompiler::initialize(uint32_t numArgs, const char **argv) {
if (options.empty()) {
// try to read options from file if not provided by commandline
std::string optionsFileName = inputFile;
size_t ext_start = optionsFileName.find(".cl");
size_t ext_start = inputFile.find(".cl");
if (ext_start != std::string::npos) {
optionsFileName.replace(ext_start, strlen(".cl"), "_options.txt");
void *pOptions = nullptr;
size_t optionsSize = loadDataFromFile(optionsFileName.c_str(), pOptions);
if (optionsSize > 0) {
options = (char *)pOptions;
// Remove comment containing copyright header
size_t commentBegin = options.find_first_of("/*");
size_t commentEnd = options.find_last_of("*/");
if (commentBegin != std::string::npos && commentEnd != std::string::npos) {
options = options.replace(commentBegin, commentEnd - commentBegin + 1, "");
size_t optionsBegin = options.find_first_not_of(" \t\n\r");
if (optionsBegin != std::string::npos) {
options = options.substr(optionsBegin, options.length());
}
}
auto trimPos = options.find_last_not_of(" \n\r");
options = options.substr(0, trimPos + 1);
if (!isQuiet())
printf("Building with options:\n%s\n", options.c_str());
std::string optionsFileName = inputFile.substr(0, ext_start);
optionsFileName.append("_options.txt");
bool optionsRead = readOptionsFromFile(options, optionsFileName);
if (optionsRead && !isQuiet()) {
printf("Building with options:\n%s\n", options.c_str());
}
std::string internalOptionsFileName = inputFile.substr(0, ext_start);
internalOptionsFileName.append("_internal_options.txt");
bool internalOptionsRead = readOptionsFromFile(internalOptions, internalOptionsFileName);
if (internalOptionsRead && !isQuiet()) {
printf("Building with internal options:\n%s\n", internalOptions.c_str());
}
deleteDataReadFromFile(pOptions);
}
}
@ -814,4 +807,30 @@ void OfflineCompiler::writeOutAllFiles() {
elfBinarySize);
}
}
bool OfflineCompiler::readOptionsFromFile(std::string &options, const std::string &file) {
if (!fileExists(file)) {
return false;
}
void *pOptions = nullptr;
size_t optionsSize = loadDataFromFile(file.c_str(), pOptions);
if (optionsSize > 0) {
// Remove comment containing copyright header
options = (char *)pOptions;
size_t commentBegin = options.find_first_of("/*");
size_t commentEnd = options.find_last_of("*/");
if (commentBegin != std::string::npos && commentEnd != std::string::npos) {
options = options.replace(commentBegin, commentEnd - commentBegin + 1, "");
size_t optionsBegin = options.find_first_not_of(" \t\n\r");
if (optionsBegin != std::string::npos) {
options = options.substr(optionsBegin, options.length());
}
}
auto trimPos = options.find_last_not_of(" \n\r");
options = options.substr(0, trimPos + 1);
}
deleteDataReadFromFile(pOptions);
return true;
}
} // namespace OCLRT

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, Intel Corporation
* Copyright (c) 2017 - 2018, Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
@ -57,6 +57,7 @@ class OfflineCompiler {
}
std::string parseBinAsCharArray(uint8_t *binary, size_t size, std::string &deviceName, std::string &fileName);
static bool readOptionsFromFile(std::string &optionsOut, const std::string &file);
protected:
OfflineCompiler();

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, Intel Corporation
* Copyright (c) 2017 - 2018, Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
@ -46,6 +46,10 @@ class MockOfflineCompiler : public OfflineCompiler {
return OfflineCompiler::parseDebugSettings();
}
std::string &getOptions() {
return options;
}
std::string &getInternalOptions() {
return internalOptions;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, Intel Corporation
* Copyright (c) 2017 - 2018, Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
@ -652,4 +652,43 @@ TEST(OfflineCompilerTest, givenInternalOptionsWhenCmdLineParsedThenOptionsAreApp
EXPECT_THAT(internalOptions, ::testing::HasSubstr(std::string("myInternalOptions")));
}
TEST(OfflineCompilerTest, givenInputOtpionsAndInternalOptionsFilesWhenOfflineCompilerIsInitializedThenCorrectOptionsAreSetAndRemainAfterBuild) {
auto mockOfflineCompiler = std::unique_ptr<MockOfflineCompiler>(new MockOfflineCompiler());
ASSERT_NE(nullptr, mockOfflineCompiler);
ASSERT_TRUE(fileExists("test_files/shouldfail_options.txt"));
ASSERT_TRUE(fileExists("test_files/shouldfail_internal_options.txt"));
const char *argv[] = {
"cloc",
"-q",
"-file",
"test_files/shouldfail.cl",
"-device",
gEnvironment->devicePrefix.c_str()};
int retVal = mockOfflineCompiler->initialize(ARRAY_COUNT(argv), argv);
EXPECT_EQ(CL_SUCCESS, retVal);
auto &options = mockOfflineCompiler->getOptions();
auto &internalOptions = mockOfflineCompiler->getInternalOptions();
EXPECT_STREQ(options.c_str(), "-shouldfailOptions");
EXPECT_STREQ(internalOptions.c_str(), "-shouldfailInternalOptions");
mockOfflineCompiler->build();
EXPECT_STREQ(options.c_str(), "-shouldfailOptions");
EXPECT_STREQ(internalOptions.c_str(), "-shouldfailInternalOptions");
}
TEST(OfflineCompilerTest, givenNonExistingFilenameWhenUsedToReadOptionsThenReadOptionsFromFileReturnsFalse) {
std::string options;
std::string file("non_existing_file");
ASSERT_FALSE(fileExists(file.c_str()));
bool result = OfflineCompiler::readOptionsFromFile(options, file);
EXPECT_FALSE(result);
}
} // namespace OCLRT

View File

@ -0,0 +1,22 @@
/*
* Copyright (c) 2018, Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
-shouldfailInternalOptions

View File

@ -0,0 +1,22 @@
/*
* Copyright (c) 2018, Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
-shouldfailOptions