{"id":13646055,"url":"https://github.com/nanoant/CMakePCHCompiler","last_synced_at":"2025-04-21T17:31:50.987Z","repository":{"id":25907181,"uuid":"29347926","full_name":"nanoant/CMakePCHCompiler","owner":"nanoant","description":"CMake precompiled header support via custom PCH compiler extension","archived":false,"fork":false,"pushed_at":"2019-10-26T09:35:06.000Z","size":60,"stargazers_count":104,"open_issues_count":9,"forks_count":19,"subscribers_count":15,"default_branch":"master","last_synced_at":"2025-02-25T04:41:47.862Z","etag":null,"topics":["clang","cmake","cmake-precompiled-headers","gcc","precompiled-headers"],"latest_commit_sha":null,"homepage":null,"language":"CMake","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nanoant.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-01-16T12:40:35.000Z","updated_at":"2024-12-16T00:34:05.000Z","dependencies_parsed_at":"2022-08-29T21:11:49.162Z","dependency_job_id":null,"html_url":"https://github.com/nanoant/CMakePCHCompiler","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nanoant%2FCMakePCHCompiler","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nanoant%2FCMakePCHCompiler/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nanoant%2FCMakePCHCompiler/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nanoant%2FCMakePCHCompiler/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nanoant","download_url":"https://codeload.github.com/nanoant/CMakePCHCompiler/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250100547,"owners_count":21374962,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["clang","cmake","cmake-precompiled-headers","gcc","precompiled-headers"],"created_at":"2024-08-02T01:02:47.569Z","updated_at":"2025-04-21T17:31:50.519Z","avatar_url":"https://github.com/nanoant.png","language":"CMake","readme":"\u003e :warning: 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.\n\nCMake Precompiled Headers\n=========================\n\nCMakePCHCompiler module defines extra `CXXPCH`/`CPCH` \"meta\"-compiler that compiles `.h` into `.pch/.gch` using existing `CXX`/`C` compiler.\n\nFor convenience it defines\n\n\ttarget_precompiled_header(target [...] header\n\t                          [REUSE other_target]\n\t                          [TYPE type])\n\nUses given `header` as precompiled header for given target.\n\nOptionally it may share compiled header object with `other_target`, so it is precompiled just once.\n\nFor advanced users it allows customizing precompiler header `type` passed to compiler, which is normally inferred from the language, e.g. `c++-header` for `CXX`.\n\n**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.\n\nFor more details how to use precompiled header with your library and/or compiler refer to their documentation i.e. GCC, Qt, etc.\n\n**IMPORTANT** Before you submit issue report or feature request, please...\n--------------------------------------------------------------------------\n\n[pchissue]: https://gitlab.kitware.com/cmake/cmake/issues/1260\n\n1. 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][pchissue].\n2. Be aware that this project is now obsolete and no further development is planned and no PRs will be accepted.\n3. Read this `README.md` file completely to understand intentions and limitations of this project.\n4. Understand that CMakePCHCompiler is neither official nor proper way to provide PCH support in CMake.\n5. Understand that the CMakePCHCompiler authors are neither compensated for their efforts not affiliated with KitWare (CMake's authors).\n\nWhy this project existed\n------------------------\n\n[pchmerge]: https://gitlab.kitware.com/cmake/cmake/merge_requests/3553\n[rfc]: https://cmake.org/pipermail/cmake-developers/2015-February/024598.html\n[cristianadam]: https://github.com/cristianadam/\n\nIn summer 2019 [@cristianadam][cristianadam] has finished [native CMake functionality for precompiled headers][pchmerge] 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][rfc].\n\n**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).\n\n\u003e :warning: 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.\n\nSee also an [umbrella issue at CMake's issue tracker][pchissue] (KitWare's GitLab) for more information on native PCH support effort.\n\nSupported \u0026 tested platforms\n----------------------------\n\n1. *CMake* version 3.0 or higher\n2. *Windows* with `MSVC`, tested on *VS2015*\n3. *OSX* with `Clang`, `GCC`, tested on *OSX 10.10* \u0026 *Xcode 6.1*\n4. *Linux* with `GCC`, tested on *Ubuntu 14.04 LTS* \u0026 *GCC 4.8*\n\nNote for MSVC users\n-------------------\n\nDue 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!\n\n[z7vscomm]: https://developercommunity.visualstudio.com/content/problem/15171/shared-precompiled-header-gots-deleted-during-buil.html\n[z7issue]: https://github.com/nanoant/CMakePCHCompiler/issues/21\n\nMore information can be found at [Visual Studio Community][z7vscomm] and is tracked at [issue #21][z7issue].\n\nExample\n-------\n\n~~~cmake\ncmake_minimum_required(VERSION 3.0)\n\nlist(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake/CMakePCHCompiler)\n\nproject(pchtest CXX CXXPCH)\n\nadd_library(engine SHARED src/engine.cpp src/library.cpp)\ntarget_precompiled_header(engine src/prefix.h)\n\nadd_executable(demo src/demo.cpp)\ntarget_link_libraries(demo engine)\ntarget_precompiled_header(demo src/prefix.h REUSE engine)\n~~~\n\nWhat it is about?\n-----------------\n\n*CMake* does not support precompiled headers by default. There are several\nmodules providing precompiled header support, but all of them use custom\ncommands and complicated wrappers to achieve their goals.\n\nThis module is somehow different. It defines a *meta C++ compiler* that simply\njust patches compiler command template for precompiled header case.\n\nNext it treats precompiled header file as source file for `CXXPCH` that makes\n*CMake* use `CXXPCH` patched instead origin `CXX` compiler template. This\nensures that all `CXX` language flags and specific settings such as these\npopulated by `add_definitions` are also applied to precompiled header.\n\nPassing same flags during precompiled header and other source files compilation\nis very important. It is simply impossible to catch all flags, such as these\ndefined after calling `target_precompiled_header` or these using *CMake*\ninternal variables such as `add_definitions`, using custom commands. This is\nthe reason for such implementation.\n\nThis module is also transparent to source code. There is absolutely no need to\nchange you source files. Only requirement is a precompiled header `.h` file\nadded to given target via `target_precompiled_header` function.\n\nNevertheless this is not an ideal solution. In perfect world it is *CMake* that\nshould handle precompiled headers generation internally, based on given\ncompiler command templates. However this may be good start to request native\nsupport using simple API of:\n\n~~~cmake\ntarget_precompiled_header(\u003ctarget\u003e \u003cpath/to/precompiled_header.h\u003e)\ntarget_precompiled_header(\u003ctarget1\u003e \u003ctarget2\u003e\n                          \u003cpath/to/precompiled_header.h\u003e)\ntarget_precompiled_header(\u003ctarget\u003e \u003cpath/to/precompiled_header.h\u003e REUSE\n                          \u003cother_target_to_reuse_precompiled_header_from\u003e)\n~~~\n\nHow does it work?\n-----------------\n\nFirst, we define new compilers `CPCH` and `CXXPCH` using `CMAKE_\u003cLANG\u003e_*`\nvariables. These compilers copy run templates and options from existing `C` and\n`CXX` compilers respectively.\n\nNext we provide `target_precompiled_header` function that enabled precompiled\nheader on given target.\n\nPre-compiler header is build in new `target.pch` subtarget using:\n\n~~~cmake\nadd_library(${target}.pch OBJECT ${header})\n~~~\n\nThis is done on purpose because of few reasons:\n\n1. *CMake* does not allow to insert source file to existing target once it has\n   been defined.\n\n2. Even if it was possible, we could not ensure precompiled header is built\n   first in main target, but adding it as subtarget we can.\n\n3. We cannot prevent `header.pch`, which is output of `CPCH/CXXPCH` compiler\n   from being linked when it is in part of main target, but if we put it into\n   OBJECT library, then by definition we skip linking process. Also we take the\n   result object to be a recompiled header for main target.\n\nLicense\n-------\n\n[authors]: https://github.com/nanoant/CMakePCHCompiler/graphs/contributors\n\nCopyright (c) 2015-2019 [CMakePCHCompiler Authors][authors]\n\nThis code is licensed under the MIT License, see [LICENSE](LICENSE) for details.\n","funding_links":[],"categories":["CMake","Utility Scripts"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnanoant%2FCMakePCHCompiler","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnanoant%2FCMakePCHCompiler","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnanoant%2FCMakePCHCompiler/lists"}