Zonr Chang | c383a50 | 2010-10-12 01:52:08 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2010, The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 17 | #include "slang_rs_reflect_utils.h" |
Ying Wang | 3f8b44d | 2010-09-04 01:17:01 -0700 | [diff] [blame] | 18 | |
Ying Wang | 3f8b44d | 2010-09-04 01:17:01 -0700 | [diff] [blame] | 19 | #include <cstdio> |
Mike Lockwood | b1980a2 | 2010-09-08 10:20:40 -0400 | [diff] [blame] | 20 | #include <cstring> |
Ying Wang | 3f8b44d | 2010-09-04 01:17:01 -0700 | [diff] [blame] | 21 | |
Zonr Chang | 8c6d9b2 | 2010-10-07 18:01:19 +0800 | [diff] [blame] | 22 | #include "llvm/ADT/StringRef.h" |
| 23 | |
| 24 | #include "slang_utils.h" |
Ying Wang | 3f8b44d | 2010-09-04 01:17:01 -0700 | [diff] [blame] | 25 | |
| 26 | namespace slang { |
| 27 | |
| 28 | using std::string; |
| 29 | |
Ying Wang | dba3111 | 2010-10-07 17:30:38 -0700 | [diff] [blame] | 30 | string RSSlangReflectUtils::GetFileNameStem(const char* fileName) { |
| 31 | const char *dot = fileName + strlen(fileName); |
| 32 | const char *slash = dot - 1; |
| 33 | while (slash >= fileName) { |
| 34 | if (*slash == '/') { |
| 35 | break; |
| 36 | } |
| 37 | if ((*slash == '.') && (*dot == 0)) { |
| 38 | dot = slash; |
| 39 | } |
| 40 | --slash; |
| 41 | } |
| 42 | ++slash; |
| 43 | return string(slash, dot - slash); |
| 44 | } |
| 45 | |
Ying Wang | 3f8b44d | 2010-09-04 01:17:01 -0700 | [diff] [blame] | 46 | string RSSlangReflectUtils::ComputePackagedPath( |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 47 | const char *prefixPath, const char *packageName) { |
Ying Wang | 3f8b44d | 2010-09-04 01:17:01 -0700 | [diff] [blame] | 48 | string packaged_path(prefixPath); |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 49 | if (!packaged_path.empty() && |
| 50 | (packaged_path[packaged_path.length() - 1] != '/')) { |
Ying Wang | 3f8b44d | 2010-09-04 01:17:01 -0700 | [diff] [blame] | 51 | packaged_path += "/"; |
| 52 | } |
| 53 | size_t s = packaged_path.length(); |
| 54 | packaged_path += packageName; |
| 55 | while (s < packaged_path.length()) { |
| 56 | if (packaged_path[s] == '.') { |
| 57 | packaged_path[s] = '/'; |
| 58 | } |
| 59 | ++s; |
| 60 | } |
| 61 | return packaged_path; |
| 62 | } |
| 63 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 64 | static string InternalFileNameConvert(const char *rsFileName, bool toLower) { |
| 65 | const char *dot = rsFileName + strlen(rsFileName); |
| 66 | const char *slash = dot - 1; |
Ying Wang | 3f8b44d | 2010-09-04 01:17:01 -0700 | [diff] [blame] | 67 | while (slash >= rsFileName) { |
| 68 | if (*slash == '/') { |
| 69 | break; |
| 70 | } |
| 71 | if ((*slash == '.') && (*dot == 0)) { |
| 72 | dot = slash; |
| 73 | } |
| 74 | --slash; |
| 75 | } |
| 76 | ++slash; |
| 77 | char ret[256]; |
Ying Wang | 3f8b44d | 2010-09-04 01:17:01 -0700 | [diff] [blame] | 78 | int i = 0; |
| 79 | for (; (i < 255) && (slash < dot); ++slash) { |
Stephen Hines | d417640 | 2010-09-16 17:14:35 -0700 | [diff] [blame] | 80 | if (isalnum(*slash) || *slash == '_') { |
| 81 | if (toLower) { |
| 82 | ret[i] = tolower(*slash); |
Ying Wang | 3f8b44d | 2010-09-04 01:17:01 -0700 | [diff] [blame] | 83 | } else { |
| 84 | ret[i] = *slash; |
| 85 | } |
Ying Wang | 3f8b44d | 2010-09-04 01:17:01 -0700 | [diff] [blame] | 86 | ++i; |
Ying Wang | 3f8b44d | 2010-09-04 01:17:01 -0700 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | ret[i] = 0; |
| 90 | return string(ret); |
| 91 | } |
| 92 | |
| 93 | std::string RSSlangReflectUtils::JavaClassNameFromRSFileName( |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 94 | const char *rsFileName) { |
Stephen Hines | d417640 | 2010-09-16 17:14:35 -0700 | [diff] [blame] | 95 | return InternalFileNameConvert(rsFileName, false); |
Ying Wang | 3f8b44d | 2010-09-04 01:17:01 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | |
| 99 | std::string RSSlangReflectUtils::BCFileNameFromRSFileName( |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 100 | const char *rsFileName) { |
Stephen Hines | d417640 | 2010-09-16 17:14:35 -0700 | [diff] [blame] | 101 | return InternalFileNameConvert(rsFileName, true); |
Ying Wang | 3f8b44d | 2010-09-04 01:17:01 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Ying Wang | 0877f05 | 2010-09-09 17:19:33 -0700 | [diff] [blame] | 104 | static bool GenerateAccessorHeader( |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 105 | const RSSlangReflectUtils::BitCodeAccessorContext &context, FILE *pfout) { |
Ying Wang | 0877f05 | 2010-09-09 17:19:33 -0700 | [diff] [blame] | 106 | fprintf(pfout, "/*\n"); |
| 107 | fprintf(pfout, " * This file is auto-generated. DO NOT MODIFY!\n"); |
| 108 | fprintf(pfout, " * The source RenderScript file: %s\n", context.rsFileName); |
| 109 | fprintf(pfout, " */\n\n"); |
| 110 | fprintf(pfout, "package %s;\n\n", context.packageName); |
Ying Wang | 3f8b44d | 2010-09-04 01:17:01 -0700 | [diff] [blame] | 111 | |
Ying Wang | 0877f05 | 2010-09-09 17:19:33 -0700 | [diff] [blame] | 112 | // add imports here. |
| 113 | |
| 114 | return true; |
| 115 | } |
| 116 | |
| 117 | static bool GenerateAccessorMethodSignature( |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 118 | const RSSlangReflectUtils::BitCodeAccessorContext &context, FILE *pfout) { |
Ying Wang | 0877f05 | 2010-09-09 17:19:33 -0700 | [diff] [blame] | 119 | // the prototype of the accessor method |
| 120 | fprintf(pfout, " // return byte array representation of the bitcode.\n"); |
| 121 | fprintf(pfout, " public static byte[] getBitCode() {\n"); |
| 122 | return true; |
| 123 | } |
| 124 | |
| 125 | // Java method size must not exceed 64k, |
| 126 | // so we have to split the bitcode into multiple segments. |
| 127 | static bool GenerateSegmentMethod( |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 128 | const char *buff, int blen, int seg_num, FILE *pfout) { |
Ying Wang | 0877f05 | 2010-09-09 17:19:33 -0700 | [diff] [blame] | 129 | |
| 130 | fprintf(pfout, " private static byte[] getSegment_%d() {\n", seg_num); |
| 131 | fprintf(pfout, " byte[] data = {\n"); |
| 132 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 133 | static const int LINE_BYTE_NUM = 16; |
Ying Wang | 0877f05 | 2010-09-09 17:19:33 -0700 | [diff] [blame] | 134 | char out_line[LINE_BYTE_NUM*6 + 10]; |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 135 | const char *out_line_end = out_line + sizeof(out_line); |
| 136 | char *p = out_line; |
Ying Wang | 0877f05 | 2010-09-09 17:19:33 -0700 | [diff] [blame] | 137 | |
| 138 | int write_length = 0; |
| 139 | while (write_length < blen) { |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 140 | p += snprintf(p, out_line_end - p, |
| 141 | " %4d,", static_cast<int>(buff[write_length])); |
Ying Wang | 0877f05 | 2010-09-09 17:19:33 -0700 | [diff] [blame] | 142 | ++write_length; |
| 143 | if (((write_length % LINE_BYTE_NUM) == 0) |
| 144 | || (write_length == blen)) { |
| 145 | fprintf(pfout, " "); |
Stephen Hines | 24f9b09 | 2010-10-06 12:58:40 -0700 | [diff] [blame] | 146 | fprintf(pfout, "%s", out_line); |
Ying Wang | 0877f05 | 2010-09-09 17:19:33 -0700 | [diff] [blame] | 147 | fprintf(pfout, "\n"); |
| 148 | p = out_line; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | fprintf(pfout, " };\n"); |
| 153 | fprintf(pfout, " return data;\n"); |
| 154 | fprintf(pfout, " }\n\n"); |
| 155 | |
| 156 | return true; |
| 157 | } |
| 158 | |
| 159 | static bool GenerateJavaCodeAccessorMethod( |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 160 | const RSSlangReflectUtils::BitCodeAccessorContext &context, FILE *pfout) { |
| 161 | FILE *pfin = fopen(context.bcFileName, "rb"); |
Ying Wang | 3f8b44d | 2010-09-04 01:17:01 -0700 | [diff] [blame] | 162 | if (pfin == NULL) { |
Ying Wang | 0877f05 | 2010-09-09 17:19:33 -0700 | [diff] [blame] | 163 | fprintf(stderr, "Error: could not read file %s\n", context.bcFileName); |
Ying Wang | 3f8b44d | 2010-09-04 01:17:01 -0700 | [diff] [blame] | 164 | return false; |
| 165 | } |
| 166 | |
Ying Wang | 0877f05 | 2010-09-09 17:19:33 -0700 | [diff] [blame] | 167 | // start the accessor method |
| 168 | GenerateAccessorMethodSignature(context, pfout); |
| 169 | fprintf(pfout, " return getBitCodeInternal();\n"); |
| 170 | // end the accessor method |
| 171 | fprintf(pfout, " };\n\n"); |
| 172 | |
| 173 | // output the data |
| 174 | // make sure the generated function for a segment won't break the Javac |
| 175 | // size limitation (64K). |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 176 | static const int SEG_SIZE = 0x2000; |
| 177 | char *buff = new char[SEG_SIZE]; |
Ying Wang | 0877f05 | 2010-09-09 17:19:33 -0700 | [diff] [blame] | 178 | int read_length; |
| 179 | int seg_num = 0; |
| 180 | int total_length = 0; |
| 181 | while ((read_length = fread(buff, 1, SEG_SIZE, pfin)) > 0) { |
| 182 | GenerateSegmentMethod(buff, read_length, seg_num, pfout); |
| 183 | ++seg_num; |
| 184 | total_length += read_length; |
| 185 | } |
| 186 | delete []buff; |
| 187 | fclose(pfin); |
| 188 | |
| 189 | // output the internal accessor method |
| 190 | fprintf(pfout, " private static int bitCodeLength = %d;\n\n", total_length); |
| 191 | fprintf(pfout, " private static byte[] getBitCodeInternal() {\n"); |
| 192 | fprintf(pfout, " byte[] bc = new byte[bitCodeLength];\n"); |
| 193 | fprintf(pfout, " int offset = 0;\n"); |
| 194 | fprintf(pfout, " byte[] seg;\n"); |
| 195 | for (int i = 0; i < seg_num; ++i) { |
| 196 | fprintf(pfout, " seg = getSegment_%d();\n", i); |
| 197 | fprintf(pfout, " System.arraycopy(seg, 0, bc, offset, seg.length);\n"); |
| 198 | fprintf(pfout, " offset += seg.length;\n"); |
| 199 | } |
| 200 | fprintf(pfout, " return bc;\n"); |
| 201 | fprintf(pfout, " }\n\n"); |
| 202 | |
| 203 | return true; |
| 204 | } |
| 205 | |
| 206 | static bool GenerateAccessorClass( |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 207 | const RSSlangReflectUtils::BitCodeAccessorContext &context, |
| 208 | const char *clazz_name, FILE *pfout) { |
Ying Wang | 0877f05 | 2010-09-09 17:19:33 -0700 | [diff] [blame] | 209 | // begin the class. |
| 210 | fprintf(pfout, "/**\n"); |
| 211 | fprintf(pfout, " * @hide\n"); |
| 212 | fprintf(pfout, " */\n"); |
| 213 | fprintf(pfout, "public class %s {\n", clazz_name); |
| 214 | fprintf(pfout, "\n"); |
| 215 | |
| 216 | bool ret = true; |
| 217 | switch (context.bcStorage) { |
| 218 | case BCST_APK_RESOURCE: |
| 219 | break; |
| 220 | case BCST_JAVA_CODE: |
| 221 | ret = GenerateJavaCodeAccessorMethod(context, pfout); |
| 222 | break; |
| 223 | default: |
| 224 | ret = false; |
Ying Wang | 3f8b44d | 2010-09-04 01:17:01 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Ying Wang | 0877f05 | 2010-09-09 17:19:33 -0700 | [diff] [blame] | 227 | // end the class. |
| 228 | fprintf(pfout, "}\n"); |
| 229 | |
| 230 | return ret; |
| 231 | } |
| 232 | |
| 233 | |
| 234 | bool RSSlangReflectUtils::GenerateBitCodeAccessor( |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 235 | const BitCodeAccessorContext &context) { |
Ying Wang | 0877f05 | 2010-09-09 17:19:33 -0700 | [diff] [blame] | 236 | string output_path = ComputePackagedPath(context.reflectPath, |
| 237 | context.packageName); |
Zonr Chang | 8c6d9b2 | 2010-10-07 18:01:19 +0800 | [diff] [blame] | 238 | if (!SlangUtils::CreateDirectoryWithParents(llvm::StringRef(output_path), |
| 239 | NULL)) { |
Ying Wang | 0877f05 | 2010-09-09 17:19:33 -0700 | [diff] [blame] | 240 | fprintf(stderr, "Error: could not create dir %s\n", |
| 241 | output_path.c_str()); |
| 242 | return false; |
| 243 | } |
| 244 | |
| 245 | string clazz_name(JavaClassNameFromRSFileName(context.rsFileName)); |
Ying Wang | 3f8b44d | 2010-09-04 01:17:01 -0700 | [diff] [blame] | 246 | clazz_name += "BitCode"; |
| 247 | string filename(clazz_name); |
| 248 | filename += ".java"; |
| 249 | |
| 250 | string output_filename(output_path); |
| 251 | output_filename += "/"; |
| 252 | output_filename += filename; |
| 253 | printf("Generating %s ...\n", filename.c_str()); |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 254 | FILE *pfout = fopen(output_filename.c_str(), "w"); |
Ying Wang | 3f8b44d | 2010-09-04 01:17:01 -0700 | [diff] [blame] | 255 | if (pfout == NULL) { |
Ying Wang | 0877f05 | 2010-09-09 17:19:33 -0700 | [diff] [blame] | 256 | fprintf(stderr, "Error: could not write to file %s\n", |
| 257 | output_filename.c_str()); |
Ying Wang | 3f8b44d | 2010-09-04 01:17:01 -0700 | [diff] [blame] | 258 | return false; |
| 259 | } |
| 260 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 261 | bool ret = GenerateAccessorHeader(context, pfout) && |
| 262 | GenerateAccessorClass(context, clazz_name.c_str(), pfout); |
Ying Wang | 3f8b44d | 2010-09-04 01:17:01 -0700 | [diff] [blame] | 263 | |
Ying Wang | 3f8b44d | 2010-09-04 01:17:01 -0700 | [diff] [blame] | 264 | fclose(pfout); |
Ying Wang | 0877f05 | 2010-09-09 17:19:33 -0700 | [diff] [blame] | 265 | return ret; |
Ying Wang | 3f8b44d | 2010-09-04 01:17:01 -0700 | [diff] [blame] | 266 | } |
Ying Wang | 3f8b44d | 2010-09-04 01:17:01 -0700 | [diff] [blame] | 267 | } |