build/buildutils/generates60lookup.py
branchRCL_3
changeset 19 04becd199f91
equal deleted inserted replaced
16:f5050f1da672 19:04becd199f91
       
     1 #! /usr/bin/python
       
     2 #
       
     3 # Copyright (c) 2009,2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 # All rights reserved.
       
     5 # This component and the accompanying materials are made available
       
     6 # under the terms of "Eclipse Public License v1.0"
       
     7 # which accompanies this distribution, and is available
       
     8 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     9 #
       
    10 # Initial Contributors:
       
    11 # Nokia Corporation - initial contribution.
       
    12 #
       
    13 # Contributors:
       
    14 #
       
    15 # Description: 
       
    16 #
       
    17 
       
    18 import sys, traceback, os, re, os.path
       
    19 from optparse import OptionParser
       
    20 
       
    21 # JNIEXPORT void JNICALL Java_com_...... (JNIEnv *, ....);
       
    22 RE_JNIDECL = re.compile(r"JNIEXPORT\s+\w+\s+JNICALL\s+(\w+)\s+ \([\s\w,\*]+\);")
       
    23 
       
    24 def getJniFunctions(javah):
       
    25     ''' Reads all .h files from hDir and returns the header file names and 
       
    26         JNI functions found on the file '''
       
    27 
       
    28     if os.path.isdir(javah):
       
    29         # Find all headers in javah
       
    30         headers = [os.path.join(javah, h) 
       
    31                    for h in os.listdir(javah) if h.endswith(".h")]
       
    32 
       
    33     elif os.path.isfile(javah):
       
    34         headers = [javah]
       
    35 
       
    36     else:
       
    37         raise Exception("Could not find %s" % javah)
       
    38 
       
    39     # Find all JNI functions in header files
       
    40     jniFunctions = []
       
    41     for headerFile in headers:
       
    42         content = open(headerFile).read()
       
    43         jniFunctions.extend(RE_JNIDECL.findall(content))
       
    44 
       
    45     return [os.path.basename(h) for h in headers], jniFunctions
       
    46 
       
    47 def writeLookup(lookupFile, headers, jniFunctions, jxeLookup):
       
    48     ''' Writes the lookupfile, generated from given headers and jni functions '''
       
    49 
       
    50     # File header
       
    51     content = r'''/*
       
    52  * Description: Automatically generated JNI lookup file. Do not modify manually.
       
    53  */
       
    54 
       
    55 #include "javasymbianoslayer.h"
       
    56 typedef void (*TFunc)();
       
    57 '''
       
    58 
       
    59     if jxeLookup:
       
    60         # Add the J9GetJXE getter as part of JNI functions
       
    61         jniFunctions.append("J9GetJXE")
       
    62 
       
    63         content = content + r'''
       
    64 #include "j9comp.h"
       
    65 extern "C" {
       
    66     extern const U_64 j9Jar[];
       
    67 
       
    68     void* J9GetJXE() {
       
    69         return const_cast<void*>(reinterpret_cast<const void*>(j9Jar));
       
    70     }
       
    71 }
       
    72 '''
       
    73 
       
    74     # Header includes
       
    75     content = content + "\n".join(['#include "%s"' % h for h in headers])
       
    76 
       
    77     # Sorted lookup table
       
    78     content = content + "\nconst FuncTable funcTable[] = {\n"
       
    79     content = content + ",\n".join(['   { "%s", (unsigned int) %s}' % 
       
    80                                     (jni, jni) for jni in sorted(jniFunctions)])
       
    81     content = content + "\n};\n"
       
    82 
       
    83     # Jnilookup method
       
    84     content = content + r'''
       
    85 IMPORT_C TFunc jni_lookup(const char* name);
       
    86 EXPORT_C TFunc jni_lookup(const char* name) {
       
    87     return (TFunc)findMethod(name, funcTable, sizeof(funcTable)/sizeof(FuncTable));
       
    88 }
       
    89 '''
       
    90 
       
    91     # Write file
       
    92     open(lookupFile, "w").write(content)
       
    93 
       
    94 
       
    95 def main():
       
    96     parser = OptionParser(usage = "%prog [options] javah-file-or-dir lookup-source.cpp")
       
    97     parser.add_option("--nojxe", dest = "jxeLookup",
       
    98                       help = "Do not generate JXE lookup code",
       
    99                       action = "store_false", default = True)
       
   100     (options, args) = parser.parse_args()
       
   101     try:
       
   102         javah, lookupFile = args
       
   103 
       
   104         headers, jniFunctions = getJniFunctions(javah)
       
   105         writeLookup(lookupFile, headers, jniFunctions, options.jxeLookup)
       
   106         
       
   107     except:
       
   108         print "Error during lookup file generation!"
       
   109         traceback.print_exc()
       
   110         sys.exit(-1)
       
   111 
       
   112 if __name__ == "__main__":
       
   113     main()