如何使用 CMake 构建 ispc 文件?

How do I use CMake to build an ispc file?

我有一个简单的项目。它包含两个文件:

main.c
kernel.ispc

(ispc 文件是 https://ispc.github.io/ 的来源)

要手动编译我将使用的文件:

ispc --target=sse2 kernel.ispc -o kernel.o
gcc -c main.c -o main.o
gcc main.o kernel.o -o my_program

所以对于我的 cmake 文件,它看起来像

project(my_program)
add_executable(my_program main.c)

但当然不会 link 因为它缺少 kernel.o

中的符号

所以问题是: 如何让 cmake 使用 ispc 编译器编译 kernel.ispc,以及如何让 cmake 将其 link 转换为 my_program?

How do I get cmake to compile kernel.ispc using the ispc compiler?

只需使用add_custom_command:

add_custom_command(OUTPUT kernel.o
                   COMMAND ispc --target=sse2 ${CMAKE_SOURCE_DIR}/kernel.ispc -o kernel.o
                   DEPENDS kernel.ispc)

How do I get cmake to then link it into my_program?

从 CMake 的角度来看,.o 文件只是可执行文件的另一个来源:

add_executable(my_program main.c kernel.o)

我知道这是一个老问题,但是 CMAKE 3.19 现在已经内置了对 ISPC 的支持。