2022-12-19 14:37:51 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2023-12-19 19:57:44 +00:00
|
|
|
#include <clang/Tooling/CommonOptionsParser.h>
|
2023-09-19 17:46:59 +01:00
|
|
|
#include <gflags/gflags.h>
|
2022-12-19 14:37:51 +00:00
|
|
|
#include <glog/logging.h>
|
|
|
|
|
2023-01-19 11:36:10 +00:00
|
|
|
#include <cstdlib>
|
2022-12-19 14:37:51 +00:00
|
|
|
#include <filesystem>
|
|
|
|
#include <iostream>
|
2023-12-19 19:57:44 +00:00
|
|
|
#include <range/v3/range/conversion.hpp>
|
|
|
|
#include <range/v3/view/transform.hpp>
|
2023-10-11 23:42:42 +01:00
|
|
|
#include <vector>
|
2022-12-19 14:37:51 +00:00
|
|
|
|
2023-04-26 16:20:53 +01:00
|
|
|
#include "oi/OICodeGen.h"
|
|
|
|
#include "oi/OIGenerator.h"
|
2022-12-19 14:37:51 +00:00
|
|
|
|
|
|
|
namespace fs = std::filesystem;
|
2023-07-26 15:31:53 +01:00
|
|
|
using namespace oi::detail;
|
2022-12-19 14:37:51 +00:00
|
|
|
|
2023-12-19 19:57:44 +00:00
|
|
|
static llvm::cl::OptionCategory OilgenCategory("oilgen options");
|
|
|
|
|
|
|
|
static llvm::cl::list<std::string> ConfigFiles(
|
|
|
|
"config-file",
|
|
|
|
llvm::cl::desc(R"(</path/to/oid.toml>)"),
|
|
|
|
llvm::cl::cat(OilgenCategory));
|
|
|
|
static llvm::cl::opt<std::string> OutputFile(
|
|
|
|
"output",
|
|
|
|
llvm::cl::desc(R"(Write output to this file.)"),
|
|
|
|
llvm::cl::init("a.o"),
|
|
|
|
llvm::cl::cat(OilgenCategory));
|
|
|
|
static llvm::cl::opt<int> DebugLevel(
|
|
|
|
"debug-level",
|
|
|
|
llvm::cl::desc(R"(Verbose level for logging.)"),
|
|
|
|
llvm::cl::init(-1),
|
|
|
|
llvm::cl::cat(OilgenCategory));
|
|
|
|
static llvm::cl::opt<std::string> DumpJit(
|
|
|
|
"dump-jit",
|
|
|
|
llvm::cl::desc(R"(Write the generated code to a file.)"),
|
|
|
|
llvm::cl::init("jit.cpp"),
|
|
|
|
llvm::cl::cat(OilgenCategory));
|
|
|
|
|
|
|
|
int main(int argc, const char* argv[]) {
|
2022-12-19 14:37:51 +00:00
|
|
|
google::InitGoogleLogging(argv[0]);
|
|
|
|
FLAGS_minloglevel = 0;
|
|
|
|
FLAGS_stderrthreshold = 0;
|
|
|
|
|
2023-12-19 19:57:44 +00:00
|
|
|
auto expectedParser =
|
|
|
|
clang::tooling::CommonOptionsParser::create(argc, argv, OilgenCategory);
|
|
|
|
if (!expectedParser) {
|
|
|
|
llvm::errs() << expectedParser.takeError();
|
|
|
|
return -1;
|
2022-12-19 14:37:51 +00:00
|
|
|
}
|
2023-12-19 19:57:44 +00:00
|
|
|
clang::tooling::CommonOptionsParser& options = expectedParser.get();
|
|
|
|
auto& compilations = options.getCompilations();
|
|
|
|
|
|
|
|
if (DebugLevel.getNumOccurrences()) {
|
|
|
|
google::LogToStderr();
|
|
|
|
google::SetStderrLogging(google::INFO);
|
|
|
|
google::SetVLOGLevel("*", DebugLevel);
|
|
|
|
// Upstream glog defines `GLOG_INFO` as 0 https://fburl.com/ydjajhz0,
|
|
|
|
// but internally it's defined as 1 https://fburl.com/code/9fwams75
|
|
|
|
gflags::SetCommandLineOption("minloglevel", "0");
|
2023-01-19 11:36:10 +00:00
|
|
|
}
|
|
|
|
|
2022-12-19 14:37:51 +00:00
|
|
|
OIGenerator oigen;
|
|
|
|
|
2023-12-19 19:57:44 +00:00
|
|
|
oigen.setConfigFilePaths(ConfigFiles |
|
|
|
|
ranges::views::transform([](const auto& p) {
|
|
|
|
return std::filesystem::path(p);
|
|
|
|
}) |
|
|
|
|
ranges::to<std::vector>());
|
|
|
|
if (DumpJit.getNumOccurrences())
|
|
|
|
oigen.setSourceFileDumpPath(DumpJit.getValue());
|
2022-12-19 14:37:51 +00:00
|
|
|
|
2023-12-19 19:57:44 +00:00
|
|
|
oigen.setOutputPath(OutputFile.getValue());
|
2023-01-25 14:37:16 +00:00
|
|
|
|
2023-12-19 19:57:44 +00:00
|
|
|
oigen.setFailIfNothingGenerated(true);
|
|
|
|
return oigen.generate(compilations, options.getSourcePathList());
|
2022-12-19 14:37:51 +00:00
|
|
|
}
|