从学长那白嫖来的Ubuntu22.04搭建llvm环境笔记,留个记录,怕后面要用找不到了

从学长那白嫖来的搭建llvm 15笔记

Building

(Prerequisite) LLVM

  1. Instal prerequisites.
 sudo apt install git
 sudo apt install cmake
  1. Make a new drectory and move into it.
 mkdir llvm
 cd llvm
  1. Clone LLVM 15.
 git clone -b llvmorg-15.0.7 https://github.com/llvm/llvm-project

if it is too slow to dowload from github , you can dowload from gitee

git clone -b llvmorg-15.0.7 https://gitee.com/mirrors/LLVM.git

also you can dowload the zip file

链接:https://pan.baidu.com/s/1pOyKoTCfEq-SzBMgH1K_ww?pwd=9999 
提取码:9999 
  1. Make a build directory and move into it.
$ cd <llvm_dir>
$ mkdir build
$ cd build
  1. build llvm (c.f., this will generate Makefile here).

llvm build目录下:
1、cmake -DLLVM_TARGETS_TO_BUILD=X86 -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX= ( p w d ) / . . / i n s t a l l − D L L V M E N A B L E P R O J E C T S = " c l a n g ; c l a n g − t o o l s − e x t r a " − G " U n i x M a k e f i l e s " − D L L V M E N A B L E A S S E R T I O N S = O N − D C M A K E C X X F L A G S = (pwd)/../install -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra" -G "Unix Makefiles" -DLLVM_ENABLE_ASSERTIONS=ON -DCMAKE_CXX_FLAGS= (pwd)/../installDLLVMENABLEPROJECTS="clang;clangtoolsextra"G"UnixMakefiles"DLLVMENABLEASSERTIONS=ONDCMAKECXXFLAGS={CMAKE_CLANG_FLAGS} -DBUILD_SHARED_LIBS=ON -DCLANG_ENABLE_STATIC_ANALYZER=ON -DLLVM_ENABLE_PLUGINS=ON …/llvm
2、 make install -j8 (8是线程数)

注意自己的线程数小于等于逻辑cpu个数 不要太多 不然可能会卡死

  1. Set environment variables.

add this to .bashrc file

 export PATH=$PATH:$LLVM_BUILD_DIR/bin

例子:export PATH=$PATH:/home/gzy/gzy_work_second/cpp/llvm/LLVM/build/bin

check llvm version: llvm-config --version 看到15.0.7就是安装好了

往下是笔记,请按自己的需求参考

Plugins

$ cd <this/repo/dir>
$ mkdir build
$ cd build
$ cmake -DLLVM_DIR=$LLVM_BUILD_DIR/lib/cmake/llvm/ ..  && make

Running

AST Visitor (PrintFunction)

clang -fplugin=<this/repo/dir>/build/lib/libPrintFunc.so \
    -c <this/repo/dir>/test/functions.c

example: clang -fplugin=/home/gzy/gzy_work/cpp/clang_plugins/clang-plugin-template/build/lib/libPrintFunction.so -c /home/gzy/gzy_work/cpp/clang_plugins/clang-plugin-template/test/functions.c

AST Matcher (explicit cast)

    <this/repo/dir>/build/bin/cast-match <this/repo/dir>/test/is_cast.c  

注意 此处可能会出现找不到头文件的情况,解决方法如下:

1、clang -E -x c - -v < /dev/null
2、将#include <…> 中的查找路径都加入到.bashrc文件中,如下:
查找路径:
#include <…> search starts here:
/home/gzy/gzy_work_second/cpp/llvm/LLVM/build/lib/clang/15.0.7/include
/usr/local/include
/usr/include/x86_64-linux-gnu
/usr/include
End of search list.
添加示例:
export MY_INCLUDES=“-I/home/gzy/gzy_work_second/cpp/llvm/LLVM/build/lib/clang/15.0.7/include -I/usr/local/include -I/usr/include/x86_64-linux-gnu -I/usr/include”

此时命令如下:

    <this/repo/dir>/build/bin/cast-match <this/repo/dir>/test/is_cast.c  -- ${MY_INCLUDES}

example: /home/gzy/gzy_work/cpp/clang_plugins/clang-plugin-template/build/bin/cast-match /home/gzy/gzy_work/cpp/clang_plugins/clang-plugin-template/test/is_cast.c – ${MY_INCLUDES}

Checker (MainCallChecker)

clang -fsyntax-only -fplugin=<this/repo/dir>/build/lib/libMainCallChecker.so \
    -Xclang -analyze -Xclang -analyzer-checker=demo.MainCallChecker \
    <this/repo/dir>/test/maincall.c

example: clang -fsyntax-only -fplugin=/home/gzy/gzy_work/cpp/clang_plugins/clang-plugin-template/build/lib/libMainCallChecker.so -Xclang -analyze -Xclang -analyzer-checker=demo.MainCallChecker /home/gzy/gzy_work/cpp/clang_plugins/clang-plugin-template/test/maincall.c

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值