~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to config/register_plugins.py

Added code necessary for building plugins dynamically.
Merged in changes from lifeless to allow autoreconf to work.
Touching plugin.ini files now triggers a rebuid - so config/autorun.sh is no
longer required to be run after touching those.
Removed the duplicate plugin names - also removed the issue that getting them
different would silently fail weirdly later.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 
19
19
# Find plugins in the tree and add them to the build system 
20
20
 
21
 
import os, ConfigParser, sys
 
21
import ConfigParser, os, sys
22
22
 
23
23
top_srcdir='.'
24
24
top_builddir='.'
25
25
plugin_ini_fname='plugin.ini'
26
26
plugin_list=[]
27
 
autogen_header="This file is generated, re-run %s to rebuild\n" % sys.argv[0]
28
27
 
29
28
class ChangeProtectedFile(object):
30
29
 
31
30
  def __init__(self, fname):
32
31
    self.real_fname= fname
33
32
    self.new_fname= "%s.new" % fname
34
 
    self.new_file= open(self.new_fname,'w')
 
33
    self.new_file= open(self.new_fname,'w+')
35
34
 
36
35
  def write(self, text):
37
36
    self.new_file.write(text)
40
39
  # over the old ones if they are different, so that we don't cause 
41
40
  # unnecessary recompiles
42
41
  def close(self):
 
42
    """Return True if the file had changed."""
 
43
    self.new_file.seek(0)
 
44
    new_content = self.new_file.read()
43
45
    self.new_file.close()
44
 
    if os.system('diff %s %s >/dev/null 2>&1' % (self.new_fname,self.real_fname)):
45
 
      try:
 
46
    try:
 
47
        old_file = file(self.real_fname, 'r')
 
48
        old_content = old_file.read()
 
49
        old_file.close()
 
50
    except IOError:
 
51
        old_content = None
 
52
    if new_content != old_content:
 
53
      if old_content != None:
46
54
        os.unlink(self.real_fname)
47
 
      except:
48
 
        pass
49
55
      os.rename(self.new_fname, self.real_fname)
50
 
    try:
51
 
      os.unlink(self.new_fname)
52
 
    except:
53
 
      pass
54
 
 
55
 
if len(sys.argv)>1:
56
 
  top_srcdir=sys.argv[1]
57
 
  top_builddir=top_srcdir
58
 
if len(sys.argv)>2:
59
 
  top_builddir=sys.argv[2]
60
 
 
61
 
plugin_ac=ChangeProtectedFile(os.path.join(top_builddir,'config','plugin.ac'))
62
 
plugin_am=ChangeProtectedFile(os.path.join(top_srcdir,'config','plugin.am'))
63
 
 
64
 
plugin_ac.write("dnl %s" % autogen_header)
65
 
plugin_am.write("# %s" % autogen_header)
66
 
plugin_am.write("PANDORA_DYNAMIC_LDADDS=\n")
67
 
 
68
 
def accumulate_plugins(arg, dirname, fnames):
69
 
  if plugin_ini_fname in fnames:
70
 
    arg.append(dirname)
71
 
 
72
 
 
73
 
os.path.walk(os.path.join(top_srcdir,"plugin"),accumulate_plugins,plugin_list)
74
 
plugin_list.sort()
75
 
 
76
 
for plugin_dir in plugin_list:
77
 
  plugin_file= os.path.join(plugin_dir,plugin_ini_fname)
78
 
  parser=ConfigParser.ConfigParser(defaults=dict(sources="",headers="", cflags="",cppflags="",cxxflags="", libs="", ldflags="", testsuite=""))
79
 
  parser.read(plugin_file)
80
 
  plugin=dict(parser.items('plugin'))
81
 
  plugin['rel_path']= plugin_dir[len(top_srcdir)+len(os.path.sep):]
82
 
  # TODO: determine path to plugin dir relative to top_srcdir... append it
83
 
  # to source files if they don't already have it
84
 
  if plugin['sources'] == "":
85
 
    plugin['sources']="%s.cc" % plugin['name']
86
 
  new_sources=""
87
 
  for src in plugin['sources'].split():
88
 
    if not src.startswith(plugin['rel_path']):
89
 
      src= os.path.join(plugin['rel_path'], src)
90
 
      new_sources= "%s %s" % (new_sources, src)
91
 
  plugin['sources']= new_sources
92
 
 
93
 
  new_headers=""
94
 
  for header in plugin['headers'].split():
95
 
    if not header.startswith(plugin['rel_path']):
96
 
      header= os.path.join(plugin['rel_path'], header)
