Generating CPP's from intermediate files?

Added by David Cowan over 1 year ago

Sorry, another n00b question...

I need to generate C++ wrapper files as an interface between my C++ code and LUA scripts... my existing makefile lists the interface files (.i) which trigger a rule which calls SWIG (www.swig.org) which generates a .CPP file which gets compiled into my executable.

How would I go about writing a new rule to add a .CPP file into the final target, which has been generated from another file? The closest thing I can see in the test folder has to do with multipass compiles - is it something to do with this?

Thanks

Dave


Replies (4)

RE: Generating CPP's from intermediate files? - Added by Joshua Jensen over 1 year ago

Nah, multipass compiles would complicate such a simple thing.

The jamplus/src/Jamfile.jam has an example of this where a mkjambase application is built and then used to output a .c file for the Jam executable build.

I have just added jamplus/tests/generatedc as a simpler example.

Finally, the LuaPlus (http://luaplus.org/) Jamfiles show a number of examples of how to achieve this.

Let me know if any of this needs clarification.

RE: Generating CPP's from intermediate files? - Added by David Cowan over 1 year ago

Thanks Joshua,

I eventually got it working this morning... I'll check what I've done against the example you've checked in shortly (just to make sure I'm not totally out of whack!)

Here's the solution I came up with anway...

actions SWIGAction
{
    $(SWIG_TOOL:C\\) -lua -c++ -Fmicrosoft -I$(BASE:\\)\SWIG\Lib -I$$(BASE:\\)\SWIG\Lib\lua -o $(1:\\:R=$(DRIVE)) $(2:\\:R=$(DRIVE))
}

rule FindSWIG
{
    local swig = [ Glob $(BASE:/)/tools/bin    :    swig.exe ]    ;
    if $(swig) = "" 
    {
        Exit SWIG \: Could not find compiler ;
    }
    SWIG_TOOL = $(swig[1]) ; 
}

rule SWIG    TARGET    :    SOURCE
{
    for SRCFILE in $(SOURCE)
    {
        local GSOURCE = $(SRCFILE:G=$(TARGET)) ;

        local OUT = $(SRCFILE:S=_wrap.cpp) ;
        OUT = $(OUT:G=$(TARGET)) ;

        Depends $(TARGET) : $(OUT) ;
        Depends $(OUT) : $(GSOURCE) ;

        SEARCH on $(GSOURCE) = $(SUBDIR) ;
        MakeLocate $(OUT) : $(SUBDIR) ;

        SWIGAction $(OUT) : $(GSOURCE) ;

        Clean clean : $(OUT) ;

        Depends all : $(TARGET) ;
        NotFile $(TARGET) ;
    }
}

#
# Verify the SWIG tool is available
#
FindSWIG    ;

local    swigFiles            =    source/gameGlobals.i
                                source/gamePlay.i
                                source/levelBuilder.i
                                source/luaStdlib.i
                                source/overWorld.i
                                ;

SWIG                $(proj)        :    $(swigFiles)    ;

C.Application        $(proj)        :
                                $(entityFiles)
                                $(enemyFiles)
                                $(cameraFiles)
                                $(gamePlayFiles)
                                $(gameFlowFiles)
                                $(inputFiles)
                                $(levelFiles)
                                $(playerFiles)
                                $(initFiles)
                                $(saveFiles)
                                $(gameFiles)
                                $(gameMachineFiles)
                                    :    windows    ;

RE: Generating CPP's from intermediate files? - Added by Joshua Jensen over 1 year ago

I have made some minor updates to what you have above.

The first is to enhance portability. Windows works fine with forward slashes nearly everywhere but in the name of the application being called. However, if you quote it, it works fine on Windows AND other host platforms JamPlus supports, too.

JamPlus is also architected internally to understand forward slashes everywhere for better consistency across platforms.

actions SWIGAction
{
    "$(SWIG_TOOL:C)" -lua -c++ -Fmicrosoft -I$(BASE)/SWIG/Lib -I$(BASE)/SWIG/Lib/lua -o $(1:R=$(DRIVE)) $(2:R=$(DRIVE))
}

Note the use of C.TargetGrist below and the Depends all comment:

rule SWIG    TARGET    :    SOURCE
{
    for SRCFILE in $(SOURCE)
    {
        # The gristing format will change when multiple toolchain support comes online.
        local GSOURCE = [ C.TargetGrist $(SRCFILE) ] ;

        local OUT = $(SRCFILE:S=_wrap.cpp) ;
        OUT = [ C.TargetGrist $(OUT) ] ;

        Depends $(TARGET) : $(OUT) ;
        Depends $(OUT) : $(GSOURCE) ;

        SEARCH on $(GSOURCE) = $(SUBDIR) ;
        MakeLocate $(OUT) : $(SUBDIR) ;

        SWIGAction $(OUT) : $(GSOURCE) ;

        Clean clean : $(OUT) ;

        # I don't believe this is necessary or wanted.  Can you explain why you have it?
#        Depends all : $(TARGET) ;
        NotFile $(TARGET) ;
    }
}

RE: Generating CPP's from intermediate files? - Added by David Cowan over 1 year ago

Thanks Joshua,

The "Depends all" was in there purely because its been 3 days of slapping sh*t in there, cutting/pasting from examples and trying out various combinations of things until I got something to work...

Thanks for the pointer towards the C.TargetGrist rule -- I missed that one.

Dave

(1-4/4)