CMake:如何在多个文件上运行自定义命令来生成源文件?

2024-04-21

我有以下情况:我想编译一些Scheme文件Gambit https://github.com/gambit/gambit成可执行文件。为此,我使用 gambit 将所有计划文件翻译/生成为 C 和目标文件,然后将其编译并链接为可执行文件。

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

/* 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)

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

    
$ 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))

EDIT:注意最后一个命令,这不是拼写错误,最后一步是使用gcc用于链接。

现在,我希望使用 CMake 来为我做到这一点。这是我的概述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 ...) 

除了纯 C/C++ 项目之外,我从未使用过 CMake。解决这个问题最明智的方法是什么?最终,我想要一个CMakeLists.txt我可以将其放置在包含所有计划文件的子目录中,让它生成所有 C 和对象文件,并让 CMake 在父目录中使用它们来创建可执行文件和库add_executable and add_library.


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

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

例如这样的事情:

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

完整的 CMakeLists.txt 可能如下所示:

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)

Test

由于我没有 gsc,我只是用 echo 和 touch 替换了自定义命令。如果我跑

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

这似乎就是你正在寻找的。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

CMake:如何在多个文件上运行自定义命令来生成源文件? 的相关文章

随机推荐