Skip to content

Commit 0961da8

Browse files
authored
Fix include path bugs from libclang on OSX with llvm 10 (#1162)
On OSX, with LLVM 10, it seems that there can be a double linkage of the libc++ that is built with llvm and the one in /usr/lib. This leads to a very subtle problem where oslc's use of libclang for preprocessing is unable to find included files when there are multiple include search paths (!), unless you have the llvm lib directory first in your DYLD_LIBRARY_PATH. That is obviously too brittle a fix. Solve by forcing use of statically linked llvm/clang libraries when on OSX and using LLVM+clang 10+, even if the user did not explicitly request that. Signed-off-by: Larry Gritz <lg@larrygritz.com>
1 parent 537b492 commit 0961da8

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/cmake/modules/FindLLVM.cmake

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,25 @@ foreach (COMPONENT clangFrontend clangDriver clangSerialization
9191
endforeach ()
9292

9393

94+
############ HACK ##############
95+
# On OSX, the Homebrew (and maybe any build) of LLVM 10.0 seems to have a
96+
# link conflict with its dependency on the llvm libc++ and the system
97+
# libc++, both can end up dynamically linked and lead to very subtle and
98+
# frustrating behavior failures (in particular, osl's use of libclang will
99+
# botch include file parsing any time LD_LIBRARY_PATH doesn't have the llvm
100+
# libc++ first).
101+
#
102+
# It seems that this is not a problem when linking against the llvm and
103+
# libclang libraries statically. So on apple and when LLVM 10+ are involved,
104+
# just force that choice. Other than larger execubales, it seems harmless,
105+
# and in any case a better choice than this beastly bug.
106+
#
107+
# We can periodically revisit this with new version of LLVM, maybe they will
108+
# fix things and we won't require this preemptive static linking.
109+
if (APPLE AND LLVM_VERSION VERSION_GREATER_EQUAL 10.0)
110+
set (LLVM_STATIC ON)
111+
endif ()
112+
94113
# shared llvm library may not be available, this is not an error if we use LLVM_STATIC.
95114
if ((LLVM_LIBRARY OR LLVM_LIBRARIES OR LLVM_STATIC) AND LLVM_INCLUDES AND LLVM_DIRECTORY AND LLVM_LIB_DIR)
96115
if (LLVM_STATIC)
@@ -100,8 +119,10 @@ if ((LLVM_LIBRARY OR LLVM_LIBRARIES OR LLVM_STATIC) AND LLVM_INCLUDES AND LLVM_D
100119
execute_process (COMMAND ${LLVM_CONFIG} --libfiles --link-static
101120
OUTPUT_VARIABLE LLVM_LIBRARIES
102121
OUTPUT_STRIP_TRAILING_WHITESPACE)
103-
string (REPLACE " " ";" LLVM_LIBRARIES "${LLVM_LIBRARIES}")
104-
set (LLVM_LIBRARY "")
122+
if (LLVM_LIBRARIES)
123+
string (REPLACE " " ";" LLVM_LIBRARIES "${LLVM_LIBRARIES}")
124+
set (LLVM_LIBRARY "")
125+
endif ()
105126
else ()
106127
set (LLVM_LIBRARIES "${LLVM_LIBRARY}")
107128
endif ()

src/liboslcomp/oslcomp.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ OSLCompilerImpl::preprocess_buffer (const std::string &buffer,
203203
headerOpts.UseBuiltinIncludes = 0;
204204
headerOpts.UseStandardSystemIncludes = 0;
205205
headerOpts.UseStandardCXXIncludes = 0;
206+
// headerOpts.Verbose = 1;
206207
std::string directory = OIIO::Filesystem::parent_path(filename);
207208
if (directory.empty())
208209
directory = OIIO::Filesystem::current_path();

0 commit comments

Comments
 (0)