Make a status test pass against old servers.
[svn.git] / build / generator / swig / external_runtime.py
bloba03e5aea7696afc33c5864019f21991e7e6e91cc
1 #!/usr/bin/env python
4 # external_runtime.py: Generate external runtime files for SWIG
7 import sys, os, re, fileinput
8 if __name__ == "__main__":
9 parent_dir = os.path.dirname(os.path.abspath(os.path.dirname(sys.argv[0])))
10 sys.path[0:0] = [ parent_dir, os.path.dirname(parent_dir) ]
11 import generator.swig
12 import generator.util.executable
13 _exec = generator.util.executable
15 class Generator(generator.swig.Generator):
16 """Generate external runtime files for SWIG"""
18 def write(self):
19 """Generate external runtimes"""
20 for lang in self.langs:
21 self.write_external_runtime(lang)
23 def write_makefile_rules(self, makefile):
24 """Write the makefile rules for generating external runtimes"""
25 makefile.write(
26 'GEN_SWIG_RUNTIME = cd $(top_srcdir) && $(PYTHON)' +
27 ' build/generator/swig/external_runtime.py build.conf $(SWIG)\n\n'
29 for lang in self.langs:
30 out = self._output_file(lang)
31 makefile.write(
32 'autogen-swig-%s: %s\n' % (self.short[lang], out) +
33 '%s: $(SWIG_CHECKOUT_FILES)\n' % out +
34 '\t$(GEN_SWIG_RUNTIME) %s\n\n' % lang
36 makefile.write('\n')
38 def write_external_runtime(self, lang):
39 """Generate external runtime header files for each SWIG language"""
41 # Runtime library names
42 runtime_library = {
43 "python": "pyrun.swg", "perl":"perlrun.swg", "ruby":"rubydef.swg"
46 # Build runtime files
47 out = self._output_file(lang)
48 if self.version() == 103024:
49 out_file = open(out, "w")
50 out_file.write(open("%s/swigrun.swg" % self.proxy_dir).read())
51 out_file.write(open("%s/common.swg" % self.proxy_dir).read())
52 out_file.write(
53 open("%s/%s" % (self.proxy_dir, runtime_library[lang])).read())
54 if lang != "ruby":
55 out_file.write(open("%s/runtime.swg" % self.proxy_dir).read())
56 out_file.close()
57 else:
58 _exec.run("%s -%s -external-runtime %s" % (self.swig_path, lang, out))
60 # SWIG 1.3.24-27 should include rubyhead.swg in their
61 # external runtime, but they don't.
62 if lang == "ruby" and self.version() < 103028:
63 runtime = open(out).read()
64 out_file = open(out, "w")
65 head = open("%s/rubyhead.swg" % self.proxy_dir).read();
66 out_file.write(head)
67 if self.version() >= 103026:
68 # SWIG 1.3.26-27 should include rubytracking.swg in their
69 # external runtime, but they don't.
70 tracking = open("%s/rubytracking.swg" % self.proxy_dir).read();
71 out_file.write(tracking)
72 out_file.write(runtime)
73 out_file.close()
75 # SWIG 1.3.25 and earlier use the wrong number of arguments in calls to
76 # SWIG_GetModule. We fix this below.
77 if self.version() <= 103025:
78 for line in fileinput.input(out, inplace=1):
79 sys.stdout.write(
80 re.sub(r"SWIG_GetModule\(\)", "SWIG_GetModule(NULL)", line)
82 def _output_file(self, lang):
83 """Return the output filename of the runtime for the given language"""
84 return '%s/swig_%s_external_runtime.swg' % (self.proxy_dir, lang)
87 if __name__ == "__main__":
88 if len(sys.argv) != 4:
89 print "Usage: %s build.conf swig"
90 print "Generates external runtime files for SWIG"
91 else:
92 gen = Generator(sys.argv[1], sys.argv[2])
93 gen.write_external_runtime(sys.argv[3])