Skip to content

Commit ce8d24d

Browse files
mnaczkCompute-Runtime-Automation
authored andcommitted
Add multi command line option for ocloc
- now ocloc is able to make multi build, all options and parameters of build are listed in separate .txt file, each line in this file is new build. Change-Id: Id74af826e8c1a4fe14c46ed6024efe2041a22fd0 Signed-off-by: Marcin Naczk <[email protected]>
1 parent 2d02435 commit ce8d24d

File tree

10 files changed

+582
-78
lines changed

10 files changed

+582
-78
lines changed

offline_compiler/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ ${IGDRCL_SOURCE_DIR}/offline_compiler/decoder/helper.h
1616
${IGDRCL_SOURCE_DIR}/offline_compiler/helper.cpp
1717
${IGDRCL_SOURCE_DIR}/offline_compiler/offline_compiler.cpp
1818
${IGDRCL_SOURCE_DIR}/offline_compiler/offline_compiler.h
19+
${IGDRCL_SOURCE_DIR}/offline_compiler/multi_command.cpp
20+
${IGDRCL_SOURCE_DIR}/offline_compiler/multi_command.h
1921
${IGDRCL_SOURCE_DIR}/offline_compiler/options.cpp
2022
${IGDRCL_SOURCE_DIR}/runtime/compiler_interface/create_main.cpp
2123
${IGDRCL_SOURCE_DIR}/runtime/helpers/abort.cpp

offline_compiler/main.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@
55
*
66
*/
77

8+
#include "offline_compiler/multi_command.h"
89
#include "offline_compiler/offline_compiler.h"
9-
#include "offline_compiler/utilities/safety_caller.h"
1010
#include "runtime/os_interface/os_library.h"
1111

1212
#include "decoder/binary_decoder.h"
1313
#include "decoder/binary_encoder.h"
1414
#include <CL/cl.h>
1515

16+
#include <fstream>
17+
#include <iostream>
18+
1619
using namespace NEO;
1720

