Skip to content

Commit 0a0cf21

Browse files
committed
[llvm-advisor] Add main command-line driver
Adds the command-line front-end that handles advisor options, finds the compiler commands, create configuration, and starts the build data collection.
1 parent a15a978 commit 0a0cf21

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#include "Config/AdvisorConfig.h"
2+
#include "Core/CompilationManager.h"
3+
#include "llvm/ADT/StringRef.h"
4+
#include "llvm/Support/CommandLine.h"
5+
#include "llvm/Support/FileSystem.h"
6+
#include "llvm/Support/InitLLVM.h"
7+
#include "llvm/Support/raw_ostream.h"
8+
9+
using namespace llvm;
10+
using namespace llvm::advisor;
11+
12+
static cl::opt<std::string> ConfigFile("config", cl::desc("Configuration file"),
13+
cl::value_desc("filename"));
14+
static cl::opt<std::string> OutputDir("output-dir",
15+
cl::desc("Output directory"),
16+
cl::value_desc("directory"));
17+
static cl::opt<bool> Verbose("verbose", cl::desc("Verbose output"));
18+
static cl::opt<bool> KeepTemps("keep-temps", cl::desc("Keep temporary files"));
19+
static cl::opt<bool> NoProfiler("no-profiler", cl::desc("Disable profiler"));
20+
21+
int main(int argc, char **argv) {
22+
InitLLVM X(argc, argv);
23+
24+
// Parse llvm-advisor options until we find the compiler
25+
std::vector<const char *> advisorArgs;
26+
advisorArgs.push_back(argv[0]);
27+
28+
int compilerArgStart = 1;
29+
bool foundCompiler = false;
30+
31+
for (int i = 1; i < argc; ++i) {
32+
StringRef arg(argv[i]);
33+
if (arg.starts_with("--") ||
34+
(arg.starts_with("-") && arg.size() > 1 && arg != "-")) {
35+
advisorArgs.push_back(argv[i]);
36+
if (arg == "--config" || arg == "--output-dir") {
37+
if (i + 1 < argc && !StringRef(argv[i + 1]).starts_with("-")) {
38+
advisorArgs.push_back(argv[++i]);
39+
}
40+
}
41+
} else {
42+
compilerArgStart = i;
43+
foundCompiler = true;
44+
break;
45+
}
46+
}
47+
48+
if (!foundCompiler) {
49+
errs() << "Error: No compiler command provided.\n";
50+
errs() << "Usage: llvm-advisor [options] <compiler> [compiler-args...]\n";
51+
return 1;
52+
}
53+
54+
// Parse llvm-advisor options
55+
int advisorArgc = advisorArgs.size();
56+
cl::ParseCommandLineOptions(advisorArgc,
57+
const_cast<char **>(advisorArgs.data()),
58+
"LLVM Compilation Advisor");
59+
60+
// Extract compiler and arguments
61+
std::string compiler = argv[compilerArgStart];
62+
std::vector<std::string> compilerArgs;
63+
for (int i = compilerArgStart + 1; i < argc; ++i) {
64+
compilerArgs.push_back(argv[i]);
65+
}
66+
67+
// Configure advisor
68+
AdvisorConfig config;
69+
if (!ConfigFile.empty()) {
70+
if (auto Err = config.loadFromFile(ConfigFile).takeError()) {
71+
errs() << "Error loading config: " << toString(std::move(Err)) << "\n";
72+
return 1;
73+
}
74+
}
75+
76+
if (!OutputDir.empty()) {
77+
config.setOutputDir(OutputDir);
78+
} else {
79+
config.setOutputDir(".llvm-advisor"); // Default hidden directory
80+
}
81+
82+
config.setVerbose(Verbose);
83+
config.setKeepTemps(KeepTemps);
84+
config.setRunProfiler(!NoProfiler);
85+
86+
// Create output directory
87+
if (auto EC = sys::fs::create_directories(config.getOutputDir())) {
88+
errs() << "Error creating output directory: " << EC.message() << "\n";
89+
return 1;
90+
}
91+
92+
if (config.getVerbose()) {
93+
outs() << "LLVM Compilation Advisor\n";
94+
outs() << "Compiler: " << compiler << "\n";
95+
outs() << "Output: " << config.getOutputDir() << "\n";
96+
}
97+
98+
// Execute with data collection
99+
CompilationManager manager(config);
100+
auto result = manager.executeWithDataCollection(compiler, compilerArgs);
101+
102+
if (result) {
103+
if (config.getVerbose()) {
104+
outs() << "Compilation completed (exit code: " << *result << ")\n";
105+
}
106+
return *result;
107+
} else {
108+
errs() << "Error: " << toString(result.takeError()) << "\n";
109+
return 1;
110+
}
111+
}

0 commit comments

Comments
 (0)