Skip to content

Ninja: Use as dependency file <objectDir/SourceName>.d if needed.

Claus Klein requested to merge ClausKlein/cmake:feature/fixDepFileName into master

Use a dependency file name like <objectDir/SourceName>.d if needed

Motivation

gcov, lcov

I have a particular problem with the cmake generated output file naming conventions (like myfile.cpp.o, myfile.cpp.gcno, ....).

These naming conventions creating trouble for me to run gcov *.cpp properly with my -o /objdir option (I had to copy them in the same directory without the .ccp extensions every time!).

see https://cmake.org/pipermail/cmake/2012-August/051628.html

Ancient cross compiler

The C compiler test fails because CMake insists on createing testCCompiler.c.r30 and there doesn't seem to be way to tell it to not include the source file name extension (here: .c) into the object file name.

However, the linker takes the first dot as extension (I know that this is horribly broken) and only takes files that have ".r30" as extension. ".c.r30" is an illegal extension.

see https://cmake.org/pipermail/cmake/2008-April/021057.html

More proprietary compilers

Ninja generator sets the name to objectpath.d:

cmGlobalNinjaGenerator::EncodeDepfileSpace(objectFileName + ".d");

WindRiver sets the dependency file name to objectDir/SourceName.d, so the ".obj" in "DEP_FILE = path/BaseName.c.obj.d" must be renamed!

see https://cmake.org/pipermail/cmake/2014-October/058963.html

GHS compiler too

Wenn using the Ninja generator to build with GHS compiler for Integrity targets, we have the same problem. We can NOT control the name of the generated dependency ".d" file while compile step!

"DEP_FILE = path/BaseName.d" **has to be used!**

see https://cmake.org/pipermail/cmake-developers/2018-January/030532.html

Solution

If we really want to avoid the extension we can set the undocumented internal implementation detail variable:

set(CMAKE_C_DEPFILE_EXTENSION_REPLACE 1)
set(CMAKE_CXX_DEPFILE_EXTENSION_REPLACE 1)

some time after the project() command call that enables the C and CXX languages.

Patch needed

Change the Nina generator:

file: Source/cmNinjaTargetGenerator.cxx
function: void cmNinjaTargetGenerator::WriteObjectBuildStatement(...)
// ...
  if (!this->NeedDepTypeMSVC(language)) {
    bool replaceExt(false);
    if (!language.empty()) {
      std::string repVar = "CMAKE_";
      repVar += language;
      repVar += "_DEPFILE_EXTENSION_REPLACE";
      replaceExt = this->Makefile->IsOn(repVar);
    }
    if (!replaceExt) {
      // use original code
      vars["DEP_FILE"] = this->GetLocalGenerator()->ConvertToOutputFormat(
        objectFileName + ".d", cmOutputConverter::SHELL);
    } else {
      // Replace the original source file extension with the
      // depend file extension.
      std::string dependFileName = cmSystemTools::GetFilenameWithoutExtension(
        objectFileName) + ".d";
      vars["DEP_FILE"] = this->GetLocalGenerator()->ConvertToOutputFormat(
        objectFileDir + "/" + dependFileName, cmOutputConverter::SHELL);
    }
  }

Caveat

As the variables are internal details this will not be guaranteed to work in the future!

Question

Is it possible to make the internal CMAKE_<LANG>_OUTPUT_EXTENSION_REPLACE option public and support it in future CMake releases including my patch?


Topic-rename: ninja-depfile-name
Superseded-by: !1781 (merged)

Edited by Brad King

Merge request reports