CMake:如何在多个文件上运行自定义命令以生成源文件? [英] CMake: How to run a custom command on a number of files to generate source files?

查看:92
本文介绍了CMake:如何在多个文件上运行自定义命令以生成源文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下情况:我想使用 Gambit 将多个Scheme文件编译为一个可执行文件。为此,我使用gambit将所有Scheme文件转换/生成为C和目标文件,然后依次将它们编译并链接为可执行文件。

I have the following situation: I want to compile a number of Scheme files with Gambit into an executable. To that end, I use gambit to translate/generate all Scheme files into C and object files, which in turn are then compiled and linked into an executable.

假定我具有以下三个文件(示例取自Gambit文档):

Assuming I have the following three files (example is taken from the Gambit documentation):

/* File: "m1.c" */
int power_of_2 (int x) { return 1<<x; }

; File: "m2.scm"
(c-declare "extern int power_of_2 ();")
(define pow2 (c-lambda (int) int "power_of_2"))
(define (twice x) (cons x x))

; File: "m3.scm"
(write (map twice (map pow2 '(1 2 3 4)))) (newline)

如果我要手工编译它,则要执行以下步骤:

If I were to compile it by hand, this were the steps to take:

    
$ gsc -c m2.scm        # create m2.c (note: .scm is optional)
$ gsc -c m3.scm        # create m3.c (note: .scm is optional)
$ gsc -link m2.c m3.c  # create the incremental link file m3_.c
$ gsc -obj m1.c m2.c m3.c m3_.c # create all object files *.o
m1.c:
m2.c:
m3.c:
m3_.c:
$ gcc m1.o m2.o m3.o m3_.o -lgambit -lm -ldl -lutil # link object files
$ ./a.out
((2 . 2) (4 . 4) (8 . 8) (16 . 16))

编辑:记住最后一个命令,这不是一个错字,最后一步是使用 gcc 进行链接。

EDIT: Mind the last command, that's not a typo, the final step is to use gcc for linking.

现在,我希望使用CMake为我完成此操作。这是我的 CMakeLists.txt 的概述:

Now, I wish to use CMake to do this for me. Here's an outline of my CMakeLists.txt:

cmake_minimum_required( VERSION 3.8...3.18 )

if( ${CMAKE_VERSION} VERSION_LESS 3.12 )
    cmake_policy( VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION} )
endif()

# Give this project a name 
project( schemetemplate VERSION 0.0.1 )

# compile all .scm files into *.c files 
# the C files as target
add_custom_target( 
    compiledSchemeFiles ALL 
    SOURCES m2.scm 
            m3.scm 
    COMMAND # this is where I call gsc(?)
    VERBATIM )

# or do I use custom command?
add_custom_command( ... )

# the final executable, how to tell add_executable I want all generated files as dependency?
add_exectuable( finalexec m1.c ...) 

我从来没有和CMake合作过C / C ++项目。解决这个问题的最明智的方法是什么?最终,我希望拥有一个 CMakeLists.txt ,我可以将其放置在包含所有我的Scheme文件的子目录中,让它生成所有C和目标文件,并在其中包含CMake。父目录使用它们通过 add_executable add_library 创建可执行文件和库。

I've never worked with CMake for anything but pure C/C++ project. What's the smartest way to go about this? Ultimately, I want so have a CMakeLists.txt that I can place in a subdirectory with all my Scheme files, have it generate all C and object files, and have CMake in the parent directory use them to create executable and libraries with add_executable and add_library.

推荐答案

您可以定义scm源文件,然后为每个文件创建一个custom_command。这将为每个.scm文件创建一个.c文件。可以将创建的文件添加到列表(例如,名为scm_c_files)并在add_executable命令中使用。最后,可以使用target_link_libraries定义使用的库。

You could define the scm source files and then create a custom_command for each of them. This would create a .c file for each .scm file. The created files could be added to a list (e.g. named scm_c_files) and used in the add_executable command. Finally, the libraries used can be defined with target_link_libraries.

我不知道gsc和相应的增量链接文件是如何工作的,但是如果您可以给增量链接文件一个自定义名称, (是否有某种-o选项?),它只是另一个add_custom_command,仅取决于从.scm文件生成的.c文件。

I don't know exactly how gsc and the corresponding incremental link file work, but if you could give the incremental link file a custom name (is there some sort of -o option for it?), it would simply be another add_custom_command that depends only on the .c files generated from the .scm files.

例如类似

add_custom_command(
        OUTPUT incLink.c
        gsc -link ${scm_c_files} -o incLink.c
        DEPENDS ${scm_c_files}
)

完整的CMakeLists.txt可能看起来像这样:

A complete CMakeLists.txt could look something like this:

cmake_minimum_required(VERSION 3.17)
project(finalexec C)

set(CMAKE_C_STANDARD 99)

set(SCM_SOURCES m2.scm m3.scm)

set(scm_c_files)
foreach(scm_source ${SCM_SOURCES})
    get_filename_component(file_c ${scm_source} NAME_WE)
    set(file_c "${file_c}.c")
    add_custom_command(
            OUTPUT ${file_c}
            COMMAND gsc -c ${CMAKE_CURRENT_SOURCE_DIR}/${scm_source}
            DEPENDS ${scm_source}
            VERBATIM
    )
    list(APPEND scm_c_files ${file_c})
endforeach()

add_custom_command(
        OUTPUT incLink.c
        COMMAND gsc -link ${scm_c_files} -o incLink.c
        DEPENDS ${scm_c_files}
)


add_executable(finalexec m1.c ${scm_c_files} incLink.c)

target_link_libraries(finalexec gambit m dl util)

测试

由于我没有gsc,所以我只将自定义命令替换为回显和触摸。如果我运行

Since I don't have gsc I just replaced the custom commands with an echo and a touch. If I run

cmake .
make 

我得到的输出如下:

[ 12%] Generating m3.c
gsc -c /Users/stephan/kk/m3.scm
[ 25%] Generating m2.c
gsc -c /Users/stephan/kk/m2.scm
[ 37%] Generating incLink.c
gsc -link m2.c m3.c -o incLink.c
[ 50%] Building C object CMakeFiles/finalexec.dir/m1.c.o
[ 62%] Building C object CMakeFiles/finalexec.dir/m2.c.o
[ 75%] Building C object CMakeFiles/finalexec.dir/m3.c.o
[ 87%] Building C object CMakeFiles/finalexec.dir/incLink.c.o
[100%] Linking C executable finalexec
[100%] Built target finalexec

这似乎是您想要的。

这篇关于CMake:如何在多个文件上运行自定义命令以生成源文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