97
 
      new_headers= "%s %s" % (new_headers, header)
98
 
  plugin['headers']= new_headers
99
 
 
100
 
  if plugin.has_key('load_by_default'):
101
 
    plugin['load_by_default']=parser.getboolean('plugin','load_by_default')
102
 
  else:
103
 
    plugin['load_by_default']=False
104
 
  # Make a yes/no version for autoconf help messages
105
 
  if plugin['load_by_default']:
106
 
    plugin['default_yesno']="yes"
107
 
  else:
108
 
    plugin['default_yesno']="no"
109
 
 
 
56
      return True
 
57
    else:
 
58
        try:
 
59
          os.unlink(self.new_fname)
 
60
        except:
 
61
          pass
 
62
 
 
63
 
 
64
def write_external_plugin():
 
65
  """Return True if the plugin had changed."""
 
66
  plugin = read_plugin_ini('.')
 
67
  expand_plugin_ini(plugin, '.')
 
68
  plugin_file = ChangeProtectedFile('pandora-plugin.ac')
 
69
  write_plugin_ac(plugin, plugin_file)
 
70
  result = plugin_file.close()
 
71
  plugin_file = ChangeProtectedFile('pandora-plugin.am')
 
72
  write_plugin_am(plugin, plugin_file)
 
73
  # Write some stub configure.ac and Makefile.am files that include the above
 
74
  result = plugin_file.close() or result
 
75
  return result
 
76
 
 
77
def write_plugin(plugin_dir):
 
78
  """Return True if the plugin had changed."""
 
79
  plugin = read_plugin_ini(plugin_dir)
 
80
  expand_plugin_ini(plugin, plugin_dir)
 
81
  plugin_file = ChangeProtectedFile(os.path.join(plugin_dir, 'pandora-plugin.ac'))
 
82
  write_plugin_ac(plugin, plugin_file)
 
83
  result = plugin_file.close()
 
84
  plugin_file = ChangeProtectedFile(os.path.join(plugin_dir, 'pandora-plugin.am'))
 
85
  write_plugin_am(plugin, plugin_file)
 
86
  result = plugin_file.close() or result
 
87
  return result
 
88
 
 
89
def write_plugin_ac(plugin, plugin_ac):
 
90
  #
 
91
  # Write plugin config instructions into plugin.ac file.
 
92
  #
110
93
  plugin_ac_file=os.path.join(plugin['rel_path'],'plugin.ac')
111
 
  plugin_am_file=os.path.join(plugin['rel_path'],'plugin.am')
112
94
  plugin_m4_dir=os.path.join(plugin['rel_path'],'m4')
113
 
 
114
95
  plugin_m4_files=[]
115
96
  if os.path.exists(plugin_m4_dir) and os.path.isdir(plugin_m4_dir):
116
97
    for m4_file in os.listdir(plugin_m4_dir):
117
98
      if os.path.splitext(m4_file)[-1] == '.m4':
118
99
        plugin_m4_files.append(os.path.join(plugin['rel_path'], m4_file))
119
 
 
120
 
  plugin['build_conditional_tag']= "BUILD_%s_PLUGIN" % plugin['name'].upper()
121
 
  plugin['name_with_dashes']= plugin['name'].replace('_','-')
122
 
  if plugin.has_key('build_conditional'):
123
 
    plugin['has_build_conditional']=True
124
 
  else:
125
 
    plugin['has_build_conditional']=False
126
 
    plugin['build_conditional']='"x${with_%(name)s_plugin}" = "xyes"' %plugin
127
 
 
128
 
  # Turn this on later plugin_lib%(name)s_plugin_la_CPPFLAGS=$(AM_CPPFLAGS) -DPANDORA_DYNAMIC_PLUGIN %(cppflags)s
129
 
 
130
 
  #
131
 
  # Write plugin build instructions into plugin.am file.
132
 
  #
133
 
  if plugin['headers'] != "":
134
 
    plugin_am.write("noinst_HEADERS += %(headers)s\n" % plugin)
135
 
  plugin_am.write("""
136
 
plugin_lib%(name)s_dir=${top_srcdir}/%(rel_path)s
137
 
EXTRA_DIST += %(rel_path)s/plugin.ini
138
 
if %(build_conditional_tag)s
139
 
  noinst_LTLIBRARIES+=plugin/lib%(name)s_plugin.la
140
 
  plugin_lib%(name)s_plugin_la_LIBADD=%(libs)s
141
 
  plugin_lib%(name)s_plugin_la_DEPENDENCIES=%(libs)s
142
 
  plugin_lib%(name)s_plugin_la_LDFLAGS=$(AM_LDFLAGS) %(ldflags)s
143
 
  plugin_lib%(name)s_plugin_la_CPPFLAGS=$(AM_CPPFLAGS) %(cppflags)s
144
 
  plugin_lib%(name)s_plugin_la_CXXFLAGS=$(AM_CXXFLAGS) %(cxxflags)s
145
 
  plugin_lib%(name)s_plugin_la_CFLAGS=$(AM_CFLAGS) %(cflags)s
146
 
 
147
 
  plugin_lib%(name)s_plugin_la_SOURCES=%(sources)s
148
 
  PANDORA_DYNAMIC_LDADDS+=${top_builddir}/plugin/lib%(name)s_plugin.la
149
 
endif
 
100
  plugin_ac.write("""dnl Generated file, run make to rebuild
 
101
dnl Config for %(title)s
150
102
""" % plugin)
151
 
  # Add this once we're actually doing dlopen (and remove -avoid-version if
152
 
  # we move to ltdl
153
 
  #pkgplugin_LTLIBRARIES+=plugin/lib%(name)s_plugin.la
154
 
  #plugin_lib%(name)s_plugin_la_LDFLAGS=-module -avoid-version -rpath $(pkgplugindir) %(libs)s
155
 
 
156
 
 
157
 
  if os.path.exists(plugin_am_file):
158
 
    plugin_am.write('include %s\n' % plugin_am_file)
159
 
 
160
 
  #
161
 
  # Write plugin config instructions into plugin.am file.
162
 
  #
163
 
  plugin_ac.write("\n\ndnl Config for %(title)s\n" % plugin)
164
103
  for m4_file in plugin_m4_files:
165
104
    plugin_ac.write('m4_sinclude([%s])\n' % m4_file)
166
105
 
200
139
        pandora_plugin_test_list="%(testsuite)s,${pandora_plugin_test_list}"
201
140
    """ % plugin)
202
141
 
203
 
  plugin_ac.write("""
204
 
        pandora_default_plugin_list="%(name)s,${pandora_default_plugin_list}"
 
142
  if plugin['static']:
 
143
    #pandora_default_plugin_list="%(name)s,${pandora_default_plugin_list}"
 
144
    plugin_ac.write("""
205
145
        pandora_builtin_list="builtin_%(name)s_plugin,${pandora_builtin_list}"
206
146
        pandora_plugin_libs="${pandora_plugin_libs} \${top_builddir}/plugin/lib%(name)s_plugin.la"
207
147
        PANDORA_PLUGIN_DEP_LIBS="${PANDORA_PLUGIN_DEP_LIBS} %(plugin_dep_libs)s"
208
 
      ])
209
 
""" % plugin)
210
 
 
211
 
plugin_ac.close()
212
 
plugin_am.close()
213
 
 
 
148
""" % plugin)
 
149
  else:
 
150
    plugin_ac.write("""
 
151
        pandora_default_plugin_list="%(name)s,${pandora_default_plugin_list}"
 
152
    """ % plugin)
 
153
  plugin_ac.write("      ])\n")
 
154
 
 
155
 
 
156
def expand_plugin_ini(plugin, plugin_dir):
 
157
    plugin['rel_path']= plugin_dir[len(top_srcdir)+len(os.path.sep):]
 
158
    # TODO: determine path to plugin dir relative to top_srcdir... append it to
 
159
    # source files if they don't already have it
 
160
    if plugin['sources'] == "":
 
161
      plugin['sources']="%s.cc" % plugin['name']
 
162
    new_sources=""
 
163
    for src in plugin['sources'].split():
 
164
      if not src.startswith(plugin['rel_path']):
 
165
        src= os.path.join(plugin['rel_path'], src)
 
166
        new_sources= "%s %s" % (new_sources, src)
 
167
    plugin['sources']= new_sources
 
168
    
 
169
    new_headers=""
 
170
    for header in plugin['headers'].split():
 
171
      if not header.startswith(plugin['rel_path']):
 
172
        header= os.path.join(plugin['rel_path'], header)
 
173
        new_headers= "%s %s" % (new_headers, header)
 
174
    plugin['headers']= new_headers
 
175
    
 
176
    # Make a yes/no version for autoconf help messages
 
177
    if plugin['load_by_default']:
 
178
      plugin['default_yesno']="yes"
 
179
    else:
 
180
      plugin['default_yesno']="no"
 
181
 
 
182
    
 
183
    plugin['build_conditional_tag']= "BUILD_%s_PLUGIN" % plugin['name'].upper()
 
