13641364cd3b5b0ee25cd044d3b5c12b5b067eec
[supertux.git] / mk / jam / flags.jam
1 #============================================================================
2 # Rules for specifying compiler and linker flags
3 #============================================================================
4
5 ##  LinkWith target : libs [ : options ]
6 ##    Link a target with libraries. The specified libraries should have
7 ##    build rules in the same project. For external libraries use the
8 ##    ExternalLibs rule. Specify the library names without any extensions or
9 ##    the leading "lib".
10 rule LinkWith
11 {
12     local i libs sharedlib ;
13
14     for i in $(>) {
15         if $($(i)_TYPE) = "library" {
16             if [ IsElem shared : $($(i)_OPTIONS) ] {
17                 libs += $(i) ;
18                 sharedlib = true ;
19             } else {
20                 # for non shared libs add inherit the compile flags and libs
21                 libs += $(i) $($(i)_LIBRARIES) ;
22                 # inherit the exported flags
23                 CppFlags $(<) : $($(i)_CPPFLAGS) : $(3) ;
24                 CFlags $(<) : $($(i)_CFLAGS) : $(3) ;
25                 C++Flags $(<) : $($(i)_CXXFLAGS) : $(3) ;
26                 LFlags $(<) : $($(i)_LIBS) : $(3) ;
27             }
28         } else {
29             echo "WARNING: Trying to link" $(i)
30                 "with" $(<) "which is not a library." ;
31         }
32     }
33     # resolve library dependencies
34     libs = [ RemoveDups $(libs) ] ;    
35     $(<)_LIBRARIES = $(libs) ;
36
37     local libfiles ;
38     for i in $(libs) {
39         libfiles += $($(i)_TARGET) ;
40     }
41
42     DEPENDS $($(<)_TARGET) : $(libfiles) ;
43     NEEDLIBS on $($(<)_TARGET) += $(libfiles) ;
44     # the usual libtool hack...
45     if $(sharedlib) {
46         LINK on $($(<)_TARGET) = "$(LIBTOOL) $(LINK)" ;
47         INSTALL on $($(<)_INSTALLTARGET) = "$(LIBTOOL) --mode=install $(INSTALL)" ;
48     }
49 }
50
51 rule CppFlags
52 {
53     CPPFLAGS on $($(<)_OBJECTS) += $(>) ;
54
55     if [ IsElem export : $(3) ] {
56         $(<)_CPPFLAGS = $(>) ;
57     }
58 }
59
60 ##  CFlags target : flags [ : options ]
61 ##    Sets cflags on all sourcefiles of a target
62 ##    This rule affects c++ and c compiler flags. Options:
63 ##      export - export the cflags to dependent targets (for example in
64 ##              a library context this will inherit the cflags to the apps using
65 ##              the library)
66 rule CFlags
67 {
68     CFLAGS on $($(<)_OBJECTS) += $(>) ;
69
70     if [ IsElem export : $(3) ] {
71         $(<)_CFLAGS = $(>) ;
72     }
73 }
74
75 rule C++Flags
76 {
77     CXXFLAGS on $($(<)_OBJECTS) += $(>) ;
78
79     if [ IsElem export : $(3) ] {
80         $(<)_CXXFLAGS = $(>) ;
81     }
82 }
83
84 ##  LFlags target : flags [ : options ]
85 ##    Sets linker flags for a library, plugin or application target
86 rule LFlags
87 {
88     LIBS on $($(<)_TARGET) += $(>) ;
89
90     if [ IsElem export : $(3) ] {
91         $(<)_LIBS = $(>) ;
92     }
93 }
94
95 ##  ExternalLibs appname : linkerflags [ : options ]
96 ##    Link an application/library/plugin with external libraries. It is
97 ##    possible to give a set of flags which will be passed to the linker when
98 ##    building the application (typically -L and -l flags).
99 rule ExternalLibs
100 {
101     local i ;
102   
103     for i in $(>)
104     {
105         CppFlags $(<) : $($(i)_CFLAGS) : $(3) ;
106         LFlags $(<) : $($(i)_LIBS) : $(3) ;
107     }
108 }
109
110 ##  ExtraObjects target : objectfiles
111 ##    Link additional object files with a target
112 rule ExtraObjects
113 {
114     EXTRAOBJECTS on $($(<)_TARGET) += $(>) ;
115     Depends $($(<)_TARGET) : $(>) ;
116     Clean $(<)clean : $(>) ;
117     Clean clean : $(>) ;
118 }
119
120 ##  IncludeDir [target : ] directories
121 ##    Description: Is used to specify the location of header files for a
122 ##    target or the whole project if no target is given.
123 ##    This rule will automatically generate the -I compiler flags and makes
124 ##    sure the dependency scanner is able to locate your header files. The
125 ##    directories are relative to the current subdir specified with the SubDir
126 ##    rule.
127 ##    Implementation: The directories are simply added to the HDRSEARCH variable
128 ##    which is respected by all jam rules.
129 rule IncludeDir
130 {
131     local dir i ;
132     
133     if $(>) {
134         for i in $(>) {
135             dir = [ ConcatDirs $(SUBDIR) $(i) ] ;
136             CppFlags $(<) : [ CreateIncludeFlags $(dir) ] ;
137
138             # needed for header scanning
139             HDRSEARCH on $($(<)_SOURCES) += $(dir) ;
140         }
141     } else {
142         for i in $(<) {
143             dir = [ ConcatDirs $(SUBDIR) $(i) ] ;
144             CPPFLAGS += [ CreateIncludeFlags $(dir) ] ;
145             # needed for header scanning
146             HDRSEARCH += $(dir) ;
147         }
148     }
149 }
150
151 rule DefineConst
152 {
153     if $(>) {
154         CppFlags $(<) : [ CreateDefineFlags $(>) ] ;
155     } else {
156         CPPFLAGS += [ CreateDefineFlags $(<) ] ;
157     }
158 }
159