Skip to content

Set CMAKE_<LANG>_COMPILER_PREDEFINES_COMMAND in Windows.MSVC.toolchain.cmake#129

Merged
MarkSchofield merged 2 commits intoMarkSchofield:mainfrom
hadogenes:mod_predefs
Jun 17, 2025
Merged

Set CMAKE_<LANG>_COMPILER_PREDEFINES_COMMAND in Windows.MSVC.toolchain.cmake#129
MarkSchofield merged 2 commits intoMarkSchofield:mainfrom
hadogenes:mod_predefs

Conversation

@hadogenes
Copy link
Copy Markdown
Contributor

@hadogenes hadogenes commented Jun 4, 2025

Setting "CMAKE__COMPILER_PREDEFINES_COMMAND" with the command that will produce the compiler's predefines unblocks downstream consumers, like 'automoc'.

Signed-off-by: Jacek Szafarkiewicz <jacek@szafar.pl>
@hadogenes hadogenes requested a review from MarkSchofield as a code owner June 4, 2025 08:30
@MarkSchofield
Copy link
Copy Markdown
Owner

I don't really follow - this doesn't really seem to be a responsibility of the toolchain..?

@hadogenes
Copy link
Copy Markdown
Contributor Author

hadogenes commented Jun 5, 2025

https://gitlab.kitware.com/cmake/cmake/-/merge_requests/8359
it is defined in ex. Modules/Compiler/GNU.cmake (as CMAKE_${lang}_COMPILER_PREDEFINES_COMMAND)

@MarkSchofield
Copy link
Copy Markdown
Owner

https://gitlab.kitware.com/cmake/cmake/-/merge_requests/8359 it is defined in ex. Modules/Compiler/GNU.cmake (as CMAKE_${lang}_COMPILER_PREDEFINES_COMMAND)

Ah, gotcha. So it's not really for generating moc_predefs.h directly - defining CMAKE__${lang}__COMPILER_PREDEFINES_COMMAND allows tooling to run that to get the predefines. And that unblocks automoc so that it can generate moc_predefs.h.

If I add, say:

execute_process(
        COMMAND ${CMAKE_CXX_COMPILER_PREDEFINES_COMMAND}
        OUTPUT_VARIABLE CXX_PREDEFINES_OUTPUT
    )
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/cxx_predefines.txt ${CXX_PREDEFINES_OUTPUT})

Then the predefines are written, but an '.obj' file is generated into my work-tree. It looks like you can redirect to nul with /Fonul. I think that's worth adding.

Is it worth adding a C equivalent, too? i.e.

set(CMAKE_C_COMPILER_PREDEFINES_COMMAND
    ${CMAKE_C_COMPILER}
        /nologo
        /Zc:preprocessor
        /PD
        /c
        /Fonul.
        ${CMAKE_ROOT}/Modules/CMakeCCompilerABI.c
)

@MarkSchofield MarkSchofield changed the title Generate moc_predefs.h file for automoc Set CMAKE_<LANG>_COMPILER_PREDEFINES_COMMAND in Windows.MSVC.toolchain.cmake Jun 12, 2025
@MarkSchofield
Copy link
Copy Markdown
Owner

@hadogenes - I pushed a commit with my recommendations. Does that work for your use case?

@MarkSchofield MarkSchofield merged commit d895710 into MarkSchofield:main Jun 17, 2025
19 checks passed
InCom-0 pushed a commit to InCom-0/cmake_WindowsToolchain that referenced this pull request Mar 10, 2026
…n.cmake (MarkSchofield#129)

* Generate moc_predefs.h file for automoc

Signed-off-by: Jacek Szafarkiewicz <jacek@szafar.pl>

* Add CMAKE_C_COMPILER_PREDEFINES_COMMAND; include '/Fonul.' to avoid .obj files being written

---------

Signed-off-by: Jacek Szafarkiewicz <jacek@szafar.pl>
Co-authored-by: Mark Schofield <34426337+MarkSchofield@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants