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-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-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"
|
|
|
|
#include "oi/OIOpts.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
|
|
|
|
|
|
|
constexpr static OIOpts opts{
|
|
|
|
OIOpt{'h', "help", no_argument, nullptr, "Print this message and exit."},
|
2023-11-13 16:50:09 +00:00
|
|
|
OIOpt{'o',
|
|
|
|
"output",
|
|
|
|
required_argument,
|
|
|
|
"<file>",
|
2023-03-07 17:45:00 +00:00
|
|
|
"Write output(s) to file(s) with this prefix."},
|
2023-11-13 16:50:09 +00:00
|
|
|
OIOpt{'c',
|
|
|
|
"config-file",
|
|
|
|
required_argument,
|
|
|
|
"<oid.toml>",
|
2022-12-19 14:37:51 +00:00
|
|
|
"Path to OI configuration file."},
|
2023-11-13 16:50:09 +00:00
|
|
|
OIOpt{'d',
|
|
|
|
"debug-level",
|
|
|
|
required_argument,
|
|
|
|
"<level>",
|
2023-09-18 13:29:59 +01:00
|
|
|
"Verbose level for logging"},
|
2023-11-13 16:50:09 +00:00
|
|
|
OIOpt{'j',
|
|
|
|
"dump-jit",
|
|
|
|
optional_argument,
|
|
|
|
"<jit.cpp>",
|
2022-12-19 14:37:51 +00:00
|
|
|
"Write generated code to a file (for debugging)."},
|
2023-11-13 16:50:09 +00:00
|
|
|
OIOpt{'e',
|
|
|
|
"exit-code",
|
|
|
|
no_argument,
|
|
|
|
nullptr,
|
2023-01-25 15:27:33 +00:00
|
|
|
"Return a bad exit code if nothing is generated."},
|
2023-11-13 16:50:09 +00:00
|
|
|
OIOpt{'p',
|
|
|
|
"pic",
|
|
|
|
no_argument,
|
|
|
|
nullptr,
|
2023-09-14 11:40:11 +01:00
|
|
|
"Generate position independent code."},
|
2022-12-19 14:37:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void usage() {
|
|
|
|
std::cerr << "usage: oilgen ARGS INPUT_OBJECT" << std::endl;
|
|
|
|
std::cerr << opts;
|
|
|
|
|
|
|
|
std::cerr << std::endl
|
|
|
|
<< "You probably shouldn't be calling this application directly. "
|
|
|
|
"It's meant to be"
|
|
|
|
<< std::endl
|
|
|
|
<< "called by a clang plugin automatically with BUCK." << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char* argv[]) {
|
|
|
|
google::InitGoogleLogging(argv[0]);
|
|
|
|
FLAGS_minloglevel = 0;
|
|
|
|
FLAGS_stderrthreshold = 0;
|
|
|
|
|
|
|
|
fs::path outputPath = "a.o";
|
2023-10-11 23:42:42 +01:00
|
|
|
std::vector<fs::path> configFilePaths;
|
2022-12-19 14:37:51 +00:00
|
|
|
fs::path sourceFileDumpPath = "";
|
2023-01-25 15:27:33 +00:00
|
|
|
bool exitCode = false;
|
2023-09-14 11:40:11 +01:00
|
|
|
bool pic = false;
|
2022-12-19 14:37:51 +00:00
|
|
|
|
|
|
|
int c;
|
2023-11-13 16:50:09 +00:00
|
|
|
while ((c = getopt_long(
|
|
|
|
argc, argv, opts.shortOpts(), opts.longOpts(), nullptr)) != -1) {
|
2022-12-19 14:37:51 +00:00
|
|
|
switch (c) {
|
2023-03-07 17:45:00 +00:00
|
|
|
case 'h':
|
|
|
|
usage();
|
|
|
|
return EXIT_SUCCESS;
|
2022-12-19 14:37:51 +00:00
|
|
|
case 'o':
|
|
|
|
outputPath = optarg;
|
|
|
|
break;
|
|
|
|
case 'c':
|
2023-10-11 23:42:42 +01:00
|
|
|
configFilePaths.emplace_back(optarg);
|
2022-12-19 14:37:51 +00:00
|
|
|
break;
|
|
|
|
case 'd':
|
2023-09-18 13:29:59 +01:00
|
|
|
google::LogToStderr();
|
|
|
|
google::SetStderrLogging(google::INFO);
|
|
|
|
google::SetVLOGLevel("*", atoi(optarg));
|
|
|
|
// 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");
|
|
|
|
break;
|
|
|
|
case 'j':
|
2022-12-19 14:37:51 +00:00
|
|
|
sourceFileDumpPath = optarg != nullptr ? optarg : "jit.cpp";
|
|
|
|
break;
|
2023-01-25 15:27:33 +00:00
|
|
|
case 'e':
|
|
|
|
exitCode = true;
|
|
|
|
break;
|
2023-09-14 11:40:11 +01:00
|
|
|
case 'p':
|
|
|
|
pic = true;
|
|
|
|
break;
|
2022-12-19 14:37:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (optind >= argc) {
|
|
|
|
usage();
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
fs::path primaryObject = argv[optind];
|
|
|
|
|
2023-01-19 11:36:10 +00:00
|
|
|
if ((setenv("DRGN_ENABLE_TYPE_ITERATOR", "1", 1)) < 0) {
|
2023-03-07 17:45:00 +00:00
|
|
|
LOG(ERROR) << "Failed to set environment variable\
|
2023-01-19 11:36:10 +00:00
|
|
|
DRGN_ENABLE_TYPE_ITERATOR\n";
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2022-12-19 14:37:51 +00:00
|
|
|
OIGenerator oigen;
|
|
|
|
|
|
|
|
oigen.setOutputPath(std::move(outputPath));
|
2023-10-11 23:42:42 +01:00
|
|
|
oigen.setConfigFilePaths(std::move(configFilePaths));
|
2022-12-19 14:37:51 +00:00
|
|
|
oigen.setSourceFileDumpPath(sourceFileDumpPath);
|
2023-01-25 15:27:33 +00:00
|
|
|
oigen.setFailIfNothingGenerated(exitCode);
|
2023-09-14 11:40:11 +01:00
|
|
|
oigen.setUsePIC(pic);
|
2022-12-19 14:37:51 +00:00
|
|
|
|
2023-01-25 14:37:16 +00:00
|
|
|
SymbolService symbols(primaryObject);
|
|
|
|
|
|
|
|
return oigen.generate(primaryObject, symbols);
|
2022-12-19 14:37:51 +00:00
|
|
|
}
|