mirror of
https://github.com/intel/compute-runtime.git
synced 2026-01-09 22:43:00 +08:00
moving ocloc to shared folder
Change-Id: Ic739cf747f7e6fae2c0cd57f0bc1aa0899d3aa53
This commit is contained in:
150
shared/offline_compiler/source/ocloc_arg_helper.cpp
Normal file
150
shared/offline_compiler/source/ocloc_arg_helper.cpp
Normal file
@@ -0,0 +1,150 @@
|
||||
/*
|
||||
* Copyright (C) 2020 Intel Corporation
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*
|
||||
*/
|
||||
|
||||
#include "ocloc_arg_helper.h"
|
||||
|
||||
#include "shared/source/helpers/file_io.h"
|
||||
#include "shared/source/helpers/string.h"
|
||||
|
||||
#include "decoder/helper.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <sstream>
|
||||
void Source::toVectorOfStrings(std::vector<std::string> &lines, bool replaceTabs) {
|
||||
std::string line;
|
||||
const char *file = reinterpret_cast<const char *>(data);
|
||||
|
||||
while (*file != '\0') {
|
||||
if (replaceTabs && *file == '\t') {
|
||||
line += ' ';
|
||||
} else if (*file == '\n') {
|
||||
lines.push_back(line);
|
||||
line = "";
|
||||
} else {
|
||||
line += *file;
|
||||
}
|
||||
file++;
|
||||
}
|
||||
}
|
||||
|
||||
Output::Output(const std::string &name, const void *data, const size_t &size)
|
||||
: name(name), size(size) {
|
||||
this->data = new uint8_t[size];
|
||||
memcpy_s(reinterpret_cast<void *>(this->data), this->size, data, size);
|
||||
};
|
||||
|
||||
OclocArgHelper::OclocArgHelper(const uint32_t numSources, const uint8_t **dataSources,
|
||||
const uint64_t *lenSources, const char **nameSources,
|
||||
const uint32_t numInputHeaders,
|
||||
const uint8_t **dataInputHeaders,
|
||||
const uint64_t *lenInputHeaders, const char **nameInputHeaders,
|
||||
uint32_t *numOutputs, uint8_t ***dataOutputs,
|
||||
uint64_t **lenOutputs, char ***nameOutputs)
|
||||
: numOutputs(numOutputs), nameOutputs(nameOutputs),
|
||||
dataOutputs(dataOutputs), lenOutputs(lenOutputs), hasOutput(numOutputs != nullptr) {
|
||||
for (uint32_t i = 0; i < numSources; ++i) {
|
||||
inputs.push_back(Source(dataSources[i], lenSources[i], nameSources[i]));
|
||||
}
|
||||
for (uint32_t i = 0; i < numInputHeaders; ++i) {
|
||||
headers.push_back(Source(dataInputHeaders[i], lenInputHeaders[i], nameInputHeaders[i]));
|
||||
}
|
||||
}
|
||||
|
||||
OclocArgHelper::~OclocArgHelper() {
|
||||
if (outputEnabled()) {
|
||||
moveOutputs();
|
||||
}
|
||||
}
|
||||
|
||||
bool OclocArgHelper::fileExists(const std::string &filename) const {
|
||||
return sourceFileExists(filename) || ::fileExists(filename);
|
||||
}
|
||||
|
||||
void OclocArgHelper::moveOutputs() {
|
||||
*numOutputs = static_cast<uint32_t>(outputs.size());
|
||||
*nameOutputs = new char *[outputs.size()];
|
||||
*dataOutputs = new uint8_t *[outputs.size()];
|
||||
*lenOutputs = new uint64_t[outputs.size()];
|
||||
for (size_t i = 0; i < outputs.size(); ++i) {
|
||||
(*nameOutputs)[i] = new char[outputs[i]->name.length() + 1];
|
||||
strncpy((*nameOutputs)[i], outputs[i]->name.c_str(), outputs[i]->name.length() + 1);
|
||||
(*dataOutputs)[i] = outputs[i]->data;
|
||||
(*lenOutputs)[i] = outputs[i]->size;
|
||||
}
|
||||
}
|
||||
|
||||
Source *OclocArgHelper::findSourceFile(const std::string &filename) {
|
||||
for (auto &source : inputs) {
|
||||
if (filename == source.name) {
|
||||
return &source;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool OclocArgHelper::sourceFileExists(const std::string &filename) const {
|
||||
for (auto &input : inputs) {
|
||||
if (filename == input.name) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
std::vector<std::string> OclocArgHelper::headersToVectorOfStrings() {
|
||||
std::vector<std::string> lines;
|
||||
for (auto &header : headers) {
|
||||
header.toVectorOfStrings(lines, true);
|
||||
}
|
||||
return lines;
|
||||
}
|
||||
|
||||
void OclocArgHelper::readFileToVectorOfStrings(const std::string &filename, std::vector<std::string> &lines) {
|
||||
if (Source *s = findSourceFile(filename)) {
|
||||
s->toVectorOfStrings(lines);
|
||||
} else {
|
||||
::readFileToVectorOfStrings(lines, filename);
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<char> OclocArgHelper::readBinaryFile(const std::string &filename) {
|
||||
if (Source *s = findSourceFile(filename)) {
|
||||
return s->toBinaryVector();
|
||||
} else {
|
||||
return ::readBinaryFile(filename);
|
||||
}
|
||||
}
|
||||
|
||||
std::unique_ptr<char[]> OclocArgHelper::loadDataFromFile(const std::string &filename, size_t &retSize) {
|
||||
if (Source *s = findSourceFile(filename)) {
|
||||
std::unique_ptr<char[]> ret(new char[static_cast<unsigned int>(s->length)]());
|
||||
memcpy(ret.get(), s->data, static_cast<size_t>(s->length));
|
||||
retSize = static_cast<size_t>(s->length);
|
||||
return ret;
|
||||
} else {
|
||||
return ::loadDataFromFile(filename.c_str(), retSize);
|
||||
}
|
||||
}
|
||||
|
||||
void OclocArgHelper::saveOutput(const std::string &filename, const void *pData, const size_t &dataSize) {
|
||||
if (outputEnabled()) {
|
||||
addOutput(filename, pData, dataSize);
|
||||
} else {
|
||||
writeDataToFile(filename.c_str(), pData, dataSize);
|
||||
}
|
||||
}
|
||||
|
||||
void OclocArgHelper::saveOutput(const std::string &filename, std::ostream &stream) {
|
||||
std::stringstream ss;
|
||||
ss << stream.rdbuf();
|
||||
if (outputEnabled()) {
|
||||
addOutput(filename, ss.str().c_str(), ss.str().length());
|
||||
} else {
|
||||
std::ofstream file(filename);
|
||||
file << ss.str();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user