|
| 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 |
0 commit comments