Skip to content

Shorten object name even if still longer than CMAKE_OBJECT_PATH_MAX

Nick Little requested to merge nicklaus.little/cmake:shorter-obj into master

The generated object file name may be too long even when the source file name is well within the limits of the operating system. This can happen because the object file name is based on the path to the build directory plus the absolute path of the source, which can result in very long file paths. CMake gets around this in most cases by substituting some portion of the object file path with an MD5 sum. However, if the resulting path would still be too long, CMake does not substitute the MD5 sum even if the resulting path would be shorter.

When the object file is placed into the generated build files, in at least some cases (the "Ninja Multi-Config" generator), the path is relativized to make it shorter. In a lot of cases, the resulting path would have been short enough if the MD5 sum substitution had occurred even though the initial resulting path was too long. For example, here is a path that was generated before this change which is over the 260 Windows file path limit:

CMakeFiles\XXXXXXXXXX.dir\Release\C_\Users\XXXXXXXXXXXXX\Documents\Code\XXXXXXXX\XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\XXX\XXXXXXXXXXXX\XXXXXXXXXX\XXXXXXXXX\XXXXXXXXXXXX\XXX\XXXX\XXXXXXX\XXXXXXXXXX\XXXXXXX\XXXXXX\XXXXXXXX\XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.cpp.obj

Here is the path generated when the MD5 sum is substituted, which is significantly shorter and able to be handled by the operating system easily:

CMakeFiles\XXXXXXXXXX.dir\Release\25097a362629fcbcdc9f6b88a98a2979\XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.cpp.obj

This change will cause the MD5 sum substitution to occur anytime the resulting path will be shorter even if the resulting file path will still exceed the CMAKE_OBJECT_PATH_MAX length. The cmLocalGeneratorShortenObjectName() function still returns false if the resulting path exceeds the CMAKE_OBJECT_PATH_MAX length, so a warning will still be issued if the path is too long. The resulting path is just less likely to cause an issue when using some generators.

Edited by Brad King

Merge request reports