从 makefile 到 CMake 的通用规则 [英] Generic rule from makefile to CMake

查看:44
本文介绍了从 makefile 到 CMake 的通用规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将流动的 vanilla makefile 行翻译"为 CMake 语法.

I am trying to "translate" the flowing vanilla makefile lines into CMake syntax.

SRCS = $(wildcard *.foo)
OBJS = $(SRCS:.foo=.bar)

my_rule: $(OBJS)

%.bar: %.foo
    make_bar_from_foo  $@  $<

丑陋的尝试

FILE(GLOB SRCS "*.foo")
SET(outFiles)
FOREACH(SRC ${SRCS})
    SET(OUTPUT_FILE_NAME "${SRC}.bar")

    ADD_CUSTOM_COMMAND(
      OUTPUT "${OUTPUT_FILE_NAME}"
      COMMAND make_bar_from_foo ${SRC} ${OUTPUT_FILE_NAME}
      DEPENDS "${SRC}")

    SET(outFiles ${outFiles} "${OUTPUT_FILE_NAME}")
ENDFOREACH(SRC)
ADD_CUSTOM_TARGET(my_rule ALL DEPENDS ${outFiles})

我知道它将为每个文件生成一个命令而不是通用命令.有什么最简单/最干净的方法吗?

I understand that it's going to generate one command per file instead of something generic. Any simplest/cleanest way to do it?

推荐答案

在 CMake 中,您始终可以声明自己的编译器语言.所以在你的情况下,你可以例如做:

In CMake you can always declare your own compiler language. So in your case you can e.g. do:

cmake_minimum_required(VERSION 2.8)

project(MakeBarFromFoo NONE)

set(
   CMAKE_FOO_COMPILE_OBJECT 
       "make_bar_from_foo <SOURCE> <OBJECT>"
)

file(GLOB SRCS "*.foo")
add_library(my_rule OBJECT ${SRCS})
set_source_files_properties(${SRCS} PROPERTIES LANGUAGE FOO)

然后您可以像使用其他对象库目标一样简单地使用它.但是,如果您 enable_language(FOO)(这需要更多工作,请参见下文),您将只会获得 .bar 扩展名之类的东西.

Then you can simply work with it as you would with other object library targets. But you will only get things like the .bar extension if you enable_language(FOO) (and that requires more work, see below).

CMake 本身提供的示例是 ASMRC 编译器.

Examples delivered with CMake itself are ASM or RC compilers.

这还需要四个你可以放入的文件,例如您项目的 CMake 文件夹:

This needs four more files you could put in e.g. your project's CMake folder:

CMakeCMakeDetermineFOOCompiler.cmake

# Find the compiler
find_program(
    CMAKE_FOO_COMPILER 
        NAMES "make_bar_from_foo" 
        HINTS "${CMAKE_SOURCE_DIR}"
        DOC "FOO compiler" 
)
mark_as_advanced(CMAKE_FOO_COMPILER)

set(CMAKE_FOO_SOURCE_FILE_EXTENSIONS foo;FOO)
set(CMAKE_FOO_OUTPUT_EXTENSION .bar)
set(CMAKE_FOO_COMPILER_ENV_VAR "FOO")

# Configure variables set in this file for fast reload later on
configure_file(${CMAKE_CURRENT_LIST_DIR}/CMakeFOOCompiler.cmake.in
               ${CMAKE_PLATFORM_INFO_DIR}/CMakeFOOCompiler.cmake)

CMakeCMakeFOOCompiler.cmake.in

set(CMAKE_FOO_COMPILER "@CMAKE_FOO_COMPILER@")
set(CMAKE_FOO_COMPILER_LOADED 1)
set(CMAKE_FOO_SOURCE_FILE_EXTENSIONS @CMAKE_FOO_SOURCE_FILE_EXTENSIONS@)
set(CMAKE_FOO_OUTPUT_EXTENSION @CMAKE_FOO_OUTPUT_EXTENSION@)
set(CMAKE_FOO_COMPILER_ENV_VAR "@CMAKE_FOO_COMPILER_ENV_VAR@")

CMakeCMakeFOOInformation.cmake

# This file sets the basic flags for the FOO compiler
if(NOT CMAKE_FOO_COMPILE_OBJECT)
    set(CMAKE_FOO_COMPILE_OBJECT "<CMAKE_FOO_COMPILER> <SOURCE> <OBJECT>")
endif()
set(CMAKE_FOO_INFORMATION_LOADED 1)

CMakeCMakeTestFOOCompiler.cmake

# For now just do nothing in here
set(CMAKE_FOO_COMPILER_WORKS 1 CACHE INTERNAL "")


然后你的 CMakeLists.txt 文件看起来像这样:

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)

project(MakeBarFromFoo NONE)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/CMake")
enable_language(FOO)

file(GLOB SRCS "*.foo")
add_library(my_rule OBJECT ${SRCS})

这篇关于从 makefile 到 CMake 的通用规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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