Commit 4f11a1bd authored by Nikola Dinev's avatar Nikola Dinev

Add option for omitting module generation

parent d484fe0a
Pipeline #301044 passed with stages
in 55 minutes and 50 seconds
......@@ -69,13 +69,7 @@ public:
outputPath = "bind_" + m.name + ".cpp";
std::filesystem::path p(outputPath);
p = p.parent_path();
p.append("__init__.py");
std::ofstream outputFile(outputPath);
std::ofstream initFile(p.c_str());
initFile << "from ." << m.pythonName << " import *\n";
outputFile << "#include <pybind11/pybind11.h>\n";
for (const auto& include : m.pybindIncludes)
......@@ -92,7 +86,7 @@ public:
outputFile << "\n"
<< "namespace py = pybind11;\n\n"
<< "PYBIND11_MODULE(" << m.pythonName << ", m)\n"
<< "void add_definitions_" << m.pythonName << "(py::module& m)\n"
<< "{\n";
for (const auto& e : m.enums) {
......@@ -102,9 +96,9 @@ public:
for (const auto& r : m.records) {
if (m.classHints.find(r->name) != m.classHints.end()) {
const auto& hints = m.classHints.at(r->name);
generateBindingsForRecord(*r, outputFile, initFile, &hints);
generateBindingsForRecord(*r, outputFile, &hints);
} else {
generateBindingsForRecord(*r, outputFile, initFile);
generateBindingsForRecord(*r, outputFile);
}
}
......@@ -112,10 +106,17 @@ public:
outputFile << "\telsa::ModuleHints::addCustomFunctions(m);\n";
outputFile << "}\n";
if (!m.noPythonModule) {
outputFile << "\n"
<< "PYBIND11_MODULE(" << m.pythonName << ", m)\n"
<< "{\n"
<< "\tadd_definitions_" << m.pythonName << "(m);\n"
<< "}";
}
}
static void generateBindingsForRecord(const elsa::Module::Record& r, std::ofstream& outputFile,
std::ofstream& initFile,
const elsa::Module::ClassHints* hints = nullptr)
{
std::string qualifiedName = r.name;
......@@ -215,8 +216,10 @@ public:
outputFile << "\n";
// add alias
if (r.alias != "")
initFile << r.alias << " = " << getPythonNameForTag(r.namespaceStrippedName) << "\n";
outputFile << "\tm.attr(\"" << r.alias << "\") = m.attr(\""
<< getPythonNameForTag(r.namespaceStrippedName) << "\");\n\n";
}
static void generateBindingsForEnum(const elsa::Module::Enum& e, std::ofstream& outputFile)
......
......@@ -85,6 +85,7 @@ namespace elsa
std::vector<std::unique_ptr<Record>> records;
std::set<std::string> includes;
std::set<std::string> pybindIncludes;
bool noPythonModule;
struct ModuleHints {
// path to hints file, empty if none is specified
......
......@@ -31,6 +31,11 @@ static llvm::cl::opt<std::string> pythonModuleName(
"pyname", llvm::cl::cat(bindingsOptions),
llvm::cl::desc("Name that should be given assigned to the generated python module. \"py\" "
"will be prepended to the value of the name option if not specified"));
static llvm::cl::opt<bool> noModule(
"no-module", llvm::cl::cat(bindingsOptions),
llvm::cl::desc("When specified the code for module generation will be omited, instead only the "
"binding definitions will be created. The definitions can later be added to an "
"arbitrary module by calling add_definitions_<pyname>(module)."));
static llvm::cl::extrahelp commonHelp(CommonOptionsParser::HelpMessage);
......@@ -624,9 +629,6 @@ public:
auto initListExpr = dyn_cast<InitListExpr>(
dyn_cast<InitListExpr>(varDecl->getInit())->getInit(0)->IgnoreImplicit());
llvm::outs() << initListExpr->getNumInits() << "\n";
initListExpr->getInit(0)->dump();
if (initListExpr) {
for (auto arg : initListExpr->inits()) {
......@@ -735,6 +737,8 @@ public:
int main(int argc, const char** argv)
{
noModule.setInitialValue(false);
CommonOptionsParser optionsParser(argc, argv, bindingsOptions);
assert((!moduleName.empty() || !pythonModuleName.empty())
......@@ -752,6 +756,8 @@ int main(int argc, const char** argv)
m.name = m.pythonName;
}
m.noPythonModule = noModule.getValue();
if (!hintsPath.empty()) {
const char* argv2[2];
argv2[0] = argv[0];
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment