Cmake: how to run bash command only when updating a file?

I am trying to write CMakeLists.txt to speed up compilation.

The executable depends on the ccp script file. I use the cppcms web application library which has a template system where .tmpl needs to be converted to .cpp files at compile time as follows:

cppcms_tmpl_cc page.tmpl -o page.cpp 

There are related questions that cover the use of bash commands in cmake:
How to run a command at compile time in a makefile generated by CMake?
CMake: how to use bash command in CMakeLists.txt
These questions cover most of my needs.

Now I want to know how to tell cmake to execute the above command and regenerate page.cpp every time page.tmpl itself has changed, and only then?

The goal, obviously, is to improve compilation time and have an updated binary file with the latest template.

(can the moderator add the cppcms tag?)

[Edit: I am trying to convert the following Makefile to cmake:

 LIBS=-lcppcms -lconfig++ -lboost_filesystem-mt all: clean gitbrowser gitbrowser: main.cpp view.cpp content.hpp gitbrowser.cpp $(CXX) -Wall main.cpp gitbrowser.cpp view.cpp -o run ${LIBS} view.cpp: page.tmpl content.hpp cppcms_tmpl_cc page.tmpl -o view.cpp 

[Edit2: I added a note about the solution on the official cppcms wiki:
http://art-blog.no-ip.info/wikipp/en/page/cppcms_1x_howto#How.to.compile.the.templates.with.cmake .

+4
source share
2 answers
 now = get_now_time() time = get_last_upd_time() if (now > time) set (LAST_UPD_TIME time CACHE INTERNAL "Defines last update time of the file" FORCE) # run bash command here endif (now > time) 

get_now_time and get_last_upd_time are fictitious functions that return timestamps (I think you can use bash commands to get these timestamps). Then you can compare them and save the timestamp of the last modification in the cache.

However, this decision looks ugly to me, as I know if you correctly define goals and the relationships between them. CMake will take care of recovering only the modified files, right? Could you show me the target definitions?

change

You can use the following CMakeLists.txt (you are not sure, it is based on my project):

 # add main target, the executable file ADD_EXECUTABLE(gitbrowser main.cpp view.cpp content.hpp gitbrowser.cpp) # linking it with necessary libraries TARGET_LINK_LIBRARIES(gitbrowser "cppcms config++ boost_filesystem-mt") # add page.cpp target ADD_CUSTOM_COMMAND( OUTPUT page.cpp COMMAND "cppcms_tmpl_cc page.tmpl -o view.cpp" DEPENDS page.tmpl content.hpp ) # and finally add dependency of the main target ADD_DEPENDENCIES(gitbrowser page.cpp) 

Good luck.

+4
source

Take a look at this CMake file from Wikipp at lines 66-72

You basically need something like this:

 add_custom_command( OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/view.cpp COMMAND cppcms_tmpl_cc view.tmpl -o ${CMAKE_CURRENT_BINARY_DIR}/view.cpp DEPENDS view.tmpl) 

Edit: Also, if you want to improve compilation speed, you can compile the view of a common object and load it dynamically.

It will also allow you not to restart the application, if you just changed the view, the shared object will be automatically reloaded after recompilation.

See http://art-blog.no-ip.info/wikipp/en/page/cppcms_1x_config#views for more details.

+1
source

Source: https://habr.com/ru/post/1334878/


All Articles