[jamming] Compiling the same file with different flags for different targets

Mihai Christodorescu announcements.for.warkdarrior at gmail.com
Wed Dec 10 09:44:27 PST 2008


Thank you for all your help. By the time I saw Craig's reply, I had
already cooked up a solution based on Diane's suggestion. Here's what I
ended up doing:

---[ new rules (in Jamrules) ]---
SUFSHLIB = .so ;
SUFSHOBJ = .slo ;

rule SharedLibrary
{
   SharedLibraryFromObjects $(<) : $(>:S=$(SUFSHOBJ)) ;
   SharedObjects $(>) ;
}

rule SharedLibraryFromObjects
{
   local _s _t ;

   # Add grist to file names
   # Add suffix to exe

   _s = [ FGristFiles $(>) ] ;
   _t = [ FAppendSuffix $(<) : $(SUFSHLIB) ] ;

   # so 'jam libfoo' works when it's really libfoo.so

   if $(_t) != $(<)
   {
      Depends $(<) : $(_t) ;
      NotFile $(<) ;
   }

   # make compiled sources a dependency of target
   Depends shared : $(_t) ;
   Depends $(_t) : $(_s) ;
   MakeLocate $(_t) : $(LOCATE_TARGET) ;

   Clean clean : $(_t) ;

   LINKFLAGS on $(_t) = $(CCFLAGS) -shared ;
   Link $(_t) : $(_s) ;
}

rule SharedObjects
{
   local _i ;

   for _i in [ FGristFiles $(<) ]
   {
      CCFLAGS on $(_i:S=$(SUFSHOBJ)) += -fPIC -fvisibility=hidden ;
      Object $(_i:S=$(SUFSHOBJ)) : $(_i) ;
      Depends obj : $(_i:S=$(SUFSHOBJ)) ;
   }
}

rule SharedObjectCcFlags
{
   CCFLAGS on [ FGristFiles $(<:S=$(SUFSHOBJ)) ] += $(>) ;
}

rule InstallSharedLibrary
{
   local _t = [ FAppendSuffix $(>) : $(SUFSHLIB) ] ;
   InstallInto $(<) : $(_t) ;
   MODE on $(_t:G=$(INSTALLGRIST)) = $(EXEMODE) ;
}
---------------------------------

As you can probably tell, they are pretty much blatant ripoffs of the
corresponding Jambase rules without the word "shared" in them.

With these new rules, I can do the following in my Jamfile (based on the
example of my original query):

---------------------------------
CC = gcc ;
LINK = gcc ;

Main test1 : test1.c lib.c ;

SharedObjectCcFlags lib.c : -DIS_LIB ;
SharedLibrary libtest : test1.c lib.c ;

InstallSharedLibrary /some_place_safe/lib : libtest ;

LINKFLAGS on test2$(SUFEXE) = -Xlinker -rpath
                              -Xlinker /some_place_safe/lib
                              -L /some_place_safe/lib -l test ;
Main test2 : test2.c ;
---------------------------------


Regards,

Mihai



More information about the jamming mailing list