General rule from makefile to cmake

I am trying to "translate" the current vanilla makefile lines into cmake syntax.

SRCS = $(wildcard *.foo) OBJS = $(SRCS:.foo=.bar) my_rule: $(OBJS) %.bar: %.foo make_bar_from_foo $@ $< 

Work is an 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 is going to generate 1 command per file instead of something in common. The easiest / cleanest way to do this?

+4
cmake makefile
Jul 10 '16 at 15:34
source share
3 answers

In CMake, you can always declare your own compiler language. Thus, in your case, you can, for example, 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 just work with it, as with other objects in the object library . But you will only get such as the .bar extension if you enable_language(FOO) (and this requires more work, see below).

Examples that come with CMake itself are ASM or RC compilers.




enable_language(FOO) version enable_language(FOO) version

This requires four more files that you could enter, for example. your CMake project:

CMake \ CMakeDetermineFOOCompiler.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) 

CMake \ CMakeFOOCompiler.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@") 

CMake \ CMakeFOOInformation.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) 

CMake \ CMakeTestFOOCompiler.cmake

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



Then your CMakeLists.txt file 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}) 
+2
Jul 10 '16 at 22:18
source share

Your attempt is correct, it can hardly be significantly improved.

This is how CMake works: each of its "rules" is specific; you cannot create a "rule for a template". The reason for this is platform interdependence: not every build system supports “templates” for rules.

On the other hand, CMake supports the creation of "rules" inside functions / macros. Thus, the typification of repeating parts can be easily reduced.

+1
Jul 10 '16 at 21:06
source share

Generally with CMake, you do not create all the object files. Just add the correct .foo files to your desired CMake commands ( add_library or add_executable ), and CMake will process it for you.

CMake is much more efficient than regular make. But you must recognize that this power comes with a change in how you use the tool.

0
Jul 10 '16 at 15:45
source share



All Articles