Boost logo

Boost-Build :

From: Reece Dunn (msclrhd_at_[hidden])
Date: 2005-11-17 16:52:23


Hi All,

I have attached a patch to add PCH support to the msvc toolset. This is
most likely the most complex toolset to get PCH support on, so extending
this for other toolsets should be trivial.

An example of using PCHs in your project is as follows:

-- Jamfile

pch mypch : pch.hpp pch.cpp ;

obj demo : demo.cpp : <pch>off ;

exe hello
   :
     main.cpp demo2.cpp demo mypch
   ;

------

where pch.hpp is the header to be used as the PCH file and pch.cpp is
the source file to create the PCH from.

Here, the sources (main.cpp and demo2.cpp) in hello are using PCHs
except for demo.cpp.

PS: Thanks Volodya for your assistance with this! :)
- Reece


# Copyright (C) Reece H. Dunn 2005.
#
# Use, modification and distribution is subject to the Boost Software
# License Version 1.0. (See accompanying file LICENSE_1_0.txt or
# http://www.boost.org/LICENSE_1_0.txt)

import type ;
import feature : feature ;
import generators ;

##### Using Pre-compiled Headers (Quick Guide) #####
#
# Make mypch.hpp a pre-compiled header (PCH) using mypch.cpp as the source file:
# pch mypch : mypch.cpp mypch.hpp ;
#
# Enable PCHs in a target:
# exe hello : mypch main.cpp hello.cpp ;
# ^^^^^ -- mypch.hpp is a PCH
#
# Don't use PCHs for a specific source:
# obj nopch : nopch.cpp : <pch>off ;
#

type.register PCH : pch ;

feature pch : # control precompiled header (PCH) generation
    on # this file has support for using PCHs (if available)
    off # this file doesn't use PCHs
  ;

feature pch-source : : free dependency ; # mypch.cpp
feature pch-header : : free dependency ; # mypch.hpp
feature pch-file : : free dependency ; # mypch.pch

class pch-generator : generator
{
    import property-set ;

    rule __init__ ( * : * )
    {
        generator.__init__ $(1) : $(2) : $(3) : $(4) : $(5) : $(6) : $(7) : $(8) : $(9) ;
    }

    rule run ( project name ? : property-set : sources * )
    {
        local r =
          [ generator.run $(project) $(name) :
            [
              property-set.create
                <pch-source>$(sources[2]) # mypch.cpp
                [ $(property-set).raw ]
            ] : $(sources)
          ] ;

        return
          [ property-set.create
              <pch-header>$(sources[1]) # mypch.hpp
              <pch-file>$(r[2]) # mypch.pch
          ] $(r) ;
    }
}

Index: msvc.jam
===================================================================
RCS file: /cvsroot/boost/boost/tools/build/v2/tools/msvc.jam,v
retrieving revision 1.64
diff -u -r1.64 msvc.jam
--- msvc.jam 14 Nov 2005 15:15:06 -0000 1.64
+++ msvc.jam 17 Nov 2005 21:38:37 -0000
@@ -18,6 +18,7 @@
 import common ;
 import "class" : new ;
 import rc ;
+import pch ;
 
 if [ MATCH (--debug-configuration) : [ modules.peek : ARGV ] ]
 {
@@ -487,6 +488,9 @@
 generators.override msvc.compile.rc : rc.resource-compile ;
 generators.register-standard msvc.compile.asm : ASM : OBJ : <toolset>msvc ;
 
+generators.register
+ [ new pch-generator msvc.compile.pch : HPP : OBJ PCH : <toolset>msvc ] ;
+
 #
 # Declare flags and action for compilation
 #
@@ -537,15 +541,41 @@
 flags msvc.compile UNDEFS <undef> ;
 flags msvc.compile INCLUDES <include> ;
 
+flags msvc.compile PCH_SOURCE <pch-source> ;
+flags msvc.compile PCH_HEADER <pch>on : <pch-header> ;
+flags msvc.compile PCH_FILE <pch>on : <pch-file> ;
+
+rule compile.c ( targets + : sources * : properties * )
+{
+ DEPENDS $(<) : [ on $(<) return $(PCH_HEADER) ] ;
+ DEPENDS $(<) : [ on $(<) return $(PCH_FILE) ] ;
+}
+
+rule compile.c++ ( targets + : sources * : properties * )
+{
+ DEPENDS $(<) : [ on $(<) return $(PCH_HEADER) ] ;
+ DEPENDS $(<) : [ on $(<) return $(PCH_FILE) ] ;
+}
+
+rule compile.pch ( targets + : sources * : properties * )
+{
+ DEPENDS $(<) : [ on $(<) return $(PCH_SOURCE) ] ;
+}
+
 # The actions differ only by explicit selection of input language
-actions compile.c bind
+actions compile.c bind PCH_HEADER PCH_FILE
+{
+ $(.CC) /Zm800 -nologo -TC -U$(UNDEFS) $(CFLAGS) $(USER_CFLAGS) @"@($(<[1]:W).rsp:E=$(nl)"$(>)" $(nl)-D$(DEFINES) $(nl)"-I$(INCLUDES)")" -c -Fo"$(<[1]:W)" -Yu"$(PCH_HEADER)" -Fp"$(PCH_FILE)"
+}
+
+actions compile.c++ bind PCH_HEADER PCH_FILE
 {
- $(.CC) /Zm800 -nologo -TC -U$(UNDEFS) $(CFLAGS) $(USER_CFLAGS) @"@($(<[1]:W).rsp:E=$(nl)"$(>)" $(nl)-D$(DEFINES) $(nl)"-I$(INCLUDES)")" -c -Fo"$(<[1]:W)"
+ $(.CC) /Zm800 -nologo -TP -U$(UNDEFS) $(CFLAGS) $(C++FLAGS) $(USER_CFLAGS) @"@($(<[1]:W).rsp:E=$(nl)"$(>)" $(nl)-D$(DEFINES) $(nl)"-I$(INCLUDES)")" -c -Fo"$(<[1]:W)" -Yu"$(PCH_HEADER)" -Fp"$(PCH_FILE)"
 }
 
-actions compile.c++ bind
+actions compile.pch bind PCH_SOURCE
 {
- $(.CC) /Zm800 -nologo -TP -U$(UNDEFS) $(CFLAGS) $(C++FLAGS) $(USER_CFLAGS) @"@($(<[1]:W).rsp:E=$(nl)"$(>)" $(nl)-D$(DEFINES) $(nl)"-I$(INCLUDES)")" -c -Fo"$(<[1]:W)"
+ $(.CC) /Zm800 -nologo -TP -U$(UNDEFS) $(CFLAGS) $(C++FLAGS) $(USER_CFLAGS) @"@($(<[1]:W).rsp:E=$(nl)"$(PCH_SOURCE)" $(nl)-D$(DEFINES) $(nl)"-I$(INCLUDES)")" -c -Fo"$(<[1])" /Yc"$(>[1])" -Fp"$(<[2])"
 }
 
 actions compile.rc


Boost-Build list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk