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
) ]
12 import generator
.util
.executable
13 _exec
= generator
.util
.executable
15 class Generator(generator
.swig
.Generator
):
16 """Generate external runtime files for SWIG"""
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"""
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
)
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
38 def write_external_runtime(self
, lang
):
39 """Generate external runtime header files for each SWIG language"""
41 # Runtime library names
43 "python": "pyrun.swg", "perl":"perlrun.swg", "ruby":"rubydef.swg"
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())
53 open("%s/%s" % (self
.proxy_dir
, runtime_library
[lang
])).read())
55 out_file
.write(open("%s/runtime.swg" % self
.proxy_dir
).read())
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();
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
)
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):
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"
92 gen
= Generator(sys
.argv
[1], sys
.argv
[2])
93 gen
.write_external_runtime(sys
.argv
[3])