Search code examples
c++objective-cxcodecmakeclang

CMake in macOS, precompiled header files (.pch) support


I was thinking about moving my project compilation from the cumbersome Xcode into the convenient environment of CMake.

However, I wasn't able to find cmake support for precompiled header files (.pch) in macOS that mimic the Xcode flow.

In Xcode, the first stage is ProcessPCH where the pch file compile is compiled as standalone file according to the source code language (my project contains m mm and cpp files)

I.E :

1. objective-c(.m): `-x objective-c-header` 
2. objective-c++(.mm): `-x objective-c++-header` 
3. c++(.cpp): `-x c++-header`
... 

and the result is binary Macho target with suffix .pch.pch.

Then, in order to precompile the pch with every source file in the project the following flag is added to the compilation command -include <pch_output_file_from_the_previous_stage>

Is there any way to do so in CMake ? are there any equivalent alternatives ?

EDIT:

I revealed that it was once possible to set up precompiled headers for macOS project with regular .h file and GCC compiler, but I haven't found the corresponding flags for clang/clang++ which are now used by Xcode

set_target_properties(
    executable
    PROPERTIES
    XCODE_ATTRIBUTE_GCC_PREFIX_HEADER "path/stdwx.h"
    XCODE_ATTRIBUTE_GCC_PRECOMPILE_PREFIX_HEADER "YES"
)

Solution

  • CMake has just gained support for PCHs, it should be available in the upcoming 3.16 release, due 2019-10-01:

    https://gitlab.kitware.com/cmake/cmake/merge_requests/3553

      target_precompile_headers(<target>
        <INTERFACE|PUBLIC|PRIVATE> [header1...]
        [<INTERFACE|PUBLIC|PRIVATE> [header2...] ...])
    

    There is ongoing discussion on supporting sharing PCHs between targets: https://gitlab.kitware.com/cmake/cmake/issues/19659

    This includes support for xcode generator too.