The clang-cl /fallback mode

介绍了Clang-CL,一种使Clang能在Windows平台上理解与Visual Studio编译器相同命令行参数的驱动模式。通过使用/fallback标志,即使部分代码无法被Clang编译,也能回退到cl.exe进行编译,适用于混合使用Clang和Visual Studio项目的场景。

The clang-cl /fallback mode

There has been a lot of work lately towards bringing an LLVM toolchain to the Windows platform (see A path forward for an LLVM toolchain on Windows). One result of that work is a new driver mode for Clang: clang-cl. This mode makes Clang understand the same kind of command-line arguments as Visual Studio’s compiler, cl.exe. For example, a typical command to compile a file into an executable with Clang might be “clang hello.cc -o hello”, whereas with cl.exe, one would use “cl.exe hello.cc /Fehello”. Now one can use the latter syntax with Clang by substituting “cl.exe” with “clang-cl”. This makes it easy to use Clang for existing projects in Visual Studio.

For the most part, clang-cl accepts exactly the same arguments as cl.exe. However, it also accepts some Clang-specific options. One such option that was added recently is the /fallback flag. The purpose of this flag is to make it easy to use clang-cl even in projects where Clang cannot yet compile all of the code. This post gives an example of how /fallback can be used.

The way clang-cl works in /fallback mode is that it first tries to compile the code with Clang, and if that fails for some reason it falls back to compiling with cl.exe. Consider the following two files, where main.cpp can be compiled by Clang, but printer.cpp cannot:

// printer.cpp:

#include <iostream>
void print_hello(const char *s) {
  std::cout << "Hello from " << s << "!" << std::endl;
}

main.cpp:

extern void print_hello(const char*);
int main(int argc, char **argv) {
  print_hello(argv[0]);
  return 0;
}

clang-cl cannot compile printer.cpp since it includes iostream which uses language features that are not completely supported yet (hopefully they will be soon). However, clang-cl can still compile the two files in fallback mode:

clang-cl /fallback /Fehello main.cpp printer.cpp

Clang will compile main.cpp successfully, print some error messages about the features it does not support for printer.cpp, and fall back to the cl.exe compiler for that file. The compiled files will be linked together into hello.exe.

Note that this is not fool proof: even if clang-cl thinks it has compiled a file successfully, there can be bugs in the generated code that cause failures at link- or run-time.

Currently, the fallback mode will cause clang-cl to fall back on any kind of error. Going forward, it will be changed to only fall back on certain kinds of internal errors, and as Clang’s Windows support improves, the fallback mode will eventually become unnecessary and removed. It is still early days for clang-cl, but you can try it out for yourself by downloading the toolchain from the LLVM Snapshot Builds website.

Posted by Hans Wennborg at 8:42 AM

[ 0% 664/150558] bc: libclcore.bc <= frameworks/rs/driver/runtime/rs_core.c FAILED: out/target/product/emulator_car_x86_64/obj/RENDERSCRIPT_BITCODE/libclcor e.bc_intermediates/rs_core.bc /bin/bash -c "PWD=/proc/self/cwd prebuilts/clang/host/linux-x86/clang-3289846/bi n/clang -Iframeworks/rs/script_api/include -Iexternal/clang/lib/Headers -MD -DRS _VERSION=24 -std=c99 -c -O3 -fno-builtin -emit-llvm -target renderscript64-linux -android -fsigned-char -D__x86_64__ -Wno-deprecated -Werror -Werror -Wall -Wext ra -Iframeworks/rs/cpu_ref -DRS_DECLARE_EXPIRED_APIS -x renderscript frameworks /rs/driver/runtime/rs_core.c -o out/target/product/emulator_car_x86_64/obj/RENDE RSCRIPT_BITCODE/libclcore.bc_intermediates/rs_core.bc" prebuilts/clang/host/linux-x86/clang-3289846/bin/clang.real: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory [ 0% 665/150558] bc: libclcore.bc <= frameworks/rs/driver/runtime/rs_cl.c FAILED: out/target/product/emulator_car_x86_64/obj/RENDERSCRIPT_BITCODE/libclcor e.bc_intermediates/rs_cl.bc /bin/bash -c "PWD=/proc/self/cwd prebuilts/clang/host/linux-x86/clang-3289846/bi n/clang -Iframeworks/rs/script_api/include -Iexternal/clang/lib/Headers -MD -DRS _VERSION=24 -std=c99 -c -O3 -fno-builtin -emit-llvm -target renderscript64-linux -android -fsigned-char -D__x86_64__ -Wno-deprecated -Werror -Werror -Wall -Wext ra -Iframeworks/rs/cpu_ref -DRS_DECLARE_EXPIRED_APIS -x renderscript frameworks /rs/driver/runtime/rs_cl.c -o out/target/product/emulator_car_x86_64/obj/RENDERS CRIPT_BITCODE/libclcore.bc_intermediates/rs_cl.bc" prebuilts/clang/host/linux-x86/clang-3289846/bin/clang.real: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory [ 0% 666/150558] bc: libclcore.bc <= frameworks/rs/driver/runtime/rs_allocation FAILED: out/target/product/emulator_car_x86_64/obj/RENDERSCRIPT_BITCODE/libclcor e.bc_intermediates/rs_allocation.bc /bin/bash -c "PWD=/proc/self/cwd prebuilts/clang/host/linux-x86/clang-3289846/bi n/clang -Iframeworks/rs/script_api/include -Iexternal/clang/lib/Headers -MD -DRS _VERSION=24 -std=c99 -c -O3 -fno-builtin -emit-llvm -target renderscript64-linux -android -fsigned-char -D__x86_64__ -Wno-deprecated -Werror -Werror -Wall -Wext ra -Iframeworks/rs/cpu_ref -DRS_DECLARE_EXPIRED_APIS -x renderscript frameworks /rs/driver/runtime/rs_allocation.c -o out/target/product/emulator_car_x86_64/obj /RENDERSCRIPT_BITCODE/libclcore.bc_intermediates/rs_allocation.bc" prebuilts/clang/host/linux-x86/clang-3289846/bin/clang.real: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory [ 0% 667/150558] bc: libclcore.bc <= frameworks/rs/driver/runtime/rs_element.c FAILED: out/target/product/emulator_car_x86_64/obj/RENDERSCRIPT_BITCODE/libclcor e.bc_intermediates/rs_element.bc /bin/bash -c "PWD=/proc/self/cwd prebuilts/clang/host/linux-x86/clang-3289846/bi n/clang -Iframeworks/rs/script_api/include -Iexternal/clang/lib/Headers -MD -DRS _VERSION=24 -std=c99 -c -O3 -fno-builtin -emit-llvm -target renderscript64-linux -android -fsigned-char -D__x86_64__ -Wno-deprecated -Werror -Werror -Wall -Wext ra -Iframeworks/rs/cpu_ref -DRS_DECLARE_EXPIRED_APIS -x renderscript frameworks /rs/driver/runtime/rs_element.c -o out/target/product/emulator_car_x86_64/obj/RE NDERSCRIPT_BITCODE/libclcore.bc_intermediates/rs_element.bc" prebuilts/clang/host/linux-x86/clang-3289846/bin/clang.real: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory [ 0% 668/150558] bc: libclcore.bc <= frameworks/rs/driver/runtime/rs_f16_math.c FAILED: out/target/product/emulator_car_x86_64/obj/RENDERSCRIPT_BITCODE/libclcor e.bc_intermediates/rs_f16_math.bc /bin/bash -c "PWD=/proc/self/cwd prebuilts/clang/host/linux-x86/clang-3289846/bi n/clang -Iframeworks/rs/script_api/include -Iexternal/clang/lib/Headers -MD -DRS _VERSION=24 -std=c99 -c -O3 -fno-builtin -emit-llvm -target renderscript64-linux -android -fsigned-char -D__x86_64__ -Wno-deprecated -Werror -Werror -Wall -Wext ra -Iframeworks/rs/cpu_ref -DRS_DECLARE_EXPIRED_APIS -x renderscript frameworks /rs/driver/runtime/rs_f16_math.c -o out/target/product/emulator_car_x86_64/obj/R ENDERSCRIPT_BITCODE/libclcore.bc_intermediates/rs_f16_math.bc" prebuilts/clang/host/linux-x86/clang-3289846/bin/clang.real: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory [ 0% 669/150558] bc: libclcore.bc <= frameworks/rs/driver/runtime/rs_mesh.c FAILED: out/target/product/emulator_car_x86_64/obj/RENDERSCRIPT_BITCODE/libclcor e.bc_intermediates/rs_mesh.bc /bin/bash -c "PWD=/proc/self/cwd prebuilts/clang/host/linux-x86/clang-3289846/bi n/clang -Iframeworks/rs/script_api/include -Iexternal/clang/lib/Headers -MD -DRS _VERSION=24 -std=c99 -c -O3 -fno-builtin -emit-llvm -target renderscript64-linux -android -fsigned-char -D__x86_64__ -Wno-deprecated -Werror -Werror -Wall -Wext ra -Iframeworks/rs/cpu_ref -DRS_DECLARE_EXPIRED_APIS -x renderscript frameworks /rs/driver/runtime/rs_mesh.c -o out/target/product/emulator_car_x86_64/obj/RENDE RSCRIPT_BITCODE/libclcore.bc_intermediates/rs_mesh.bc" prebuilts/clang/host/linux-x86/clang-3289846/bin/clang.real: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory [ 0% 670/150558] bc: libclcore.bc <= frameworks/rs/driver/runtime/rs_matrix.c FAILED: out/target/product/emulator_car_x86_64/obj/RENDERSCRIPT_BITCODE/libclcor e.bc_intermediates/rs_matrix.bc /bin/bash -c "PWD=/proc/self/cwd prebuilts/clang/host/linux-x86/clang-3289846/bi n/clang -Iframeworks/rs/script_api/include -Iexternal/clang/lib/Headers -MD -DRS _VERSION=24 -std=c99 -c -O3 -fno-builtin -emit-llvm -target renderscript64-linux -android -fsigned-char -D__x86_64__ -Wno-deprecated -Werror -Werror -Wall -Wext ra -Iframeworks/rs/cpu_ref -DRS_DECLARE_EXPIRED_APIS -x renderscript frameworks /rs/driver/runtime/rs_matrix.c -o out/target/product/emulator_car_x86_64/obj/REN DERSCRIPT_BITCODE/libclcore.bc_intermediates/rs_matrix.bc" prebuilts/clang/host/linux-x86/clang-3289846/bin/clang.real: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory 23:03:47 ninja failed with: exit status 1
最新发布
10-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值