[CMake] Using m4 as a additional preprocessor for C/C++

Prasad H. L. hlprasu at gmail.com
Fri Jun 5 16:27:22 EDT 2009


Consolidating all the things, the working CMakeLists.txt is as follows -

------------------------------------
project(cmake_test)
cmake_minimum_required(VERSION 2.6)

macro(m4tocxx outfilename)
add_custom_command(
	OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${outfilename}.cpp
	COMMAND m4 -P -s ${CMAKE_CURRENT_SOURCE_DIR}/${outfilename}.m4 >
${CMAKE_CURRENT_BINARY_DIR}/${outfilename}.cpp
	DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${outfilename}.m4
)
set_source_files_properties(${outfilename}.cpp PROPERTIES GENERATED true)
endmacro(m4tocxx)


add_executable(testcmake testcmake.cpp)

m4tocxx(testcmake)
-------------------------------------------------------------

where m4tocxx is the macro to convert from m4 to cpp.
Objective of the above CMakeLists.txt :- The source file
is testcmake.m4 which is to be preprocessed to testcmake.cpp and
linked to form the executable testcmake.

Thanks to Denis and Robert again for all their suggestions.

If anybody knows a way by which this macro could be made
to get called for all m4 files automatically in the dependence order,
please let me know. Again, as pointed out by Tyler, not by FILE(GLOB...)...


Regards,
Prasad

2009/6/5 Prasad H. L. <hlprasu at gmail.com>:
> Can I define a new source type, like .m4 and add rules for it? That
> would be ideal, I feel, for my case.
>
> 2009/6/5 Tyler Roscoe <tyler at cryptio.net>:
>> On Fri, Jun 05, 2009 at 10:10:07PM +0530, Prasad H. L. wrote:
>>> 2009/6/5 Denis Scherbakov <denis_scherbakov at yahoo.com>:
>>> >
>>> >> Is it possible to set the preprocessing to all .m4 files
>>> >> avoiding a
>>> >> manual specification of list of files to processed?
>>> >
>>> > If your list of files changes all the time, you can use FILE(GLOB...)
>>> > to find all *.m4 files to automate process.
>>>
>>> I try this out.
>>
>> FYI this approach is not recommended since CMake then has no way to know
>> if you added a new file and thus cannot regenerate your build system for
>> you automatically.
>>
>> tyler
>>


More information about the CMake mailing list