184
    plugin['name_with_dashes']= plugin['name'].replace('_','-')
 
185
    if plugin.has_key('build_conditional'):
 
186
      plugin['has_build_conditional']=True
 
187
    else:
 
188
      plugin['has_build_conditional']=False
 
189
      plugin['build_conditional']='"x${with_%(name)s_plugin}" = "xyes"' %plugin
 
190
 
 
191
 
 
192
def read_plugin_ini(plugin_dir):
 
193
    plugin_file= os.path.join(plugin_dir,plugin_ini_fname)
 
194
    parser=ConfigParser.ConfigParser(defaults=dict(sources="",headers="", cflags="",cppflags="",cxxflags="", libs="", ldflags="", testsuite=""))
 
195
    parser.read(plugin_file)
 
196
    plugin=dict(parser.items('plugin'))
 
197
    plugin['name']= os.path.basename(plugin_dir)
 
198
    if plugin.has_key('load_by_default'):
 
199
      plugin['load_by_default']=parser.getboolean('plugin','load_by_default')
 
200
    else:
 
201
      plugin['load_by_default']=False
 
202
    if plugin.has_key('static'):
 
203
      plugin['static']= parser.getboolean('plugin','static')
 
204
    else:
 
205
      plugin['static']= False
 
206
 
 
207
    return plugin
 
208
 
 
209
 
 
210
def write_plugin_am(plugin, plugin_am):
 
211
  """Write an automake fragment for this plugin.
 
212
  
 
213
  :param plugin: The plugin dict.
 
214
  :param plugin_am: The file to write to.
 
215
  """
 
216
  # The .plugin.ini.stamp avoids changing the datestamp on plugin.ini which can
 
217
  # confuse VCS systems.
 
218
  plugin_am.write("""## Generated by register_plugins.py
 
219
EXTRA_DIST += %(rel_path)s/pandora-plugin.ac
 
220
 
 
221
${top_srcdir}/%(rel_path)s/pandora-plugin.am: ${top_srcdir}/config/register_plugins.py %(rel_path)s/.plugin.ini.stamp
 
222
 
 
223
${top_srcdir}/%(rel_path)s/pandora-plugin.ac: ${top_srcdir}/config/register_plugins.py %(rel_path)s/.plugin.ini.stamp
 
224
 
 
225
configure: ${top_srcdir}/%(rel_path)s/pandora-plugin.ac
 
226
 
 
227
# Prevent errors when a plugin dir is removed
 
228
%(rel_path)s/plugin.ini:
 
229
 
 
230
# Failures to update the plugin.ini are ignored to permit plugins to be deleted
 
231
# cleanly.
 
232
%(rel_path)s/.plugin.ini.stamp: %(rel_path)s/plugin.ini
 
233
        @if [ ! -e ${top_srcdir}/%(rel_path)s/plugin.ini ]; then \\
 
234
            echo "%(rel_path)s/plugin.ini is missing"; \\
 
235
        else \\
 
236
            cmp -s $< $@; \\
 
237
            if [ $$? -ne 0 ]; then \\
 
238
              echo 'cd ${srcdir} && python config/register_plugins.py ${top_srcdir} ${top_builddir} %(rel_path)s'; \\
 
239
              unchanged=`cd ${srcdir} && python config/register_plugins.py ${top_srcdir} ${top_builddir} %(rel_path)s` ;\\
 
240
              if [ $$? -ne 0 ]; then \\
 
241
                echo "**** register_plugins failed ****"; \\
 
242
                false; \\
 
243
              fi && \\
 
244
              for plugin_dir in $$unchanged; do \\
 
245
                echo "plugin $$plugin_dir unchanged." ; \\
 
246
                touch -f -r $$plugin_dir/pandora-plugin.am $$plugin_dir/.plugin.ini.stamp; \\
 
247
              done && \\
 
248
              cp $< $@ || echo "Failed to update $@"; \\
 
249
            fi; \\
 
250
        fi
 
251
""" % plugin)
 
252
  if plugin['headers'] != "":
 
253
    plugin_am.write("noinst_HEADERS += %(headers)s\n" % plugin)
 
254
  if plugin['static']:
 
