|
1 # |
|
2 # Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 # All rights reserved. |
|
4 # This component and the accompanying materials are made available |
|
5 # under the terms of "Eclipse Public License v1.0" |
|
6 # which accompanies this distribution, and is available |
|
7 # at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 # |
|
9 # Initial Contributors: |
|
10 # Nokia Corporation - initial contribution. |
|
11 # |
|
12 # Contributors: |
|
13 # |
|
14 # Description: |
|
15 # |
|
16 #! /usr/bin/python |
|
17 |
|
18 import sys |
|
19 import traceback |
|
20 import os |
|
21 import os.path |
|
22 import sys |
|
23 import subprocess |
|
24 import shutil |
|
25 import re |
|
26 import types |
|
27 import zipfile |
|
28 |
|
29 from os.path import abspath |
|
30 |
|
31 """ |
|
32 #ifdef __JAVA_API_ENHANCEMENTS_SECURITY |
|
33 #if defined (CHAPI_SUPPORT) |
|
34 #ifdef __JAVA_API_ENHANCEMENTS_SECURITY |
|
35 #ifdef RD_JAVA_VOLUME_CONTROL |
|
36 #ifdef BLUETOOTH11 |
|
37 #ifdef BLUETOOTH10 |
|
38 #ifdef MIDP20BUILD |
|
39 #ifdef TESTBUILD |
|
40 #ifdef LCDUI_DEBUG |
|
41 #ifdef TRACE_GRAPHICS |
|
42 #ifdef GRAPHICS_BUFFER_DRAWPIXELS |
|
43 #ifndef XDEBUG |
|
44 #ifdef DEBUG |
|
45 #ifdef RD_JAVA_OMA_DRM_V2 |
|
46 #ifdef _APIEXT_SECURITY_ |
|
47 #ifdef BLUETOOTH_MASTER_SUPPORT |
|
48 #ifdef RD_JAVA_VMPORT_LAYER_IN_USE |
|
49 #ifdef RD_JAVA_LC |
|
50 |
|
51 |
|
52 """ |
|
53 |
|
54 def main(): |
|
55 try: |
|
56 source = sys.argv[1] |
|
57 destination = sys.argv[2] |
|
58 print "source = "+source+", destination = "+destination |
|
59 javaCpp(source, destination) |
|
60 except: |
|
61 print "Error during javaCpp " |
|
62 traceback.print_exc() |
|
63 sys.exit(-1) |
|
64 |
|
65 |
|
66 def javaCpp(sourceDirs, destinationDir): |
|
67 |
|
68 # Find all .java-files to preprocess. If any two or more files represent |
|
69 # the same class, discard the ones specified first using jsourcetree. |
|
70 dirs = sourceDirs.strip().split(":") |
|
71 #dirs = ["/s60/mw/java/javalegacy/j2me/components/lcduib/src/epoc/classes", |
|
72 # "/s60/mw/java/javaextensions/supplements/nokialcdui/javasrc" |
|
73 # ] |
|
74 allJavas = {} |
|
75 for d in dirs: |
|
76 # Get all .java-files under sourcetree |
|
77 print "DIR = "+d |
|
78 javas = getJavaFiles(d) |
|
79 |
|
80 for file in javas: |
|
81 allJavas[file] = os.path.abspath(os.path.join(d, file)) |
|
82 |
|
83 for file in allJavas.keys(): |
|
84 # File name for preprocessed Java file |
|
85 cppFile = os.path.join(destinationDir, file) |
|
86 |
|
87 # File name to original Java file |
|
88 javaFile = allJavas[file] |
|
89 |
|
90 if not uptodate(javaFile, cppFile): |
|
91 doCpp(javaFile, cppFile) |
|
92 |
|
93 |
|
94 def getJavaFiles(javasDir): |
|
95 startDir = os.path.abspath(javasDir) |
|
96 |
|
97 files = [] |
|
98 suffix = ".java" |
|
99 |
|
100 def callBack(arg, dirname, files): |
|
101 # Remove startDir-part from dirname |
|
102 dirname = dirname[len(startDir) + 1:] |
|
103 for file in files: |
|
104 if file.endswith(suffix): |
|
105 # replace '\' with '/' and strip the class name. |
|
106 res = os.path.join(dirname, file) |
|
107 arg.append(res) |
|
108 |
|
109 os.path.walk(startDir, callBack, files) |
|
110 |
|
111 return files |
|
112 |
|
113 |
|
114 def doCpp( |
|
115 srcFile, |
|
116 dstFile): |
|
117 |
|
118 macros = [ |
|
119 'JDEBUG(aMessage)=', |
|
120 'JDEBUG_STATIC(aMessage)=', |
|
121 'JPRINT(aMessage)=', |
|
122 'JASSERT(aAssertion)=', |
|
123 'JASSERT_STATIC(aAssertion)=', |
|
124 'JASSERT_TXT(aAssertion,aMessage)=', |
|
125 'JASSERT_TXT_STATIC(aAssertion,aMessage)=', |
|
126 'JSOURCEDEBUG(aSourceCode)=', |
|
127 'RD_JAVA_OMJ', |
|
128 'RD_JAVA_OMA_DRM_V2', |
|
129 'RD_JAVA_VOLUME_CONTROL' |
|
130 ] |
|
131 |
|
132 if not os.path.exists(dstFile): |
|
133 makeDirs(dstFile, isFile = True) |
|
134 |
|
135 # Use cpp defined on environment CPPCMD, as the cpp/gcc varies between symsee, abld, sbs, |
|
136 # linux, ... |
|
137 if "CPPCMD" in os.environ: |
|
138 cmd = os.environ["CPPCMD"] |
|
139 |
|
140 else: |
|
141 cmd = " ".join([ |
|
142 'gcc', # Generic GCC |
|
143 '-E', # Preprocess only |
|
144 '-undef', # Do not predefine any macros |
|
145 '-P', # Do not generate #line directives |
|
146 '-x', 'assembler-with-cpp', # Language as C++ |
|
147 '-w', # No warnings |
|
148 '-traditional-cpp']) # Use traditional precompilation |
|
149 |
|
150 cmd = cmd + " " + " ".join( |
|
151 ["-D" + m for m in macros] + |
|
152 [srcFile, "-o", dstFile]) |
|
153 |
|
154 doExecute(cmd) |
|
155 |
|
156 def doExecute( |
|
157 cmd, |
|
158 *args, |
|
159 **kwd): |
|
160 """Any arguments are passed directly to subprocess.call(). """ |
|
161 |
|
162 |
|
163 # Construct string to be displayed on screen |
|
164 """ |
|
165 msg = "" |
|
166 if kwd.has_key('cwd'): |
|
167 msg = "[" + kwd['cwd'] + "] " |
|
168 if type(cmd) == types.StringType: |
|
169 msg += cmd |
|
170 elif type(cmd) == types.ListType: |
|
171 msg += " ".join(cmd) |
|
172 else: |
|
173 raise Error, "Invalid type %s for cmd argument" % type(cmd) |
|
174 sys.stderr.write("cmd = %s\n" % msg) |
|
175 """ |
|
176 kwd['args'] = cmd |
|
177 |
|
178 process = subprocess.Popen(*args, **kwd) |
|
179 return |
|
180 |
|
181 def uptodate(source, target): |
|
182 |
|
183 if not os.path.exists(target): |
|
184 return False |
|
185 |
|
186 try: |
|
187 fileTimeSrc = os.path.getmtime(source) |
|
188 fileTimeDst = os.path.getmtime(target) |
|
189 if fileTimeSrc > fileTimeDst : |
|
190 result = False |
|
191 else: |
|
192 result = True |
|
193 |
|
194 except: |
|
195 # One or more files don't exist |
|
196 result = False |
|
197 |
|
198 return result |
|
199 |
|
200 def makeDirs(directory, isFile = False): |
|
201 """ Creates a directory if it doesn't exist. |
|
202 |
|
203 Doesn't do anything if the directory already exists. |
|
204 |
|
205 If isFile is True, the last element of the directory path is assumed to be |
|
206 a file and is discarded from the path to be created. """ |
|
207 |
|
208 if isFile: |
|
209 directory = os.path.dirname(directory) |
|
210 |
|
211 try: |
|
212 os.makedirs(directory) |
|
213 except: |
|
214 pass |
|
215 |
|
216 if __name__ == "__main__": |
|
217 main() |