1// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
2//
3// SPDX-License-Identifier: GPL-3.0-or-later
4
5#include "commandexecuter.h"
6#include "common/util/commandparser.h"
7
8#include "services/builder/builderservice.h"
9#include "services/option/optionmanager.h"
10
11#include <QCoreApplication>
12#include <QCommandLineOption>
13#include <QDebug>
14#include <QDir>
15
16#include <iostream>
17
18CommandExecuter &CommandExecuter::instance()
19{
20 static CommandExecuter ins;
21 return ins;
22}
23
24void CommandExecuter::buildProject()
25{
26 QString sourcePath = CommandParser::instance().value("b");
27 QString destPath = CommandParser::instance().value("d");
28 QString kit = CommandParser::instance().value("k");
29 QStringList args = CommandParser::instance().value("a").trimmed().split(" ", QString::SkipEmptyParts);
30 QString elfPath = CommandParser::instance().value("t");
31
32 if (sourcePath.isEmpty()) {
33 std::cout << std::string("Please input project directory!") << std::endl;
34 return;
35 }
36 if (!QDir::isAbsolutePath(sourcePath)) {
37 QString path = QDir::currentPath();
38 sourcePath = path + QDir::separator() + sourcePath;
39 }
40 if (!QDir(sourcePath).exists()) {
41 std::cout << std::string("Please input correct working directory!") << std::endl;
42 return;
43 }
44 if (!QDir::isAbsolutePath(destPath))
45 destPath = QDir::currentPath() + QDir::separator() + destPath;
46 if (!QDir::isAbsolutePath(elfPath))
47 elfPath = QDir::currentPath() + QDir::separator() + elfPath;
48
49 QStringList arguments;
50 arguments << "-S";
51 arguments << sourcePath;
52 arguments << "-B";
53 arguments << destPath;
54 auto &ctx = dpfInstance.serviceContext();
55 dpfservice::BuilderService *builderService = ctx.service<dpfservice::BuilderService>(dpfservice::BuilderService::name());
56
57 QList<BuildCommandInfo> buildCommandInfos;
58 BuildCommandInfo buildInfo;
59 buildInfo.workingDir = sourcePath;
60 buildInfo.program = OptionManager::getInstance()->getToolPath(kit);
61 buildInfo.kitName = kit.toLower();
62 if (buildInfo.kitName == "cmake")
63 buildInfo.arguments = arguments;
64 buildCommandInfos.append(buildInfo);
65 if (!destPath.isEmpty())
66 buildInfo.workingDir = destPath;
67 buildInfo.arguments = args;
68 buildCommandInfos.append(buildInfo);
69 QFuture<void> future = QtConcurrent::run([=](){
70 builderService->interface.builderCommand(buildCommandInfos, true);
71 });
72 future.waitForFinished();
73
74 if (CommandParser::instance().isSet("t")) {
75 buildInfo.elfPath = elfPath;
76 buildInfo.program = "/usr/bin/objcopy";
77 QStringList exeFiles = exeFileName(buildInfo.workingDir);
78 for (QString exeFile : exeFiles) {
79 buildInfo.arguments = QStringList() << "--add-section" << ".note.builder=" + elfPath << exeFile;
80 }
81 }
82 builderService->interface.builderCommand({buildInfo}, true);
83}
84
85CommandExecuter::CommandExecuter(QObject *parent)
86 :QObject (parent)
87{
88}
89
90QStringList CommandExecuter::exeFileName(QDir dir)
91{
92 QStringList exeFiles;
93 dir.setFilter(QDir::Files | QDir::NoDotAndDotDot);
94 QFileInfoList files = dir.entryInfoList();
95 foreach (QFileInfo fileInfo, files) {
96 if (fileInfo.isHidden())
97 continue;
98 if (fileInfo.isExecutable())
99 exeFiles.append(fileInfo.fileName());
100 }
101 return exeFiles;
102}
103