* Copyright (c) 2026 Huawei Technologies Co., Ltd.
* This program is free software, you can redistribute it and/or modify it under the terms and conditions of
* CANN Open Software License Agreement Version 2.0 (the "License").
* Please refer to the License for details. You may not use this file except in compliance with the License.
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
* See LICENSE in the root of the software repository for the full text of the License.
*/
#include "common_gen_utils.h"
#include <sstream>
#include <fstream>
#include <algorithm>
namespace att {
namespace test {
std::string RemoveAutoFuseTilingHeadGuards(const std::string &input) {
std::istringstream iss(input);
std::ostringstream oss;
std::string line;
const std::string guard_token = "__AUTOFUSE_TILING_FUNC_COMMON_H__";
while (std::getline(iss, line)) {
if (line.find(guard_token) == std::string::npos) {
oss << line << "\n";
}
}
return oss.str();
}
void CombineTilings(const std::map<std::string, std::string> &tilings, std::string &result) {
const std::string tiling_head = "TilingHead";
const std::string tiling_data = "TilingData";
result += RemoveAutoFuseTilingHeadGuards(tilings.at(tiling_head));
const std::string include_str = "#include \"autofuse_tiling_func_common.h\"";
for (const auto &[key, value] : tilings) {
if (key == tiling_head || key.find(tiling_data) != std::string::npos) {
continue;
}
size_t include_pos = value.find(include_str);
if (include_pos != std::string::npos) {
size_t content_start = include_pos + include_str.length();
while (content_start < value.size() && (value[content_start] == '\n' || value[content_start] == '\r')) {
content_start++;
}
result += value.substr(content_start);
} else {
result += value;
}
if (!result.empty() && result.back() != '\n') {
result += '\n';
}
}
}
void AddHeaderGuardToFile(const std::string &file_name, const std::string ¯o_name) {
std::string content;
std::ifstream in_file(file_name);
if (in_file.is_open()) {
std::string line;
while (std::getline(in_file, line)) {
content += line + "\n";
}
in_file.close();
}
std::ofstream out_file;
out_file.open(file_name, std::ios::out);
out_file << "#ifndef " << macro_name << "\n";
out_file << "#define " << macro_name << "\n";
out_file << "\n";
out_file << content;
out_file << "\n";
out_file << "#endif // " << macro_name << "\n";
out_file.close();
}
}
}