255
    plugin_am.write("""
 
256
plugin_lib%(name)s_dir=${top_srcdir}/%(rel_path)s
 
257
EXTRA_DIST += %(rel_path)s/plugin.ini
 
258
if %(build_conditional_tag)s
 
259
  noinst_LTLIBRARIES+=plugin/lib%(name)s_plugin.la
 
260
  plugin_lib%(name)s_plugin_la_LIBADD=%(libs)s
 
261
  plugin_lib%(name)s_plugin_la_DEPENDENCIES=%(libs)s
 
262
  plugin_lib%(name)s_plugin_la_LDFLAGS=$(AM_LDFLAGS) %(ldflags)s
 
263
  plugin_lib%(name)s_plugin_la_CPPFLAGS=$(AM_CPPFLAGS) -DPANDORA_MODULE_NAME=%(name)s %(cppflags)s
 
264
  plugin_lib%(name)s_plugin_la_CXXFLAGS=$(AM_CXXFLAGS) %(cxxflags)s
 
265
  plugin_lib%(name)s_plugin_la_CFLAGS=$(AM_CFLAGS) %(cflags)s
 
266
 
 
267
  plugin_lib%(name)s_plugin_la_SOURCES=%(sources)s
 
268
  PANDORA_DYNAMIC_LDADDS+=${top_builddir}/plugin/lib%(name)s_plugin.la
 
269
endif
 
270
""" % plugin)
 
271
  else:
 
272
    plugin_am.write("""
 
273
plugin_lib%(name)s_dir=${top_srcdir}/%(rel_path)s
 
274
EXTRA_DIST += %(rel_path)s/plugin.ini
 
275
if %(build_conditional_tag)s
 
276
  pkgplugin_LTLIBRARIES+=plugin/lib%(name)s_plugin.la
 
277
  plugin_lib%(name)s_plugin_la_LDFLAGS=-module -avoid-version -rpath $(pkgplugindir) $(AM_LDFLAGS) %(ldflags)s
 
278
  plugin_lib%(name)s_plugin_la_LIBADD=%(libs)s
 
279
  plugin_lib%(name)s_plugin_la_DEPENDENCIES=%(libs)s
 
280
  plugin_lib%(name)s_plugin_la_CPPFLAGS=$(AM_CPPFLAGS) -DPANDORA_DYNAMIC_PLUGIN -DPANDORA_MODULE_NAME=%(name)s %(cppflags)s
 
281
  plugin_lib%(name)s_plugin_la_CXXFLAGS=$(AM_CXXFLAGS) %(cxxflags)s
 
282
  plugin_lib%(name)s_plugin_la_CFLAGS=$(AM_CFLAGS) %(cflags)s
 
283
 
 
284
  plugin_lib%(name)s_plugin_la_SOURCES=%(sources)s
 
285
endif
 
286
""" % plugin)
 
287
  plugin_am_file=os.path.join(plugin['rel_path'],'plugin.am')
 
288
  if os.path.exists(plugin_am_file):
 
289
    plugin_am.write('include %s\n' % plugin_am_file)
 
290
 
 
291
 
 
292
if len(sys.argv)>1:
 
293
  top_srcdir=sys.argv[1]
 
294
  top_builddir=sys.argv[2]
 
295
 
 
296
if not os.path.exists("config/plugin.list"):
 
297
  os.system("find plugin -name 'plugin.ini' | xargs -n1 dirname | xargs -n1 basename | sort -b -d > .plugin.scan")
 
298
  os.system("cp .plugin.scan config/plugin.list")
 
299
 
 
300
 
 
301
if len(sys.argv)>1:
 
302
  if len(sys.argv)>3:
 
303
    plugin_list = [top_srcdir + '/' + plugin_name for plugin_name in sys.argv[3:]]
 
304
  else:
 
305
    def accumulate_plugins(arg, dirname, fnames):
 
306
      # plugin_ini_fname is a name in dirname indicating dirname is a plugin.
 
307
      if plugin_ini_fname in fnames:
 
308
        arg.append(dirname)
 
309
    os.path.walk(os.path.join(top_srcdir,"plugin"),accumulate_plugins,plugin_list)
 
310
 
 
311
if os.path.exists('plugin.ini'):
 
312
  # Are we in a plugin dir which wants to have a self-sufficient build system?
 
313
  write_external_plugin()
 
314
else:
 
315
  plugin_list.sort()
 
316
  for plugin_dir in plugin_list:
 
317
    if not write_plugin(plugin_dir):
 
318
      print plugin_dir
 
319
 
 
320
if not os.path.exists("config/plugin-list.am"):
 
321
  os.system(r"sed 's,^\(.*\)$,include plugin/\1/pandora-plugin.am,' < config/plugin.list > config/plugin-list.am")
 
322
 
 
323
if not os.path.exists("config/plugin-list.ac"):
 
324
  os.system(r"sed 's,^\(.*\)$,m4_sinclude(plugin/\1/pandora-plugin.ac),'  < config/plugin.list > config/plugin-list.ac")