• Stars
    star
    103
  • Rank 333,046 (Top 7 %)
  • Language CMake
  • License
    MIT License
  • Created almost 10 years ago
  • Updated about 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

CMake precompiled header support via custom PCH compiler extension

⚠️ This project is obsolete as CMake 3.16 or higher provides native support for precompiled headers. No PRs will be accepted and no further development is planned.

CMake Precompiled Headers

CMakePCHCompiler module defines extra CXXPCH/CPCH "meta"-compiler that compiles .h into .pch/.gch using existing CXX/C compiler.

For convenience it defines

target_precompiled_header(target [...] header
                          [REUSE other_target]
                          [TYPE type])

Uses given header as precompiled header for given target.

Optionally it may share compiled header object with other_target, so it is precompiled just once.

For advanced users it allows customizing precompiler header type passed to compiler, which is normally inferred from the language, e.g. c++-header for CXX.

NOTE: While CMakePCHCompiler ensures that precompiled header is included as first compile unit for each source file, it is still recommended to keep #include "prefix.h" (where prefix.h is your header file you want to pre-compile - header argument) in your source code to ensure your code remains portable regardless of precompiled headers being enabled or not.

For more details how to use precompiled header with your library and/or compiler refer to their documentation i.e. GCC, Qt, etc.

IMPORTANT Before you submit issue report or feature request, please...

  1. Be aware that CMake 3.16 or higher provides built-in support for precompiled headers, and all related to CMake issues should be reported at CMake's issue tracker.
  2. Be aware that this project is now obsolete and no further development is planned and no PRs will be accepted.
  3. Read this README.md file completely to understand intentions and limitations of this project.
  4. Understand that CMakePCHCompiler is neither official nor proper way to provide PCH support in CMake.
  5. Understand that the CMakePCHCompiler authors are neither compensated for their efforts not affiliated with KitWare (CMake's authors).

Why this project existed

In summer 2019 @cristianadam has finished native CMake functionality for precompiled headers that has been merged to mainline CMake and staged for 3.16.0. Until that, native support for precompiled headers was requested for several years at CMake's mailing lists and/or its issue tracker. This project was started in 2015 as a proof-of-concept implementation accompanying RFC: CMake precompiled header support and custom compiler based implementation.

NOTE: This project was and is neither trying to be official nor proper way to provide PCH support in CMake. Authors stated that only viable and future-proof solution was to implement this functionality natively in CMake, and this was done for CMake version 3.16. CMakePCHCompiler authors are not affiliated with KitWare (CMake's authors).

⚠️ Right not this project is considered obsolete, no further development is planned and no PRs will be accepted. It is left here for historical purposes.

See also an umbrella issue at CMake's issue tracker (KitWare's GitLab) for more information on native PCH support effort.

Supported & tested platforms

  1. CMake version 3.0 or higher
  2. Windows with MSVC, tested on VS2015
  3. OSX with Clang, GCC, tested on OSX 10.10 & Xcode 6.1
  4. Linux with GCC, tested on Ubuntu 14.04 LTS & GCC 4.8

Note for MSVC users

Due to the problem in MSVC 2010 and higher's Microsoft.Cpp.Win32.targets deleting PCH, this module currently enforces /Z7 compiler flag for MSVC, hence debug information is stored on .obj files instead of .pdb program database. This is certainly not a perfect solution, but only one that is known to work so far. If you know any better workaround please submit PR. Thanks!

More information can be found at Visual Studio Community and is tracked at issue #21.

Example

cmake_minimum_required(VERSION 3.0)

list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake/CMakePCHCompiler)

project(pchtest CXX CXXPCH)

add_library(engine SHARED src/engine.cpp src/library.cpp)
target_precompiled_header(engine src/prefix.h)

add_executable(demo src/demo.cpp)
target_link_libraries(demo engine)
target_precompiled_header(demo src/prefix.h REUSE engine)

What it is about?

CMake does not support precompiled headers by default. There are several modules providing precompiled header support, but all of them use custom commands and complicated wrappers to achieve their goals.

This module is somehow different. It defines a meta C++ compiler that simply just patches compiler command template for precompiled header case.

Next it treats precompiled header file as source file for CXXPCH that makes CMake use CXXPCH patched instead origin CXX compiler template. This ensures that all CXX language flags and specific settings such as these populated by add_definitions are also applied to precompiled header.

Passing same flags during precompiled header and other source files compilation is very important. It is simply impossible to catch all flags, such as these defined after calling target_precompiled_header or these using CMake internal variables such as add_definitions, using custom commands. This is the reason for such implementation.

