dynconfig: --with-modulesdir should be a Samba option
[metze/samba/wip.git] / dynconfig / wscript
1 #!/usr/bin/env python
2
3 import string, Logs, Utils, Options, sys, Build, os, intltool, optparse, textwrap
4 from samba_utils import EXPAND_VARIABLES, os_path_relpath
5
6 class SambaIndentedHelpFormatter (optparse.IndentedHelpFormatter):
7     """Format help with indented section bodies.
8     """
9
10     def __init__(self,
11                  indent_increment=2,
12                  max_help_position=12,
13                  width=None,
14                  short_first=1):
15         optparse.IndentedHelpFormatter.__init__(
16             self, indent_increment, max_help_position, width, short_first)
17
18     def format_option(self, option):
19         # The help for each option consists of two parts:
20         #   * the opt strings and metavars
21         #     eg. ("-x", or "-fFILENAME, --file=FILENAME")
22         #   * the user-supplied help string
23         #     eg. ("turn on expert mode", "read data from FILENAME")
24         #
25         # If possible, we write both of these on the same line:
26         #   -x      turn on expert mode
27         #
28         # But if the opt string list is too long, we put the help
29         # string on a second line, indented to the same column it would
30         # start in if it fit on the first line.
31         #   -fFILENAME, --file=FILENAME
32         #           read data from FILENAME
33         result = []
34         opts = self.option_strings[option]
35         opt_width = self.help_position - self.current_indent - 2
36         if len(opts) > opt_width:
37             opts = "%*s%s\n" % (self.current_indent, "", opts)
38             indent_first = self.help_position
39         else:                       # start help on same line as opts
40             opts = "%*s%-*s  " % (self.current_indent, "", opt_width, opts)
41             indent_first = 0
42         result.append(opts)
43         if option.help:
44             help_text = self.expand_default(option)
45             if string.find(help_text, '\n') == -1:
46                 help_lines = textwrap.wrap(help_text, self.help_width)
47             else:
48                 help_lines = help_text.splitlines()
49             result.append("%*s%s\n" % (indent_first, "", help_lines[0]))
50             result.extend(["%*s%s\n" % (self.help_position, "", line)
51                            for line in help_lines[1:]])
52         elif opts[-1] != "\n":
53             result.append("\n")
54         return "".join(result)
55
56
57 # list of directory options to offer in configure
58 #
59 # 'STD-PATH'  - the default path without --enable-fhs
60 # 'FHS-PATH'  - the default path with --enable-fhs
61 #
62 # 'OPTION'    - the configure option to overwrite the default (optional)
63 # 'HELPTEXT'  - the help text of the configure option (optional)
64 #
65 # 'OVERWRITE' - The option referrs to itself and was already from
66 #               the basic GNU options from the gnu_dirs tool.
67 #               We may overwrite the related path. (Default: False)
68 #
69 # 'DELAY'     - The option referrs to other options in the dynconfig list.
70 #               We delay the intialization into a later stage. This
71 #               makes sure the recursion works. (Default: False)
72 #
73 dynconfig = {
74     'BINDIR' : {
75          'STD-PATH':  '${BINDIR}',
76          'FHS-PATH':  '${BINDIR}',
77          'OVERWRITE': True,
78     },
79     'SBINDIR' : {
80          'STD-PATH':  '${SBINDIR}',
81          'FHS-PATH':  '${SBINDIR}',
82          'OVERWRITE': True,
83     },
84     'LIBDIR' : {
85          'STD-PATH':  '${LIBDIR}',
86          'FHS-PATH':  '${LIBDIR}',
87          'OVERWRITE': True,
88     },
89     'LIBEXECDIR' : {
90          'STD-PATH':  '${LIBEXECDIR}',
91          'FHS-PATH':  '${LIBEXECDIR}',
92          'OVERWRITE': True,
93     },
94     'DATADIR' : {
95          'STD-PATH':  '${DATADIR}',
96          'FHS-PATH':  '${DATADIR}',
97          'OVERWRITE': True,
98     },
99     'LOCALEDIR' : {
100          'STD-PATH':  '${LOCALEDIR}',
101          'FHS-PATH':  '${LOCALEDIR}',
102          'OVERWRITE': True,
103     },
104     'PYTHONDIR' : {
105          'STD-PATH':  '${PYTHONDIR}',
106          'FHS-PATH':  '${PYTHONDIR}',
107          'OVERWRITE': True,
108     },
109     'PYTHONARCHDIR' : {
110          'STD-PATH':  '${PYTHONARCHDIR}',
111          'FHS-PATH':  '${PYTHONARCHDIR}',
112          'OVERWRITE': True,
113     },
114     'INCLUDEDIR' : {
115          'STD-PATH':  '${INCLUDEDIR}',
116          'FHS-PATH':  '${INCLUDEDIR}/samba-4.0',
117          'OVERWRITE': True,
118     },
119     'SCRIPTSBINDIR' : {
120          'STD-PATH':  '${SBINDIR}',
121          'FHS-PATH':  '${SBINDIR}',
122     },
123     'SETUPDIR' : {
124          'STD-PATH':  '${DATADIR}/setup',
125          'FHS-PATH':  '${DATADIR}/samba/setup',
126     },
127     'PKGCONFIGDIR' : {
128          'STD-PATH':  '${LIBDIR}/pkgconfig',
129          'FHS-PATH':  '${LIBDIR}/pkgconfig',
130     },
131     'SWATDIR' : {
132          'STD-PATH':  '${DATADIR}/swat',
133          'FHS-PATH':  '${DATADIR}/samba/swat',
134     },
135     'CODEPAGEDIR' : {
136          'STD-PATH':  '${DATADIR}/codepages',
137          'FHS-PATH':  '${DATADIR}/samba/codepages',
138     },
139     'MODULESDIR' : {
140          'STD-PATH':  '${LIBDIR}',
141          'FHS-PATH':  '${LIBDIR}/samba',
142          'OPTION':    '--with-modulesdir',
143          'HELPTEXT':  'Which directory to use for Samba modules',
144          'OVERWRITE': True,
145     },
146     'PAMMODULESDIR' : {
147          'STD-PATH':  '${LIBDIR}/security',
148          'FHS-PATH':  '${LIBDIR}/security',
149          'OPTION':    '--with-pammodulesdir',
150          'HELPTEXT':  'Which directory to use for PAM modules',
151     },
152     'CONFIGDIR' : {
153          'STD-PATH':  '${SYSCONFDIR}',
154          'FHS-PATH':  '${SYSCONFDIR}/samba',
155          'OPTION':    '--with-configdir',
156          'HELPTEXT':  'Where to put configuration files',
157     },
158     'PRIVATE_DIR' : {
159          'STD-PATH':  '${PREFIX}/private',
160          'FHS-PATH':  '${LOCALSTATEDIR}/lib/samba/private',
161          'OPTION':    '--with-privatedir',
162          'HELPTEXT':  'Where to put sam.ldb and other private files',
163     },
164     'LOCKDIR' : {
165          'STD-PATH':  '${LOCALSTATEDIR}/lock',
166          'FHS-PATH':  '${LOCALSTATEDIR}/lock/samba',
167          'OPTION':    '--with-lockdir',
168          'HELPTEXT':  'Where to put short term disposable state files',
169     },
170     'PIDDIR' : {
171          'STD-PATH':  '${LOCALSTATEDIR}/run',
172          'FHS-PATH':  '${LOCALSTATEDIR}/run/samba',
173          'OPTION':    '--with-piddir',
174          'HELPTEXT':  'Where to put pid files',
175     },
176     'STATEDIR' : {
177          'STD-PATH':  '${LOCALSTATEDIR}/locks',
178          'FHS-PATH':  '${LOCALSTATEDIR}/lib/samba',
179          'OPTION':    '--with-statedir',
180          'HELPTEXT':  'Where to put persistent state files',
181     },
182     'CACHEDIR' : {
183          'STD-PATH':  '${LOCALSTATEDIR}/cache',
184          'FHS-PATH':  '${LOCALSTATEDIR}/cache/samba',
185          'OPTION':    '--with-cachedir',
186          'HELPTEXT':  'Where to put temporary cache files',
187     },
188     'LOGFILEBASE' : {
189          'STD-PATH':  '${LOCALSTATEDIR}',
190          'FHS-PATH':  '${LOCALSTATEDIR}/log/samba',
191          'OPTION':    '--with-logfilebase',
192          'HELPTEXT':  'Where to put log files',
193     },
194     'SOCKET_DIR' : {
195          'STD-PATH':  '${LOCALSTATEDIR}/run',
196          'FHS-PATH':  '${LOCALSTATEDIR}/run/samba',
197          'OPTION':    '--with-sockets-dir',
198          'HELPTEXT':  'socket directory',
199     },
200     'PRIVILEGED_SOCKET_DIR' : {
201          'STD-PATH':  '${LOCALSTATEDIR}/lib',
202          'FHS-PATH':  '${LOCALSTATEDIR}/lib/samba',
203          'OPTION':    '--with-privileged-socket-dir',
204          'HELPTEXT':  'privileged socket directory',
205     },
206     'WINBINDD_SOCKET_DIR' : {
207          'STD-PATH':  '${SOCKET_DIR}/winbindd',
208          'FHS-PATH':  '${SOCKET_DIR}/winbindd',
209          'DELAY':     True,
210     },
211     'WINBINDD_PRIVILEGED_SOCKET_DIR' : {
212          'STD-PATH':  '${PRIVILEGED_SOCKET_DIR}/winbindd_privileged',
213          'FHS-PATH':  '${PRIVILEGED_SOCKET_DIR}/winbindd_privileged',
214          'DELAY':     True,
215     },
216     'NMBDSOCKETDIR' : {
217          'STD-PATH':  '${SOCKET_DIR}/nmbd',
218          'FHS-PATH':  '${SOCKET_DIR}/nmbd',
219          'DELAY':     True,
220     },
221     'NTP_SIGND_SOCKET_DIR' : {
222          'STD-PATH':  '${SOCKET_DIR}/ntp_signd',
223          'FHS-PATH':  '${SOCKET_DIR}/ntp_signd',
224          'DELAY':     True,
225     },
226     'NCALRPCDIR' : {
227          'STD-PATH':  '${SOCKET_DIR}/ncalrpc',
228          'FHS-PATH':  '${SOCKET_DIR}/ncalrpc',
229          'DELAY':     True,
230     },
231     'CONFIGFILE' : {
232          'STD-PATH':  '${CONFIGDIR}/smb.conf',
233          'FHS-PATH':  '${CONFIGDIR}/smb.conf',
234          'DELAY':     True,
235     },
236     'LMHOSTSFILE' : {
237          'STD-PATH':  '${CONFIGDIR}/lmhosts',
238          'FHS-PATH':  '${CONFIGDIR}/lmhosts',
239          'DELAY':     True,
240     },
241     'SMB_PASSWD_FILE' : {
242          'STD-PATH':  '${PRIVATE_DIR}/smbpasswd',
243          'FHS-PATH':  '${PRIVATE_DIR}/smbpasswd',
244          'DELAY':     True,
245     },
246 }
247
248 def set_options(opt):
249     opt.parser.formatter = SambaIndentedHelpFormatter()
250     opt.parser.formatter.width=Utils.get_term_cols()
251
252     for k in ('--with-modulesdir'):
253         option = opt.parser.get_option(k)
254         if option:
255             opt.parser.remove_option(k)
256
257     # get all the basic GNU options from the gnu_dirs tool
258
259     opt_group=opt.add_option_group('Samba-specific directory layout','')
260
261     fhs_help  = "Use FHS-compliant paths (default no)\n"
262     fhs_help += "You should consider using this together with:\n"
263     fhs_help += "--prefix=/usr --sysconfdir=/etc --locatestatedir=/var"
264     opt_group.add_option('--enable-fhs', help=fhs_help,
265                    action="store_true", dest='ENABLE_FHS', default=False)
266
267     for varname in dynconfig.keys():
268         if 'OPTION' not in dynconfig[varname]:
269             continue
270         opt = dynconfig[varname]['OPTION']
271         if 'HELPTEXT' in dynconfig[varname]:
272             txt = dynconfig[varname]['HELPTEXT']
273         else:
274             txt = "dynconfig path %s" % (varname)
275         def_std = dynconfig[varname]['STD-PATH']
276         def_fhs = dynconfig[varname]['FHS-PATH']
277
278         help = "%s\n[STD-Default: %s]\n[FHS-Default: %s]" % (txt, def_std, def_fhs)
279         opt_group.add_option(opt, help=help, dest=varname, action="store")
280
281 def configure(conf):
282     # get all the basic GNU options from the gnu_dirs tool
283
284     if Options.options.ENABLE_FHS:
285         flavor = 'FHS-PATH'
286     else:
287         flavor = 'STD-PATH'
288         if conf.env.PREFIX == '/usr' or conf.env.PREFIX == '/usr/local':
289            Logs.error("Don't install directly under /usr or /usr/local without using the FHS option (--enable-fhs)")
290            raise Utils.WafError("ERROR: invalid --prefix=%s value" % (conf.env.PREFIX))
291
292     explicit_set ={}
293
294     dyn_vars = {}
295     for varname in dynconfig.keys():
296         dyn_vars[varname] = dynconfig[varname][flavor]
297         if 'OVERWRITE' in dynconfig[varname] and dynconfig[varname]['OVERWRITE']:
298             # we may overwrite this option
299             continue
300         conf.ASSERT(varname not in conf.env, "Variable %s already defined" % varname)
301
302     # the explicit block
303     for varname in dynconfig.keys():
304         if 'OPTION' not in dynconfig[varname]:
305             continue
306         value = getattr(Options.options, varname, None)
307         if value is None:
308            continue
309         conf.ASSERT(value != '', "Empty dynconfig value for %s" % varname)
310         conf.env[varname] = value
311         # mark it as explicit from the command line
312         explicit_set[varname] = value
313
314     # defaults stage 1 after the explicit block
315     for varname in dynconfig.keys():
316         if 'DELAY' in dynconfig[varname] and dynconfig[varname]['DELAY']:
317             # this option referrs to other options,
318             # so it needs to wait for stage 2.
319             continue
320         value = EXPAND_VARIABLES(conf, dyn_vars[varname])
321         conf.ASSERT(value != '', "Empty dynconfig value for %s" % varname)
322         if varname not in explicit_set:
323             # only overwrite if not specified explicitly on the command line
324             conf.env[varname] = value
325
326     # defaults stage 2 after the explicit block
327     for varname in dynconfig.keys():
328         if 'DELAY' not in dynconfig[varname] or not dynconfig[varname]['DELAY']:
329             # this option was already handled in stage 1.
330             continue
331         value = EXPAND_VARIABLES(conf, dyn_vars[varname])
332         conf.ASSERT(value != '', "Empty dynconfig value for %s" % varname)
333         if varname not in explicit_set:
334             # only overwrite if not specified explicitly on the command line
335             conf.env[varname] = value
336
337     # display the expanded pathes for the user
338     for varname in dynconfig.keys():
339         value = conf.env[varname]
340         conf.start_msg("Dynconfig[%s]: " % (varname))
341         conf.end_msg("'%s'" % (value), 'GREEN')
342
343 def dynconfig_cflags(bld, list=None):
344     '''work out the extra CFLAGS for dynconfig.c'''
345     cflags = []
346     # override some paths when running from the build directory
347     override = { 'MODULESDIR'    : 'bin/modules',
348                  'PYTHONDIR'     : 'bin/python',
349                  'PYTHONARCHDIR' : 'bin/python',
350                  'BINDIR'        : 'bin',
351                  'SBINDIR'       : 'bin',
352                  'CODEPAGEDIR'   : os.path.join(bld.env.srcdir, 'codepages'),
353                  'SCRIPTSBINDIR' : os.path.join(bld.env.srcdir, 'source4/scripting/bin'),
354                  'SETUPDIR'      : os.path.join(bld.env.srcdir, 'source4/setup') }
355     for varname in dynconfig.keys():
356         if list and not varname in list:
357             continue
358         value = bld.env[varname]
359         if not Options.is_install:
360             if varname in override:
361                 value = os.path.join(os.getcwd(), override[varname])
362         cflags.append('-D%s="%s"' % (varname, value))
363     return cflags
364 Build.BuildContext.dynconfig_cflags = dynconfig_cflags
365
366 def build(bld):
367     cflags = bld.dynconfig_cflags()
368     version_header = 'version.h'
369     bld.SAMBA_SUBSYSTEM('DYNCONFIG',
370                         'dynconfig.c',
371                         deps='replace talloc',
372                         public_headers=os_path_relpath(os.path.join(Options.launch_dir, version_header), bld.curdir),
373                         header_path='samba',
374                         cflags=cflags)
375
376     # install some extra empty directories
377     bld.INSTALL_DIRS("", "${CONFIGDIR} ${PRIVATE_DIR} ${LOGFILEBASE}");
378     bld.INSTALL_DIRS("", "${PRIVATE_DIR} ${PRIVILEGED_SOCKET_DIR}")
379     bld.INSTALL_DIRS("", "${STATEDIR} ${CACHEDIR}");
380
381     # these might be on non persistent storage
382     bld.INSTALL_DIRS("", "${LOCKDIR} ${PIDDIR} ${SOCKET_DIR}")