在OSX上构建和使用LLVM 3.8的通行证 [英] Building and using a pass for LLVM 3.8 on OSX

查看:227
本文介绍了在OSX上构建和使用LLVM 3.8的通行证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用llvm 3.8构建并在OSX上应用通行证. 我使用带有以下公式的brew安装了llvm 3.8: 通行证内有以下内容:

I'm trying to build and apply a pass on OSX using llvm 3.8. I installed llvm 3.8 using brew with this formula: $brew install llvm38 Inside the pass I have the following:

static RegisterPass<SwiftPass> X("pass", "My Pass");

用于构建通行证的我的Makefile如下所示:

My Makefile to build the pass looks like this:

LIB_NAME = pass$(SUFIX)
LDFLAGS = $(shell $(LLVM_PATH)llvm-config --ldflags)
CXXFLAGS = -g -Wall -fno-rtti -fPIC -std=c++11 -shared -dynamiclib $(shell $(LLVM_PATH)llvm-config  --cxxflags --system-libs --libs core executionengine interpreter mc support nativecodegen)
COMPILER = clang++
all: $(LIB_NAME)
$(LIB_NAME): $(OBJ)
    $(COMPILER) $(CXXFLAGS) $(LDFLAGS) $^ -o $@

如果给我一些clang: warning: -l[some lib]: 'linker' input unused.它还给了我

If gives me a few clang: warning: -l[some lib]: 'linker' input unused. It also gives me:

clang: warning: argument unused during compilation: '-shared'
clang: warning: argument unused during compilation: '-dynamiclib'

但是它输出一个.dylib和.o,所以我继续尝试一下. 所以现在有了通行证,我可以将其应用到我的.bc文件,为此,我使用这样的Makefile(我为简洁起见省略了定义):

But it outputs a .dylib and .o so I proceed to try it out. So now that I have a pass I can apply it to my .bc file and for this I use a Makefile like this (I omited the definitions for brevity):

$(LLVM_OPT) -load $(PASSFILE) $(PASSNAME) $(NAME).bc -o $(NAME).afterMyPass.bc

这意味着:

opt -load pass.dylib -pass int.bc -o int.afterMyPass.bc

并将其退还给我:

opt: Unknown command line argument '-pass'.  Try: '/usr/local/Cellar/llvm38/3.8.0/lib/llvm-3.8/bin/opt -help'
opt: Did you mean '-slsr'?
make: *** [pass] Error 1

我的猜测是这是由编译期间未使用的参数引起的,但是我不知道为什么首先要使用它们,因为构建共享库需要它们.

My guess is that this is caused by the unused arguments during compilation but I don't know why they are unused in the first place since they are required to build a shared library.

我在做什么错,我该如何解决?

What am I doing wrong and how can I fix this?

推荐答案

我已经解决了该问题. Makefile存在两个问题.

I have resolved the issue. There are two issues with your Makefile.

首先,如果您是平面命名空间,则需要通过传递ld标志来抑制未定义符号.或在使用两级命名空间时使用动态查找进行链接.

First you need to suppress the undefine symbols by passing ld flags if you are flat namespace. Or use dynamic look up for linking while using two-level namespace.

第二个问题在您解决第一个问题时发生.导致在执行opt的过程中使用平面命名空间:

Second issue happens when you resolve the first issue. It causes in flat namespace during execution of opt:

命令行错误:选项'pass-remarks'已多次注册! LLVM错误:已注册的命令行选项不一致

CommandLine Error: Option 'pass-remarks' registered more than once! LLVM ERROR: inconsistency in registered CommandLine options

您不需要将--libs传递给llvm-config.它会导致与来自opt和您的pass的符号发生冲突.在二级名称空间中,不会出现此错误.只是您的选项是Unknown命令行参数,如先前的错误.

You don't need to pass --libs to llvm-config. It causes conflict with symbols coming from opt and your pass. In two level name space, it won't give this error. Just that your option is Unknown command line argument like previous error.

我的Makefile(对于默认的两层名称空间,我也有一个ldflag注释.LLVM项目使用平面名称空间,因此我使用了该名称):

My Makefile (I have a commented ldflag also for two-level namespace which is default. LLVM project uses flat namespace, so I have used that):

SUFIX=.so
OBJ=pass.o
LIB_NAME = pass$(SUFIX)
LLVM_PATH=/usr/local/Cellar/llvm38/3.8.0/bin/
LDFLAGS = $(shell $(LLVM_PATH)llvm-config-3.8 --ldflags)
LDFLAGS +=-Wl,-dead_strip -Wl,-flat_namespace -Wl,-undefined -Wl,suppress
#LDFLAGS += -Wl,-dead_strip -Wl,-undefined,dynamic_lookup
CXXFLAGS = -g -Wall -fno-rtti -fPIC -std=c++11 $(shell $(LLVM_PATH)llvm-config-3.8  --cxxflags --system-libs)
COMPILER = clang++
all: $(LIB_NAME)
$(LIB_NAME): $(OBJ)
        $(COMPILER) $(CXXFLAGS) -bundle $(LDFLAGS) $^ -o $@
$(OBJ): pass.cpp
        $(COMPILER) $(CXXFLAGS) -c $^ -o $@

我做了一个小的改动-bundle只是为了正确起见,因为它不是共享库,而是可加载的包,不能作为库链接.如果您不添加它,那没关系,但是您的输出不会被捆绑.

One other minor change that I did is added -bundle just for sake of correctness because it's not shared library but loadable bundle which can't be linked against as a library. If you don't add it, it doesn't matter but your output isn't bundle.

Stackoverflow导致Makefile问题.复制时,将制表符替换为空格.另请参阅: https://developer .apple.com/library/mac/documentation/DeveloperTools/Conceptual/MachOTopics/1-Articles/executing_files.html 了解为什么需要压制.

Stackoverflow is causing problem with makefile. While copying replace tabs with spaces. Also refer this: https://developer.apple.com/library/mac/documentation/DeveloperTools/Conceptual/MachOTopics/1-Articles/executing_files.html for why you need to suppress.

我真的很喜欢这个问题(即使花了我整整半天的时间来理解和解决).

I really enjoyed this problem(even though it took me whole 1 and half day to understand and resolve).

这篇关于在OSX上构建和使用LLVM 3.8的通行证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