Skip to content

Commit

Permalink
Build libclang as a static library and link it into LibClang
Browse files Browse the repository at this point in the history
  • Loading branch information
sethfowler committed Jun 13, 2013
1 parent bb67b9b commit b98dbf9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ config.status
src/Clang/Internal/FFI_stub_ffi.c
src/Clang/Internal/FFI_stub_ffi.h
cabal-dev
build/*
2 changes: 2 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
[submodule "llvm"]
path = llvm
url = git://github.com/sfowler/llvm.git
ignore = untracked
[submodule "clang"]
path = clang
url = git://github.com/sfowler/clang.git
ignore = untracked
40 changes: 39 additions & 1 deletion Setup.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,38 @@ main = defaultMainWithHooks simpleUserHooks
, postInst = myPostInst
}

libclangLibraries :: [String]
libclangLibraries =
[ "-lclang_static"
, "-lclangARCMigrate"
, "-lclangAST"
, "-lclangAnalysis"
, "-lclangBasic"
, "-lclangCodeGen"
, "-lclangDriver"
, "-lclangEdit"
, "-lclangFormat"
, "-lclangFrontend"
, "-lclangFrontendTool"
, "-lclangLex"
, "-lclangParse"
, "-lclangRewriteCore"
, "-lclangRewriteFrontend"
, "-lclangSema"
, "-lclangSerialization"
, "-lclangTooling"
, "-lLLVMSupport"
, "-lLLVMMCParser"
, "-lLLVMMC"
, "-lLLVMBitReader"
]

libClangConfHook (pkg, pbi) flags = do
lbi <- confHook simpleUserHooks (pkg, pbi) flags

putStrLn "Ensuring the build directory exists..."
system $ "mkdir -p build"

putStrLn "Linking clang repository into llvm repository..."
system $ "ln -sf `pwd`/clang llvm/tools/clang"

Expand All @@ -33,6 +62,8 @@ libClangConfHook (pkg, pbi) flags = do
++ " --disable-jit"
++ " --disable-docs"
++ " --enable-bindings=none"
++ " --disable-pic"
++ " --disable-shared"
++ " --prefix=`pwd`/out"

let lpd = localPkgDescr lbi
Expand All @@ -45,7 +76,7 @@ libClangConfHook (pkg, pbi) flags = do
--, extraLibs = extraLibs libbi ++ ["clang", "pthread"]
, includeDirs = includeDirs libbi ++ [curDir ++ "/build/out/include"]
, ccOptions = ccOptions libbi ++ ["-I.", "-I" ++ curDir ++ "/build/out/include"]
, ldOptions = ldOptions libbi ++ ["-Wl,-Bstatic", "-lclang", "-Wl,-Bdynamic"]
, ldOptions = ldOptions libbi ++ libclangLibraries
}

let lib' = lib { libBuildInfo = libbi' }
Expand All @@ -56,6 +87,13 @@ libClangConfHook (pkg, pbi) flags = do
libClangBuildHook pkg lbi flags ppHandlers = do
putStrLn "Building llvm and clang..."
system $ "cd build && make -j8 install"

-- OS X's linker _really_ wants to link dynamically, and it doesn't support
-- the options you'd usually use to control that on Linux. We rename the
-- libclang library to make sure the linker does what we intend.
putStrLn "Ensuring libclang is available as libclang_static..."
system $ "cd build/out/lib && test -e libclang.a && mv libclang.a libclang_static.a"

(buildHook simpleUserHooks) pkg lbi flags ppHandlers

libClangCleanHook pkg v hooks flags = do
Expand Down

2 comments on commit b98dbf9

@ghorn
Copy link
Contributor

@ghorn ghorn commented on b98dbf9 Jun 13, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you ensuring that it is built statically? I thought it only works in ghci if you enable shared? Also, I'm getting an error about needing -fPIC, see #25 (comment)

Maybe this is to deal with LD_LIBRARY_PATH problems?

@sethfowler
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the opposite, at least on OS X - it only works in ghci if it's built statically. I hope the reverse isn't true on linux!

Thanks for posting the log on the issue; I'm about to go to bed but I'll look into it tomorrow.

Please sign in to comment.