forked from mesonbuild/meson
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dependencies/clang: Add a dependency for Clang
Clang has two ways to be found, CMake, and by hand. Clang has some hurdles of use because of the way it's installed on many Linux distros, either in a separate prefix, in a prefix with LLVM, or in a common prefix, which requires some amount of effort to make it work.
- Loading branch information
Showing
10 changed files
with
436 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
## A Clang dependency | ||
|
||
This helps to simplify the use of libclang, removing the need to try cmake and | ||
then falling back to not cmake. It also transparently handles the issues | ||
associated with different paths to find Clang on different OSes. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright © 2024 Intel Corporation | ||
*/ | ||
|
||
#include <clang-c/Index.h> | ||
|
||
#include <stdio.h> | ||
|
||
int main(int argc, char * argv[]) { | ||
if (argc < 2) { | ||
fprintf(stderr, "At least one argument is required!\n"); | ||
return 1; | ||
} | ||
|
||
const char * file = argv[1]; | ||
|
||
CXIndex index = clang_createIndex(0, 0); | ||
CXTranslationUnit unit = clang_parseTranslationUnit( | ||
index, | ||
file, NULL, 0, | ||
NULL, 0, | ||
CXTranslationUnit_None); | ||
|
||
if (unit == NULL) { | ||
return 1; | ||
} | ||
|
||
clang_disposeTranslationUnit(unit); | ||
clang_disposeIndex(index); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
* Copyright (c) 2010, Larry Olson | ||
* Copyright © 2024 Intel Corporation | ||
* | ||
* Taken from: https://github.com/loarabia/Clang-tutorial/blob/master/CItutorial2.cpp | ||
* | ||
* With updates for LLVM and Clang 18 | ||
*/ | ||
|
||
#include "llvm/Config/llvm-config.h" | ||
|
||
#if LLVM_VERSION_MAJOR >= 18 | ||
#include "llvm/TargetParser/Host.h" | ||
#else | ||
#include "llvm/Support/Host.h" | ||
#endif | ||
#include "llvm/ADT/IntrusiveRefCntPtr.h" | ||
|
||
#include "clang/Basic/DiagnosticOptions.h" | ||
#include "clang/Frontend/TextDiagnosticPrinter.h" | ||
#include "clang/Frontend/CompilerInstance.h" | ||
#include "clang/Basic/TargetOptions.h" | ||
#include "clang/Basic/TargetInfo.h" | ||
#include "clang/Basic/FileManager.h" | ||
#include "clang/Basic/SourceManager.h" | ||
#include "clang/Lex/Preprocessor.h" | ||
#include "clang/Basic/Diagnostic.h" | ||
|
||
#include <iostream> | ||
|
||
/****************************************************************************** | ||
* | ||
*****************************************************************************/ | ||
int main(int argc, const char * argv[]) | ||
{ | ||
using clang::CompilerInstance; | ||
using clang::TargetOptions; | ||
using clang::TargetInfo; | ||
#if LLVM_VERSION_MAJOR >= 18 | ||
using clang::FileEntryRef; | ||
#else | ||
using clang::FileEntry; | ||
#endif | ||
using clang::Token; | ||
using clang::DiagnosticOptions; | ||
using clang::TextDiagnosticPrinter; | ||
|
||
if (argc != 2) { | ||
std::cerr << "Need exactly 2 arguments." << std::endl; | ||
return 1; | ||
} | ||
|
||
CompilerInstance ci; | ||
DiagnosticOptions diagnosticOptions; | ||
ci.createDiagnostics(); | ||
|
||
std::shared_ptr<clang::TargetOptions> pto = std::make_shared<clang::TargetOptions>(); | ||
pto->Triple = llvm::sys::getDefaultTargetTriple(); | ||
TargetInfo *pti = TargetInfo::CreateTargetInfo(ci.getDiagnostics(), pto); | ||
ci.setTarget(pti); | ||
|
||
ci.createFileManager(); | ||
ci.createSourceManager(ci.getFileManager()); | ||
ci.createPreprocessor(clang::TU_Complete); | ||
|
||
|
||
#if LLVM_VERSION_MAJOR >= 18 | ||
const FileEntryRef pFile = ci.getFileManager().getFileRef(argv[1]).get(); | ||
#else | ||
const FileEntry *pFile = ci.getFileManager().getFile(argv[1]).get(); | ||
#endif | ||
ci.getSourceManager().setMainFileID( ci.getSourceManager().createFileID( pFile, clang::SourceLocation(), clang::SrcMgr::C_User)); | ||
ci.getPreprocessor().EnterMainSourceFile(); | ||
ci.getDiagnosticClient().BeginSourceFile(ci.getLangOpts(), | ||
&ci.getPreprocessor()); | ||
Token tok; | ||
bool err; | ||
do { | ||
ci.getPreprocessor().Lex(tok); | ||
err = ci.getDiagnostics().hasErrorOccurred(); | ||
if (err) break; | ||
ci.getPreprocessor().DumpToken(tok); | ||
std::cerr << std::endl; | ||
} while ( tok.isNot(clang::tok::eof)); | ||
ci.getDiagnosticClient().EndSourceFile(); | ||
|
||
return err ? 1 : 0; | ||
} |
Oops, something went wrong.