This module is also transparent to source code. There is absolutely no need to change you source files. Only requirement is a precompiled header .h file added to given target via target_precompiled_header function.

Nevertheless this is not an ideal solution. In perfect world it is CMake that should handle precompiled headers generation internally, based on given compiler command templates. However this may be good start to request native support using simple API of:

target_precompiled_header(<target> <path/to/precompiled_header.h>)
target_precompiled_header(<target1> <target2>
                          <path/to/precompiled_header.h>)
target_precompiled_header(<target> <path/to/precompiled_header.h> REUSE
                          <other_target_to_reuse_precompiled_header_from>)

How does it work?

First, we define new compilers CPCH and CXXPCH using CMAKE_<LANG>_* variables. These compilers copy run templates and options from existing C and CXX compilers respectively.

Next we provide target_precompiled_header function that enabled precompiled header on given target.

Pre-compiler header is build in new target.pch subtarget using:

add_library(${target}.pch OBJECT ${header})

This is done on purpose because of few reasons:

  1. CMake does not allow to insert source file to existing target once it has been defined.

  2. Even if it was possible, we could not ensure precompiled header is built first in main target, but adding it as subtarget we can.

  3. We cannot prevent header.pch, which is output of CPCH/CXXPCH compiler from being linked when it is in part of main target, but if we put it into OBJECT library, then by definition we skip linking process. Also we take the result object to be a recompiled header for main target.

License

Copyright (c) 2015-2019 CMakePCHCompiler Authors

This code is licensed under the MIT License, see LICENSE for details.

More Repositories

1

DisableTurboBoost.kext

Disables Turbo Boost Technology on Mac OS X for reliable OpenMP/CL benchmarking
C
268
star
2

WebFrameworkBenchmark

Benchmarking some web frameworks
Java
55
star
3

glua

glua = LuaJIT + FFI + OpenGL/GLUT
Lua
44
star
4

nim-orm

Nim's ORM module
Nim
38
star
5

mountblockd

Launch daemon blocking auto mounting of certain volumes
C
36
star
6

osxglut

GLUT for Mac OS X fork with Core Profile and scroll wheel support
C
12
star
7

CFNetwork

CoreNetwork Apple source mirror
C
9
star
8

ssebench

Little (SSE) 4x4 matrix multiplication test for various compilers
C
8
star
9

mjpeg

MJPEG is small command line utility to create OpenDML AVI from JPEG series
C
8
star
10

efi

Grub 2.00 based triple boot for Mac/Parallels
Shell
7
star
11

Mac-gtk2-theme

Mac GTK2 theme for OSX GTK2 applications
5
star
12

jupterlab-macos-runner

Standalone app running jupyter-lab in the background, showing JupyterLab UI in WebKit window
Objective-C
4
star
13

cutescope

ADALM2000 oscilloscope app using Qt, libm2k and iio
C++
4
star
14

keyremapd

DoubleCommand inspired user-space key remapper daemon
C
4
star
15

DocSets

Dash & Xcode docsets for various documentations
Shell
4
star
16

JSObjCBridge

A JavaScriptCore to Objective-C bridge
Objective-C
4
star
17

Skim

Mirror of Skim PDF reader and note-taker for OS X Subversion repository with custom fixes (devel branch)
Objective-C
3
star
18

ldaptestserver

LDAP test server written in Go
Go
3
star
19

sinatra-hat

Sinatra Ruby gem extensions for gettext, caching, reloading, etc.
Ruby
3
star
20

trim-enabler-script

A clone/mirror of public gist.github.com/4058659
Shell
2
star
21

blueswitch

Ensured that desired Bluetooth host device (e.g. USB dongle) remains active in macOS (after reboot)
2
star
22

CMakeCUDACompiler

CMake CUDA support as compiler (alternative to FindCUDA)
1
star
23

CF

CoreFoundation Apple source mirror
C
1
star
24

nanoANT.tmbundle

nanoANT's TextMate 2 theme
1
star
25

assembly.tmbundle

Assembly (x86+SSE+AVX) bundle for TextMate
1
star
26

flops

FLOPs counter for x86 SSE and AVX based on awesome code of Mystical:
C++
1
star
27

Fetch.framework

Fast *CoreNetwork* based HTTP in-order fetcher
Objective-C
1
star
28

ChromeSVG2Clipboard

Copy SVG from Chrome into a clipboard as PNG compatible with Microsoft Office
C#
1
star