Boost logo

Boost-Build :

Subject: Re: [Boost-build] How to use generators to CPP and LIB
From: Alexander Sack (pisymbol_at_[hidden])
Date: 2009-01-30 08:50:54


On Fri, Jan 30, 2009 at 3:59 AM, Duncan Rance <gmane_at_[hidden]> wrote:
> Hello again,
>
> Perhaps I'm missing something obvious or what I want to do isn't possible?
>
> Have I been clear in my description? The essential problem I'm trying
> to solve is to use one source-type to produce two different
> target-types. Where one target-type is H and is compiled into my
> target exe, and the other is a LIB containing different code that is
> ultimately compiled into the same exe.
>
> Is this even possible? Is this documented somewhere?

Duncan, are you trying to do to create a header file that is used to
build the lib or does the input file actually create a lib and an h?

Let me share some code that I used as practice (with a little help
from this list) to do this (using Google Protobuf's):

PROTO -> CPP
PROTO -> H

I used the following generator, proto.jam:

import type ;
import generators ;
import feature ;
import common ;
import "class" : new ;

gPROTOC_CMD = [ common.get-invocation-command protoc : protoc ] ;

type.register PROTO : proto ;

class proto-generator : generator
{
    import "class" : new ;

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

    rule run ( project name ? : property-set : sources * )
    {
        if ! $(sources[2])
        {
            # Accept only single source.
            local t = [ $(sources[1]).type ] ;
            if $(t) = PROTO
            {
                # If no output name is specified, guess it from sources.
                if ! $(name)
                {
                    name = [ generator.determine-output-name $(sources) ] ;
                }

                local a = [ new action $(sources[1]) : proto.protoc :
$(property-set) ] ;
                local t = [ new file-target $(name).pb.cc exact : CPP : $(project) : $(a) ] ;
                local h = [ new file-target $(name).pb.h exact : H : $(project) : $(a) ] ;

                return [ virtual-target.register $(t) ] [
virtual-target.register $(h) ] ;
            }
        }
    }
}

generators.register [ new proto-generator proto.proto : PROTO : CPP H ] ;

actions protoc
{
    $(gPROTOC_CMD) --proto_path=$(>[1]:D) --cpp_out=$(<[1]:D) $(>)
}

Alright, this will take a .proto file run protoc on it which generates
both a C++ and header files. I can then use these files in a target
like this:

lib mylib : [ glob *.proto ] : <include>. ;

This will run protoc on the .proto file and generate the C++ and
header files, compile, link, etc.

I can also use the header from this build by doing when building a
binary (this might be what you are trying to do with the lib):

exe myexe : [ glob *.cc ] : <include>. <implicit-dependency>/mproject//mylib ;

This will include the header file I generated from above in the linking process.

Does any of this help? :D

-aps


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