r10510: Decrease the amount of data included by includes.h a bit
[amitay/samba.git] / source4 / SConstruct
1 #!/usr/bin/env python
2
3 # This is the experimental scons build script for Samba 4. For a proper 
4 # build use the old build system (configure + make). scons may
5 # eventually replace this system.
6 #
7 # Copyright (C) 2005 Jelmer Vernooij <jelmer@samba.org>
8 # Published under the GNU GPL
9 #
10 # TODO:
11 # - finish fallback code
12 # - support for init functions
13 # - separate config file for lib/replace/
14 # - Subsystem() ?
15
16 import cPickle, string, os
17
18 opts = Options(None, ARGUMENTS)
19 opts.AddOptions(
20                 BoolOption('developer','enable developer flags', False),
21                 PathOption('prefix','installation prefix','/usr/local/samba'),
22                 BoolOption('configure','run configure checks', False),
23 )
24
25 hostenv = Environment(
26                 toolpath=['build/scons','.'],
27                 tools=['default','pidl','proto','et','asn1'],
28                 options=opts,
29                 CPPPATH=['#include','#','#lib'], 
30         CPPDEFINES={'_SAMBA_BUILD_': None},
31                 )
32
33 hostenv.Help(opts.GenerateHelpText(hostenv))
34
35 # We don't care about NFS builds...
36 hostenv.SetOption('max_drift', 1)
37
38 if hostenv['developer']:
39         hostenv.Append(CCFLAGS='-Wall')
40         hostenv.Append(CCFLAGS='-Wshadow')
41         hostenv.Append(CCFLAGS='-Werror-implicit-function-declaration')
42         hostenv.Append(CCFLAGS='-Wstrict-prototypes')
43         hostenv.Append(CCFLAGS='-Wpointer-arith')
44         hostenv.Append(CCFLAGS='-Wcast-qual')
45         hostenv.Append(CCFLAGS='-Wcast-align')
46         hostenv.Append(CCFLAGS='-Wwrite-strings')
47         hostenv.Append(CCFLAGS='-Wmissing-format-attribute')
48         hostenv.Append(CCFLAGS='-Wformat=2')
49         hostenv.Append(CCFLAGS='-Wno-format-y2k')
50         hostenv.Append(CCFLAGS='-Wno-declaration-after-statement')
51
52 # Some tools get confused if $HOME isn't defined
53 hostenv.Append(ENV={'HOME': os.environ['HOME']})
54
55 # Store configuration data in a dictionary.
56
57 def saveconfig(data):
58         """Save configuration dict to a file."""
59         cached = cPickle.dump(data, open('sconf.cache', 'w'))
60
61 def loadconfig():
62         """Load configuration dict from a file."""
63         try:
64                 return cPickle.load(open('sconf.cache', 'r'))
65         except IOError:
66                 return None
67
68 defines = loadconfig()
69
70 if defines == None:
71         hostenv['configure'] = 1
72
73 if hostenv['configure']:
74         defines = {}
75
76 Export('defines')
77
78 hostenv.Append(CPPPATH = ['#heimdal_build', '#heimdal/lib/krb5',
79                           '#heimdal/lib/hdb', '#heimdal/lib/gssapi',
80                           '#heimdal/lib/asn1', '#heimdal/lib/des',
81                           '#heimdal/kdc', '#heimdal/lib/roken',
82                           '#heimdal/lib/com_err'])
83
84 Export('hostenv')
85
86 buildenv = hostenv
87
88 Export('buildenv')
89
90 cross_compiling = 0
91
92 if cross_compiling:
93         buildenv = hostenv.Copy()
94         buildenv.BuildDir('build-env','.')
95
96 dynenv = hostenv.Copy()
97
98 paths = { 
99         'BINDIR': 'bin',
100         'SBINDIR': 'sbin',
101         'CONFIGFILE': 'cfg',
102         'LOGFILEBASE': 'lfb',
103         'NCALRPCDIR': 'ncalrpc',
104         'LMHOSTSFILE': 'lmhosts',
105         'LIBDIR': 'libdir',
106         'SHLIBEXT': 'ext',
107         'LOCKDIR': 'lockdir',
108         'PIDDIR': 'piddir',
109         'PRIVATE_DIR': 'private',
110         'SWATDIR': 'swat'
111 }
112
113 Export('paths')
114
115 if hostenv['configure']:
116         
117         conf = hostenv.Configure()
118
119         for h in ['sys/select.h','fcntl.h','sys/fcntl.h'] + \
120                 ['utime.h','grp.h','sys/id.h','limits.h','memory.h'] + \
121                 ['compat.h','math.h','sys/param.h','ctype.h','sys/wait.h'] + \
122                 ['sys/resource.h','sys/ioctl.h','sys/ipc.h','sys/mode.h'] + \
123                 ['sys/mman.h','sys/filio.h','sys/priv.h','sys/shm.h','string.h'] + \
124                 ['strings.h','stdlib.h','sys/vfs.h','sys/fs/s5param.h','sys/filsys.h'] + \
125                 ['termios.h','termio.h','fnmatch.h','pwd.h','sys/termio.h'] + \
126                 ['sys/time.h','sys/statfs.h','sys/statvfs.h','stdarg.h'] + \
127                 ['sys/syslog.h','syslog.h','stdint.h','inttypes.h','locale.h'] + \
128                 ['shadow.h','nss.h','nss_common.h','ns_api.h','sys/security.h'] + \
129                 ['security/pam_appl.h','sys/capability.h'] + \
130                 ['sys/acl.h','stdbool.h', 'netinet/in.h', 'sys/socket.h', 'arpa/inet.h', 'netdb.h']:
131                 if conf.CheckCHeader(h):
132                         defines['HAVE_' + h.upper().replace('.','_').replace('/','_')] = 1
133
134         for f in ['setsid','pipe','crypt16','getauthuid','strftime','sigprocmask',
135                 'sigblock','sigaction','initgroups','setgroups','sysconf', 'getpwanam',
136                 'setlinebuf','srandom','random','srand','rand','usleep','timegm',
137                 'backtrace','setbuffer']:
138                 if conf.CheckFunc(f):
139                         defines['HAVE_' + f.upper()] = 1
140
141         # Pull in GNU extensions
142         defines['_GNU_SOURCE'] = 1
143
144         # Hardcode signal return type for now
145         defines['RETSIGTYPE'] = 'void'
146         
147         if conf.CheckType('comparison_fn_t', '#define _GNU_SOURCE\n#include <stdlib.h>'):
148                 defines['HAVE_COMPARISON_FN_T'] = 1
149
150         if conf.CheckType('sig_atomic_t', '#include <signal.h>'):
151                 defines['HAVE_SIG_ATOMIC_T_TYPE'] = 1
152
153         if conf.TryCompile("""
154 #include <sys/types.h>
155
156 int main() 
157 {
158         volatile int i = 0;
159         return 0;
160 }""", '.c'):
161                 defines['HAVE_VOLATILE'] = 1
162
163         if conf.TryCompile("""
164 #include <stdio.h>
165
166 int main() 
167 {
168    typedef struct {unsigned x;} FOOBAR;
169    #define X_FOOBAR(x) ((FOOBAR) { x })
170    #define FOO_ONE X_FOOBAR(1)
171    FOOBAR f = FOO_ONE;   
172    static struct {
173         FOOBAR y; 
174         } f2[] = {
175                 {FOO_ONE}
176         };   
177         return 0;
178 }""", '.c'):
179                 defines['HAVE_IMMEDIATE_STRUCTURES'] = 1
180
181         hostenv.AlwaysBuild('include/config.h')
182
183         if conf.TryCompile("""
184 #include <sys/types.h>
185 #include <sys/time.h>
186 #include <time.h>
187
188 int
189 main ()
190 {
191 if ((struct tm *) 0)
192 return 0;
193   return 0;
194 }
195 """, '.c'):
196                 defines['TIME_WITH_SYS_TIME'] = 1
197
198         if conf.TryCompile("""
199 #include <sys/time.h>
200 #include <unistd.h>
201 main() { struct timeval tv; exit(gettimeofday(&tv, NULL));}
202 """, '.c'):
203                 defines['HAVE_GETTIMEOFDAY_TZ'] = 1
204
205         # Check for header that defines "DIR"
206         for h in ['dirent.h','sys/ndir.h','sys/dir.h','ndir.h']:
207                 if conf.TryCompile("""
208 #include <%s>
209
210 int main() { DIR *x; return 0; }""" % h, '.c'):
211                         defines['HAVE_' + h.upper().replace('.','_').replace('/','_')] = 1
212                         break
213
214         conf.Finish()
215
216 [dynenv.Append(CPPDEFINES = {p: '\\"%s\\"' % paths[p]}) for p in paths]
217         
218 dynconfig = dynenv.Object('dynconfig.c')
219 Export('dynconfig')
220
221 hostenv.proto_headers = []
222
223 SConscript(
224                 dirs=['dsdb', 'libcli', 'lib','torture','rpc_server','cldap_server',
225                 'nbt_server','client','ldap_server','libnet','nsswitch','web_server',
226                 'smbd','dsdb','heimdal_build','ntptr','kdc','smb_server','ntvfs',
227                 'winbind','scripting','auth', 'librpc','script/tests'])
228
229 # proto.h
230
231 def create_global_proto(env, target, source):
232         fd = open(str(target[0]), 'w')
233         [fd.write('#include "%s"\n' % x) for x in source]
234         fd.close()
235
236 def create_global_proto_print(*args, **kwargs):
237         print 'Building global proto.h'
238
239 hostenv.Command('include/proto.h', hostenv.proto_headers,
240         Action(create_global_proto, create_global_proto_print))
241
242 # Save configuration
243
244 if hostenv['configure']:
245         saveconfig(defines)
246
247 # How to create config.h file
248 def create_config_h(env, target, source):
249         fd = open(str(target[0]), 'w')
250         [fd.write('#define %s %s\n' % (x, defines[x])) for x in defines]
251         fd.close()
252
253 def create_config_h_print(*args, **kwargs):
254         print 'Building config.h'
255
256 hostenv.Command('include/config.h', [],
257                 Action(create_config_h, create_config_h_print))
258 hostenv.Append(CPPDEFINES = {'HAVE_CONFIG_H': 1})