From 3dc6926765715c32f915379f57565891f00be16f Mon Sep 17 00:00:00 2001 From: zhangrengao Date: Fri, 10 Feb 2023 15:33:50 +0800 Subject: [PATCH] Fix failed to operate file of length over 260 problem on Windows of es2abc&js2abc&mergeabc Issue: I6E941 Signed-off-by: zhangrengao Change-Id: Iab32de437c2d9abd8017b3859a886a31d8177514 --- es2panda/BUILD.gn | 5 +++- es2panda/aot/main.cpp | 6 ++-- es2panda/aot/options.cpp | 3 +- es2panda/es2panda.cpp | 3 +- es2panda/util/moduleHelpers.cpp | 3 +- es2panda/util/symbolTable.cpp | 8 ++++-- merge_abc/BUILD.gn | 1 + merge_abc/src/mergeProgram.cpp | 3 +- merge_abc/src/protobufHelper.cpp | 32 +++++++++++++++++++++ merge_abc/src/protobufHelper.h | 25 ++++++++++++++++ merge_abc/src/protobufSnapshotGenerator.cpp | 26 ++++------------- ts2panda/ts2abc/ts2abc.cpp | 13 +++++---- 12 files changed, 92 insertions(+), 36 deletions(-) create mode 100644 merge_abc/src/protobufHelper.cpp create mode 100644 merge_abc/src/protobufHelper.h diff --git a/es2panda/BUILD.gn b/es2panda/BUILD.gn index 10029782aa..10eaae0fd1 100644 --- a/es2panda/BUILD.gn +++ b/es2panda/BUILD.gn @@ -381,7 +381,10 @@ ohos_static_library("es2panda_lib") { use_rtti = true } - configs = [ ":es2abc_config_common" ] + configs = [ + ":es2abc_config_common", + "$ets_frontend_root/merge_abc:panda_assembly_proto_public_config", + ] include_dirs = [ "${target_out_dir}", diff --git a/es2panda/aot/main.cpp b/es2panda/aot/main.cpp index e1ca0abdef..cc1459af84 100644 --- a/es2panda/aot/main.cpp +++ b/es2panda/aot/main.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -90,14 +91,15 @@ static bool GenerateMultiProgram(const std::unordered_mapsecond; - if (!panda::pandasm::AsmEmitter::EmitPrograms(output, progs, true)) { + if (!panda::pandasm::AsmEmitter::EmitPrograms(panda::proto::ConvertPlatFormPath(output), progs, true)) { std::cerr << "Failed to emit merged program, error: " << panda::pandasm::AsmEmitter::GetLastError() << std::endl; return false; } } else { for (auto &prog: programs) { - if (!panda::pandasm::AsmEmitter::Emit(prog.second, *(prog.first), nullptr, nullptr, true)) { + if (!panda::pandasm::AsmEmitter::Emit(panda::proto::ConvertPlatFormPath(prog.second), *(prog.first), nullptr, + nullptr, true)) { std::cout << "Failed to emit single program, error: " << panda::pandasm::AsmEmitter::GetLastError() << std::endl; return false; diff --git a/es2panda/aot/options.cpp b/es2panda/aot/options.cpp index 03796d15cb..7db94e68e9 100644 --- a/es2panda/aot/options.cpp +++ b/es2panda/aot/options.cpp @@ -17,6 +17,7 @@ #include "mergeProgram.h" #include "os/file.h" +#include #include #include #if defined(PANDA_TARGET_WINDOWS) @@ -60,7 +61,7 @@ bool Options::CollectInputFilesFromFileList(const std::string &input) { std::ifstream ifs; std::string line; - ifs.open(input.c_str()); + ifs.open(panda::proto::ConvertPlatFormPath(input.c_str())); if (!ifs.is_open()) { std::cerr << "Failed to open source list: " << input << std::endl; return false; diff --git a/es2panda/es2panda.cpp b/es2panda/es2panda.cpp index 6a488c535f..bc32cac3c3 100644 --- a/es2panda/es2panda.cpp +++ b/es2panda/es2panda.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include @@ -147,7 +148,7 @@ void Compiler::DumpAsm(const panda::pandasm::Program *prog) static bool ReadFileToBuffer(const std::string &file, std::stringstream &ss) { - std::ifstream inputStream(file, std::ios::binary); + std::ifstream inputStream(panda::proto::ConvertPlatFormPath(file), std::ios::binary); if (inputStream.fail()) { std::cerr << "Failed to read file to buffer: " << file << std::endl; return false; diff --git a/es2panda/util/moduleHelpers.cpp b/es2panda/util/moduleHelpers.cpp index 69174c9e67..f589a948a3 100644 --- a/es2panda/util/moduleHelpers.cpp +++ b/es2panda/util/moduleHelpers.cpp @@ -16,6 +16,7 @@ #include "moduleHelpers.h" #include +#include namespace panda::es2panda::util { void ModuleHelpers::CompileNpmModuleEntryList(const std::string &entriesInfo, @@ -24,7 +25,7 @@ void ModuleHelpers::CompileNpmModuleEntryList(const std::string &entriesInfo, panda::ArenaAllocator *allocator) { std::stringstream ss; - std::ifstream inputStream(entriesInfo); + std::ifstream inputStream(panda::proto::ConvertPlatFormPath(entriesInfo)); if (inputStream.fail()) { std::cerr << "Failed to read file to buffer: " << entriesInfo << std::endl; return; diff --git a/es2panda/util/symbolTable.cpp b/es2panda/util/symbolTable.cpp index 2b31ca0c8b..b233f81de3 100644 --- a/es2panda/util/symbolTable.cpp +++ b/es2panda/util/symbolTable.cpp @@ -15,6 +15,8 @@ #include "symbolTable.h" +#include + #include #include @@ -33,7 +35,7 @@ bool SymbolTable::Initialize() if (!dumpSymbolTable_.empty()) { std::fstream fs; - fs.open(dumpSymbolTable_, std::ios_base::out | std::ios_base::trunc); + fs.open(panda::proto::ConvertPlatFormPath(dumpSymbolTable_), std::ios_base::out | std::ios_base::trunc); if (!fs.is_open()) { std::cerr << "Failed to create output symbol table: " << dumpSymbolTable_ << std::endl; return false; @@ -48,7 +50,7 @@ bool SymbolTable::ReadSymbolTable(const std::string &symbolTable) { std::ifstream ifs; std::string line; - ifs.open(symbolTable.c_str()); + ifs.open(panda::proto::ConvertPlatFormPath(symbolTable.c_str())); if (!ifs.is_open()) { std::cerr << "Failed to open symbol table: " << symbolTable << std::endl; return false; @@ -92,7 +94,7 @@ void SymbolTable::WriteSymbolTable(const std::string &content) { std::lock_guard lock(m_); std::fstream fs; - fs.open(dumpSymbolTable_, std::ios_base::app | std::ios_base::in); + fs.open(panda::proto::ConvertPlatFormPath(dumpSymbolTable_), std::ios_base::app | std::ios_base::in); if (fs.is_open()) { fs << content; fs.close(); diff --git a/merge_abc/BUILD.gn b/merge_abc/BUILD.gn index d99ce82a27..406dc637fa 100644 --- a/merge_abc/BUILD.gn +++ b/merge_abc/BUILD.gn @@ -34,6 +34,7 @@ protobuf_snapshot_generator_sources = [ "src/ideHelpersProto.cpp", "src/mergeProgram.cpp", "src/metaProto.cpp", + "src/protobufHelper.cpp", "src/protobufSnapshotGenerator.cpp", ] diff --git a/merge_abc/src/mergeProgram.cpp b/merge_abc/src/mergeProgram.cpp index 3b4037d420..a7c61b4948 100644 --- a/merge_abc/src/mergeProgram.cpp +++ b/merge_abc/src/mergeProgram.cpp @@ -19,6 +19,7 @@ #include "options.h" #include "mergeProgram.h" +#include "protobufHelper.h" #if defined(PANDA_TARGET_WINDOWS) #include @@ -132,7 +133,7 @@ bool MergeProgram::CollectProtoFiles(std::string &input, const std::string &prot return false; } if (isList) { - std::ifstream in(inputAbs.Value()); + std::ifstream in(ConvertPlatFormPath(inputAbs.Value())); std::string line; constexpr const char CARRIAGE = '\r'; while (getline(in, line)) { diff --git a/merge_abc/src/protobufHelper.cpp b/merge_abc/src/protobufHelper.cpp new file mode 100644 index 0000000000..94b0ad76ad --- /dev/null +++ b/merge_abc/src/protobufHelper.cpp @@ -0,0 +1,32 @@ +/** + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "protobufHelper.h" +#include "assembly-program.h" + +namespace panda::proto { +std::string ConvertPlatFormPath(const std::string &path) +{ +#ifdef PANDA_TARGET_WINDOWS + if (path.length() < _MAX_PATH) { + return path; + } else { + return panda::os::file::File::GetExtendedLengthStylePath(path); + } +#else + return path; +#endif // PANDA_TARGET_WINDOWS +} +} // panda::proto \ No newline at end of file diff --git a/merge_abc/src/protobufHelper.h b/merge_abc/src/protobufHelper.h new file mode 100644 index 0000000000..50581eae75 --- /dev/null +++ b/merge_abc/src/protobufHelper.h @@ -0,0 +1,25 @@ +/** + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef MERGE_ABC_POROBUFHELPER_H +#define MERGE_ABC_POROBUFHELPER_H + +#include + +namespace panda::proto { +// Fix failed to operate file of length over 260 problem on Windows +std::string ConvertPlatFormPath(const std::string &path); +} // panda::proto +#endif \ No newline at end of file diff --git a/merge_abc/src/protobufSnapshotGenerator.cpp b/merge_abc/src/protobufSnapshotGenerator.cpp index fbb9e35104..6cf7101939 100644 --- a/merge_abc/src/protobufSnapshotGenerator.cpp +++ b/merge_abc/src/protobufSnapshotGenerator.cpp @@ -16,29 +16,16 @@ #include "assemblyProgramProto.h" #include "assembly-program.h" #include "protobufSnapshotGenerator.h" +#include "protobufHelper.h" namespace panda::proto { -static void GetFileStream(const std::string &path, std::ios::openmode mode, std::fstream &fs) -{ -#ifdef PANDA_TARGET_WINDOWS - if (path.length() < _MAX_PATH) { - fs = std::fstream(path, mode); - } else { - fs = std::fstream(panda::os::file::File::GetExtendedLengthStylePath(path), mode); - } -#else - fs = std::fstream(path, mode); -#endif // PANDA_TARGET_WINDOWS -} - void ProtobufSnapshotGenerator::GenerateSnapshot(const panda::pandasm::Program &program, const std::string &outputName) { protoPanda::Program protoProgram; Program::Serialize(program, protoProgram); - std::fstream output; - GetFileStream(outputName, std::ios::out | std::ios::trunc | std::ios::binary, output); + std::fstream output(ConvertPlatFormPath(outputName), std::ios::out | std::ios::trunc | std::ios::binary); if (!output) { std::cerr << "Failed to create: " << outputName << std::endl; return; @@ -50,8 +37,7 @@ void ProtobufSnapshotGenerator::GenerateSnapshot(const panda::pandasm::Program & void ProtobufSnapshotGenerator::GenerateProgram(const std::string &inputName, panda::pandasm::Program &prog, panda::ArenaAllocator *allocator) { - std::fstream input; - GetFileStream(inputName, std::ios::in | std::ios::binary, input); + std::fstream input(ConvertPlatFormPath(inputName), std::ios::in | std::ios::binary); if (!input) { std::cerr << "Failed to open: " << inputName << std::endl; return; @@ -70,8 +56,7 @@ void ProtobufSnapshotGenerator::UpdateCacheFile( { protoPanda::CompositeProgram protoCompositeProgram; CompositeProgram::Serialize(compositeProgramMap, isDebug, protoCompositeProgram); - std::fstream output; - GetFileStream(cacheFilePath, std::ios::out | std::ios::trunc | std::ios::binary, output); + std::fstream output(ConvertPlatFormPath(cacheFilePath), std::ios::out | std::ios::trunc | std::ios::binary); if (!output) { std::cerr << "Failed to create cache file: " << cacheFilePath << std::endl; return; @@ -83,8 +68,7 @@ void ProtobufSnapshotGenerator::UpdateCacheFile( std::map *ProtobufSnapshotGenerator::GetCacheContext( const std::string &cacheFilePath, bool isDebug, panda::ArenaAllocator *allocator) { - std::fstream input; - GetFileStream(cacheFilePath, std::ios::in | std::ios::binary, input); + std::fstream input(ConvertPlatFormPath(cacheFilePath), std::ios::in | std::ios::binary); if (!input) { std::cout << "Cache file: " << cacheFilePath << " doesn't exist" << std::endl; return nullptr; diff --git a/ts2panda/ts2abc/ts2abc.cpp b/ts2panda/ts2abc/ts2abc.cpp index 777118eee3..b0cda0a2a6 100644 --- a/ts2panda/ts2abc/ts2abc.cpp +++ b/ts2panda/ts2abc/ts2abc.cpp @@ -28,6 +28,7 @@ #include "ts2abc_options.h" #include "ts2abc.h" #include "protobufSnapshotGenerator.h" +#include "protobufHelper.h" #ifdef ENABLE_BYTECODE_OPT #include "optimize_bytecode.h" @@ -1458,19 +1459,21 @@ static bool EmitProgram(const std::string &output, int optLevel, std::string opt panda::pandasm::AsmEmitter::PandaFileToPandaAsmMaps maps {}; panda::pandasm::AsmEmitter::PandaFileToPandaAsmMaps* mapsp = &maps; - if (!panda::pandasm::AsmEmitter::Emit(output.c_str(), prog, statp, mapsp, emitDebugInfo)) { + if (!panda::pandasm::AsmEmitter::Emit(panda::proto::ConvertPlatFormPath(output.c_str()), prog, statp, mapsp, + emitDebugInfo)) { std::cerr << "Failed to emit binary data: " << panda::pandasm::AsmEmitter::GetLastError() << std::endl; return false; } - panda::bytecodeopt::OptimizeBytecode(&prog, mapsp, output.c_str(), true); + panda::bytecodeopt::OptimizeBytecode(&prog, mapsp, panda::proto::ConvertPlatFormPath(output.c_str()), true); if (g_compilerOutputProto.size() > 0) { panda::proto::ProtobufSnapshotGenerator::GenerateSnapshot(prog, g_compilerOutputProto); return true; } - if (!panda::pandasm::AsmEmitter::Emit(output.c_str(), prog, statp, mapsp, emitDebugInfo)) { + if (!panda::pandasm::AsmEmitter::Emit(panda::proto::ConvertPlatFormPath(output.c_str()), prog, statp, mapsp, + emitDebugInfo)) { std::cerr << "Failed to emit binary data: " << panda::pandasm::AsmEmitter::GetLastError() << std::endl; return false; } @@ -1631,7 +1634,7 @@ bool CompileNpmEntries(const std::string &input, const std::string &output) } std::stringstream ss; - std::ifstream inputStream(input); + std::ifstream inputStream(panda::proto::ConvertPlatFormPath(input)); if (inputStream.fail()) { std::cerr << "Failed to read file to buffer: " << input << std::endl; return false; @@ -1677,7 +1680,7 @@ bool HandleJsonFile(const std::string &input, std::string &data) } std::ifstream file; - file.open(fpath); + file.open(panda::proto::ConvertPlatFormPath(fpath)); if (file.fail()) { std::cerr << "failed to open:" << fpath << std::endl; return false; -- Gitee