[CMake] DEPFILE usage in add_custom_command(...)

Attila Krasznahorkay attila.krasznahorkay at gmail.com
Wed Dec 12 04:49:31 EST 2018


Dear All,

I'm trying to use the DEPFILE argument of add_custom_command(...) to be able to tell Ninja exactly when it should re-run that custom command. But unfortunately I'm not able to get it to work as I'd like. :-(

I'm running a custom command that processes a header file through a custom executable. (It's a long story, see https://gitlab.kitware.com/cmake/cmake/issues/18695 and https://gitlab.kitware.com/cmake/cmake/issues/16830 for some more details.) I run that custom executable using a script file that I generate during the CMake configuration. In that script file I generate a dependency file using:

   set( cmd "${cmd}${CMAKE_CXX_COMPILER} -I\"$<JOIN:${incdirs},\" -I\">\"" )
   set( cmd "${cmd} -D$<JOIN:${definitions}, -D>" )
   if( CMAKE_OSX_SYSROOT )
      set( cmd "${cmd} -isysroot ${CMAKE_OSX_SYSROOT}" )
   endif()
   set( cmd "${cmd} -MM -MT ${rootmap_name} -MF ${dictname}.d ${ARG_HEADER}\n" )

This then results in a file like:

/Users/krasznaa/ATLAS/sw/projects/volatile/externals/build_test/DictionaryPackage/CMakeFiles/DictionaryPackageDict.dsomap: \
  /Users/krasznaa/ATLAS/sw/projects/volatile/externals/build_test/DictionaryPackage/CMakeFiles/DictionaryPackageDict.h \
  /Users/krasznaa/ATLAS/sw/projects/volatile/externals/CMakeDictionaryTests/DictionaryPackage/DictionaryPackage/MyClass.h \
  /Users/krasznaa/ATLAS/sw/projects/volatile/externals/CMakeDictionaryTests/DictionaryPackage/DictionaryPackage/HelperHeader.h \
  /Users/krasznaa/ATLAS/sw/projects/volatile/externals/CMakeDictionaryTests/DictionaryPackage/DictionaryPackage/MyOtherClass.h \
  /Users/krasznaa/ATLAS/sw/projects/volatile/externals/CMakeDictionaryTests/AthLinks/AthLinks/DataLink.h \
  /Users/krasznaa/ATLAS/sw/projects/volatile/externals/CMakeDictionaryTests/AthLinks/AthLinks/ElementLink.h \
  /Users/krasznaa/ATLAS/sw/projects/volatile/externals/CMakeDictionaryTests/AthLinks/AthLinks/ElementLinkVector.h \
  /Users/krasznaa/ATLAS/sw/projects/volatile/externals/CMakeDictionaryTests/AthContainers/AthContainers/tools/AuxTypeVectorFactory.h

Which looks correct to me. The generated Ninja file refers to this dependency file with:

#############################################
# Custom command for DictionaryPackage/CMakeFiles/DictionaryPackageDict.dsomap

build DictionaryPackage/CMakeFiles/DictionaryPackageDict.dsomap DictionaryPackage/CMakeFiles/DictionaryPackageDictReflexDict.cxx x86_64-mac1014-clang100-opt/lib/libDictionaryPackageDict_rdict.pcm: CUSTOM_COMMAND DictionaryPackage/CMakeFiles/DictionaryPackageDict.h /Users/krasznaa/ATLAS/sw/projects/volatile/externals/CMakeDictionaryTests/DictionaryPackage/DictionaryPackage/DictionaryPackageDict.h DictionaryPackage/CMakeFiles/selection.xml DictionaryPackage/CMakeFiles/DictionaryPackageDict.h /Users/krasznaa/ATLAS/sw/projects/volatile/externals/CMakeDictionaryTests/DictionaryPackage/DictionaryPackage/DictionaryPackageDict.h
  COMMAND = cd /Users/krasznaa/ATLAS/sw/projects/volatile/externals/build_test/DictionaryPackage/CMakeFiles && /Users/krasznaa/Software/CMake/3.11.4/bin/cmake -E make_directory /Users/krasznaa/ATLAS/sw/projects/volatile/externals/build_test/x86_64-mac1014-clang100-opt/lib && sh /Users/krasznaa/ATLAS/sw/projects/volatile/externals/build_test/DictionaryPackage/CMakeFiles/makeDictionaryPackageDictReflexDict.sh
  DESC = Generating DictionaryPackageDictReflexDict.cxx
  depfile = /Users/krasznaa/ATLAS/sw/projects/volatile/externals/build_test/DictionaryPackage/CMakeFiles/DictionaryPackageDictReflexDict.d
  restat = 1

Which again, looks correct to me. But... This setup results in Ninja *always* re-running this command on every re-build. Even when I didn't modify any of the files referenced in that dependency file. :-(

Does anyone have an idea what I'm doing wrong? Unfortunately I didn't manage to find examples of using DEPFILE that would've helped me further than this... Any help would be very much appreciated.

Cheers,
         Attila



More information about the CMake mailing list