Skip to content

AUTOMOC: Automatically use response ("options") file for MOC compiler

dgehri requested to merge dgehri/cmake:feature/moc-rsp into master

AutoMOC: Automatically use response ("options") file for moc compiler when exceeding Windows command line limit

Issue

Windows has a hard-coded limit for the length of any command line of 32767 characters (see https://devblogs.microsoft.com/oldnewthing/20031210-00/?p=41553). On the other hand, CMake targets with many dependencies may result is long lists of include options to be passed to the compiler, linker or the MOC compiler.

As a work-around, many tools support so-called "response files" (or "options file" for MOC: https://doc.qt.io/qt-5/moc.html), where the arguments to be passed to a command are written to a text file, and the path to that text file is then added to the command line (typically as @<path-to-response-file>.

While it is possible to instruct e.g. Ninja to use response files (CMAKE_NINJA_FORCE_RESPONSE_FILE), CMake currently does not provide a way to do the same for the MOC compiler.

Proposed Solution

This PR enables automatic use of a response/options file for the MOC compiler during AutoMOC (AUTOMOC=ON), whenever the MOC command line length exceeds the allowable limit. The actual limit is set to 32000, slightly below the maximum 32767, to allow for a small margin in case downstream code decides to add additional options.

If CMake detects that the limit will be exceeded, the options are written into an options file next to the output file, named <output-file-path>.rsp, and its path passed to the MOC compiler as @<output-file-path>.rsp.

Rational for Proposed Solution

This PR handles the command line limit by automatically switching to a response file, and without the need for the user to set a dedicated CMake variable. The reason for this choice is that there is no other possible workaround: if a response file is not created, MOC is guaranteed to fail. As such, the user doesn't really have an option not to handle the case.

Testing

This PR was tested with an actual code base exhibiting the problem, and was found to correctly circumvent failure to run MOC whenever the command line limit is exceeded.

Thanks for considering this PR.

Topic-rename: automoc-rsp

Edited by Brad King

Merge request reports