Building and using a pass for LLVM 3.8 on OSX

笑着哭i 提交于 2019-12-01 07:44:04

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

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.

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

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

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.

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 $@

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 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).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!