How to generate cscope.files from CMakeLists.txt

2019-05-25 04:15发布

问题:

I'm using CMake to build my code. CMakeLists.txt contains the list of all the .C or .CPP files and all the include directories.

How can I implement CMakeLists.txt so that I can have a make option that generates cscope.files for me containing the list of all the .C and .CPP (or .CXX) files and also all the .h files from the include directories in it?

For e.g. I should be able to run make cscopefiles and it will generate cscope.files for me.

Or is there already such an option?

回答1:

You could also extend Fraser's answer with a configure_file call, so the final file only changes when its content has changed.

file(WRITE
  ${CMAKE_BINARY_DIR}/cscope.files.in
  "${AllFiles}")
configure_file(
  ${CMAKE_BINARY_DIR}/cscope.files.in
  ${CMAKE_BINARY_DIR}/cscope.files
  COPYONLY)

That could help minimize incremental rebuilds if something else (a custom command) depends on the cscope.files output file. The configure_file call will only update the output file if the result is different from its current contents.



回答2:

You could generate the file every time CMake runs by adding something like the following: (this assumes your current list of files are in 2 variables called SourceFiles and HeaderFiles)

set(AllFiles ${SourceFiles} ${HeaderFiles})
string(REPLACE ";" "\n" AllFiles "${AllFiles}")
file(WRITE ${CMAKE_BINARY_DIR}/cscope.files "${AllFiles}")


标签: c++ c cmake cscope