Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
134 views
in Technique[技术] by (71.8m points)

Generic rule from makefile to 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  $@  $<

Working ugly attempt

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

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

Examples delivered with CMake itself are ASM or RC compilers.


The enable_language(FOO) Version

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

Then your CMakeLists.txt file just looks like this:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...