build: cope with the common gen_ndr files being in the git tree
[obnox/samba/samba-obnox.git] / buildtools / wafsamba / samba_pidl.py
1 # waf build tool for building IDL files with pidl
2
3 from TaskGen import before
4 import Build, os
5 from samba_utils import *
6
7 def SAMBA_PIDL(bld, pname, source,
8                options='',
9                output_dir='.',
10                symlink=False):
11     '''Build a IDL file using pidl.
12        This will produce up to 13 output files depending on the options used'''
13
14     bname = source[0:-4]; # strip off the .idl suffix
15     name = "%s_%s" % (pname, bname.upper())
16
17     if not SET_TARGET_TYPE(bld, name, 'PIDL'):
18         return
19
20     bld.SET_BUILD_GROUP('build_source')
21
22     # the output files depend on the options used. Use this dictionary
23     # to map between the options and the resulting file names
24     options_map = { '--header'            : '%s.h',
25                     '--ndr-parser'        : 'ndr_%s.c ndr_%s.h',
26                     '--samba3-ndr-server' : 'srv_%s.c srv_%s.h',
27                     '--samba3-ndr-client' : 'cli_%s.c cli_%s.h',
28                     '--server'            : 'ndr_%s_s.c',
29                     '--client'            : 'ndr_%s_c.c ndr_%s_c.h',
30                     '--python'            : 'py_%s.c',
31                     '--tdr-parser'        : 'tdr_%s.c tdr_%s.h',
32                     }
33
34     table_header_idx = None
35     out_files = []
36     options_list = TO_LIST(options)
37
38     for o in options_list:
39         if o in options_map:
40             ofiles = TO_LIST(options_map[o])
41             for f in ofiles:
42                 out_files.append(os.path.join(output_dir, f % bname))
43                 if f == 'ndr_%s.h':
44                     # remember this one for the tables generation
45                     table_header_idx = len(out_files) - 1
46
47     # depend on the full pidl sources
48     source = TO_LIST(source)
49     pidl_src = [x.relpath_gen(bld.path) for x in
50                 bld.srcnode.ant_glob('pidl/**/*', flat=False)]
51     source.extend(pidl_src)
52
53     # the cd .. is needed because pidl currently is sensitive to the directory it is run in
54     t = bld(rule='cd .. && ${PIDL} ${OPTIONS} --outputdir ${OUTPUTDIR} -- ${SRC[0].abspath(env)}',
55             ext_out = '.c',
56             before  = 'cc',
57             shell   = True,
58             source  = source,
59             target  = out_files,
60             name    = name)
61
62     t.env.PIDL = "../pidl/pidl"
63     t.env.OPTIONS = TO_LIST(options)
64
65     # this rather convoluted set of path calculations is to cope with the possibility
66     # that gen_ndr is a symlink into the source tree. By doing this for the source3
67     # gen_ndr directory we end up generating identical output in gen_ndr for the old
68     # build system and the new one. That makes keeping things in sync much easier.
69     # eventually we should drop the gen_ndr files in git, but in the meanwhile this works
70     outdir = bld.bldnode.name + '/' + bld.path.find_dir(output_dir).bldpath(t.env)
71
72     if not os.path.lexists(outdir):
73         link_source = os.path.normpath(os.path.join(bld.curdir,output_dir))
74         link_source = os_path_relpath(link_source, os.path.dirname(outdir))
75         os.symlink(link_source, outdir)
76
77     real_outputdir = os.path.realpath(outdir)
78     t.env.OUTPUTDIR = os_path_relpath(real_outputdir, bld.bldnode.name + '/..')
79
80     if table_header_idx is not None:
81         pidl_headers = LOCAL_CACHE(bld, 'PIDL_HEADERS')
82         pidl_headers[name] = [bld.path.find_or_declare(out_files[table_header_idx])]
83
84     t.more_includes = '#' + bld.path.relpath_gen(bld.srcnode)
85 Build.BuildContext.SAMBA_PIDL = SAMBA_PIDL
86
87
88 def SAMBA_PIDL_LIST(bld, name, source,
89                     options='',
90                     output_dir='.',
91                     symlink=False):
92     '''A wrapper for building a set of IDL files'''
93     for p in TO_LIST(source):
94         bld.SAMBA_PIDL(name, p, options=options, output_dir=output_dir, symlink=symlink)
95 Build.BuildContext.SAMBA_PIDL_LIST = SAMBA_PIDL_LIST
96
97
98 #################################################################
99 # the rule for generating the NDR tables
100 from TaskGen import feature, before
101 @feature('collect')
102 @before('exec_rule')
103 def collect(self):
104     pidl_headers = LOCAL_CACHE(self.bld, 'PIDL_HEADERS')
105     for (name, hd) in pidl_headers.items():
106         y = self.bld.name_to_obj(name, self.env)
107         self.bld.ASSERT(y is not None, 'Failed to find PIDL header %s' % name)
108         y.post()
109         for node in hd:
110             self.source += " " + node.relpath_gen(self.path)
111
112
113 def SAMBA_PIDL_TABLES(bld, name, target):
114     headers = bld.env.PIDL_HEADERS
115     t = bld(
116             features = 'collect',
117             rule     = '${SRC} --output ${TGT} | sed "s|default/||" > ${TGT}',
118             ext_out  = '.c',
119             before   = 'cc',
120             shell    = True,
121             source   = '../../librpc/tables.pl',
122             target   = target,
123             name     = name)
124 Build.BuildContext.SAMBA_PIDL_TABLES = SAMBA_PIDL_TABLES
125