用CMake的方始來編譯LLVM的Kaleidoscope範例
用CMake的方始來編譯LLVM的Kaleidoscope範例
最近在練習LLVM的Kaleidoscope的範例,他的範例是用指令來編譯整個程式,但是我想說
改成用cmake來編譯,但是一直發生find_package(LLVM)有問題,整個訊息就像以下一樣
CMake Warning at CMakeLists.txt:4 (find_package):
By not providing "FindLLVM.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "LLVM", but
CMake did not find one.
Could not find a package configuration file provided by "LLVM" with any of
the following names:
LLVMConfig.cmake
llvm-config.cmake
Add the installation prefix of "LLVM" to CMAKE_PREFIX_PATH or set
"LLVM_DIR" to a directory containing one of the above files. If "LLVM"
provides a separate development package or SDK, be sure it has been
installed.
因為我的系統是Ubunut 13.10,我也確定了我有裝LLVM的Library,一直花了很多時間在
改CMakeLists.txt,也一直找不到問題。後來搜尋找到一封email(就是Ref.1)裡面說明了
因為一些原因Ubuntu LLVM/Clang development套件沒有包含正確的CMake module檔案,
所以變成我們要自己手動安裝LLVM跟Clang,那安裝的方式就在下面。
手動安裝LLVM
$> tar xaf llvm-3.4.tar.gz
$> tar xaf clang-3.4.tar.gz
$> cp -R clang-3.4 llvm-3.4/tools/clang
$> cd llvm-3.4
$> mkdir build
$> cd build
我是將LLVM安裝在/opt/llvm,如果你要改目錄的話就改-DCMAKE_INSTALL_PREFIX
$> cmake -DCMAKE_PREFIX_PATH='/opt/llvm' -G 'Unix Makefiles' . ../
$> make
$> make install
Kaleidoscope範例的架構
.
├── build
├── CMakeLists.txt
├── main.cc
└── README.md
CMakeLists.txt Example
在自己的專案下建立CMakeLists.txt,並將下面的內容寫到裡面
cmake_minimum_required(VERSION 2.8)
project(main)
find_package(LLVM)
include_directories( ${LLVM_INCLUDE_DIRS} )
link_directories( ${LLVM_LIBRARY_DIRS} )
set(CMAKE_CXX_FLAGS "-O0 -ggdb -D__STDC_CONSTANT_MACROS -D__STDC_LIMIT_MACROS")
set(CMAKE_CXX_FLAGS_DEBUG "-O0 -ggdb")
set(CMAKE_CXX_FLAGS_RELEASE "-O0 -ggdb")
llvm_map_components_to_libraries(REQ_LLVM_LIBRARIES jit native)
add_executable(main main.cc)
target_link_libraries(main ${REQ_LLVM_LIBRARIES})
編譯
建立好CMakeLists.txt之後,再來就是開始編譯Kaleidoscope啦
$> mkdir build & cd build
$> cmake -DCMAKE_PREFIX_PATH='/opt/llvm' -G 'Unix Makefiles' . ../
結語
第一次用CMAKE在做編譯真的花了我不少時間去理解CMAKE的語法,但是最後知道怎麼用後之後
真的發覺比用autoconf, automake還方便多了。
留言