Skip to content

Commit 6a1e4d0

Browse files
committed
Add user defined libraries to interpreted souffle
1 parent 293f6a2 commit 6a1e4d0

File tree

6 files changed

+37
-47
lines changed

6 files changed

+37
-47
lines changed

src/Interpreter.h

+35-12
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@
3636
#include <vector>
3737
#include <dlfcn.h>
3838

39-
#define SOUFFLE_DLL "libfunctors.so"
40-
4139
namespace souffle {
4240

4341
class InterpreterProgInterface;
@@ -75,16 +73,38 @@ class Interpreter {
7573
}
7674

7775
if (!Global::config().has("libraries")) {
78-
Global::config().set("libraries", SOUFFLE_DLL);
76+
Global::config().set("libraries", "functors");
77+
}
78+
if (!Global::config().has("library-dir")) {
79+
Global::config().set("library-dir", ".");
7980
}
80-
std::cout << "libraries: <" << Global::config().get("libraries") << ">" << std::endl;
81-
for (const std::string& library : splitString(Global::config().get("libraries"), ',')) {
82-
auto tmp = dlopen(library.c_str(), RTLD_LAZY);
83-
std::cout << tmp << std::endl;
84-
dll.push_back(tmp);
85-
if (dll.back() == nullptr) {
86-
std::cerr << "Cannot find '" << library << "' DLL" << std::endl;
87-
exit(1);
81+
for (const std::string& library : splitString(Global::config().get("libraries"), ' ')) {
82+
// The library may be blank
83+
if (library.empty()) {
84+
continue;
85+
}
86+
auto paths = splitString(Global::config().get("library-dir"), ' ');
87+
// Set up our paths to have a library appended
88+
for (std::string& path : paths) {
89+
if (path.back() != '/') {
90+
path += '/';
91+
}
92+
}
93+
94+
if (library.find('/') != std::string::npos) {
95+
paths.clear();
96+
}
97+
98+
paths.push_back("");
99+
100+
void* tmp = nullptr;
101+
for (const std::string& path : paths) {
102+
std::string fullpath = path + "lib" + library + ".so";
103+
tmp = dlopen(fullpath.c_str(), RTLD_LAZY);
104+
if (tmp != nullptr) {
105+
dll.push_back(tmp);
106+
break;
107+
}
88108
}
89109
}
90110

@@ -94,7 +114,10 @@ class Interpreter {
94114
void* getMethodHandle(const std::string& method) {
95115
// load DLLs (if not done yet)
96116
for (void* libHandle : loadDLL()) {
97-
return dlsym(libHandle, method.c_str());
117+
auto* methodHandle = dlsym(libHandle, method.c_str());
118+
if (methodHandle != nullptr) {
119+
return methodHandle;
120+
}
98121
}
99122
return nullptr;
100123
}

src/LVM.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -464,8 +464,7 @@ void LVM::execute(std::unique_ptr<LVMCode>& codeStream, InterpreterContext& ctxt
464464

465465
auto fn = reinterpret_cast<void (*)()>(getMethodHandle(name));
466466
if (fn == nullptr) {
467-
std::cerr << "Cannot find user-defined operator " << name << " in " << SOUFFLE_DLL
468-
<< std::endl;
467+
std::cerr << "Cannot find user-defined operator " << name << std::endl;
469468
exit(1);
470469
}
471470

src/LVM.h

-2
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@
3737
#include <vector>
3838
#include <dlfcn.h>
3939

40-
#define SOUFFLE_DLL "libfunctors.so"
41-
4240
namespace souffle {
4341

4442
class InterpreterProgInterface;

src/LVMGenerator.h

-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
#include "LVMCode.h"
1919
#include "RamVisitor.h"
2020

21-
#define SOUFFLE_DLL "libfunctors.so"
22-
2321
namespace souffle {
2422

2523
/**

src/RAMI.h

-28
Original file line numberDiff line numberDiff line change
@@ -141,34 +141,6 @@ class RAMI : public Interpreter {
141141
environment[ramRel2.getName()] = rel1;
142142
}
143143

144-
/** Load dll */
145-
const std::vector<void*>& loadDLL() {
146-
if (!dll.empty()) {
147-
return dll;
148-
}
149-
150-
if (!Global::config().has("libraries")) {
151-
Global::config().set("libraries", SOUFFLE_DLL);
152-
}
153-
for (const std::string& library : splitString(Global::config().get("libraries"), ',')) {
154-
dll.push_back(dlopen(library.c_str(), RTLD_LAZY));
155-
if (dll.back() == nullptr) {
156-
std::cerr << "Cannot find '" << library << "' DLL" << std::endl;
157-
exit(1);
158-
}
159-
}
160-
161-
return dll;
162-
}
163-
164-
void* getMethodHandle(const std::string& method) {
165-
// load DLLs (if not done yet)
166-
for (void* libHandle : loadDLL()) {
167-
return dlsym(libHandle, method.c_str());
168-
}
169-
return nullptr;
170-
}
171-
172144
private:
173145
friend InterpreterProgInterface;
174146

src/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ int main(int argc, char** argv) {
190190
{"generate", 'g', "FILE", "", false,
191191
"Generate C++ source code for the given Datalog program and write it to "
192192
"<FILE>."},
193-
{"library-dir", 'L', "DIR", ".", true, "Specify directory for library files."},
193+
{"library-dir", 'L', "DIR", "", true, "Specify directory for library files."},
194194
{"libraries", 'l', "FILE", "", true, "Specify libraries."},
195195
{"no-warn", 'w', "", "", false, "Disable warnings."},
196196
{"magic-transform", 'm', "RELATIONS", "", false,

0 commit comments

Comments
 (0)