codeql/swift/swift-autobuilder/swift-autobuilder.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

126 строки
4.4 KiB
C++
Исходник Обычный вид История

2022-10-12 09:55:00 +03:00
#include <iostream>
#include <vector>
#include <filesystem>
2023-08-31 09:12:52 +03:00
#include "swift/swift-autobuilder/XcodeTarget.h"
#include "swift/swift-autobuilder/BuildRunner.h"
#include "swift/swift-autobuilder/ProjectParser.h"
#include "swift/logging/SwiftLogging.h"
2023-08-31 09:12:52 +03:00
#include "swift/swift-autobuilder/CustomizingBuildLink.h"
2022-10-12 09:55:00 +03:00
static constexpr std::string_view uiTest = "com.apple.product-type.bundle.ui-testing";
static constexpr std::string_view unitTest = "com.apple.product-type.bundle.unit-test";
static constexpr std::string_view unknownType = "<unknown_target_type>";
2022-10-12 09:55:00 +03:00
const std::string_view codeql::programName = "autobuilder";
constexpr codeql::SwiftDiagnostic noProjectFound{
.id = "no-project-found",
.name = "No Xcode project or workspace found",
.action = "Set up a [manual build command][1].\n\n[1]: " MANUAL_BUILD_COMMAND_HELP_LINK};
constexpr codeql::SwiftDiagnostic noSwiftTarget{
.id = "no-swift-target",
.name = "No Swift compilation target found",
.action = "To analyze a custom set of source files, set up a [manual build "
"command][1].\n\n[1]: " MANUAL_BUILD_COMMAND_HELP_LINK};
static codeql::Logger& logger() {
static codeql::Logger ret{"main"};
return ret;
}
2022-10-17 12:24:20 +03:00
struct CLIArgs {
std::string workingDir;
bool dryRun;
2022-10-12 09:55:00 +03:00
};
static bool endsWith(std::string_view s, std::string_view suffix) {
return s.size() >= suffix.size() && s.substr(s.size() - suffix.size()) == suffix;
}
static bool isNonSwiftOrTestTarget(const XcodeTarget& t) {
return t.fileCount == 0 || t.type == uiTest || t.type == unitTest ||
// unknown target types can be legitimate, let's do a name-based heuristic then
(t.type == unknownType && (endsWith(t.name, "Tests") || endsWith(t.name, "Test")));
}
static void buildSwiftPackages(const std::vector<std::filesystem::path>& swiftPackages,
bool dryRun) {
auto any_successful =
std::any_of(std::begin(swiftPackages), std::end(swiftPackages), [&](auto& packageFile) {
LOG_INFO("Building Swift package: {}", packageFile);
return buildSwiftPackage(packageFile, dryRun);
});
if (!any_successful) {
codeql::Log::flush();
exit(1);
}
}
static bool autobuild(const CLIArgs& args) {
auto structure = scanProjectStructure(args.workingDir);
auto& xcodeTargets = structure.xcodeTargets;
auto& swiftPackages = structure.swiftPackages;
for (const auto& t : xcodeTargets) {
LOG_INFO("{}", t);
}
// Filter out targets that are tests or have no swift source files
xcodeTargets.erase(
std::remove_if(std::begin(xcodeTargets), std::end(xcodeTargets), isNonSwiftOrTestTarget),
std::end(xcodeTargets));
2022-10-12 09:55:00 +03:00
// Sort targets by the amount of files in each
std::sort(std::begin(xcodeTargets), std::end(xcodeTargets),
[](XcodeTarget& lhs, XcodeTarget& rhs) { return lhs.fileCount > rhs.fileCount; });
if (structure.xcodeEncountered && xcodeTargets.empty() && swiftPackages.empty()) {
// Report error only when there are no Xcode targets and no Swift packages
DIAGNOSE_ERROR(noSwiftTarget, "All targets found within Xcode projects or workspaces either "
"contain no Swift source files, or are tests.");
return false;
} else if (!structure.xcodeEncountered && swiftPackages.empty()) {
2023-08-31 09:12:52 +03:00
DIAGNOSE_ERROR(
noProjectFound,
"`autobuild` detected neither an Xcode project or workspace, nor a Swift package");
return false;
} else if (!xcodeTargets.empty()) {
LOG_INFO("Building Xcode target: {}", xcodeTargets.front());
installDependencies(structure, args.dryRun);
auto buildSucceeded = buildXcodeTarget(xcodeTargets.front(), args.dryRun);
// If build failed, try to build Swift packages
if (!buildSucceeded && !swiftPackages.empty()) {
buildSwiftPackages(swiftPackages, args.dryRun);
}
return buildSucceeded;
} else if (!swiftPackages.empty()) {
buildSwiftPackages(swiftPackages, args.dryRun);
2022-10-12 09:55:00 +03:00
}
return true;
2022-10-12 09:55:00 +03:00
}
2022-10-17 12:24:20 +03:00
static CLIArgs parseCLIArgs(int argc, char** argv) {
2022-10-12 09:55:00 +03:00
bool dryRun = false;
std::string path;
if (argc == 3) {
path = argv[2];
if (std::string(argv[1]) == "-dry-run") {
dryRun = true;
}
} else if (argc == 2) {
path = argv[1];
} else {
2022-10-17 12:24:20 +03:00
path = std::filesystem::current_path();
2022-10-12 09:55:00 +03:00
}
2022-10-17 12:24:20 +03:00
return CLIArgs{path, dryRun};
}
int main(int argc, char** argv) {
auto args = parseCLIArgs(argc, argv);
auto success = autobuild(args);
codeql::Log::flush();
if (!success) {
return 1;
}
2022-10-12 09:55:00 +03:00
return 0;
}