blob: 877206489268fb21351e53009ad7bfffe817bc2f [file] [log] [blame]
zonr6315f762010-10-05 15:35:14 +08001#include "slang_rs_reflect_utils.h"
Ying Wang3f8b44d2010-09-04 01:17:01 -07002
3#include <cstdlib>
4#include <cstdio>
Mike Lockwoodb1980a22010-09-08 10:20:40 -04005#include <cstring>
Ying Wang3f8b44d2010-09-04 01:17:01 -07006
7#include <sys/stat.h>
8#include <sys/types.h>
9
10namespace slang {
11
12using std::string;
13
14string RSSlangReflectUtils::ComputePackagedPath(
zonr6315f762010-10-05 15:35:14 +080015 const char *prefixPath, const char *packageName) {
Ying Wang3f8b44d2010-09-04 01:17:01 -070016 string packaged_path(prefixPath);
zonr6315f762010-10-05 15:35:14 +080017 if (!packaged_path.empty() &&
18 (packaged_path[packaged_path.length() - 1] != '/')) {
Ying Wang3f8b44d2010-09-04 01:17:01 -070019 packaged_path += "/";
20 }
21 size_t s = packaged_path.length();
22 packaged_path += packageName;
23 while (s < packaged_path.length()) {
24 if (packaged_path[s] == '.') {
25 packaged_path[s] = '/';
26 }
27 ++s;
28 }
29 return packaged_path;
30}
31
zonr6315f762010-10-05 15:35:14 +080032static string InternalFileNameConvert(const char *rsFileName, bool toLower) {
33 const char *dot = rsFileName + strlen(rsFileName);
34 const char *slash = dot - 1;
Ying Wang3f8b44d2010-09-04 01:17:01 -070035 while (slash >= rsFileName) {
36 if (*slash == '/') {
37 break;
38 }
39 if ((*slash == '.') && (*dot == 0)) {
40 dot = slash;
41 }
42 --slash;
43 }
44 ++slash;
45 char ret[256];
Ying Wang3f8b44d2010-09-04 01:17:01 -070046 int i = 0;
47 for (; (i < 255) && (slash < dot); ++slash) {
Stephen Hinesd4176402010-09-16 17:14:35 -070048 if (isalnum(*slash) || *slash == '_') {
49 if (toLower) {
50 ret[i] = tolower(*slash);
Ying Wang3f8b44d2010-09-04 01:17:01 -070051 } else {
52 ret[i] = *slash;
53 }
Ying Wang3f8b44d2010-09-04 01:17:01 -070054 ++i;
Ying Wang3f8b44d2010-09-04 01:17:01 -070055 }
56 }
57 ret[i] = 0;
58 return string(ret);
59}
60
61std::string RSSlangReflectUtils::JavaClassNameFromRSFileName(
zonr6315f762010-10-05 15:35:14 +080062 const char *rsFileName) {
Stephen Hinesd4176402010-09-16 17:14:35 -070063 return InternalFileNameConvert(rsFileName, false);
Ying Wang3f8b44d2010-09-04 01:17:01 -070064}
65
66
67std::string RSSlangReflectUtils::BCFileNameFromRSFileName(
zonr6315f762010-10-05 15:35:14 +080068 const char *rsFileName) {
Stephen Hinesd4176402010-09-16 17:14:35 -070069 return InternalFileNameConvert(rsFileName, true);
Ying Wang3f8b44d2010-09-04 01:17:01 -070070}
71
zonr6315f762010-10-05 15:35:14 +080072bool RSSlangReflectUtils::mkdir_p(const char *path) {
Ying Wang3f8b44d2010-09-04 01:17:01 -070073 char buf[256];
74 char *tmp, *p = NULL;
75 size_t len = strlen(path);
76
77 if (len + 1 <= sizeof(buf))
78 tmp = buf;
79 else
zonr6315f762010-10-05 15:35:14 +080080 tmp = new char[len + 1];
Ying Wang3f8b44d2010-09-04 01:17:01 -070081
82 strcpy(tmp, path);
83
84 if (tmp[len - 1] == '/')
85 tmp[len - 1] = 0;
86
87 for (p = tmp + 1; *p; p++) {
88 if (*p == '/') {
89 *p = 0;
90 mkdir(tmp, S_IRWXU);
91 *p = '/';
92 }
93 }
94 mkdir(tmp, S_IRWXU);
95
96 if (tmp != buf)
97 delete[] tmp;
98
99 return true;
100}
101
Ying Wang0877f052010-09-09 17:19:33 -0700102static bool GenerateAccessorHeader(
zonr6315f762010-10-05 15:35:14 +0800103 const RSSlangReflectUtils::BitCodeAccessorContext &context, FILE *pfout) {
Ying Wang0877f052010-09-09 17:19:33 -0700104 fprintf(pfout, "/*\n");
105 fprintf(pfout, " * This file is auto-generated. DO NOT MODIFY!\n");
106 fprintf(pfout, " * The source RenderScript file: %s\n", context.rsFileName);
107 fprintf(pfout, " */\n\n");
108 fprintf(pfout, "package %s;\n\n", context.packageName);
Ying Wang3f8b44d2010-09-04 01:17:01 -0700109
Ying Wang0877f052010-09-09 17:19:33 -0700110 // add imports here.
111
112 return true;
113}
114
115static bool GenerateAccessorMethodSignature(
zonr6315f762010-10-05 15:35:14 +0800116 const RSSlangReflectUtils::BitCodeAccessorContext &context, FILE *pfout) {
Ying Wang0877f052010-09-09 17:19:33 -0700117 // the prototype of the accessor method
118 fprintf(pfout, " // return byte array representation of the bitcode.\n");
119 fprintf(pfout, " public static byte[] getBitCode() {\n");
120 return true;
121}
122
123// Java method size must not exceed 64k,
124// so we have to split the bitcode into multiple segments.
125static bool GenerateSegmentMethod(
zonr6315f762010-10-05 15:35:14 +0800126 const char *buff, int blen, int seg_num, FILE *pfout) {
Ying Wang0877f052010-09-09 17:19:33 -0700127
128 fprintf(pfout, " private static byte[] getSegment_%d() {\n", seg_num);
129 fprintf(pfout, " byte[] data = {\n");
130
zonr6315f762010-10-05 15:35:14 +0800131 static const int LINE_BYTE_NUM = 16;
Ying Wang0877f052010-09-09 17:19:33 -0700132 char out_line[LINE_BYTE_NUM*6 + 10];
zonr6315f762010-10-05 15:35:14 +0800133 const char *out_line_end = out_line + sizeof(out_line);
134 char *p = out_line;
Ying Wang0877f052010-09-09 17:19:33 -0700135
136 int write_length = 0;
137 while (write_length < blen) {
zonr6315f762010-10-05 15:35:14 +0800138 p += snprintf(p, out_line_end - p,
139 " %4d,", static_cast<int>(buff[write_length]));
Ying Wang0877f052010-09-09 17:19:33 -0700140 ++write_length;
141 if (((write_length % LINE_BYTE_NUM) == 0)
142 || (write_length == blen)) {
143 fprintf(pfout, " ");
Stephen Hines24f9b092010-10-06 12:58:40 -0700144 fprintf(pfout, "%s", out_line);
Ying Wang0877f052010-09-09 17:19:33 -0700145 fprintf(pfout, "\n");
146 p = out_line;
147 }
148 }
149
150 fprintf(pfout, " };\n");
151 fprintf(pfout, " return data;\n");
152 fprintf(pfout, " }\n\n");
153
154 return true;
155}
156
157static bool GenerateJavaCodeAccessorMethod(
zonr6315f762010-10-05 15:35:14 +0800158 const RSSlangReflectUtils::BitCodeAccessorContext &context, FILE *pfout) {
159 FILE *pfin = fopen(context.bcFileName, "rb");
Ying Wang3f8b44d2010-09-04 01:17:01 -0700160 if (pfin == NULL) {
Ying Wang0877f052010-09-09 17:19:33 -0700161 fprintf(stderr, "Error: could not read file %s\n", context.bcFileName);
Ying Wang3f8b44d2010-09-04 01:17:01 -0700162 return false;
163 }
164
Ying Wang0877f052010-09-09 17:19:33 -0700165 // start the accessor method
166 GenerateAccessorMethodSignature(context, pfout);
167 fprintf(pfout, " return getBitCodeInternal();\n");
168 // end the accessor method
169 fprintf(pfout, " };\n\n");
170
171 // output the data
172 // make sure the generated function for a segment won't break the Javac
173 // size limitation (64K).
zonr6315f762010-10-05 15:35:14 +0800174 static const int SEG_SIZE = 0x2000;
175 char *buff = new char[SEG_SIZE];
Ying Wang0877f052010-09-09 17:19:33 -0700176 int read_length;
177 int seg_num = 0;
178 int total_length = 0;
179 while ((read_length = fread(buff, 1, SEG_SIZE, pfin)) > 0) {
180 GenerateSegmentMethod(buff, read_length, seg_num, pfout);
181 ++seg_num;
182 total_length += read_length;
183 }
184 delete []buff;
185 fclose(pfin);
186
187 // output the internal accessor method
188 fprintf(pfout, " private static int bitCodeLength = %d;\n\n", total_length);
189 fprintf(pfout, " private static byte[] getBitCodeInternal() {\n");
190 fprintf(pfout, " byte[] bc = new byte[bitCodeLength];\n");
191 fprintf(pfout, " int offset = 0;\n");
192 fprintf(pfout, " byte[] seg;\n");
193 for (int i = 0; i < seg_num; ++i) {
194 fprintf(pfout, " seg = getSegment_%d();\n", i);
195 fprintf(pfout, " System.arraycopy(seg, 0, bc, offset, seg.length);\n");
196 fprintf(pfout, " offset += seg.length;\n");
197 }
198 fprintf(pfout, " return bc;\n");
199 fprintf(pfout, " }\n\n");
200
201 return true;
202}
203
204static bool GenerateAccessorClass(
zonr6315f762010-10-05 15:35:14 +0800205 const RSSlangReflectUtils::BitCodeAccessorContext &context,
206 const char *clazz_name, FILE *pfout) {
Ying Wang0877f052010-09-09 17:19:33 -0700207 // begin the class.
208 fprintf(pfout, "/**\n");
209 fprintf(pfout, " * @hide\n");
210 fprintf(pfout, " */\n");
211 fprintf(pfout, "public class %s {\n", clazz_name);
212 fprintf(pfout, "\n");
213
214 bool ret = true;
215 switch (context.bcStorage) {
216 case BCST_APK_RESOURCE:
217 break;
218 case BCST_JAVA_CODE:
219 ret = GenerateJavaCodeAccessorMethod(context, pfout);
220 break;
221 default:
222 ret = false;
Ying Wang3f8b44d2010-09-04 01:17:01 -0700223 }
224
Ying Wang0877f052010-09-09 17:19:33 -0700225 // end the class.
226 fprintf(pfout, "}\n");
227
228 return ret;
229}
230
231
232bool RSSlangReflectUtils::GenerateBitCodeAccessor(
zonr6315f762010-10-05 15:35:14 +0800233 const BitCodeAccessorContext &context) {
Ying Wang0877f052010-09-09 17:19:33 -0700234 string output_path = ComputePackagedPath(context.reflectPath,
235 context.packageName);
236 if (!mkdir_p(output_path.c_str())) {
237 fprintf(stderr, "Error: could not create dir %s\n",
238 output_path.c_str());
239 return false;
240 }
241
242 string clazz_name(JavaClassNameFromRSFileName(context.rsFileName));
Ying Wang3f8b44d2010-09-04 01:17:01 -0700243 clazz_name += "BitCode";
244 string filename(clazz_name);
245 filename += ".java";
246
247 string output_filename(output_path);
248 output_filename += "/";
249 output_filename += filename;
250 printf("Generating %s ...\n", filename.c_str());
zonr6315f762010-10-05 15:35:14 +0800251 FILE *pfout = fopen(output_filename.c_str(), "w");
Ying Wang3f8b44d2010-09-04 01:17:01 -0700252 if (pfout == NULL) {
Ying Wang0877f052010-09-09 17:19:33 -0700253 fprintf(stderr, "Error: could not write to file %s\n",
254 output_filename.c_str());
Ying Wang3f8b44d2010-09-04 01:17:01 -0700255 return false;
256 }
257
zonr6315f762010-10-05 15:35:14 +0800258 bool ret = GenerateAccessorHeader(context, pfout) &&
259 GenerateAccessorClass(context, clazz_name.c_str(), pfout);
Ying Wang3f8b44d2010-09-04 01:17:01 -0700260
Ying Wang3f8b44d2010-09-04 01:17:01 -0700261 fclose(pfout);
Ying Wang0877f052010-09-09 17:19:33 -0700262 return ret;
Ying Wang3f8b44d2010-09-04 01:17:01 -0700263}
Ying Wang3f8b44d2010-09-04 01:17:01 -0700264}