Merge pull request #1263 from abidrahmank:pyCLAHE_24
[profile/ivi/opencv.git] / platforms / scripts / ABI_compat_generator.py
1 #!/usr/bin/python
2
3 from optparse import OptionParser
4 from shutil import rmtree
5 import os
6
7
8 architecture = 'armeabi'
9 excludedHeaders = set(['hdf5.h', 'cap_ios.h', 'ios.h', 'eigen.hpp', 'cxeigen.hpp']) #TOREMOVE
10 systemIncludes = ['sources/cxx-stl/gnu-libstdc++/4.6/include', \
11     '/opt/android-ndk-r8c/platforms/android-8/arch-arm', # TODO: check if this one could be passed as command line arg
12     'sources/cxx-stl/gnu-libstdc++/4.6/libs/armeabi-v7a/include']
13 targetLibs = ['libopencv_java.so']
14 preamble = ['Eigen/Core']
15 # TODO: get gcc_options automatically
16 gcc_options = ['-fexceptions', '-frtti', '-Wno-psabi', '--sysroot=/opt/android-ndk-r8c/platforms/android-8/arch-arm', '-fpic', '-D__ARM_ARCH_5__', '-D__ARM_ARCH_5T__', '-D__ARM_ARCH_5E__', '-D__ARM_ARCH_5TE__', '-fsigned-char', '-march=armv5te', '-mtune=xscale', '-msoft-float', '-fdata-sections', '-ffunction-sections', '-Wa,--noexecstack   ', '-W', '-Wall', '-Werror=return-type', '-Werror=address', '-Werror=sequence-point', '-Wformat', '-Werror=format-security', '-Wmissing-declarations', '-Wundef', '-Winit-self', '-Wpointer-arith', '-Wshadow', '-Wsign-promo', '-Wno-narrowing', '-fdiagnostics-show-option', '-fomit-frame-pointer', '-mthumb', '-fomit-frame-pointer', '-O3', '-DNDEBUG ', '-DNDEBUG']
17 excludedOptionsPrefix = '-W'
18
19
20
21 def GetHeaderFiles(root):
22     headers = []
23     for path in os.listdir(root):
24         if not os.path.isdir(os.path.join(root, path)) \
25             and os.path.splitext(path)[1] in ['.h', '.hpp'] \
26             and not path in excludedHeaders:
27             headers.append(os.path.join(root, path))
28     return sorted(headers)
29
30
31
32 def GetClasses(root, prefix):
33     classes = []
34     if ('' != prefix):
35         prefix = prefix + '.'
36     for path in os.listdir(root):
37         currentPath = os.path.join(root, path)
38         if (os.path.isdir(currentPath)):
39             classes += GetClasses(currentPath, prefix + path)
40         else:
41             name = str.split(path, '.')[0]
42             ext = str.split(path, '.')[1]
43             if (ext == 'class'):
44                 classes.append(prefix + name)
45     return classes
46
47
48
49 def GetJavaHHeaders():
50     print('\nGenerating JNI headers for Java API ...')
51
52     javahHeaders = os.path.join(managerDir, 'javah_generated_headers')
53     if os.path.exists(javahHeaders):
54         rmtree(javahHeaders)
55     os.makedirs(os.path.join(os.getcwd(), javahHeaders))
56
57     AndroidJavaDeps = os.path.join(SDK_path, 'platforms/android-11/android.jar')
58
59     classPath = os.path.join(managerDir, 'sdk/java/bin/classes')
60     if not os.path.exists(classPath):
61         print('Error: no Java classes found in \'%s\'' % classPath)
62         quit()
63
64     allJavaClasses = GetClasses(classPath, '')
65     if not allJavaClasses:
66         print('Error: no Java classes found')
67         quit()
68
69     for currentClass in allJavaClasses:
70         os.system('javah -d %s -classpath %s:%s %s' % (javahHeaders, classPath, \
71             AndroidJavaDeps, currentClass))
72
73     print('\nBuilding JNI headers list ...')
74     jniHeaders = GetHeaderFiles(javahHeaders)
75
76     return jniHeaders
77
78
79
80 def GetImmediateSubdirs(dir):
81     return [name for name in os.listdir(dir)
82             if os.path.isdir(os.path.join(dir, name))]
83
84
85
86 def GetOpenCVModules():
87     makefile = open(os.path.join(managerDir, 'sdk/native/jni/OpenCV.mk'), 'r')
88     makefileStr = makefile.read()
89     left = makefileStr.find('OPENCV_MODULES:=') + len('OPENCV_MODULES:=')
90     right = makefileStr[left:].find('\n')
91     modules = makefileStr[left:left+right].split()
92     modules = filter(lambda x: x != 'ts' and x != 'androidcamera', modules)
93     return modules
94
95
96
97 def FindHeaders():
98     headers = []
99
100     print('\nBuilding Native OpenCV header list ...')
101
102     cppHeadersFolder = os.path.join(managerDir, 'sdk/native/jni/include/opencv2')
103
104     modulesFolders = GetImmediateSubdirs(cppHeadersFolder)
105     modules = GetOpenCVModules()
106
107     cppHeaders = []
108     for m in modules:
109         for f in modulesFolders:
110             moduleHeaders = []
111             if f == m:
112                 moduleHeaders += GetHeaderFiles(os.path.join(cppHeadersFolder, f))
113                 if m == 'flann':
114                     flann = os.path.join(cppHeadersFolder, f, 'flann.hpp')
115                     moduleHeaders.remove(flann)
116                     moduleHeaders.insert(0, flann)
117                 cppHeaders += moduleHeaders
118
119
120     cppHeaders += GetHeaderFiles(cppHeadersFolder)
121     headers += cppHeaders
122
123     cHeaders = GetHeaderFiles(os.path.join(managerDir, \
124         'sdk/native/jni/include/opencv'))
125     headers += cHeaders
126
127     headers += GetJavaHHeaders()
128
129     return headers
130
131
132
133 def FindLibraries():
134     libraries = []
135     for lib in targetLibs:
136         libraries.append(os.path.join(managerDir, 'sdk/native/libs', architecture, lib))
137     return libraries
138
139
140
141 def FindIncludes():
142     includes = [os.path.join(managerDir, 'sdk', 'native', 'jni', 'include'),
143         os.path.join(managerDir, 'sdk', 'native', 'jni', 'include', 'opencv'),
144         os.path.join(managerDir, 'sdk', 'native', 'jni', 'include', 'opencv2')]
145
146     for inc in systemIncludes:
147         includes.append(os.path.join(NDK_path, inc))
148
149     return includes
150
151
152
153 def FilterGCCOptions():
154     gcc = filter(lambda x: not x.startswith(excludedOptionsPrefix), gcc_options)
155     return sorted(gcc)
156
157
158
159 def WriteXml(version, headers, includes, libraries):
160     xmlName = version + '.xml'
161
162     print '\noutput file: ' + xmlName
163     try:
164         xml = open(xmlName, 'w')
165     except:
166         print 'Error: Cannot open output file "%s" for writing' % xmlName
167         quit()
168
169     xml.write('<descriptor>')
170
171     xml.write('\n\n<version>')
172     xml.write('\n\t%s' % version)
173     xml.write('\n</version>')
174
175     xml.write('\n\n<headers>')
176     xml.write('\n\t%s' % '\n\t'.join(headers))
177     xml.write('\n</headers>')
178
179     xml.write('\n\n<include_paths>')
180     xml.write('\n\t%s' % '\n\t'.join(includes))
181     xml.write('\n</include_paths>')
182
183     # TODO: uncomment when Eigen problem is solved
184     # xml.write('\n\n<include_preamble>')
185     # xml.write('\n\t%s' % '\n\t'.join(preamble))
186     # xml.write('\n</include_preamble>')
187
188     xml.write('\n\n<libs>')
189     xml.write('\n\t%s' % '\n\t'.join(libraries))
190     xml.write('\n</libs>')
191
192     xml.write('\n\n<gcc_options>')
193     xml.write('\n\t%s' % '\n\t'.join(gcc_options))
194     xml.write('\n</gcc_options>')
195
196     xml.write('\n\n</descriptor>')
197
198
199
200 if __name__ == '__main__':
201     usage = '%prog <OpenCV_Manager install directory> <OpenCV_Manager version>'
202     parser = OptionParser(usage = usage)
203
204     args = parser.parse_args()
205     if 2 != len(args):
206         parser.print_help()
207         quit()
208
209     managerDir = args[1][0]
210     version = args[1][1]
211
212     NDK_path = '/opt/android-ndk-r8c'
213     print '\nUsing Android NDK from "%s"' % NDK_path
214
215     SDK_path = '~/NVPACK/android-sdk-linux'
216     print '\nUsing Android SDK from "%s"' % SDK_path
217
218     headers = FindHeaders()
219
220     includes = FindIncludes()
221
222     libraries = FindLibraries()
223
224     gcc_options = FilterGCCOptions()
225
226     WriteXml(version, headers, includes, libraries)