Jump to content
  • Advertisement
Sign in to follow this  
stodge

Using Makefile to generate ToLua bindings - problems

This topic is 4434 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I need to update a Makefile in my little project that uses Lua and ToLua. I want to change the Makefile to automatically invoke ToLua on package files I create (.pkg), which contain C++ classes that will be exported to Lua. I thought I could do something like this: OBJS=file1.o file2.o Then define actions for the stages: 1) Run ToLua on .pkg file for each object file 2) Compile each .cpp that is generated (file1.cpp) by the ToLua tool into an object 3) Link each object file into a shared library The problem is that I can almost do this, except for two things: 1) It runs ToLua twice for the file1.o but it doesn't run for file2.o. 2) It always makes things, even if the .pkg file hasn't changed. Assuming I have something rough like the following, what am I doing wrong? Thanks
 ##############################################################
# Makefile for building the Habitat audio packages on Linux.
##############################################################

# Include the default configuration for the Linux build.
include ../../HabitatConfig.mk

# Include the external configuration for the Linux build.
include ../../HabitatExternalConfig.mk

# Override the suffix for C++ files.
.cpp.o:
        $(G++) $(INCLUDES) $(EXTERNAL_INCS) $(WARN) -c $<
#       $(G++) -MMD $(INCLUDES) $(EXTERNAL_INCS) $(WARN) -c $< >> depend.txt

##############################################################
# Define the source and object files for the audio engine.
##############################################################

SRCS=hbAudioManager_lua.cpp

OBJS=hbAudioManager_lua.o

PKGS=hbAudioManager.pkg

##############################################################

T=$(AUDIO_PACKAGE_LIB)

$(T): $(OBJS)
        @echo Building the Habitat audio packages.
        $(AR) $(INSTALL_LIB)/$@ $(OBJS)
        $(RANLIB) $(INSTALL_LIB)/$@

$(OBJS):$(SRCS)
        $(G++) $(INCLUDES) $(EXTERNAL_INCS) $(WARN) -c $<

$(SRCS):$(PKGS)
        $(TOLUA) -n $(basename $<) -H $(basename $<)_lua.h -o $(basename $<)_lua.cpp $(basename $<).pkg

clean:
        @echo Cleaning the Habitat audio packages.
        rm -fv $(OBJS) $(INSTALL_LIB)/$(T)
        rm -rf *.cpp *.h

##############################################################
# End of makefile.
##############################################################
# DO NOT DELETE 

Share this post


Link to post
Share on other sites
Advertisement
I don't see anything obvious, but I don't see file1.o or file2.o in any of the targets or dependencies.

One thing you can do to simplify the makefile is this: you should have a rule for building a specific file only if you need one. In your case, rather than using a rule to build a specific .cpp file from a .pkg file. You should have a rule that applies to all .cpp targets. Make will figure out that it should use that rule, so you shouldn't tell it explicitly.

For example, I would rewrite your makefile like this (ignoring any problems that I'm not aware of):
    OBJS=hbAudioManager_lua.o

T=$(AUDIO_PACKAGE_LIB)

$(T): $(OBJS)
...

%.o: %.cpp # make will try to use this rule to build .o files (BTW, same as .cpp.o)
...

%_lua.cpp: %.pkg # make will try use this rule to build _lua.cpp files
...

Share this post


Link to post
Share on other sites
Thanks John, the file1 was just for an example. I was going to write a simple example but I thought it would be easier to just post the makefile.

I understand what you're saying, and I've been racking my brains trying to understand how to do that. Maybe with your post I'll be able to solve it.


Thanks!

Share this post


Link to post
Share on other sites
I quickly tried that - it seems to work ok except for one thing; it always makes the files, even if nothing has changed. Any suggestions on how to only regenerate/build the files when one or more have changed?

Thanks again, much appreciated.

Share this post


Link to post
Share on other sites
Ok I got it working using:

T=$(AUDIO_PACKAGE_LIB)

$(T): $(OBJS)
@echo Building the Habitat audio packages.
$(AR) $(INSTALL_LIB)/$@ $(OBJS)
$(RANLIB) $(INSTALL_LIB)/$@

%_lua.o: %_lua.cpp
@echo Compiling $<
$(G++) $(INCLUDES) $(EXTERNAL_INCS) $(WARN) -c $<

%_lua.cpp: %.pkg
@echo Creating packages $<
$(TOLUA) -n $(basename $<) -H $(basename $<)_lua.h -o $(basename $<)_lua.cpp $<



Except it deletes the CPP files before Make exits? Odd...

Thanks

Share this post


Link to post
Share on other sites
Quote:
Original post by stodge
Except it deletes the CPP files before Make exits? Odd...

Intermediate files (such as files produced by implicit rules) are deleted when they are no longer needed. You can use the special target .SECONDARY to prevent this. Something like this I think:
    .SECONDARY: %_lua.o %_lua.cpp 

Share this post


Link to post
Share on other sites
Ah makes some sense. I'll dig through the Make manual and read about that.

Thanks, appreciate it!

Share this post


Link to post
Share on other sites
target `.SECONDARY' doesn't match the target pattern

at this line:

.SECONDARY: %_lua.cpp: %.pkg
@echo Creating packages $<
$(TOLUA) -n $(basename $<) -H $(basename $<)_lua.h -o $(basename $<)_lua.cpp $<

Still reading the manual...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!