CMake doesn't respect 8.3 short paths for source and build directory

Note

I’ve posted this here because New Issue · CMake / CMake · GitLab (kitware.com) refused to accept the text below as:

The form contains the following error:

  • Your issue has been recognized as spam. Please, change the content to proceed.

Preamble

In order to have support for long paths on Windows CMake needs to support short paths first.

Long paths support for Windows

Windows has support for long paths (>255 characters) since Windows 10 version 1607 released on August 2015. But the applications need to opt-in in order to use the new functionality.

The Visual C++ compiler has been developed in the nineties and the reported issue is being considered but not yet fixed (Jan 2024).

Ninja build system has been fixed but the 1.12.0 release has not been made, latest release was v1.11.1 on Aug 30, 2022.

Qt Applications Windows problem

At QTCREATORBUG-26786 there is an issue compiling Qt Creator itself.
At QTBUG-117413 there is a bug report with a broader Qt application issue.

This is an issue since user cannot compile Qt applications on Windows with the recommended Visual C++ toolchain.

Small Hello World example

Let’s say we have this main.cpp C++ Hello World program:

#include <iostream>

using namespace std;

int main()
{
    cout << "Hello Long Path Windows World!" << endl;
    return 0;
}

and a CMakeLists.txt like this:

cmake_minimum_required(VERSION 3.5)

