[CMake] Build rule for specific file extension
Michael Wild
themiwi at gmail.com
Fri Jun 25 08:22:59 EDT 2010
On 25. Jun, 2010, at 13:26 , Mathias Lafeldt wrote:
> Hi,
>
> is there a way to tell CMake how to "compile" files based on their
> extension?
>
> For example, here's a make rule to build an object file from a *.lst file:
>
> %.o : %.lst
> echo "#include \"irx_imports.h\"" > build-imports.c
> cat $< >> build-imports.c
> $(IOP_CC) $(IOP_CFLAGS) build-imports.c -o $@
> -rm -f build-imports.c
>
> How can I convert this to CMake? I considered using add_custom_target,
> but I don't know how to apply it to all files with the same extension.
>
> -Mathias
No, there is no way to do this. Regrettable as it is, I also think that there are probably pretty good reasons for not providing such a facility. The usual thing to do is to write a function which loops over all the input files, creates a custom command for each of the files and then returns the list of the products in a variable. E.g.
find_program(FOO_EXECUTABLE foo)
function(preprocess_foo out_var)
set(result)
foreach(in_f ${ARGN})
file(RELATIVE_PATH out_f ${CMAKE_CURRENT_SOURCE_DIR} ${in_f})
set(out_f "${CMAKE_CURRENT_BINARY_DIR}/${out_f}")
add_custom_command(OUTPUT ${out_f}
COMMAND ${FOO_EXECUTABLE} -o ${out_f} ${in_f}
DEPENDS ${in_f}
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
COMMENT "Creating preprocessed foo file ${out_f}"
VERBATIM
)
list(APPEND result ${out_f})
endforeach()
set(${out_var} "${result}" PARENT_SCOPE)
endfunction()
preprocess_foo(prep_files a.foo b.foo c.foo)
add_executable(bar ${prep_files})
HTH
Michael
More information about the CMake
mailing list