-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Expose clang resource/include directory through CMake variable #58918
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Normal usage patterns of libclang should normally find clang's resource directory without requiring you to explicitly pass it in. If that isn't happening, something has gone wrong. See CIndexer::getClangResourcesPath. |
Here is how I am using libclang database = clang_CompilationDatabase_fromDirectory(builddir, &error);
if (error) {
fprintf(stderr, "Failed to read the compilation database\n");
return EX_IOERR;
} int
mock_collect(const char *file)
{
char **argv;
CXIndex index;
CXCursor cursor;
unsigned int argc;
enum CXErrorCode ret;
CXTranslationUnit tu;
unsigned int real_argc;
CXCompileCommand command;
CXCompileCommands commands;
commands = clang_CompilationDatabase_getCompileCommands(database, file);
if (clang_CompileCommands_getSize(commands) != 1) {
fprintf(stderr, "More than one series of compile commands detected for %s\n", file);
return EX_SOFTWARE;
}
command = clang_CompileCommands_getCommand(commands, 0);
argc = clang_CompileCommand_getNumArgs(command);
argv = calloc(argc + 2, sizeof(*argv));
argv[0] = "-DHSE_MOCK=__attribute__((__annotate__(\"mock\")))";
argv[1] = "-I";
argv[2] = includedir; // $resourcedir/include
real_argc = 3;
for (unsigned int i = 1; i < argc; i++) {
CXString arg = clang_CompileCommand_getArg(command, i);
const char *arg_data = clang_getCString(arg);
/* Ignore warnings */
if (strstr(arg_data, "-W") == arg_data)
continue;
argv[real_argc++] = strdup(arg_data);
clang_disposeString(arg);
}
index = clang_createIndex(0, 1);
ret = clang_parseTranslationUnit2(index, file, (const char **)argv, real_argc - 1, NULL, 0, 0,
&tu);
if (ret) {
fprintf(stderr, "Failed to parse the translation unit: %d\n", ret);
return EX_SOFTWARE;
}
cursor = clang_getTranslationUnitCursor(tu);
clang_visitChildren(cursor, visit_tu, NULL);
clang_disposeTranslationUnit(tu);
clang_disposeIndex(index);
clang_CompileCommands_dispose(commands);
for (unsigned int i = 3; i < real_argc; i++)
free(argv[i]);
free(argv);
return 0;
} I just started using libclang, so I don't know what I might be doing wrong. I can point at the repository this exists in, but this is pretty much everything. |
I tried switching to |
Here is a command like which I pass to
Note that the last 2 arguments are me trying to work around this issue |
Looks like the relevant bits were last touched in https://reviews.llvm.org/D124815 . CC @aykevl |
I appreciate your help up to this point with my issue :) |
@efriedma-quic true, but the patch should only have an effect when |
I am using libclang to parse C source files. Given a small program like:
libclang should report
fatal error: 'stddef.h' file not found
.On my system (Fedora 36), the resource directory is located:
Which means the include directory is located at
$(clang -print-resource-dir)/include)
. The clang cmake modules already expose a variable calledCLANG_INCLUDE_DIRS
, but the only directory exposed in this variable is$CMAKE_INSTALL_PREFIX/include
, which isn't helpful for what I need.Ideally these constants would be exposed in a cross buildsystem way like #9777, but I am willing to settle for just appending to the
CLANG_INCLUDE_DIRS
array since that should be as little effort as possible to solve what I need.The text was updated successfully, but these errors were encountered: