blob: 098f20eb4e593de458dcb3728aa3f1ed2de1f73d [file] [log] [blame]
Zonr Changc383a502010-10-12 01:52:08 +08001/*
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
zonr6315f762010-10-05 15:35:14 +080017#include "slang_rs_reflect_utils.h"
Ying Wang3f8b44d2010-09-04 01:17:01 -070018
Ying Wang3f8b44d2010-09-04 01:17:01 -070019#include <cstdio>
Mike Lockwoodb1980a22010-09-08 10:20:40 -040020#include <cstring>
Ying Wang3f8b44d2010-09-04 01:17:01 -070021
Zonr Chang8c6d9b22010-10-07 18:01:19 +080022#include "llvm/ADT/StringRef.h"
23
24#include "slang_utils.h"
Ying Wang3f8b44d2010-09-04 01:17:01 -070025
26namespace slang {
27
28using std::string;
29
Ying Wangdba31112010-10-07 17:30:38 -070030string 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 Wang3f8b44d2010-09-04 01:17:01 -070046string RSSlangReflectUtils::ComputePackagedPath(
zonr6315f762010-10-05 15:35:14 +080047 const char *prefixPath, const char *packageName) {
Ying Wang3f8b44d2010-09-04 01:17:01 -070048 string packaged_path(prefixPath);
zonr6315f762010-10-05 15:35:14 +080049 if (!packaged_path.empty() &&
50 (packaged_path[packaged_path.length() - 1] != '/')) {
Ying Wang3f8b44d2010-09-04 01:17:01 -070051 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
zonr6315f762010-10-05 15:35:14 +080064static string InternalFileNameConvert(const char *rsFileName, bool toLower) {
65 const char *dot = rsFileName + strlen(rsFileName);
66 const char *slash = dot - 1;
Ying Wang3f8b44d2010-09-04 01:17:01 -070067 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 Wang3f8b44d2010-09-04 01:17:01 -070078 int i = 0;
79 for (; (i < 255) && (slash < dot); ++slash) {
Stephen Hinesd4176402010-09-16 17:14:35 -070080 if (isalnum(*slash) || *slash == '_') {
81 if (toLower) {
82 ret[i] = tolower(*slash);
Ying Wang3f8b44d2010-09-04 01:17:01 -070083 } else {
84 ret[i] = *slash;
85 }
Ying Wang3f8b44d2010-09-04 01:17:01 -070086 ++i;
Ying Wang3f8b44d2010-09-04 01:17:01 -070087 }
88 }
89 ret[i] = 0;
90 return string(ret);
91}
92
93std::string RSSlangReflectUtils::JavaClassNameFromRSFileName(
zonr6315f762010-10-05 15:35:14 +080094 const char *rsFileName) {
Stephen Hinesd4176402010-09-16 17:14:35 -070095 return InternalFileNameConvert(rsFileName, false);
Ying Wang3f8b44d2010-09-04 01:17:01 -070096}
97
98
99std::string RSSlangReflectUtils::BCFileNameFromRSFileName(
zonr6315f762010-10-05 15:35:14 +0800100 const char *rsFileName) {
Stephen Hinesd4176402010-09-16 17:14:35 -0700101 return InternalFileNameConvert(rsFileName, true);
Ying Wang3f8b44d2010-09-04 01:17:01 -0700102}
103
Ying Wang0877f052010-09-09 17:19:33 -0700104static bool GenerateAccessorHeader(
zonr6315f762010-10-05 15:35:14 +0800105 const RSSlangReflectUtils::BitCodeAccessorContext &context, FILE *pfout) {
Ying Wang0877f052010-09-09 17:19:33 -0700106 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 Wang3f8b44d2010-09-04 01:17:01 -0700111
Ying Wang0877f052010-09-09 17:19:33 -0700112 // add imports here.
113
114 return true;
115}
116
117static bool GenerateAccessorMethodSignature(
zonr6315f762010-10-05 15:35:14 +0800118 const RSSlangReflectUtils::BitCodeAccessorContext &context, FILE *pfout) {
Ying Wang0877f052010-09-09 17:19:33 -0700119 // 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.
127static bool GenerateSegmentMethod(
zonr6315f762010-10-05 15:35:14 +0800128 const char *buff, int blen, int seg_num, FILE *pfout) {
Ying Wang0877f052010-09-09 17:19:33 -0700129
130 fprintf(pfout, " private static byte[] getSegment_%d() {\n", seg_num);
131 fprintf(pfout, " byte[] data = {\n");
132
zonr6315f762010-10-05 15:35:14 +0800133 static const int LINE_BYTE_NUM = 16;
Ying Wang0877f052010-09-09 17:19:33 -0700134 char out_line[LINE_BYTE_NUM*6 + 10];
zonr6315f762010-10-05 15:35:14 +0800135 const char *out_line_end = out_line + sizeof(out_line);
136 char *p = out_line;
Ying Wang0877f052010-09-09 17:19:33 -0700137
138 int write_length = 0;
139 while (write_length < blen) {
zonr6315f762010-10-05 15:35:14 +0800140 p += snprintf(p, out_line_end - p,
141 " %4d,", static_cast<int>(buff[write_length]));
Ying Wang0877f052010-09-09 17:19:33 -0700142 ++write_length;
143 if (((write_length % LINE_BYTE_NUM) == 0)
144 || (write_length == blen)) {
145 fprintf(pfout, " ");
Stephen Hines24f9b092010-10-06 12:58:40 -0700146 fprintf(pfout, "%s", out_line);
Ying Wang0877f052010-09-09 17:19:33 -0700147 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
159static bool GenerateJavaCodeAccessorMethod(
zonr6315f762010-10-05 15:35:14 +0800160 const RSSlangReflectUtils::BitCodeAccessorContext &context, FILE *pfout) {
161 FILE *pfin = fopen(context.bcFileName, "rb");
Ying Wang3f8b44d2010-09-04 01:17:01 -0700162 if (pfin == NULL) {
Ying Wang0877f052010-09-09 17:19:33 -0700163 fprintf(stderr, "Error: could not read file %s\n", context.bcFileName);
Ying Wang3f8b44d2010-09-04 01:17:01 -0700164 return false;
165 }
166
Ying Wang0877f052010-09-09 17:19:33 -0700167 // 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).
zonr6315f762010-10-05 15:35:14 +0800176 static const int SEG_SIZE = 0x2000;
177 char *buff = new char[SEG_SIZE];
Ying Wang0877f052010-09-09 17:19:33 -0700178 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
206static bool GenerateAccessorClass(
zonr6315f762010-10-05 15:35:14 +0800207 const RSSlangReflectUtils::BitCodeAccessorContext &context,
208 const char *clazz_name, FILE *pfout) {
Ying Wang0877f052010-09-09 17:19:33 -0700209 // 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 Wang3f8b44d2010-09-04 01:17:01 -0700225 }
226
Ying Wang0877f052010-09-09 17:19:33 -0700227 // end the class.
228 fprintf(pfout, "}\n");
229
230 return ret;
231}
232
233
234bool RSSlangReflectUtils::GenerateBitCodeAccessor(
zonr6315f762010-10-05 15:35:14 +0800235 const BitCodeAccessorContext &context) {
Ying Wang0877f052010-09-09 17:19:33 -0700236 string output_path = ComputePackagedPath(context.reflectPath,
237 context.packageName);
Zonr Chang8c6d9b22010-10-07 18:01:19 +0800238 if (!SlangUtils::CreateDirectoryWithParents(llvm::StringRef(output_path),
239 NULL)) {
Ying Wang0877f052010-09-09 17:19:33 -0700240 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 Wang3f8b44d2010-09-04 01:17:01 -0700246 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());
zonr6315f762010-10-05 15:35:14 +0800254 FILE *pfout = fopen(output_filename.c_str(), "w");
Ying Wang3f8b44d2010-09-04 01:17:01 -0700255 if (pfout == NULL) {
Ying Wang0877f052010-09-09 17:19:33 -0700256 fprintf(stderr, "Error: could not write to file %s\n",
257 output_filename.c_str());
Ying Wang3f8b44d2010-09-04 01:17:01 -0700258 return false;
259 }
260
zonr6315f762010-10-05 15:35:14 +0800261 bool ret = GenerateAccessorHeader(context, pfout) &&
262 GenerateAccessorClass(context, clazz_name.c_str(), pfout);
Ying Wang3f8b44d2010-09-04 01:17:01 -0700263
Ying Wang3f8b44d2010-09-04 01:17:01 -0700264 fclose(pfout);
Ying Wang0877f052010-09-09 17:19:33 -0700265 return ret;
Ying Wang3f8b44d2010-09-04 01:17:01 -0700266}
Ying Wang3f8b44d2010-09-04 01:17:01 -0700267}