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 | |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 17 | #ifndef _FRAMEWORKS_COMPILE_SLANG_SLANG_REFLECT_UTILS_H_ // NOLINT |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 18 | #define _FRAMEWORKS_COMPILE_SLANG_SLANG_REFLECT_UTILS_H_ |
Ying Wang | 3f8b44d | 2010-09-04 01:17:01 -0700 | [diff] [blame] | 19 | |
Jean-Luc Brouillet | 129fd82 | 2014-05-28 17:46:10 -0700 | [diff] [blame] | 20 | #include <fstream> |
Ying Wang | 3f8b44d | 2010-09-04 01:17:01 -0700 | [diff] [blame] | 21 | #include <string> |
| 22 | |
| 23 | namespace slang { |
| 24 | |
Ying Wang | 0877f05 | 2010-09-09 17:19:33 -0700 | [diff] [blame] | 25 | // BitCode storage type |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 26 | enum BitCodeStorageType { BCST_APK_RESOURCE, BCST_JAVA_CODE, BCST_CPP_CODE }; |
Ying Wang | 0877f05 | 2010-09-09 17:19:33 -0700 | [diff] [blame] | 27 | |
Ying Wang | 3f8b44d | 2010-09-04 01:17:01 -0700 | [diff] [blame] | 28 | class RSSlangReflectUtils { |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 29 | public: |
Ying Wang | 0877f05 | 2010-09-09 17:19:33 -0700 | [diff] [blame] | 30 | // Encode a binary bitcode file into a Java source file. |
| 31 | // rsFileName: the original .rs file name (with or without path). |
Stephen Hines | 9ae18b2 | 2014-06-10 23:53:00 -0700 | [diff] [blame] | 32 | // bc32FileName: path of the 32-bit bitcode file |
| 33 | // bc64FileName: path of the 64-bit bitcode file |
Ying Wang | 0877f05 | 2010-09-09 17:19:33 -0700 | [diff] [blame] | 34 | // reflectPath: where to output the generated Java file, no package name in |
| 35 | // it. |
| 36 | // packageName: the package of the output Java file. |
Stephen Hines | fc4f78b | 2014-06-10 18:07:10 -0700 | [diff] [blame] | 37 | // verbose: whether or not to print out additional info about compilation. |
| 38 | // bcStorage: where to emit bitcode to (resource file or embedded). |
Ying Wang | 0877f05 | 2010-09-09 17:19:33 -0700 | [diff] [blame] | 39 | struct BitCodeAccessorContext { |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 40 | const char *rsFileName; |
Stephen Hines | 9ae18b2 | 2014-06-10 23:53:00 -0700 | [diff] [blame] | 41 | const char *bc32FileName; |
| 42 | const char *bc64FileName; |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 43 | const char *reflectPath; |
| 44 | const char *packageName; |
Jean-Luc Brouillet | 129fd82 | 2014-05-28 17:46:10 -0700 | [diff] [blame] | 45 | const std::string *licenseNote; |
Stephen Hines | fc4f78b | 2014-06-10 18:07:10 -0700 | [diff] [blame] | 46 | bool verbose; |
Ying Wang | 0877f05 | 2010-09-09 17:19:33 -0700 | [diff] [blame] | 47 | BitCodeStorageType bcStorage; |
| 48 | }; |
Ying Wang | 3f8b44d | 2010-09-04 01:17:01 -0700 | [diff] [blame] | 49 | |
Ying Wang | dba3111 | 2010-10-07 17:30:38 -0700 | [diff] [blame] | 50 | // 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 Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 54 | static std::string GetFileNameStem(const char *fileName); |
Ying Wang | dba3111 | 2010-10-07 17:30:38 -0700 | [diff] [blame] | 55 | |
Jean-Luc Brouillet | 129fd82 | 2014-05-28 17:46:10 -0700 | [diff] [blame] | 56 | // Compute a Java source file path from a given prefixPath and its package. |
Ying Wang | 0877f05 | 2010-09-09 17:19:33 -0700 | [diff] [blame] | 57 | // Eg, given prefixPath=./foo/bar and packageName=com.x.y, then it returns |
| 58 | // ./foo/bar/com/x/y |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 59 | static std::string ComputePackagedPath(const char *prefixPath, |
| 60 | const char *packageName); |
Ying Wang | 3f8b44d | 2010-09-04 01:17:01 -0700 | [diff] [blame] | 61 | |
Ying Wang | 0877f05 | 2010-09-09 17:19:33 -0700 | [diff] [blame] | 62 | // Compute Java class name from a .rs file name. |
Stephen Hines | d417640 | 2010-09-16 17:14:35 -0700 | [diff] [blame] | 63 | // 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 Wang | 0877f05 | 2010-09-09 17:19:33 -0700 | [diff] [blame] | 66 | // rsFileName: the input .rs file name (with or without path). |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 67 | static std::string JavaClassNameFromRSFileName(const char *rsFileName); |
Ying Wang | 0877f05 | 2010-09-09 17:19:33 -0700 | [diff] [blame] | 68 | |
| 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 Hines | d417640 | 2010-09-16 17:14:35 -0700 | [diff] [blame] | 71 | // class (something like R.raw.<bitcode_filename>), Any non-alnum, |
| 72 | // non-underscore character will be discarded. |
Ying Wang | 0877f05 | 2010-09-09 17:19:33 -0700 | [diff] [blame] | 73 | // The difference from JavaClassNameFromRSFileName() is that the result is |
Stephen Hines | d417640 | 2010-09-16 17:14:35 -0700 | [diff] [blame] | 74 | // converted to lowercase. |
| 75 | // E.g. with rsFileName=./foo/bar/my-Renderscript_file.rs it returns |
| 76 | // "myrenderscript_file" |
Ying Wang | 0877f05 | 2010-09-09 17:19:33 -0700 | [diff] [blame] | 77 | // rsFileName: the input .rs file name (with or without path). |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 78 | static std::string BCFileNameFromRSFileName(const char *rsFileName); |
Ying Wang | 0877f05 | 2010-09-09 17:19:33 -0700 | [diff] [blame] | 79 | |
Stephen Hines | 44d495d | 2014-05-22 19:42:55 -0700 | [diff] [blame] | 80 | // 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 Wang | 0877f05 | 2010-09-09 17:19:33 -0700 | [diff] [blame] | 87 | // Generate the bit code accessor Java source file. |
Jean-Luc Brouillet | 129fd82 | 2014-05-28 17:46:10 -0700 | [diff] [blame] | 88 | static bool GenerateJavaBitCodeAccessor(const BitCodeAccessorContext &context); |
Ying Wang | 3f8b44d | 2010-09-04 01:17:01 -0700 | [diff] [blame] | 89 | }; |
Jean-Luc Brouillet | 129fd82 | 2014-05-28 17:46:10 -0700 | [diff] [blame] | 90 | |
| 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". |
| 95 | std::string JoinPath(const std::string &path1, const std::string &path2); |
| 96 | |
Jean-Luc Brouillet | efcff10 | 2014-06-03 16:13:51 -0700 | [diff] [blame] | 97 | /* 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 | */ |
| 102 | std::string RootNameFromRSFileName(const std::string &rsFileName); |
| 103 | |
Jean-Luc Brouillet | 129fd82 | 2014-05-28 17:46:10 -0700 | [diff] [blame] | 104 | /* This class is used to generate one source file. There will be one instance |
| 105 | * for each generated file. |
| 106 | */ |
| 107 | class GeneratedFile : public std::ofstream { |
| 108 | public: |
| 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 Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 114 | * If optionalLicense is nullptr, a default license is used. |
Jean-Luc Brouillet | 129fd82 | 2014-05-28 17:46:10 -0700 | [diff] [blame] | 115 | */ |
| 116 | bool startFile(const std::string &outPath, const std::string &outFileName, |
| 117 | const std::string &sourceFileName, |
Stephen Hines | fc4f78b | 2014-06-10 18:07:10 -0700 | [diff] [blame] | 118 | const std::string *optionalLicense, bool isJava, bool verbose); |
Jean-Luc Brouillet | 129fd82 | 2014-05-28 17:46:10 -0700 | [diff] [blame] | 119 | void closeFile(); |
| 120 | |
Jean-Luc Brouillet | c83b790 | 2014-07-25 15:50:32 -0700 | [diff] [blame] | 121 | 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 Brouillet | 129fd82 | 2014-05-28 17:46:10 -0700 | [diff] [blame] | 124 | |
| 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 | |
| 145 | private: |
| 146 | std::string mIndent; // The correct spacing at the beginning of each line. |
| 147 | }; |
| 148 | |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 149 | } // namespace slang |
Ying Wang | 3f8b44d | 2010-09-04 01:17:01 -0700 | [diff] [blame] | 150 | |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 151 | #endif // _FRAMEWORKS_COMPILE_SLANG_SLANG_REFLECT_UTILS_H_ NOLINT |