[CMake] how to replace text in generated files

Andy Cedilnik andy.cedilnik at kitware.com
Mon Mar 21 09:18:02 EST 2005


Hi Alex,

What you can do is to write a cmake script that does what you want it to
do. Then you call it during build stage using:

ADD_CUSTOM_COMMAND(...
...
${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/my_fancy_script.cmake
...)

       Andy

Alexander Neundorf wrote:

>Hi, 
> 
>while trying to build kde stuff I came across this rule: 
> 
> echo '#include <kdialog.h>' > snippetsettingsbase.cpp 
> echo '#include <klocale.h>' >> snippetsettingsbase.cpp 
> uic -tr tr2i18n -i snippetsettingsbase.h  snippetsettingsbase.ui > 
>snippetsettingsbase.cpp.temp ; ret=$$?; \ 
>	$(PERL) -pe "s,tr2i18n( \"\" ),QString::null,g" 
>snippetsettingsbase.cpp.temp | $(PERL) -pe "s,tr2i18n( \"\"\, 
>\"\" ),QString::null,g" | $(PERL) -pe "s,image([0-9]
>[0-9]*)_data,img\$$1_snippetsettingsbase,g" | $(PERL) -pe "s,: 
>QWizard\(,: KWizard(,g" >> snippetsettingsbase.cpp ;\ 
>	rm -f snippetsettingsbase.cpp.temp ;\ 
>	if test "$$ret" = 0; then echo '#include 
>"snippetsettingsbase.moc"' >> snippetsettingsbase.cpp; else rm -f 
>snippetsettingsbase.cpp ; exit $$ret ; fi 
> 
>So in short, this rule creates a text file containing two includes, then 
>runs a command (uic) which output is appended to this file, and then runs 
>perl several times to replace stuff. 
> 
>How can I do this (if possible portable) with cmake ? 
>I have the following: 
> 
>MACRO(KDE_ADD_UI_FILES _sources ) 
>   FOREACH (_current_FILE ${ARGN}) 
>      GET_FILENAME_COMPONENT(_basename ${_current_FILE} NAME_WE) 
>      SET(_header ${CMAKE_CURRENT_BINARY_DIR}/${_basename}.h) 
>      SET(_src ${CMAKE_CURRENT_BINARY_DIR}/${_basename}.cpp) 
>      SET(_moc ${CMAKE_CURRENT_BINARY_DIR}/${_basename}.moc.cpp) 
>	   
>      ADD_CUSTOM_COMMAND(OUTPUT ${_header} 
>         COMMAND uic 
>         ARGS  -nounload -o ${_header} 
>         ${CMAKE_CURRENT_SOURCE_DIR}/${_current_FILE} 
>         DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_current_FILE} 
>      )                 
>      ADD_CUSTOM_COMMAND(OUTPUT ${_src} 
>         COMMAND uic 
>         ARGS -nounload -tr tr2i18n -o ${_src} -impl ${_header}  
>         ${CMAKE_CURRENT_SOURCE_DIR}/${_current_FILE}  
>         DEPENDS ${_header} 
>      )                 
>      ADD_CUSTOM_COMMAND(OUTPUT ${_moc} 
>         COMMAND moc 
>         ARGS ${_header} -o ${_moc} 
>         DEPENDS ${_header} 
>      )                 
>      SET(${_sources} ${${_sources}} ${_src} ${_moc} ) 
>   ENDFOREACH (_current_FILE) 
>ENDMACRO(KDE_ADD_UI_FILES) 
> 
>Now I could do FILE(READ ...) and then STRING(REGEX REPLACE ...) or 
>something like this, but this would be executed during the cmake run, not 
>during the build phase. Any ideas ? How about adding such functionality 
>to "cmake -E " ?  
>I.e. cmake -E file1 existing_file file_which_will_be_appended_to_file1 
>and cmake -E sed  <some regexp stuff> 
>or more general 
>cmake -E <execute_some_cmake_stuff> 
>(not exactly sure how to do this, but the idea would be to have cmake 
>execute some part of the CMakeLists.txt during the build phase so that 
>any cmake functions would be available also during the build phase). 
> 
>Bye 
>Alex 
> 
>
>  
>


-- 
Andy Cedilnik
Kitware Inc.



More information about the CMake mailing list