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
OilVectorOfStrings.o OilVectorOfStrings.o
jit.cpp jit.cpp
JitCompiled.o JitCompiled.*.o

View File

@ -15,14 +15,11 @@ oilgen:
(cd ../../ && make oid-devel) (cd ../../ && make oid-devel)
ln -s ../../build/oilgen oilgen ln -s ../../build/oilgen oilgen
JitCompiled.o: oilgen OilVectorOfStrings.o OilVectorOfStrings: oilgen OilVectorOfStrings.o
./oilgen --exit-code -o JitCompiled.o -c ../../build/sample.oid.toml -d OilVectorOfStrings.o ${CXX} ${CXXFLAGS} OilVectorOfStrings.o $$(./oilgen --exit-code -o JitCompiled.o -c ../../build/sample.oid.toml -d OilVectorOfStrings.o) -o OilVectorOfStrings
OilVectorOfStrings: OilVectorOfStrings.o JitCompiled.o
${CXX} ${CXXFLAGS} OilVectorOfStrings.o JitCompiled.o -o OilVectorOfStrings
all: OilVectorOfStrings all: OilVectorOfStrings
clean: 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 <boost/core/demangle.hpp>
#include <fstream> #include <fstream>
#include <iostream>
#include <unordered_map> #include <unordered_map>
#include <variant> #include <variant>
@ -122,15 +123,15 @@ OIGenerator::findOilTypesAndNames(drgnplusplus::program& prog) {
return out; return out;
} }
bool OIGenerator::generateForType(const OICodeGen::Config& generatorConfig, fs::path OIGenerator::generateForType(const OICodeGen::Config& generatorConfig,
const OICompiler::Config& compilerConfig, const OICompiler::Config& compilerConfig,
const drgn_qualified_type& type, const drgn_qualified_type& type,
const std::string& linkageName, const std::string& linkageName,
SymbolService& symbols) { SymbolService& symbols) {
auto codegen = OICodeGen::buildFromConfig(generatorConfig, symbols); auto codegen = OICodeGen::buildFromConfig(generatorConfig, symbols);
if (!codegen) { if (!codegen) {
LOG(ERROR) << "failed to initialise codegen"; LOG(ERROR) << "failed to initialise codegen";
return false; return {};
} }
std::string code = std::string code =
@ -142,7 +143,7 @@ bool OIGenerator::generateForType(const OICodeGen::Config& generatorConfig,
if (!codegen->generate(code)) { if (!codegen->generate(code)) {
LOG(ERROR) << "failed to generate code"; LOG(ERROR) << "failed to generate code";
return false; return {};
} }
std::string sourcePath = sourceFileDumpPath; std::string sourcePath = sourceFileDumpPath;
@ -156,7 +157,15 @@ bool OIGenerator::generateForType(const OICodeGen::Config& generatorConfig,
} }
OICompiler compiler{{}, compilerConfig}; 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) { 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 = std::vector<std::tuple<drgn_qualified_type, std::string>> oilTypes =
findOilTypesAndNames(prog); 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{}; OICodeGen::Config generatorConfig{};
OICompiler::Config compilerConfig{}; OICompiler::Config compilerConfig{};
if (!OIUtils::processConfigFile(configFilePath, compilerConfig, if (!OIUtils::processConfigFile(configFilePath, compilerConfig,
@ -192,8 +195,11 @@ int OIGenerator::generate(fs::path& primaryObject, SymbolService& symbols) {
size_t failures = 0; size_t failures = 0;
for (const auto& [type, linkageName] : oilTypes) { for (const auto& [type, linkageName] : oilTypes) {
if (!generateForType(generatorConfig, compilerConfig, type, linkageName, if (auto obj = generateForType(generatorConfig, compilerConfig, type,
symbols)) { linkageName, symbols);
!obj.empty()) {
std::cout << obj.string() << std::endl;
} else {
LOG(WARNING) << "failed to generate for symbol `" << linkageName LOG(WARNING) << "failed to generate for symbol `" << linkageName
<< "`. this is non-fatal but the call will not work."; << "`. this is non-fatal but the call will not work.";
failures++; failures++;

View File

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

View File

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