Skip to content

Commit 543d603

Browse files
committed
Add option to control use of the @native.link annotation
1 parent 1d34fdb commit 543d603

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed

bindgen/Main.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ int main(int argc, char *argv[]) {
3030
llvm::cl::opt<std::string> Package(
3131
"package", llvm::cl::cat(Category),
3232
llvm::cl::desc("Package name of generated Scala file"));
33+
llvm::cl::opt<bool> NoLinkName(
34+
"no-link", llvm::cl::cat(Category),
35+
llvm::cl::desc("Library does not require linking"));
36+
llvm::cl::opt<std::string> LinkName(
37+
"link", llvm::cl::cat(Category),
38+
llvm::cl::desc("Library to link with, e.g. -luv for libuv"));
3339
clang::tooling::CommonOptionsParser op(argc, (const char **)argv, Category);
3440
clang::tooling::ClangTool Tool(op.getCompilations(),
3541
op.getSourcePathList());
@@ -42,6 +48,14 @@ int main(int argc, char *argv[]) {
4248
return -1;
4349
}
4450

51+
auto linkName = LinkName.getValue();
52+
if (linkName.empty()) {
53+
linkName = libName;
54+
}
55+
if (NoLinkName.getValue()) {
56+
linkName = "";
57+
}
58+
4559
auto objectName = libName;
4660
if (objectName == "native") {
4761
/* there are at most 3 objects in the file.
@@ -56,7 +70,7 @@ int main(int argc, char *argv[]) {
5670

5771
locations.clear();
5872

59-
IR ir(libName, objectName, Package.getValue());
73+
IR ir(libName, linkName, objectName, Package.getValue());
6074
ScalaFrontendActionFactory actionFactory(ir);
6175

6276
int result = Tool.run(&actionFactory);

bindgen/ir/IR.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#include "IR.h"
22
#include "../Utils.h"
33

4-
IR::IR(std::string libName, std::string objectName, std::string packageName)
5-
: libName(std::move(libName)), objectName(std::move(objectName)),
6-
packageName(packageName) {
7-
}
4+
IR::IR(std::string libName, std::string linkName, std::string objectName,
5+
std::string packageName)
6+
: libName(std::move(libName)), linkName(std::move(linkName)),
7+
objectName(std::move(objectName)), packageName(packageName) {}
88

99
void IR::addFunction(std::string name, std::vector<Parameter> parameters,
1010
std::string retType, bool isVariadic) {
@@ -52,8 +52,11 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const IR &ir) {
5252
std::string objectName = handleReservedWords(ir.objectName);
5353

5454
if (!ir.libObjEmpty()) {
55-
s << "@native.link(\"" << ir.libName << "\")\n"
56-
<< "@native.extern\n"
55+
if (!ir.linkName.empty()) {
56+
s << "@native.link(\"" << ir.linkName << "\")\n";
57+
}
58+
59+
s << "@native.extern\n"
5760
<< "object " << objectName << " {\n";
5861

5962
for (const auto &typeDef : ir.typeDefs) {

bindgen/ir/IR.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
*/
1212
class IR {
1313
public:
14-
explicit IR(std::string libName, std::string objectName,
15-
std::string packageName);
14+
explicit IR(std::string libName, std::string linkName,
15+
std::string objectName, std::string packageName);
1616

1717
void addFunction(std::string name, std::vector<Parameter> parameters,
1818
std::string, bool isVariadic);
@@ -104,8 +104,9 @@ class IR {
104104

105105
bool existsFunctionWithName(std::string functionName);
106106

107-
std::string libName; // name of the library
108-
std::string objectName; // name of Scala object
107+
std::string libName; // name of the library
108+
std::string linkName; // name of the library to link with
109+
std::string objectName; // name of Scala object
109110
std::vector<Function> functions;
110111
std::vector<TypeDef> typeDefs;
111112
std::vector<Struct> structs;

0 commit comments

Comments
 (0)