blob: 1cbc09891d853fa289b457ef1514172d8816ee53 [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
Jean-Luc Brouillet2ce118e2014-05-27 17:41:22 -070017#ifndef _FRAMEWORKS_COMPILE_SLANG_SLANG_REFLECT_UTILS_H_ // NOLINT
Stephen Hinese639eb52010-11-08 19:27:20 -080018#define _FRAMEWORKS_COMPILE_SLANG_SLANG_REFLECT_UTILS_H_
Ying Wang3f8b44d2010-09-04 01:17:01 -070019
Jean-Luc Brouillet129fd822014-05-28 17:46:10 -070020#include <fstream>
Ying Wang3f8b44d2010-09-04 01:17:01 -070021#include <string>
22
23namespace slang {
24
Ying Wang0877f052010-09-09 17:19:33 -070025// BitCode storage type
Jean-Luc Brouillet2ce118e2014-05-27 17:41:22 -070026enum BitCodeStorageType { BCST_APK_RESOURCE, BCST_JAVA_CODE, BCST_CPP_CODE };
Ying Wang0877f052010-09-09 17:19:33 -070027
Ying Wang3f8b44d2010-09-04 01:17:01 -070028class RSSlangReflectUtils {
Jean-Luc Brouillet2ce118e2014-05-27 17:41:22 -070029public:
Ying Wang0877f052010-09-09 17:19:33 -070030 // Encode a binary bitcode file into a Java source file.
31 // rsFileName: the original .rs file name (with or without path).
Stephen Hines9ae18b22014-06-10 23:53:00 -070032 // bc32FileName: path of the 32-bit bitcode file
33 // bc64FileName: path of the 64-bit bitcode file
Ying Wang0877f052010-09-09 17:19:33 -070034 // reflectPath: where to output the generated Java file, no package name in
35 // it.
36 // packageName: the package of the output Java file.
Stephen Hinesfc4f78b2014-06-10 18:07:10 -070037 // verbose: whether or not to print out additional info about compilation.
38 // bcStorage: where to emit bitcode to (resource file or embedded).
Ying Wang0877f052010-09-09 17:19:33 -070039 struct BitCodeAccessorContext {
zonr6315f762010-10-05 15:35:14 +080040 const char *rsFileName;
Stephen Hines9ae18b22014-06-10 23:53:00 -070041 const char *bc32FileName;
42 const char *bc64FileName;
zonr6315f762010-10-05 15:35:14 +080043 const char *reflectPath;
44 const char *packageName;
Jean-Luc Brouillet129fd822014-05-28 17:46:10 -070045 const std::string *licenseNote;
Stephen Hinesfc4f78b2014-06-10 18:07:10 -070046 bool verbose;
Ying Wang0877f052010-09-09 17:19:33 -070047 BitCodeStorageType bcStorage;
48 };
Ying Wang3f8b44d2010-09-04 01:17:01 -070049
Ying Wangdba31112010-10-07 17:30:38 -070050 // Return the stem of the file name, i.e., remove the dir and the extension.
51 // Eg, foo.ext -> foo
52 // foo.bar.ext -> foo.bar
53 // ./path/foo.ext -> foo
Jean-Luc Brouillet2ce118e2014-05-27 17:41:22 -070054 static std::string GetFileNameStem(const char *fileName);
Ying Wangdba31112010-10-07 17:30:38 -070055
Jean-Luc Brouillet129fd822014-05-28 17:46:10 -070056 // Compute a Java source file path from a given prefixPath and its package.
Ying Wang0877f052010-09-09 17:19:33 -070057 // Eg, given prefixPath=./foo/bar and packageName=com.x.y, then it returns
58 // ./foo/bar/com/x/y
zonr6315f762010-10-05 15:35:14 +080059 static std::string ComputePackagedPath(const char *prefixPath,
60 const char *packageName);
Ying Wang3f8b44d2010-09-04 01:17:01 -070061
Ying Wang0877f052010-09-09 17:19:33 -070062 // Compute Java class name from a .rs file name.
Stephen Hinesd4176402010-09-16 17:14:35 -070063 // Any non-alnum, non-underscore characters will be discarded.
64 // E.g. with rsFileName=./foo/bar/my-Renderscript_file.rs it returns
65 // "myRenderscript_file".
Ying Wang0877f052010-09-09 17:19:33 -070066 // rsFileName: the input .rs file name (with or without path).
zonr6315f762010-10-05 15:35:14 +080067 static std::string JavaClassNameFromRSFileName(const char *rsFileName);
Ying Wang0877f052010-09-09 17:19:33 -070068
69 // Compute a bitcode file name (no extension) from a .rs file name.
70 // Because the bitcode file name may be used as Resource ID in the generated
Stephen Hinesd4176402010-09-16 17:14:35 -070071 // class (something like R.raw.<bitcode_filename>), Any non-alnum,
72 // non-underscore character will be discarded.
Ying Wang0877f052010-09-09 17:19:33 -070073 // The difference from JavaClassNameFromRSFileName() is that the result is
Stephen Hinesd4176402010-09-16 17:14:35 -070074 // converted to lowercase.
75 // E.g. with rsFileName=./foo/bar/my-Renderscript_file.rs it returns
76 // "myrenderscript_file"
Ying Wang0877f052010-09-09 17:19:33 -070077 // rsFileName: the input .rs file name (with or without path).
zonr6315f762010-10-05 15:35:14 +080078 static std::string BCFileNameFromRSFileName(const char *rsFileName);
Ying Wang0877f052010-09-09 17:19:33 -070079
Stephen Hines44d495d2014-05-22 19:42:55 -070080 // Compute the bitcode-containing class name from a .rs filename.
81 // Any non-alnum, non-underscore characters will be discarded.
82 // E.g. with rsFileName=./foo/bar/my-Renderscript_file.rs it returns
83 // "myRenderscript_fileBitCode".
84 // rsFileName: the input .rs file name (with or without path).
85 static std::string JavaBitcodeClassNameFromRSFileName(const char *rsFileName);
86
Ying Wang0877f052010-09-09 17:19:33 -070087 // Generate the bit code accessor Java source file.
Jean-Luc Brouillet129fd822014-05-28 17:46:10 -070088 static bool GenerateJavaBitCodeAccessor(const BitCodeAccessorContext &context);
Ying Wang3f8b44d2010-09-04 01:17:01 -070089};
Jean-Luc Brouillet129fd822014-05-28 17:46:10 -070090
91// Joins two sections of a path, inserting a separator if needed.
92// E.g. JoinPath("foo/bar", "baz/a.java") returns "foo/bar/baz/a.java",
93// JoinPath("foo", "/bar/baz") returns "foo/bar/baz", and
94// JoinPath("foo/", "/bar") returns "foo/bar".
95std::string JoinPath(const std::string &path1, const std::string &path2);
96
Jean-Luc Brouilletefcff102014-06-03 16:13:51 -070097/* Compute a safe root name from a .rs file name. Any non-alphanumeric,
98 * non-underscore characters will be discarded.
99 * E.g. RootNameFromRSFileName("./foo/bar/my-Renderscript_file.rs") returns
100 * "myRenderscript_file".
101 */
102std::string RootNameFromRSFileName(const std::string &rsFileName);
103
Jean-Luc Brouillet129fd822014-05-28 17:46:10 -0700104/* This class is used to generate one source file. There will be one instance
105 * for each generated file.
106 */
107class GeneratedFile : public std::ofstream {
108public:
109 /* Starts the file by:
110 * - creating the parent directories (if needed),
111 * - opening the stream,
112 * - writing out the license,
113 * - writing a message that this file has been auto-generated.
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700114 * If optionalLicense is nullptr, a default license is used.
Jean-Luc Brouillet129fd822014-05-28 17:46:10 -0700115 */
116 bool startFile(const std::string &outPath, const std::string &outFileName,
117 const std::string &sourceFileName,
Stephen Hinesfc4f78b2014-06-10 18:07:10 -0700118 const std::string *optionalLicense, bool isJava, bool verbose);
Jean-Luc Brouillet129fd822014-05-28 17:46:10 -0700119 void closeFile();
120
Jean-Luc Brouilletc83b7902014-07-25 15:50:32 -0700121 void increaseIndent(); // Increases the new line indentation by 4.
122 void decreaseIndent(); // Decreases the new line indentation by 4.
123 void comment(const std::string& s); // Outputs a multiline comment.
Jean-Luc Brouillet129fd822014-05-28 17:46:10 -0700124
125 // Starts a control block. This works both for Java and C++.
126 void startBlock() {
127 *this << " {\n";
128 increaseIndent();
129 }
130
131 // Ends a control block.
132 void endBlock(bool addSemicolon = false) {
133 decreaseIndent();
134 indent() << "}" << (addSemicolon ? ";" : "") << "\n\n";
135 }
136
137 /* Indents the line. By returning *this, we can use like this:
138 * mOut.ident() << "a = b;\n";
139 */
140 std::ofstream &indent() {
141 *this << mIndent;
142 return *this;
143 }
144
145private:
146 std::string mIndent; // The correct spacing at the beginning of each line.
147};
148
Jean-Luc Brouillet2ce118e2014-05-27 17:41:22 -0700149} // namespace slang
Ying Wang3f8b44d2010-09-04 01:17:01 -0700150
Jean-Luc Brouillet2ce118e2014-05-27 17:41:22 -0700151#endif // _FRAMEWORKS_COMPILE_SLANG_SLANG_REFLECT_UTILS_H_ NOLINT