f968a74cfe167c020bede3cf5402031fda579ac7
[supertux.git] / mk / jam / library.jam
1 #============================================================================
2 # Rules for library creation
3 #============================================================================
4
5 ##  Library libname : sources [ : options ]
6 ##    Build a library out of sourcefiles. All sourcefiles will be passed
7 ##    to the Objects rule which tries to compile them into object-files. You
8 ##    can create rules for your own filetypes with the UserObject rule. Header
9 ##    files will just be ignored. They are only used for MSVC projectfile
10 ##    generation.  If additional objects are given (fourth argument), they
11 ##    should be the output of CompileObjects, and will be included in the
12 ##    library.
13 ##    Available options are 'shared' if you want to build a shared library on
14 ##    platforms which support that. You can specify the 'noinstall' option if
15 ##    you don't want an install target is generated.
16 ##    Don't specify any extensions for the library name, also leave out the
17 ##    leading "lib".
18 rule Library
19 {
20     # check options
21     CheckOptions noinstall independent shared : $(3) : $(<) ;
22
23     local no_scan_archive = $(NOARSCAN) ;
24     local target = [ ConstructLibraryTarget $(<) : $(3) ] ;
25     local sources = [ SearchSource $(>) ] ;
26     local objects = [ CompileObjects $(sources) : $(3) ] ;
27     local install_targets ;
28
29     $(<)_TYPE = library ;
30     $(<)_OBJECTS = $(objects) ;
31     $(<)_SOURCES = $(sources) ;
32     $(<)_TARGET = $(target) ;
33     $(<)_OPTIONS = $(3) ;
34
35     # create target clean rule
36     Always $(<)clean ;
37     NotFile $(<)clean ;
38     Clean $(<)clean : $(objects) ; # create target clean rule 
39
40     # so 'jam foo' works when it's really foo.exe (Windows) or foo.app (MacOS/X)
41     if $(target) != $(<)
42     {
43         Depends $(<) : $(target) ;
44         NotFile $(<) ;                                                              }
45
46     # library depends on its member objects
47     if ! [ IsElem independent : $(3) ]
48     {
49         if $(KEEPOBJS)
50         {
51             Depends obj : $(objects) ;
52         }
53         else
54         {
55             Depends libs : $(<) ;
56         }
57     }
58
59     # Generate install rules
60     if ! [ IsElem noinstall : $(3) ]
61     {
62         install_targets = [ DoInstall $(target) : $(libdir) : $(INSTALL) : nopackage ] ;
63         Depends install_lib : $(install_targets) ;
64     }
65
66     if [ IsElem shared : $(3) ]
67     {
68         if ! $(LIBTOOL) {
69             exit "LIBTOOL not defined, can't create dynamic library." ;
70         }
71         no_scan_archive = 1 ;
72         DoLibToolClean ;
73
74         if $(install_targets) {
75             INSTALL on $(install_targets) = "$(LIBTOOL) --mode=install $(INSTALL)" ;
76             InvokeLibtoolFinish $(install_targets) ;
77         }
78     }
79
80     # Set LOCATE for the library and its contents.  The bound
81     # value shows up as $(NEEDLIBS) on the Link actions.
82     # For compatibility, we only do this if the library doesn't
83     # already have a path.
84     if ! $(target:D)
85     {
86         MakeLocate $(target) $(target)($(objects:BS)) : $(LOCATE_TARGET) ;
87     }
88
89     if ! $(no_scan_archive)
90     {
91         # If we can scan the library, we make the library depend
92         # on its members and each member depend on the on-disk
93         # object file.
94         Depends $(target) : $(target)($(objects:BS)) ;
95
96         local i ;
97         for i in $(objects)
98         {
99             Depends $(target)($(i:BS)) : $(i) ;
100         }
101     } else {
102         Depends $(target) : $(objects) ;
103     }
104
105     if $(CRELIB) { CreLib $(target) : $(objects[1]) ; }
106
107     SystemLinkLibrary $(<) : $(objects) : $(3) ;
108
109     # If we can't scan the library, we have to leave the .o's around.
110     if ! ( $(no_scan_archive) || $(NOARUPDATE) )
111     {
112         RmTemps $(target) : $(objects) ;
113     }
114
115     # Import default flags
116     CppFlags $(<) : $(LIBRARY_CPPFLAGS) ;
117     CFlags $(<) : $(LIBRARY_CFLAGS) ;
118     C++Flags $(<) : $(LIBRARY_CXXFLAGS) ;
119     LFlags $(<) : $(LIBRARY_LIBS) ;
120
121     # Sources are part of the package
122     if ! [ IsElem nopackage : $(3) ]
123     {
124       Package $(sources) ;
125     }
126
127     return $(target) ;
128 }
129
130 ##  LibraryVersion
131 ##    Specify the version of a library. The version should have the form
132 ##    major:minor:patchlevel
133 rule LibraryVersion
134 {
135     LIBS on $($(<)_TARGET) = -version-info $(>) ;
136 }
137
138 #----------------------------------------------------------------------------
139 # private part
140
141 # default implementation of SystemLinkLibrary
142 rule SystemLinkLibrary
143 {
144     local target = $($(<)_TARGET) ;
145    
146     if [ IsElem shared : $(3) ] {
147         LINK on $(target) = "$(LIBTOOL) --mode=link $(LINK) -rpath $(libdir)" ;
148         LinkLibrary $(target) : $(>) ;
149     } else {
150         Archive $(target) : $(>) ;
151         if $(RANLIB) { Ranlib $(target) ; }
152     }
153                                                                                 
154     Clean clean : $(target) ;
155     Clean $(<)clean : $(target) ;
156 }
157
158 actions together Ranlib
159 {
160     $(RANLIB) $(<)
161 }
162
163 actions LinkLibrary bind NEEDLIBS bind EXTRAOBJECTS
164 {
165     $(LINK) -o $(<) $(>) $(EXTRAOBJECTS) $(NEEDLIBS) $(LIBS)
166 }
167
168 # Construct pseudo target libs which is used instead of the pseudo target lib
169 # in Jambase
170 Depends lib : libs ;
171 NotFile libs ;
172