00098dec05ba68d65cccd315a17160e044e3399c
[samba.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                     '--dcom-proxy'        : '%s_p.c',
33                     '--com-header'        : 'com_%s.h'
34                     }
35
36     table_header_idx = None
37     out_files = []
38     options_list = TO_LIST(options)
39
40     for o in options_list:
41         if o in options_map:
42             ofiles = TO_LIST(options_map[o])
43             for f in ofiles:
44                 out_files.append(os.path.join(output_dir, f % bname))
45                 if f == 'ndr_%s.h':
46                     # remember this one for the tables generation
47                     table_header_idx = len(out_files) - 1
48
49     # depend on the full pidl sources
50     source = TO_LIST(source)
51     try:
52         pidl_src_nodes = bld.pidl_files_cache
53     except AttributeError:
54         bld.pidl_files_cache = bld.srcnode.ant_glob('pidl/lib/Parse/**/*.pm', flat=False)
55         bld.pidl_files_cache.extend(bld.srcnode.ant_glob('pidl', flat=False))
56         pidl_src_nodes = bld.pidl_files_cache
57
58     # the cd .. is needed because pidl currently is sensitive to the directory it is run in
59     t = bld(rule='cd .. && ${PERL} ${PIDL} --quiet ${OPTIONS} --outputdir ${OUTPUTDIR} -- ${SRC[0].abspath(env)}',
60             ext_out    = '.c',
61             before     = 'cc',
62             on_results = True,
63             shell      = True,
64             source     = source,
65             target     = out_files,
66             name       = name,
67             samba_type = 'PIDL')
68
69     # prime the list of nodes we are dependent on with the cached pidl sources
70     t.allnodes = pidl_src_nodes
71
72     t.env.PIDL = os.path.join(bld.srcnode.abspath(), 'pidl/pidl')
73     t.env.OPTIONS = TO_LIST(options)
74
75     # this rather convoluted set of path calculations is to cope with the possibility
76     # that gen_ndr is a symlink into the source tree. By doing this for the source3
77     # gen_ndr directory we end up generating identical output in gen_ndr for the old
78     # build system and the new one. That makes keeping things in sync much easier.
79     # eventually we should drop the gen_ndr files in git, but in the meanwhile this works
80     outdir = bld.path.find_dir(output_dir).abspath(t.env)
81
82     if symlink and not os.path.lexists(outdir):
83         link_source = os.path.normpath(os.path.join(bld.curdir,output_dir))
84         os.symlink(link_source, outdir)
85
86     real_outputdir = os.path.realpath(outdir)
87     t.env.OUTPUTDIR = os_path_relpath(real_outputdir, os.path.dirname(bld.env.BUILD_DIRECTORY))
88
89     if table_header_idx is not None:
90         pidl_headers = LOCAL_CACHE(bld, 'PIDL_HEADERS')
91         pidl_headers[name] = [bld.path.find_or_declare(out_files[table_header_idx])]
92
93     t.more_includes = '#' + bld.path.relpath_gen(bld.srcnode)
94 Build.BuildContext.SAMBA_PIDL = SAMBA_PIDL
95
96
97 def SAMBA_PIDL_LIST(bld, name, source,
98                     options='',
99                     output_dir='.',
100                     symlink=False):
101     '''A wrapper for building a set of IDL files'''
102     for p in TO_LIST(source):
103         bld.SAMBA_PIDL(name, p, options=options, output_dir=output_dir, symlink=symlink)
104 Build.BuildContext.SAMBA_PIDL_LIST = SAMBA_PIDL_LIST
105
106
107 #################################################################
108 # the rule for generating the NDR tables
109 from TaskGen import feature, before
110 @feature('collect')
111 @before('exec_rule')
112 def collect(self):
113     pidl_headers = LOCAL_CACHE(self.bld, 'PIDL_HEADERS')
114     for (name, hd) in pidl_headers.items():
115         y = self.bld.name_to_obj(name, self.env)
116         self.bld.ASSERT(y is not None, 'Failed to find PIDL header %s' % name)
117         y.post()
118         for node in hd:
119             self.source += " " + node.relpath_gen(self.path)
120
121
122 def SAMBA_PIDL_TABLES(bld, name, target):
123     '''generate the pidl NDR tables file'''
124     headers = bld.env.PIDL_HEADERS
125     bld.SET_BUILD_GROUP('main')
126     t = bld(
127             features = 'collect',
128             rule     = '${SRC} --output ${TGT} | sed "s|default/||" > ${TGT}',
129             ext_out  = '.c',
130             before   = 'cc',
131             on_results = True,
132             shell    = True,
133             source   = '../../librpc/tables.pl',
134             target   = target,
135             name     = name)
136     t.env.LIBRPC = os.path.join(bld.srcnode.abspath(), 'librpc')
137 Build.BuildContext.SAMBA_PIDL_TABLES = SAMBA_PIDL_TABLES
138