oilgen: change interface to support multiple calls in one cu

This commit is contained in:
Jake Hillion 2023-03-07 09:45:00 -08:00 committed by Jake Hillion
parent ac7a1d4aba
commit 53ba312f1e
5 changed files with 39 additions and 34 deletions

View File

@ -2,5 +2,5 @@ oilgen
OilVectorOfStrings
OilVectorOfStrings.o
jit.cpp
JitCompiled.o
JitCompiled.*.o

View File

@ -15,14 +15,11 @@ oilgen:
(cd ../../ && make oid-devel)
ln -s ../../build/oilgen oilgen
JitCompiled.o: oilgen OilVectorOfStrings.o
./oilgen --exit-code -o JitCompiled.o -c ../../build/sample.oid.toml -d OilVectorOfStrings.o
OilVectorOfStrings: OilVectorOfStrings.o JitCompiled.o
${CXX} ${CXXFLAGS} OilVectorOfStrings.o JitCompiled.o -o OilVectorOfStrings
OilVectorOfStrings: oilgen OilVectorOfStrings.o
${CXX} ${CXXFLAGS} OilVectorOfStrings.o $$(./oilgen --exit-code -o JitCompiled.o -c ../../build/sample.oid.toml -d OilVectorOfStrings.o) -o OilVectorOfStrings
all: OilVectorOfStrings
clean:
rm -f oilgen OilVectorOfStrings{,.o,.o.dwarf} JitCompiled.o jit.cpp
rm -f oilgen OilVectorOfStrings{,.o,.o.dwarf} JitCompiled.*.o jit.cpp

View File

@ -20,6 +20,7 @@
#include <boost/core/demangle.hpp>
#include <fstream>
#include <iostream>
#include <unordered_map>
#include <variant>
@ -122,15 +123,15 @@ OIGenerator::findOilTypesAndNames(drgnplusplus::program& prog) {
return out;
}
bool OIGenerator::generateForType(const OICodeGen::Config& generatorConfig,
const OICompiler::Config& compilerConfig,
const drgn_qualified_type& type,
const std::string& linkageName,
SymbolService& symbols) {
fs::path OIGenerator::generateForType(const OICodeGen::Config& generatorConfig,
const OICompiler::Config& compilerConfig,
const drgn_qualified_type& type,
const std::string& linkageName,
SymbolService& symbols) {
auto codegen = OICodeGen::buildFromConfig(generatorConfig, symbols);
if (!codegen) {
LOG(ERROR) << "failed to initialise codegen";
return false;
return {};
}
std::string code =
@ -142,7 +143,7 @@ bool OIGenerator::generateForType(const OICodeGen::Config& generatorConfig,
if (!codegen->generate(code)) {
LOG(ERROR) << "failed to generate code";
return false;
return {};
}
std::string sourcePath = sourceFileDumpPath;
@ -156,7 +157,15 @@ bool OIGenerator::generateForType(const OICodeGen::Config& generatorConfig,
}
OICompiler compiler{{}, compilerConfig};
return compiler.compile(code, sourcePath, outputPath);
// TODO: Revert to outputPath and remove printing when typegraph is done.
fs::path tmpObject = outputPath;
tmpObject.replace_extension("." + linkageName + ".o");
if (!compiler.compile(code, sourcePath, tmpObject)) {
return {};
}
return tmpObject;
}
int OIGenerator::generate(fs::path& primaryObject, SymbolService& symbols) {
@ -175,12 +184,6 @@ int OIGenerator::generate(fs::path& primaryObject, SymbolService& symbols) {
std::vector<std::tuple<drgn_qualified_type, std::string>> oilTypes =
findOilTypesAndNames(prog);
if (size_t count = oilTypes.size(); count > 1) {
LOG(WARNING) << "oilgen can currently only generate for one type per "
"compilation unit and we found "
<< count;
}
OICodeGen::Config generatorConfig{};
OICompiler::Config compilerConfig{};
if (!OIUtils::processConfigFile(configFilePath, compilerConfig,
@ -192,8 +195,11 @@ int OIGenerator::generate(fs::path& primaryObject, SymbolService& symbols) {
size_t failures = 0;
for (const auto& [type, linkageName] : oilTypes) {
if (!generateForType(generatorConfig, compilerConfig, type, linkageName,
symbols)) {
if (auto obj = generateForType(generatorConfig, compilerConfig, type,
linkageName, symbols);
!obj.empty()) {
std::cout << obj.string() << std::endl;
} else {
LOG(WARNING) << "failed to generate for symbol `" << linkageName
<< "`. this is non-fatal but the call will not work.";
failures++;

View File

@ -22,8 +22,6 @@
#include "OICodeGen.h"
#include "OICompiler.h"
namespace fs = std::filesystem;
namespace ObjectIntrospection {
class OIGenerator {
@ -44,9 +42,9 @@ class OIGenerator {
}
private:
fs::path outputPath;
fs::path configFilePath;
fs::path sourceFileDumpPath;
std::filesystem::path outputPath;
std::filesystem::path configFilePath;
std::filesystem::path sourceFileDumpPath;
bool failIfNothingGenerated;
std::unordered_map<std::string, std::string> oilStrongToWeakSymbolsMap(
@ -55,10 +53,10 @@ class OIGenerator {
std::vector<std::tuple<drgn_qualified_type, std::string>>
findOilTypesAndNames(drgnplusplus::program& prog);
bool generateForType(const OICodeGen::Config& generatorConfig,
const OICompiler::Config& compilerConfig,
const drgn_qualified_type& type,
const std::string& linkageName, SymbolService& symbols);
std::filesystem::path generateForType(
const OICodeGen::Config& generatorConfig,
const OICompiler::Config& compilerConfig, const drgn_qualified_type& type,
const std::string& linkageName, SymbolService& symbols);
};
} // namespace ObjectIntrospection

View File

@ -29,7 +29,8 @@ using namespace ObjectIntrospection;
constexpr static OIOpts opts{
OIOpt{'h', "help", no_argument, nullptr, "Print this message and exit."},
OIOpt{'o', "output", required_argument, "<file>", "Write output to file."},
OIOpt{'o', "output", required_argument, "<file>",
"Write output(s) to file(s) with this prefix."},
OIOpt{'c', "config-file", required_argument, "<oid.toml>",
"Path to OI configuration file."},
OIOpt{'d', "dump-jit", optional_argument, "<jit.cpp>",
@ -63,6 +64,9 @@ int main(int argc, char* argv[]) {
while ((c = getopt_long(argc, argv, opts.shortOpts(), opts.longOpts(),
nullptr)) != -1) {
switch (c) {
case 'h':
usage();
return EXIT_SUCCESS;
case 'o':
outputPath = optarg;
break;
@ -85,7 +89,7 @@ int main(int argc, char* argv[]) {
fs::path primaryObject = argv[optind];
if ((setenv("DRGN_ENABLE_TYPE_ITERATOR", "1", 1)) < 0) {
std::cerr << "Failed to set environment variable\
LOG(ERROR) << "Failed to set environment variable\
DRGN_ENABLE_TYPE_ITERATOR\n";
exit(EXIT_FAILURE);
}