1821
int main(int numArgs, const char *argv[]) {
@@ -33,9 +36,20 @@ int main(int numArgs, const char *argv[]) {
3336
} else {
3437
return retVal;
3538
}
39+
} else if (numArgs > 1 && !strcmp(argv[1], "-multi")) {
40+
int retValue = CL_SUCCESS;
41+
42+
auto pMulti = std::unique_ptr<MultiCommand>(MultiCommand::create(numArgs, argv, retValue));
43+
44+
return retValue;
3645
} else {
3746
int retVal = CL_SUCCESS;
38-
OfflineCompiler *pCompiler = OfflineCompiler::create(numArgs, argv, retVal);
47+
std::vector<std::string> allArgs;
48+
if (numArgs > 1) {
49+
allArgs.assign(argv, argv + numArgs);
50+
}
51+
52+
OfflineCompiler *pCompiler = OfflineCompiler::create(numArgs, allArgs, retVal);
3953

4054
if (retVal == CL_SUCCESS) {
4155
retVal = buildWithSafetyGuard(pCompiler);

offline_compiler/multi_command.cpp

Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
/*
2+
* Copyright (C) 2019 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: MIT
5+
*
6+
*/
7+
8+
#include "offline_compiler/multi_command.h"
9+
10+
namespace NEO {
11+
int MultiCommand::singleBuild(size_t numArgs, const std::vector<std::string> &allArgs) {
12+
int retVal;
13+
std::string buildLog;
14+
OfflineCompiler *pCompiler = OfflineCompiler::create(numArgs, allArgs, retVal);
15+
if (retVal == CL_SUCCESS) {
16+
retVal = buildWithSafetyGuard(pCompiler);
17+
18+
buildLog = pCompiler->getBuildLog();
19+
if (buildLog.empty() == false) {
20+
printf("%s\n", buildLog.c_str());
21+
}
22+
23+
if (retVal == CL_SUCCESS) {
24+
if (!pCompiler->isQuiet())
25+
printf("Build succeeded.\n");
26+
} else {
27+
printf("Build failed with error code: %d\n", retVal);
28+
}
29+
}
30+
if (buildLog.empty() == false) {
31+
singleBuilds.push_back(pCompiler);
32+
} else {
33+
delete pCompiler;
34+
}
35+
return retVal;
36+
}
37+
38+
MultiCommand::MultiCommand() = default;
39+
40+
MultiCommand::~MultiCommand() {
41+
deleteBuildsWithWarnigs();
42+
}
43+
44+
void MultiCommand::deleteBuildsWithWarnigs() {
45+
for (OfflineCompiler *pSingle : singleBuilds)
46+
delete pSingle;
47+
singleBuilds.clear();
48+
}
49+
50+
MultiCommand *MultiCommand::create(int numArgs, const char *argv[], int &retVal) {
51+
retVal = CL_SUCCESS;
52+
auto pMultiCommand = new MultiCommand();
53+
54+
if (pMultiCommand) {
55+
retVal = pMultiCommand->initialize(numArgs, argv);
56+
}
57+
58+
if (retVal != CL_SUCCESS) {
59+
delete pMultiCommand;
60+
pMultiCommand = nullptr;
61+
}
62+
63+
return pMultiCommand;
64+
}
65+
66+
std::string MultiCommand::eraseExtensionFromPath(std::string &filePath) {
67+
size_t extPos = filePath.find_last_of(".", filePath.size());
68+
if (extPos == std::string::npos) {
69+
extPos = filePath.size();
70+
}
71+
std::string fileName;
72+
std::string fileTrunk = filePath.substr(0, extPos);
73+
74+
return fileTrunk;
75+
}
76+
77+
void MultiCommand::addAdditionalOptionsToSingleCommandLine(std::vector<std::string> &singleLineWithArguments, int buildId) {
78+
std::string OutFileName;
79+
bool hasOutDir = false;
80+
bool hasSpecificName = false;
81+
for (auto arg : singleLineWithArguments) {
82+
if (arg == "-out_dir") {
83+
hasOutDir = true;
84+
}
85+
if (arg == "-output") {
86+
hasSpecificName = true;
87+
}
88+
}
89+
if (!hasOutDir) {
90+
singleLineWithArguments.push_back("-out_dir");
91+
OutDirForBuilds = eraseExtensionFromPath(pathToCMD);
92+
singleLineWithArguments.push_back(OutDirForBuilds);
93+
}
94+
if (!hasSpecificName) {
95+
singleLineWithArguments.push_back("-output");
96+
OutFileName = "build_no_" + std::to_string(buildId + 1);
97+
singleLineWithArguments.push_back(OutFileName);
98+
}
99+
if (quiet)
100+
singleLineWithArguments.push_back("-q");
101+
}
102+
103+
int MultiCommand::initialize(int numArgs, const char *argv[]) {
104+
int retVal = CL_SUCCESS;
105+
106+
if (numArgs > 2)
107+
pathToCMD = argv[2];
108+
else {
109+
printf("Lack of file with build arguments\n");
110+
return INVALID_COMMAND_LINE;
111+
}
112+
if (numArgs > 3 && strcmp(argv[3], "-q") == 0)
113+
quiet = true;
114+
115+
//save file with builds arguments to vector of strings, line by line
116+
openFileWithBuildsArguments();
117+
if (!lines.empty()) {
118+
for (unsigned int i = 0; i < lines.size(); i++) {
119+
std::vector<std::string> singleLineWithArguments;
120+
unsigned int numberOfArg;
121+
122+
singleLineWithArguments.push_back(argv[0]);
123+
retVal = splitLineInSeparateArgs(singleLineWithArguments, lines[i], i);
124+
if (retVal != CL_SUCCESS) {
125+
retValues.push_back(retVal);
126+
continue;
127+
}
128+
129+
addAdditionalOptionsToSingleCommandLine(singleLineWithArguments, i);
130+
131+
numberOfArg = static_cast<unsigned int>(singleLineWithArguments.size());
132+
133+
if (!quiet)
134+
printf("\nCommand number %d: ", i + 1);
135+
retVal = singleBuild(numberOfArg, singleLineWithArguments);
136+
retValues.push_back(retVal);
137+
}
138+
139+
return showResults();
140+
} else
141+
return INVALID_COMMAND_LINE;
142+
}
143+
144+
int MultiCommand::splitLineInSeparateArgs(std::vector<std::string> &qargs, const std::string &command, int numberOfBuild) {
145+
unsigned int len = static_cast<unsigned int>(command.length());
146+
147+
bool qot = false, sqot = false;
148+
int arglen;
149+
150+
for (unsigned int i = 0; i < len; i++) {
151+
int start = i;
152+
if (command[i] == '\"') {
153+
qot = true;
154+
} else if (command[i] == '\'')
155+
sqot = true;
156+
157+
if (qot) {
158+
i++;
159+
start++;
160+
while (i < len && command[i] != '\"')
161+
i++;
162+
if (i < len)
163+
qot = false;
164+
arglen = i - start;
165+
i++;
166+
} else if (sqot) {
167+
i++;
168+
while (i < len && command[i] != '\'')
169+
i++;
170+
if (i < len)
171+
sqot = false;
172+
arglen = i - start;
173+
i++;
174+
} else {
175+
while (i < len && command[i] != ' ')
176+
i++;
177+
arglen = i - start;
178+
}
179+
qargs.push_back(command.substr(start, arglen));
180+
}
181+
if (qot || sqot) {
182+
printf("One of the quotes is open in build number %d\n", numberOfBuild + 1);
183+
return INVALID_COMMAND_LINE;
184+
}
185+
return CL_SUCCESS;
186+
}
187+
188+
void MultiCommand::openFileWithBuildsArguments() {
189+
std::fstream multiCmdFile;
190+
std::stringstream fileContent;
191+
multiCmdFile.open(pathToCMD, std::fstream::in);
192+
193+
if (multiCmdFile.is_open()) {
194+
std::string param;
195+
fileContent << multiCmdFile.rdbuf();
196+
multiCmdFile.close();
197+
while (std::getline(fileContent, param, '\n')) {
198+
param.erase(param.find_last_not_of(" \r\t") + 1);
199+
param.erase(0, param.find_first_not_of(" \r\t"));
200+
if (!param.empty()) {
201+
lines.push_back(param);
202+
}
203+
}
204+
} else {
205+
printf("Can not open file with builds arguments\n");
206+
}
207+
}
208+
209+
int MultiCommand::showResults() {
210+
int retValue = CL_SUCCESS;
211+
int indexRetVal = 0;
212+
for (int retVal : retValues) {
213+
if (retVal != CL_SUCCESS) {
214+
if (retValue == CL_SUCCESS)
215+
retValue = retVal;
216+
if (!quiet)
217+
printf("Build %d: failed. Error code: %d\n", indexRetVal, retVal);
218+
} else {
219+
if (!quiet)
220+
printf("Build %d: successful\n", indexRetVal);
221+
}
222+
indexRetVal++;
223+
}
224+
return retValue;
225+
}
226+
} // namespace NEO

offline_compiler/multi_command.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (C) 2019 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: MIT
5+
*
6+
*/
7+
8+
#include "offline_compiler/offline_compiler.h"
9+
#include "offline_compiler/utilities/safety_caller.h"
10+
#include "runtime/os_interface/os_library.h"
11+
12+
#include "decoder/binary_decoder.h"
13+
#include "decoder/binary_encoder.h"
14+
#include <CL/cl.h>
15+
16+
#include <fstream>
17+
#include <iostream>
18+
19+
namespace NEO {
20+
21+
class MultiCommand {
22+
public:
23+
static MultiCommand *create(int numArgs, const char *argv[], int &retVal);
24+
void deleteBuildsWithWarnigs();
25+
26+
std::vector<OfflineCompiler *> singleBuilds;
27+
28+
MultiCommand &operator=(const MultiCommand &) = delete;
29+
MultiCommand(const MultiCommand &) = delete;
30+
~MultiCommand();
31+
32+
std::string OutDirForBuilds;
33+
34+
protected:
35+
int splitLineInSeparateArgs(std::vector<std::string> &qargs, const std::string &command, int numberOfBuild);
36+
void openFileWithBuildsArguments();
37+
void addAdditionalOptionsToSingleCommandLine(std::vector<std::string> &, int);
38+
int initialize(int numArgs, const char *argv[]);
39+
int showResults();
40+
int singleBuild(size_t numArgs, const std::vector<std::string> &allArgs);
41+
std::string eraseExtensionFromPath(std::string &filePath);
42+
43+
std::vector<int> retValues;
44+
std::string pathToCMD;
45+
std::vector<std::string> lines;
46+
bool quiet = false;
47+
48+
MultiCommand();
49+
};
50+
} // namespace NEO

0 commit comments

Comments
 (0)