project(HelloWorld LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_executable(HelloWorld main.cpp)

include(GNUInstallDirs)
install(TARGETS HelloWorld
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

These two files are located in the following path:

C:\Projects\ThisPathWasArtificiallyMadeLongInOrderToTestHowAWindowsCompilerWouldBehaveWhenHavingAPathLongerThanWhatMAXPATHIsAllowingNamelyLongerThan255Characters

Then I’ve tried the following configure command:

$ cmake -S HelloWorld -B build-HelloWorld-Qt_6_6_0_msvc2022_arm64-Debug -GNinja -DCMAKE_BUILD_TYPE=Debug

This fails due to long path support missing from cl.exe and ninja.exe:

configuration error message
$ cmake -S HelloWorld -B build-HelloWorld-Qt_6_6_0_msvc2022_arm64-Debug -GNinja -DCMAKE_BUILD_TYPE=Debug
-- The CXX compiler identification is unknown
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2022/BuildTools/VC/Tools/MSVC/14.38.33130/bin/Hostarm64/arm64/cl.exe
CMake Warning in C:/Projects/ThisPathWasArtificiallyMadeLongInOrderToTestHowAWindowsCompilerWouldBehaveWhenHavingAPathLongerThanWhatMAXPATHIsAllowingNamelyLongerThan255Characters/build-HelloWorld-Qt_6_6_0_msvc2022_arm64-Debug/CMakeFiles/CMakeScratch/TryCompile-qo4hom/CMakeLists.txt:
  The object file directory
C:/Projects/ThisPathWasArtificiallyMadeLongInOrderToTestHowAWindowsCompilerWouldBehaveWhenHavingAPathLongerThanWhatMAXPATHIsAllowingNamelyLongerThan255Characters/build-HelloWorld-Qt_6_6_0_msvc2022_arm64-Debug/CMakeFiles/CMakeScratch/TryCompile-qo4hom/CMakeFiles/cmTC_87735.dir/./

has 279 characters. The maximum full path to an object file is 250
characters (see CMAKE_OBJECT_PATH_MAX). Object file

testCXXCompiler.cxx.obj

cannot be safely placed under this directory. The build may not work
correctly.

CMake Error:
Running

‘C:/Tools/Ninja/ninja.exe’ ‘-C’ ‘C:/Projects/ThisPathWasArtificiallyMadeLongInOrderToTestHowAWindowsCompilerWouldBehaveWhenHavingAPathLongerThanWhatMAXPATHIsAllowingNamelyLongerThan255Characters/build-HelloWorld-Qt_6_6_0_msvc2022_arm64-Debug/CMakeFiles/CMakeScratch/TryCompile-qo4hom’ ‘-t’ ‘recompact’

failed with:

ninja: error: loading ‘build.ninja’: The system cannot find the path specified.

CMake Error at C:/Program Files/CMake/share/cmake-3.28/Modules/CMakeTestCXXCompiler.cmake:49 (try_compile):
Failed to generate test project build system.
Call Stack (most recent call first):
CMakeLists.txt:3 (project)

– Configuring incomplete, errors occurred!

C:\Projects\ThisPathWasArtificiallyMadeLongInOrderToTestHowAWindowsCompilerWouldBehaveWhenHavingAPathLongerThanWhatMAXPATHIsAllowingNamelyLongerThan255Characters

Short 8.3 paths

CMake has already instances of using 8.3 short paths for Ninja for the C/C++ compiler path and for a Visual C++ 6.0 linker issue

Aimed with this information I’ve had a look at how the 8.3 short paths would look like:

26/01/2024  13:36    <DIR>          THISPA~1     ThisPathWasArtificiallyMadeLongInOrderToTestHowAWindowsCompilerWouldBehaveWhenHavingAPathLongerThanWhatMAXPATHIsAllowingNamelyLongerThan255Characters

 Directory of C:\Projects\THISPA~1

26/01/2024  14:45    <DIR>          BUILD-~1     build-HelloWorld-Qt_6_6_0_msvc2022_arm64-Debug
26/01/2024  14:10    <DIR>          HELLOW~1     HelloWorld

My expectation was that if I provide short 8.3 names CMake would simply work:

$ cmake -S c:\Projects\THISPA~1\HELLOW~1 -B c:\projects\THISPA~1\BUILD-~1 -GNinja -DCMAKE_BUILD_TYPE=Debug
configuration error message
$ cmake -S c:\Projects\THISPA~1\HELLOW~1 -B c:\projects\THISPA~1\BUILD-~1 -GNinja -DCMAKE_BUILD_TYPE=Debug
-- The CXX compiler identification is unknown
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2022/BuildTools/VC/Tools/MSVC/14.38.33130/bin/Hostarm64/arm64/cl.exe
CMake Warning in C:/Projects/ThisPathWasArtificiallyMadeLongInOrderToTestHowAWindowsCompilerWouldBehaveWhenHavingAPathLongerThanWhatMAXPATHIsAllowingNamelyLongerThan255Characters/build-HelloWorld-Qt_6_6_0_msvc2022_arm64-Debug/CMakeFiles/CMakeScratch/TryCompile-obxgga/CMakeLists.txt:
  The object file directory
C:/Projects/ThisPathWasArtificiallyMadeLongInOrderToTestHowAWindowsCompilerWouldBehaveWhenHavingAPathLongerThanWhatMAXPATHIsAllowingNamelyLongerThan255Characters/build-HelloWorld-Qt_6_6_0_msvc2022_arm64-Debug/CMakeFiles/CMakeScratch/TryCompile-obxgga/CMakeFiles/cmTC_e312f.dir/./

has 279 characters. The maximum full path to an object file is 250
characters (see CMAKE_OBJECT_PATH_MAX). Object file

testCXXCompiler.cxx.obj

cannot be safely placed under this directory. The build may not work
correctly.

CMake Error:
Running

‘C:/Tools/Ninja/ninja.exe’ ‘-C’ ‘C:/Projects/ThisPathWasArtificiallyMadeLongInOrderToTestHowAWindowsCompilerWouldBehaveWhenHavingAPathLongerThanWhatMAXPATHIsAllowingNamelyLongerThan255Characters/build-HelloWorld-Qt_6_6_0_msvc2022_arm64-Debug/CMakeFiles/CMakeScratch/TryCompile-obxgga’ ‘-t’ ‘recompact’

failed with:

ninja: error: loading ‘build.ninja’: The system cannot find the path specified.

CMake Error at C:/Program Files/CMake/share/cmake-3.28/Modules/CMakeTestCXXCompiler.cmake:49 (try_compile):
Failed to generate test project build system.
Call Stack (most recent call first):
CMakeLists.txt:3 (project)

– Configuring incomplete, errors occurred!

Short 8.3 path ‘simulation’

Then I decided to move the long path directory somewhere else and create the short paths explicitly (created THISPA~1, HELLOW~1 and BUILD-~1)

The configuration worked as expected:

C:\Projects\THISPA~1
$ cmake -S c:\Projects\THISPA~1\HELLOW~1 -B c:\projects\THISPA~1\BUILD-~1 -GNinja -DCMAKE_BUILD_TYPE=Debug
-- The CXX compiler identification is MSVC 19.38.33134.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2022/BuildTools/VC/Tools/MSVC/14.38.33130/bin/Hostarm64/arm64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done (0.9s)
-- Generating done (0.0s)
-- Build files have been written to: C:/Projects/THISPA~1/BUILD-~1

Then I’ve copied the content of the short build directory into the long directory and swapped back the long path directory.

Building worked as expected since all paths are short paths:

C:\Projects\THISPA~1
$ cmake --build BUILD-~1 --verbose
Change Dir: 'C:/Projects/ThisPathWasArtificiallyMadeLongInOrderToTestHowAWindowsCompilerWouldBehaveWhenHavingAPathLongerThanWhatMAXPATHIsAllowingNamelyLongerThan255Characters/build-HelloWorld-Qt_6_6_0_msvc2022_arm64-Debug'

Run Build Command(s): C:/Tools/Ninja/ninja.exe -v
[1/2] C:\PROGRA~2\MICROS~2\2022\BUILDT~1\VC\Tools\MSVC\1438~1.331\bin\HOSTAR~1\arm64\cl.exe  /nologo /TP   /DWIN32 /D_WINDOWS /W3 /GR /EHsc /MDd /Zi /Ob0 /Od /RTC1 -std:c++17 /showIncludes /FoCMakeFiles\HelloWorld.dir\main.cpp.obj /FdCMakeFiles\HelloWorld.dir\ /FS -c C:\Projects\THISPA~1\HELLOW~1\main.cpp
[2/2] C:\Windows\system32\cmd.exe /C "cd . && "C:\Program Files\CMake\bin\cmake.exe" -E vs_link_exe --intdir=CMakeFiles\HelloWorld.dir --rc=C:\PROGRA~2\WI3CF2~1\10\bin\100226~1.0\arm64\rc.exe --mt=C:\PROGRA~2\WI3CF2~1\10\bin\100226~1.0\arm64\mt.exe --manifests  -- C:\PROGRA~2\MICROS~2\2022\BUILDT~1\VC\Tools\MSVC\1438~1.331\bin\HOSTAR~1\arm64\link.exe /nologo CMakeFiles\HelloWorld.dir\main.cpp.obj  /out:HelloWorld.exe /implib:HelloWorld.lib /pdb:HelloWorld.pdb /version:0.0 /machine:ARM64 /debug /INCREMENTAL /subsystem:console  kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib && cd ."


C:\Projects\THISPA~1
$ cmake --build BUILD-~1 --verbose
Change Dir: 'C:/Projects/ThisPathWasArtificiallyMadeLongInOrderToTestHowAWindowsCompilerWouldBehaveWhenHavingAPathLongerThanWhatMAXPATHIsAllowingNamelyLongerThan255Characters/build-HelloWorld-Qt_6_6_0_msvc2022_arm64-Debug'

Run Build Command(s): C:/Tools/Ninja/ninja.exe -v
ninja: no work to do.


C:\Projects\THISPA~1
$ build-HelloWorld-Qt_6_6_0_msvc2022_arm64-Debug\HelloWorld.exe
Hello Long Path Windows World!

Conclusion

If CMake would support short 8.3 paths it would also support long paths and help A LOT of Windows users that complain about this.

Tools like Qt Creator would then give the 8.3 paths for the -S and -B parameters and the CMake would just do its job! :smile:

I’ve marked the submission as ham on gitlab. Please try posting the issue again, and then post the issue number back here.

Ah, I see CMake Issue 25639, thanks.

1 Like