1 /**
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "mergeProgram.h"
17 #include "options.h"
18 #include "protobufSnapshotGenerator.h"
19
20 #include <assembly-emitter.h>
21 #include <mem/pool_manager.h>
22
23 namespace panda::proto {
24 using mem::MemConfig;
25
26 class ProtoMemManager {
27 public:
ProtoMemManager()28 explicit ProtoMemManager()
29 {
30 constexpr auto COMPILER_SIZE = 512_MB;
31
32 MemConfig::Initialize(0, 0, COMPILER_SIZE, 0);
33 PoolManager::Initialize(PoolType::MMAP);
34 }
35
36 NO_COPY_SEMANTIC(ProtoMemManager);
37 NO_MOVE_SEMANTIC(ProtoMemManager);
38
~ProtoMemManager()39 ~ProtoMemManager()
40 {
41 PoolManager::Finalize();
42 MemConfig::Finalize();
43 }
44 };
45
Run(int argc,const char ** argv)46 int Run(int argc, const char **argv)
47 {
48 auto options = std::make_unique<Options>();
49 if (!options->Parse(argc, argv)) {
50 std::cerr << options->ErrorMsg() << std::endl;
51 return 1;
52 }
53
54 std::string protoPathInput = options->ProtoPathInput();
55 std::string protoBinSuffix = options->ProtoBinSuffix();
56 std::string outputFilePath = options->OutputFilePath();
57 if (outputFilePath.empty()) {
58 outputFilePath = panda::os::file::File::GetExecutablePath().Value();
59 }
60
61 std::vector<std::string> protoFiles;
62 if (!MergeProgram::CollectProtoFiles(protoPathInput, protoBinSuffix, protoFiles)) {
63 return 1;
64 }
65
66 panda::ArenaAllocator allocator(panda::SpaceType::SPACE_TYPE_COMPILER, nullptr, true);
67
68 std::vector<panda::pandasm::Program *> programs;
69 programs.reserve(protoFiles.size());
70 for (size_t i = 0; i < protoFiles.size(); i++) {
71 auto *prog = allocator.New<panda::pandasm::Program>();
72 programs.emplace_back(prog);
73 }
74
75 size_t idx = 0;
76 for (auto &protoFile : protoFiles) {
77 proto::ProtobufSnapshotGenerator::GenerateProgram(protoFile, *(programs[idx++]), &allocator);
78 }
79
80 std::string outputFileName = outputFilePath.append(panda::os::file::File::GetPathDelim()).
81 append(options->OutputFileName());
82 if (!panda::pandasm::AsmEmitter::EmitPrograms(panda::os::file::File::GetExtendedFilePath(outputFileName), programs,
83 true)) {
84 return 1;
85 }
86
87 return 0;
88 }
89 } // namespace panda::proto
90
main(int argc,const char ** argv)91 int main(int argc, const char **argv)
92 {
93 panda::proto::ProtoMemManager mm;
94 return panda::proto::Run(argc, argv);
95 }
96