在windows上构建LLVM 7.0.1

关于在windows上构建LLVM,网上有不少文章,但都是互相抄来的,写作时极不认真,不是少这个,就是少那个,没有一篇是可以完整照着做下来的,实在气人。

本文的安装和配置过程,我亲自操作过好几遍,不惜为此重装操作系统,相信大家只要按照步骤严格操作,一般不会出意外的。

整个过程预计耗时4~5小时,具体取决于网速和机器配置,请大家在操作之前做好心里准备。

我的操作系统是Win7x64,计划以x64为目标平台来构建。

准备清单:

1、Visual Studio 2017 Community

2、LLVM-7.0.1-win64 src

    【LLVM】http://releases.llvm.org/7.0.1/llvm-7.0.1.src.tar.xz
    【CLang】http://releases.llvm.org/7.0.1/cfe-7.0.1.src.tar.xz
    【Compiler-rt】http://releases.llvm.org/7.0.1/compiler-rt-7.0.1.src.tar.xz

3、CMake-3.14.0 x64

4、python-3.7.1-amd64

5、strawberry-perl-5.28.1.1-64bit

6、GetGnuWin32-0.6.3

准备上述资源时,请务必保证版本号不低于上述所列要求,最好和我的保持一致。

安装步骤:

1、安装CMake,安装时注意勾选:把bin路径加入PATH

2、安装python,安装时注意勾选:把bin路径加入PATH

     如果忘了加入PATH,后面CMake配置VS工程会失败。

3、安装strawberry-perl,这也是CMake依赖的,它在安装后会自动加入PATH。

4、安装GetGnuWin32,把bin路径加入PATH

    进入bin目录,执行download.bat,下载所有组件,耗时很长,慢慢等待。

    一定要等所有组件全部下载完,否则,后面CMake配置VS工程会失败。

5、安装VS2017,把C++桌面开发通用桌面开发,都勾选上,里面有C++编译器和CMake相关工具。

6、解压缩源代码:

     解压缩llvm-7.0.1.src.tar.xz,调整目录为 E:/llvm

     解压缩cfe-7.0.1.src.tar.xz,调整目录为E:/llvm/tools/clang

     解压缩compiler-rt-7.0.1.src.tar.xz,调整目录为E:/llvm/projects/clang

7、运行VS2017的x64 本机工具命令行程序,执行以下命令来生成VS解决方案:

C:Program Files (x86)Microsoft Visual Studio2017Community> e:
E:> cd llvm
E:llvm> mkdir build
E:llvmuild> cd build
E:llvmuild> cmake -G "Visual Studio 15 Win64" e:/llvm

注意,Visual Studio 15表示VS2017,Win64表示目标平台是x64。

如果是64位平台,请务必选Win64,否则编译出的程序无法运行!

顺便说一句,网上几乎所有文章都会告诉你用 -Thost=x64来设置目标x64平台,实际操作下来,根本无效!请严格按照我这里的操作。

CMake生成过程中,肯定会出现很多looking for xxx - not found的提示,没关系,不影响构建。

如果构建顺利,最后会显示如下结果:

-- Configuring done
-- Generating done
-- Build files have been written to: E:/llvm/build

8、打开VS2017,打开解决方案 E:/llvm/build/LLVM.sln,切换为Release模式。

     找到CMakePredefinedTargets目录下的ALL_BUILD工程,开始构建,耗时非常非常长,耐心等待!

     构建完成后,输出的bin和lib在以下位置:E:/llvm/build/Release

9、确认编译出的bin是x86还是x64的

C:Program Files (x86)Microsoft Visual Studio2017Community> clang -v 

clang version 7.0.1(tags/RELEASE_701/final)

Target:x86_64-pc-windows-msvc

Thread model: posix

InstallerDir: E:llvmuildReleasein

10、编译测试程序:

#include <stdio.h>

int main() {
    printf("Hello world!");
    return 0;
}

 运行VS2017的x64 本机工具命令行程序,通过以下命令进行编译:

C:Program Files (x86)Microsoft Visual Studio2017Community> D:
D:> clang --target=x86_64-pc-windows-msvc hello.c -o hello.exe
D:> hello.exe
Hello world!

如果包含中文,会有乱码问题。代码如下:

#include <stdio.h>

int main() {
    printf("你好 world!");
    return 0;
}

做了各种尝试,结论如下:

1、把文件保存为UTF-8格式是没有用的!

2、调用setlocale函数也是没有用的!

可行方案如下:

把文件保存为ASCII格式,编译时产生一个警告:

warning: illegal character encoding in string literal

    [-Winvalid-source-encoding]

需要带上一个参数,忽略这个警告。如下:

C:Program Files (x86)Microsoft Visual Studio2017Community> D:
D:> clang --target=x86_64-pc-windows-msvc -Wno-invalid-source-encoding hello.c -o hello.exe
D:> hello.exe
你好 world!

最后再测试一下lli.exe

C:Program Files (x86)Microsoft Visual Studio2017Community> D:
D:> clang -emit-llvm -c hello.c -o hello.bc -Wno-invalid-source-encoding
D:> lli hello.bc
你好 world!
原文地址:https://www.cnblogs.com/lavezhang/p/10398579.html