Skip to content

Commit 16f715d

Browse files
committed
[llvm-advisor] add initial project structure and configuration
The AdvisorConfig class provides JSON based configuration loading with file classification patterns and output directory management.
1 parent 267b136 commit 16f715d

File tree

4 files changed

+127
-0
lines changed

4 files changed

+127
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
cmake_minimum_required(VERSION 3.18)
2+
3+
set(LLVM_TOOL_LLVM_ADVISOR_BUILD_DEFAULT ON)
4+
set(LLVM_REQUIRE_EXE_NAMES llvm-advisor)
5+
6+
add_subdirectory(src)
7+
8+
# Set the executable name
9+
set_target_properties(llvm-advisor PROPERTIES
10+
OUTPUT_NAME llvm-advisor)
11+
12+
# Install the binary
13+
install(TARGETS llvm-advisor
14+
RUNTIME DESTINATION bin
15+
COMPONENT llvm-advisor)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"outputDir": ".llvm-advisor",
3+
"verbose": false,
4+
"keepTemps": false,
5+
"runProfiler": true,
6+
"timeout": 60
7+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include "AdvisorConfig.h"
2+
#include "llvm/Support/FileSystem.h"
3+
#include "llvm/Support/JSON.h"
4+
#include "llvm/Support/MemoryBuffer.h"
5+
#include "llvm/Support/Path.h"
6+
7+
namespace llvm {
8+
namespace advisor {
9+
10+
AdvisorConfig::AdvisorConfig() {
11+
// Use relative path as default, will be resolved by CompilationManager
12+
OutputDir_ = ".llvm-advisor";
13+
}
14+
15+
Expected<bool> AdvisorConfig::loadFromFile(const std::string &path) {
16+
auto BufferOrError = MemoryBuffer::getFile(path);
17+
if (!BufferOrError) {
18+
return createStringError(BufferOrError.getError(),
19+
"Cannot read config file");
20+
}
21+
22+
auto Buffer = std::move(*BufferOrError);
23+
Expected<json::Value> JsonOrError = json::parse(Buffer->getBuffer());
24+
if (!JsonOrError) {
25+
return JsonOrError.takeError();
26+
}
27+
28+
auto &Json = *JsonOrError;
29+
auto *Obj = Json.getAsObject();
30+
if (!Obj) {
31+
return createStringError(std::make_error_code(std::errc::invalid_argument),
32+
"Config file must contain JSON object");
33+
}
34+
35+
if (auto outputDirOpt = Obj->getString("outputDir"); outputDirOpt) {
36+
OutputDir_ = outputDirOpt->str();
37+
}
38+
39+
if (auto verboseOpt = Obj->getBoolean("verbose"); verboseOpt) {
40+
Verbose_ = *verboseOpt;
41+
}
42+
43+
if (auto keepTempsOpt = Obj->getBoolean("keepTemps"); keepTempsOpt) {
44+
KeepTemps_ = *keepTempsOpt;
45+
}
46+
47+
if (auto runProfileOpt = Obj->getBoolean("runProfiler"); runProfileOpt) {
48+
RunProfiler_ = *runProfileOpt;
49+
}
50+
51+
if (auto timeoutOpt = Obj->getInteger("timeout"); timeoutOpt) {
52+
TimeoutSeconds_ = static_cast<int>(*timeoutOpt);
53+
}
54+
55+
return true;
56+
}
57+
58+
std::string AdvisorConfig::getToolPath(const std::string &tool) const {
59+
// For now, just return the tool name and rely on PATH
60+
return tool;
61+
}
62+
63+
} // namespace advisor
64+
} // namespace llvm
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#ifndef LLVM_ADVISOR_CONFIG_H
2+
#define LLVM_ADVISOR_CONFIG_H
3+
4+
#include "llvm/Support/Error.h"
5+
#include <string>
6+
7+
namespace llvm {
8+
namespace advisor {
9+
10+
class AdvisorConfig {
11+
public:
12+
AdvisorConfig();
13+
14+
Expected<bool> loadFromFile(const std::string &path);
15+
16+
void setOutputDir(const std::string &dir) { OutputDir_ = dir; }
17+
void setVerbose(bool verbose) { Verbose_ = verbose; }
18+
void setKeepTemps(bool keep) { KeepTemps_ = keep; }
19+
void setRunProfiler(bool run) { RunProfiler_ = run; }
20+
void setTimeout(int seconds) { TimeoutSeconds_ = seconds; }
21+
22+
const std::string &getOutputDir() const { return OutputDir_; }
23+
bool getVerbose() const { return Verbose_; }
24+
bool getKeepTemps() const { return KeepTemps_; }
25+
bool getRunProfiler() const { return RunProfiler_; }
26+
int getTimeout() const { return TimeoutSeconds_; }
27+
28+
std::string getToolPath(const std::string &tool) const;
29+
30+
private:
31+
std::string OutputDir_;
32+
bool Verbose_ = false;
33+
bool KeepTemps_ = false;
34+
bool RunProfiler_ = true;
35+
int TimeoutSeconds_ = 60;
36+
};
37+
38+
} // namespace advisor
39+
} // namespace llvm
40+
41+
#endif

0 commit comments

Comments
 (0)