1 # Licensed under the Apache License, Version 2.0 (the "License");
2 # you may not use this file except in compliance with the License.
3 # You may obtain a copy of the License at
5 # http://www.apache.org/licenses/LICENSE-2.0
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS,
9 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 # See the License for the specific language governing permissions and
11 # limitations under the License.
13 import os, sys, platform, subprocess
16 machine = platform.machine()
18 if machine.startswith("iPod") or machine.startswith("iPhone"):
20 elif sys.platform == "win32" and "--compiler=mingw32" in sys.argv:
23 platform = sys.platform
25 # Add or edit the entry corresponding to your system in the JDK, INCLUDES,
26 # CFLAGS, DEBUG_CFLAGS, LFLAGS and JAVAC dictionaries below.
27 # These entries are used to build JCC _and_ by JCC to drive compiling and
28 # linking via distutils or setuptools the extensions it generated code for.
30 # The key for your system is determined by the platform variable defined
33 # Instead of editing the entries below, you may also override these
34 # dictionaries with JCC_JDK, JCC_INCLUDES, JCC_CFLAGS, JCC_DEBUG_CFLAGS,
35 # JCC_LFLAGS and JCC_JAVAC environment variables using os.pathsep as value
38 if platform in ("win32", "mingw32"):
40 from helpers.windows import JAVAHOME
43 elif platform in ("darwin",):
45 from helpers.darwin import JAVAHOME
53 'ipod': '/usr/include/gcc',
54 'linux2': '/usr/lib/jvm/java-6-openjdk',
55 'sunos5': '/usr/jdk/instances/jdk1.6.0',
58 'freebsd7': '/usr/local/diablo-jdk1.6.0'
60 if 'JCC_JDK' in os.environ:
61 JDK[platform] = os.environ['JCC_JDK']
65 raise RuntimeError('''
67 Can't determine where the Java JDK has been installed on this machine.
69 Please set the environment variable JCC_JDK to that location before
73 elif not os.path.isdir(JDK[platform]):
74 raise RuntimeError('''
76 Java JDK directory '%s' does not exist.
78 Please set the environment variable JCC_JDK to the correct location before
84 'darwin': ['%(darwin)s/Headers' %(JDK)],
85 'ipod': ['%(ipod)s/darwin/default' %(JDK)],
86 'linux2': ['%(linux2)s/include' %(JDK),
87 '%(linux2)s/include/linux' %(JDK)],
88 'sunos5': ['%(sunos5)s/include' %(JDK),
89 '%(sunos5)s/include/solaris' %(JDK)],
90 'win32': ['%(win32)s/include' %(JDK),
91 '%(win32)s/include/win32' %(JDK)],
92 'mingw32': ['%(mingw32)s/include' %(JDK),
93 '%(mingw32)s/include/win32' %(JDK)],
94 'freebsd7': ['%(freebsd7)s/include' %(JDK),
95 '%(freebsd7)s/include/freebsd' %(JDK)],
99 'darwin': ['-fno-strict-aliasing', '-Wno-write-strings'],
100 'ipod': ['-Wno-write-strings'],
101 'linux2': ['-fno-strict-aliasing', '-Wno-write-strings'],
102 'sunos5': ['-features=iddollar',
103 '-erroff=badargtypel2w,wbadinitl,wvarhidemem'],
104 'win32': ["/EHsc", "/D_CRT_SECURE_NO_WARNINGS"], # MSVC 9 (2008)
105 'mingw32': ['-fno-strict-aliasing', '-Wno-write-strings'],
106 'freebsd7': ['-fno-strict-aliasing', '-Wno-write-strings'],
109 # added to CFLAGS when JCC is invoked with --debug
111 'darwin': ['-O0', '-g', '-DDEBUG'],
112 'ipod': ['-O0', '-g', '-DDEBUG'],
113 'linux2': ['-O0', '-g', '-DDEBUG'],
114 'sunos5': ['-DDEBUG'],
115 'win32': ['/Od', '/DDEBUG'],
116 'mingw32': ['-O0', '-g', '-DDEBUG'],
117 'freebsd7': ['-O0', '-g', '-DDEBUG'],
121 'darwin': ['-framework', 'JavaVM'],
122 'ipod': ['-ljvm', '-lpython%s.%s' %(sys.version_info[0:2]),
123 '-L/usr/lib/gcc/arm-apple-darwin9/4.0.1'],
124 'linux2/i386': ['-L%(linux2)s/jre/lib/i386' %(JDK), '-ljava',
125 '-L%(linux2)s/jre/lib/i386/client' %(JDK), '-ljvm',
126 '-Wl,-rpath=%(linux2)s/jre/lib/i386:%(linux2)s/jre/lib/i386/client' %(JDK)],
127 'linux2/i686': ['-L%(linux2)s/jre/lib/i386' %(JDK), '-ljava',
128 '-L%(linux2)s/jre/lib/i386/client' %(JDK), '-ljvm',
129 '-Wl,-rpath=%(linux2)s/jre/lib/i386:%(linux2)s/jre/lib/i386/client' %(JDK)],
130 'linux2/x86_64': ['-L%(linux2)s/jre/lib/amd64' %(JDK), '-ljava',
131 '-L%(linux2)s/jre/lib/amd64/server' %(JDK), '-ljvm',
132 '-Wl,-rpath=%(linux2)s/jre/lib/amd64:%(linux2)s/jre/lib/amd64/server' %(JDK)],
133 'sunos5': ['-L%(sunos5)s/jre/lib/i386' %(JDK), '-ljava',
134 '-L%(sunos5)s/jre/lib/i386/client' %(JDK), '-ljvm',
135 '-R%(sunos5)s/jre/lib/i386:%(sunos5)s/jre/lib/i386/client' %(JDK)],
136 'win32': ['/LIBPATH:%(win32)s/lib' %(JDK), 'jvm.lib'],
137 'mingw32': ['-L%(mingw32)s/lib' %(JDK), '-ljvm'],
138 'freebsd7': ['-L%(freebsd7)s/jre/lib/i386' %(JDK), '-ljava', '-lverify',
139 '-L%(freebsd7)s/jre/lib/i386/client' %(JDK), '-ljvm',
140 '-Wl,-rpath=%(freebsd7)s/jre/lib/i386:%(freebsd7)s/jre/lib/i386/client' %(JDK)],
144 'win32': ["/IMPLIB:%s"],
145 'mingw32': ["-Wl,--out-implib,%s"]
148 if platform == 'linux2':
149 LFLAGS['linux2'] = LFLAGS['linux2/%s' %(machine)]
152 'darwin': ['javac', '-target', '1.5'],
153 'ipod': ['jikes', '-cp', '/usr/share/classpath/glibj.zip'],
156 'win32': ['%(win32)s/bin/javac.exe' %(JDK)],
157 'mingw32': ['%(mingw32)s/bin/javac.exe' %(JDK)],
158 'freebsd7': ['javac'],
162 'darwin': ['javadoc'],
164 'linux2': ['javadoc'],
165 'sunos5': ['javadoc'],
166 'win32': ['%(win32)s/bin/javadoc.exe' %(JDK)],
167 'mingw32': ['%(mingw32)s/bin/javadoc.exe' %(JDK)],
168 'freebsd7': ['javadoc'],
172 if 'USE_DISTUTILS' in os.environ:
174 from setuptools import setup, Extension
175 from pkg_resources import require
176 with_setuptools = require('setuptools')[0].parsed_version
178 enable_shared = False
179 with_setuptools_c7 = ('00000000', '00000006', '*c', '00000007', '*final')
181 if with_setuptools >= with_setuptools_c7 and 'NO_SHARED' not in os.environ:
182 if platform in ('darwin', 'ipod', 'win32'):
184 elif platform == 'linux2':
185 from helpers.linux import patch_setuptools
186 enable_shared = patch_setuptools(with_setuptools)
187 elif platform == 'mingw32':
189 # need to monkeypatch the CygwinCCompiler class to generate
190 # jcc.lib in the correct place
191 from helpers.mingw32 import JCCMinGW32CCompiler
192 import distutils.cygwinccompiler
193 distutils.cygwinccompiler.Mingw32CCompiler = JCCMinGW32CCompiler
196 if sys.version_info < (2, 4):
197 raise ImportError, 'setuptools is required when using Python 2.3'
199 from distutils.core import setup, Extension
200 with_setuptools = None
201 enable_shared = False
206 _jcc_argsep = os.environ.get('JCC_ARGSEP', os.pathsep)
208 if 'JCC_INCLUDES' in os.environ:
209 _includes = os.environ['JCC_INCLUDES'].split(_jcc_argsep)
211 _includes = INCLUDES[platform]
213 if 'JCC_CFLAGS' in os.environ:
214 _cflags = os.environ['JCC_CFLAGS'].split(_jcc_argsep)
216 _cflags = CFLAGS[platform]
218 if 'JCC_DEBUG_CFLAGS' in os.environ:
219 _debug_cflags = os.environ['JCC_DEBUG_CFLAGS'].split(_jcc_argsep)
221 _debug_cflags = DEBUG_CFLAGS[platform]
223 if 'JCC_LFLAGS' in os.environ:
224 _lflags = os.environ['JCC_LFLAGS'].split(_jcc_argsep)
226 _lflags = LFLAGS[platform]
228 if 'JCC_IMPLIB_LFLAGS' in os.environ:
229 _implib_lflags = os.environ['JCC_IMPLIB_LFLAGS'].split(_jcc_argsep)
231 _implib_lflags = IMPLIB_LFLAGS.get(platform, [])
233 if 'JCC_JAVAC' in os.environ:
234 _javac = os.environ['JCC_JAVAC'].split(_jcc_argsep)
236 _javac = JAVAC[platform]
238 if 'JCC_JAVADOC' in os.environ:
239 _javadoc = os.environ['JCC_JAVADOC'].split(_jcc_argsep)
241 _javadoc = JAVADOC[platform]
243 from helpers.build import jcc_build_py
245 jcc_build_py.config_file = \
246 os.path.join(os.path.dirname(os.path.abspath(__file__)),
248 jcc_build_py.config_text = \
250 'INCLUDES=%s' %(_includes),
251 'CFLAGS=%s' %(_cflags),
252 'DEBUG_CFLAGS=%s' %(_debug_cflags),
253 'LFLAGS=%s' %(_lflags),
254 'IMPLIB_LFLAGS=%s' %(_implib_lflags),
255 'SHARED=%s' %(enable_shared),
256 'VERSION="%s"' %(jcc_ver),
263 cflags = ['-DPYTHON'] + _cflags
265 cflags += _debug_cflags
266 includes = _includes + [boot, 'jcc/sources']
269 if platform == 'win32':
271 elif platform == 'sunos5':
276 sources = ['jcc/sources/jcc.cpp',
277 'jcc/sources/JCCEnv.cpp',
278 'jcc/sources/JObject.cpp',
279 'jcc/sources/JArray.cpp',
280 'jcc/sources/functions.cpp',
281 'jcc/sources/types.cpp']
282 for path, dirs, names in os.walk(boot):
284 if name.endswith('.cpp'):
285 sources.append(os.path.join(path, name))
286 package_data = ['sources/*.cpp', 'sources/*.h', 'patches/patch.*']
288 if with_setuptools and enable_shared:
289 from subprocess import Popen, PIPE
290 from setuptools import Library
292 kwds = { "extra_compile_args": cflags,
293 "include_dirs": includes,
294 "define_macros": [('_jcc_lib', None),
295 ('JCC_VER', '"%s"' %(jcc_ver))],
296 "sources": sources[0:2] }
298 if platform in ('darwin', 'ipod'):
299 kwds["extra_link_args"] = \
300 lflags + ['-install_name', '@rpath/libjcc.dylib',
301 '-current_version', jcc_ver,
302 '-compatibility_version', jcc_ver]
303 elif platform == 'linux2':
304 kwds["extra_link_args"] = \
305 lflags + ['-lpython%s.%s' %(sys.version_info[0:2])]
306 kwds["force_shared"] = True # requires jcc/patches/patch.43
307 elif platform in IMPLIB_LFLAGS:
308 jcclib = 'jcc%s.lib' %(debug and '_d' or '')
309 implib_flags = ' '.join(IMPLIB_LFLAGS[platform])
310 kwds["extra_link_args"] = \
311 lflags + [implib_flags %(os.path.join('jcc', jcclib))]
312 package_data.append(jcclib)
314 kwds["extra_link_args"] = lflags
316 extensions.append(Library('jcc', **kwds))
319 args.extend(('-d', 'jcc/classes'))
320 args.append('java/org/apache/jcc/PythonVM.java')
321 args.append('java/org/apache/jcc/PythonException.java')
322 if not os.path.exists('jcc/classes'):
323 os.makedirs('jcc/classes')
325 process = Popen(args, stderr=PIPE)
327 raise type(e), "%s: %s" %(e, args)
329 if process.returncode != 0:
330 raise OSError, process.stderr.read()
331 package_data.append('classes/org/apache/jcc/PythonVM.class')
332 package_data.append('classes/org/apache/jcc/PythonException.class')
335 args.extend(('-d', 'javadoc', '-sourcepath', 'java', 'org.apache.jcc'))
337 process = Popen(args, stderr=PIPE)
339 raise type(e), "%s: %s" %(e, args)
341 if process.returncode != 0:
342 raise OSError, process.stderr.read()
344 extensions.append(Extension('jcc._jcc',
345 extra_compile_args=cflags,
346 extra_link_args=lflags,
347 include_dirs=includes,
348 define_macros=[('_java_generics', None),
349 ('JCC_VER', '"%s"' %(jcc_ver))],
355 'description': 'a C++ code generator for calling Java from C++/Python',
356 'long_description': open('DESCRIPTION').read(),
357 'author': 'Andi Vajda',
358 'author_email': 'vajda@apache.org',
359 'classifiers': ['Development Status :: 5 - Production/Stable',
360 'Environment :: Console',
361 'Intended Audience :: Developers',
362 'License :: OSI Approved :: Apache Software License',
363 'Operating System :: OS Independent',
364 'Programming Language :: C++',
365 'Programming Language :: Java',
366 'Programming Language :: Python',
367 'Topic :: Software Development :: Code Generators',
368 'Topic :: Software Development :: Libraries :: Java Libraries'],
370 'package_dir': {'jcc': 'jcc'},
371 'package_data': {'jcc': package_data},
372 'ext_modules': extensions,
373 "cmdclass": {"build_py": jcc_build_py},
376 args['zip_safe'] = False
381 if __name__ == "__main__":
382 main('--debug' in sys.argv)