1 /*
2 * Copyright (c) 2024-2025 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 "dep_analyzer.h"
17
FilterArgs(ark::Span<const char * const> args,int & newArgc,const char ** & newArgv)18 static void FilterArgs(ark::Span<const char *const> args, int &newArgc, const char **&newArgv)
19 {
20 ASSERT(args.size() > 1);
21 std::vector<const char *> filteredArgs;
22 filteredArgs.push_back(args[0]);
23 for (size_t i = 1; i < args.size(); ++i) {
24 if (std::strncmp(args[i], "--output", std::strlen("--output")) == 0) {
25 continue;
26 }
27 filteredArgs.push_back(args[i]);
28 }
29
30 newArgc = static_cast<int>(filteredArgs.size());
31 if (newArgc <= 0 || static_cast<size_t>(newArgc) > args.size()) {
32 return;
33 }
34 newArgv = new const char *[newArgc];
35 std::copy(filteredArgs.begin(), filteredArgs.end(), newArgv);
36 }
37
ParseOption(ark::Span<const char * const> args)38 static std::string ParseOption(ark::Span<const char *const> args)
39 {
40 ASSERT(args.size() > 1);
41 for (size_t i = 1; i < args.size(); ++i) {
42 if (std::strncmp(args[i], "--output", std::strlen("--output")) == 0) {
43 if (std::strchr(args[i], '=') != nullptr) {
44 std::string arg = args[i];
45 return arg.substr(std::strlen("--output="));
46 }
47 return "./fileList.txt";
48 }
49 }
50 return "";
51 }
52
main(int argc,const char ** argv)53 int main(int argc, const char **argv)
54 {
55 ark::Span<const char *const> args(argv, static_cast<size_t>(argc));
56 std::string outFilePath {ParseOption(args)};
57 int newArgc = 0;
58 const char **newArgv = nullptr;
59 FilterArgs(args, newArgc, newArgv);
60
61 DepAnalyzer da;
62 if (da.AnalyzeDeps(newArgc, newArgv) != 0) {
63 return 1;
64 }
65
66 if (outFilePath.empty()) {
67 da.Dump();
68 } else {
69 da.Dump(outFilePath);
70 }
71 return 0;
72 }
73