Transition to waf 1.8: replaced on_results by update_outputs
[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, sys, Logs
5 from samba_utils import *
6
7 def SAMBA_PIDL(bld, pname, source,
8                options='',
9                output_dir='.',
10                generate_tables=True):
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     bname = os.path.basename(bname)
16     name = "%s_%s" % (pname, bname.upper())
17
18     if not SET_TARGET_TYPE(bld, name, 'PIDL'):
19         return
20
21     bld.SET_BUILD_GROUP('build_source')
22
23     # the output files depend on the options used. Use this dictionary
24     # to map between the options and the resulting file names
25     options_map = { '--header'            : '%s.h',
26                     '--ndr-parser'        : 'ndr_%s.c ndr_%s.h',
27                     '--samba3-ndr-server' : 'srv_%s.c srv_%s.h',
28                     '--samba3-ndr-client' : 'cli_%s.c cli_%s.h',
29                     '--server'            : 'ndr_%s_s.c',
30                     '--client'            : 'ndr_%s_c.c ndr_%s_c.h',
31                     '--python'            : 'py_%s.c',
32                     '--tdr-parser'        : 'tdr_%s.c tdr_%s.h',
33                     '--dcom-proxy'        : '%s_p.c',
34                     '--com-header'        : 'com_%s.h'
35                     }
36
37     table_header_idx = None
38     out_files = []
39     options_list = TO_LIST(options)
40
41     for o in options_list:
42         if o in options_map:
43             ofiles = TO_LIST(options_map[o])
44             for f in ofiles:
45                 out_files.append(os.path.join(output_dir, f % bname))
46                 if f == 'ndr_%s.h':
47                     # remember this one for the tables generation
48                     table_header_idx = len(out_files) - 1
49
50     # depend on the full pidl sources
51     source = TO_LIST(source)
52     try:
53         pidl_src_nodes = bld.pidl_files_cache
54     except AttributeError:
55         bld.pidl_files_cache = bld.srcnode.ant_glob('pidl/lib/Parse/**/*.pm', flat=False)
56         bld.pidl_files_cache.extend(bld.srcnode.ant_glob('pidl', flat=False))
57         pidl_src_nodes = bld.pidl_files_cache
58
59     # the cd .. is needed because pidl currently is sensitive to the directory it is run in
60     cpp = ""
61     cc = ""
62     if bld.CONFIG_SET("CPP") and bld.CONFIG_GET("CPP") != "":
63         if isinstance(bld.CONFIG_GET("CPP"), list):
64             cpp = 'CPP="%s"' % " ".join(bld.CONFIG_GET("CPP"))
65         else:
66             cpp = 'CPP="%s"' % bld.CONFIG_GET("CPP")
67
68     if cpp == "CPP=xlc_r":
69         cpp = ""
70
71
72     if bld.CONFIG_SET("CC"):
73         if isinstance(bld.CONFIG_GET("CC"), list):
74             cc = 'CC="%s"' % " ".join(bld.CONFIG_GET("CC"))
75         else:
76             cc = 'CC="%s"' % bld.CONFIG_GET("CC")
77
78     t = bld(rule='cd .. && %s %s ${PERL} "${PIDL}" --quiet ${OPTIONS} --outputdir ${OUTPUTDIR} -- "${SRC[0].abspath(env)}"' % (cpp, cc),
79             ext_out    = '.c',
80             before     = 'cc',
81             update_outputs = True,
82             shell      = True,
83             source     = source,
84             target     = out_files,
85             name       = name,
86             samba_type = 'PIDL')
87
88     # prime the list of nodes we are dependent on with the cached pidl sources
89     t.allnodes = pidl_src_nodes
90
91     t.env.PIDL = os.path.join(bld.srcnode.abspath(), 'pidl/pidl')
92     t.env.OPTIONS = TO_LIST(options)
93     t.env.OUTPUTDIR = bld.bldnode.name + '/' + bld.path.find_dir(output_dir).bldpath(t.env)
94
95     if generate_tables and table_header_idx is not None:
96         pidl_headers = LOCAL_CACHE(bld, 'PIDL_HEADERS')
97         pidl_headers[name] = [bld.path.find_or_declare(out_files[table_header_idx])]
98
99     t.more_includes = '#' + bld.path.relpath_gen(bld.srcnode)
100 Build.BuildContext.SAMBA_PIDL = SAMBA_PIDL
101
102
103 def SAMBA_PIDL_LIST(bld, name, source,
104                     options='',
105                     output_dir='.',
106                     generate_tables=True):
107     '''A wrapper for building a set of IDL files'''
108     for p in TO_LIST(source):
109         bld.SAMBA_PIDL(name, p, options=options, output_dir=output_dir, generate_tables=generate_tables)
110 Build.BuildContext.SAMBA_PIDL_LIST = SAMBA_PIDL_LIST
111
112
113 #################################################################
114 # the rule for generating the NDR tables
115 from TaskGen import feature, before
116 @feature('collect')
117 @before('exec_rule')
118 def collect(self):
119     pidl_headers = LOCAL_CACHE(self.bld, 'PIDL_HEADERS')
120     for (name, hd) in pidl_headers.items():
121         y = self.bld.name_to_obj(name, self.env)
122         self.bld.ASSERT(y is not None, 'Failed to find PIDL header %s' % name)
123         y.post()
124         for node in hd:
125             self.bld.ASSERT(node is not None, 'Got None as build node generating PIDL table for %s' % name)
126             self.source += " " + node.relpath_gen(self.path)
127
128
129 def SAMBA_PIDL_TABLES(bld, name, target):
130     '''generate the pidl NDR tables file'''
131     headers = bld.env.PIDL_HEADERS
132     bld.SET_BUILD_GROUP('main')
133     t = bld(
134             features = 'collect',
135             rule     = '${PERL} ${SRC} --output ${TGT} | sed "s|default/||" > ${TGT}',
136             ext_out  = '.c',
137             before   = 'cc',
138             update_outputs = True,
139             shell    = True,
140             source   = '../../librpc/tables.pl',
141             target   = target,
142             name     = name)
143     t.env.LIBRPC = os.path.join(bld.srcnode.abspath(), 'librpc')
144 Build.BuildContext.SAMBA_PIDL_TABLES = SAMBA_PIDL_TABLES
145