如何在CMake中编译非通用语言

How to compile non-common language in CMake

我正在尝试为我的一个项目从 Makefile 切换到 CMake。问题是在编译步骤中,我使用非通用语言编译器(Futhark)生成.c 和.h 文件。 Makefile 的相关部分如下所示。

fut_compile: futhark/select_where.fut
  mkdir -p build && futhark c --library futhark/select_where.fut -o build/select_where

fut_shared_lib: build/select_where.c build/select_where.h
  gcc build/select_where.c -o build/lib_select_where.so -fPIC -shared

# Generate output directory
fut_result: build/lib_select_where.so db_gpu_load.cpp CLI.cpp
  g++ -std=c++11 build/lib_select_where.so db_gpu_load.cpp CLI.cpp -o build/out

我想象 CMake 文件看起来像这样:

# ??
# ?? futhark c --library futhark/select_where.fut -o build/select_where
# ??

add_library(mylib SHARED
   build/select_where.c
   build/lib_select_where.so
)

add_executable(Exe CLI.cpp  db_gpu_load.cpp)

TARGET_LINK_LIBRARIES(Exe LINK_PUBLIC mylib)

是否有标准为什么要添加生成源文件和头文件的非标准语言的编译,然后在CMake中生成共享库时包含这些文件?我是 CMake 的新手,所以非常感谢任何帮助。

您可以使用 CMake 的 execute_process 命令来 运行 futhark 并在 CMake 配置阶段生成 .c.h 文件。这样,这些文件就可以在以后的 CMake 命令中使用,例如 add_library():

# Generate the .c and .h files into the current CMake binary directory.
execute_process(COMMAND 
    futhark c --library ${CMAKE_CURRENT_LIST_DIR}/futhark/select_where.fut -o select_where
    WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
)

# Reference the generated .c when specifying the lib_select_where library target.
add_library(lib_select_where SHARED
    ${CMAKE_CURRENT_BINARY_DIR}/select_where.c
)

# Use the generated .h when compiling this, by adding its location as an include directory.
target_include_directories(lib_select_where PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
# Add the -fPIC compile option.
target_compile_options(lib_select_where PRIVATE -fPIC)

这样做:

// build/select_where.c build/select_where.h: futhark/select_where.fut
//  mkdir -p build && futhark c --library futhark/select_where.fut -o build/select_where
add_custom_command(
     OUTPUT
         ${CMAKE_CURRENT_BINARY_DIR}/select_where.c
         ${CMAKE_CURRENT_BINARY_DIR}/select_where.h
     COMMAND futhark c 
         --library futhark/select_where.fut 
         -o ${CMAKE_CURRENT_BINARY_DIR}/select_where
     DEPENDS futhark/select_where.fut
     WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
     VERBATIM
)

// build/lib_select_where.so: build/select_where.c build/select_where.h
//    gcc build/select_where.c -o build/lib_select_where.so -fPIC -shared
add_library(select_where SHARED
   ${CMAKE_CURRENT_BINARY_DIR}/select_where.c
   ${CMAKE_CURRENT_BINARY_DIR}/select_where.h
)
// maybe target_include_diresctories(select_where PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
// so that selct_where.h is visible, dunno

// build/out: build/lib_select_where.so db_gpu_load.cpp CLI.cpp
//    g++ -std=c++11 build/lib_select_where.so db_gpu_load.cpp CLI.cpp -o build/out
add_executable(out 
    db_gpu_load.cpp
    CLI.cpp
)
target_link_libraries(out PRIVATE
    select_where
)
set_target_properties(out PROPERTIES
    CMAKE_CXX_STANDARD            CXX11
    CMAKE_CXX_STANDARD_REQUIRED   TRUE
)