blob: 0c1143d20eb535f9c1526b3165724d2e63bf66c1 [file] [log] [blame]
Zonr Changc383a502010-10-12 01:52:08 +08001/*
Stephen Hines0a813a32012-08-03 16:52:40 -07002 * Copyright 2010-2012, The Android Open Source Project
Zonr Changc383a502010-10-12 01:52:08 +08003 *
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
Xusong Wang19ea0eb2020-11-10 11:25:05 -080017#include "clang/Basic/AllDiagnostics.h"
Stephen Hines8b5c5c62014-06-06 18:03:18 -070018#include "clang/Basic/DiagnosticOptions.h"
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070019#include "clang/Driver/DriverDiagnostic.h"
Stephen Hinesa1f95ee2013-08-09 01:26:08 -070020#include "clang/Driver/Options.h"
Jean-Luc Brouillet5309b0c2015-05-01 16:13:11 -070021#include "clang/Frontend/CompilerInvocation.h"
22#include "clang/Frontend/FrontendDiagnostic.h"
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070023#include "clang/Frontend/TextDiagnosticPrinter.h"
Stephen Hines8f4d9722011-12-05 14:14:42 -080024#include "clang/Frontend/Utils.h"
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070025
Stephen Hinese639eb52010-11-08 19:27:20 -080026#include "llvm/ADT/SmallVector.h"
Loganbe274822011-02-16 22:02:54 +080027#include "llvm/ADT/IntrusiveRefCntPtr.h"
Stephen Hinese639eb52010-11-08 19:27:20 -080028
Stephen Hinesa1f95ee2013-08-09 01:26:08 -070029#include "llvm/Option/OptTable.h"
Pirama Arumuga Nainar1906a002015-06-29 10:30:41 -070030#include "llvm/Support/Allocator.h"
Stephen Hinese639eb52010-11-08 19:27:20 -080031#include "llvm/Support/ManagedStatic.h"
32#include "llvm/Support/MemoryBuffer.h"
Loganbe274822011-02-16 22:02:54 +080033#include "llvm/Support/Path.h"
Stephen Hinesba7c6dc2011-09-07 19:57:04 -070034#include "llvm/Support/raw_ostream.h"
Jean-Luc Brouillet5309b0c2015-05-01 16:13:11 -070035#include "llvm/Support/Signals.h"
Pirama Arumuga Nainar1906a002015-06-29 10:30:41 -070036#include "llvm/Support/StringSaver.h"
Pirama Arumuga Nainarb6a14352016-07-26 11:39:47 -070037#include "llvm/Support/TargetSelect.h"
mkopec1c460b372012-01-09 11:21:50 -050038#include "llvm/Target/TargetMachine.h"
Stephen Hinese639eb52010-11-08 19:27:20 -080039
Elliott Hughes9534de12015-07-29 14:07:37 -070040#include "os_sep.h"
Stephen Hines8b5c5c62014-06-06 18:03:18 -070041#include "rs_cc_options.h"
Stephen Hinese639eb52010-11-08 19:27:20 -080042#include "slang.h"
Stephen Hines6e6578a2011-02-07 18:05:48 -080043#include "slang_assert.h"
Stephen Hines8f4d9722011-12-05 14:14:42 -080044#include "slang_diagnostic_buffer.h"
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070045#include "slang_rs_reflect_utils.h"
David Grossd80e58b2017-07-24 11:41:12 -070046#include "slang_rs_reflection_state.h"
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070047
Stephen Hines8b5c5c62014-06-06 18:03:18 -070048#include <list>
49#include <set>
50#include <string>
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070051
Jean-Luc Brouilletc2473632015-05-14 13:07:47 -070052namespace {
Pirama Arumuga Nainar98cfae42016-03-03 23:56:27 -080053class StringSet {
Jean-Luc Brouilletc2473632015-05-14 13:07:47 -070054public:
Pirama Arumuga Nainar98cfae42016-03-03 23:56:27 -080055 const char *save(const char *Str) {
56 return Strings.save(Str);
Jean-Luc Brouilletc2473632015-05-14 13:07:47 -070057 }
58
Pirama Arumuga Nainar98cfae42016-03-03 23:56:27 -080059 StringSet() : Strings(A), A() {}
60
61 llvm::StringSaver & getStringSaver() { return Strings; }
Pirama Arumuga Nainar1906a002015-06-29 10:30:41 -070062
Jean-Luc Brouilletc2473632015-05-14 13:07:47 -070063private:
Pirama Arumuga Nainar98cfae42016-03-03 23:56:27 -080064 llvm::StringSaver Strings;
Pirama Arumuga Nainar1906a002015-06-29 10:30:41 -070065 llvm::BumpPtrAllocator A;
Jean-Luc Brouilletc2473632015-05-14 13:07:47 -070066};
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070067}
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070068
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070069static const char *DetermineOutputFile(const std::string &OutputDir,
Stephen Hines9ae18b22014-06-10 23:53:00 -070070 const std::string &PathSuffix,
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070071 const char *InputFile,
Stephen Hinese639eb52010-11-08 19:27:20 -080072 slang::Slang::OutputType OutputType,
Jean-Luc Brouilletc2473632015-05-14 13:07:47 -070073 StringSet *SavedStrings) {
Stephen Hinese639eb52010-11-08 19:27:20 -080074 if (OutputType == slang::Slang::OT_Nothing)
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070075 return "/dev/null";
76
77 std::string OutputFile(OutputDir);
78
Stephen Hines7f5704e2014-06-10 18:06:50 -070079 // Append '/' to Opts.mBitcodeOutputDir if not presents
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070080 if (!OutputFile.empty() &&
Raphael8d5a2f62011-02-08 00:15:05 -080081 (OutputFile[OutputFile.size() - 1]) != OS_PATH_SEPARATOR)
82 OutputFile.append(1, OS_PATH_SEPARATOR);
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070083
Stephen Hines9ae18b22014-06-10 23:53:00 -070084 if (!PathSuffix.empty()) {
85 OutputFile.append(PathSuffix);
86 OutputFile.append(1, OS_PATH_SEPARATOR);
87 }
88
Stephen Hinese639eb52010-11-08 19:27:20 -080089 if (OutputType == slang::Slang::OT_Dependency) {
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070090 // The build system wants the .d file name stem to be exactly the same as
91 // the source .rs file, instead of the .bc file.
Stephen Hinese639eb52010-11-08 19:27:20 -080092 OutputFile.append(slang::RSSlangReflectUtils::GetFileNameStem(InputFile));
93 } else {
94 OutputFile.append(
95 slang::RSSlangReflectUtils::BCFileNameFromRSFileName(InputFile));
96 }
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070097
Stephen Hinese639eb52010-11-08 19:27:20 -080098 switch (OutputType) {
99 case slang::Slang::OT_Dependency: {
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700100 OutputFile.append(".d");
101 break;
102 }
Stephen Hinese639eb52010-11-08 19:27:20 -0800103 case slang::Slang::OT_Assembly: {
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700104 OutputFile.append(".S");
105 break;
106 }
Stephen Hinese639eb52010-11-08 19:27:20 -0800107 case slang::Slang::OT_LLVMAssembly: {
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700108 OutputFile.append(".ll");
109 break;
110 }
Stephen Hinese639eb52010-11-08 19:27:20 -0800111 case slang::Slang::OT_Object: {
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700112 OutputFile.append(".o");
113 break;
114 }
Stephen Hinese639eb52010-11-08 19:27:20 -0800115 case slang::Slang::OT_Bitcode: {
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700116 OutputFile.append(".bc");
117 break;
118 }
Stephen Hinese639eb52010-11-08 19:27:20 -0800119 case slang::Slang::OT_Nothing:
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700120 default: {
Stephen Hines6e6578a2011-02-07 18:05:48 -0800121 slangAssert(false && "Invalid output type!");
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700122 }
123 }
124
Pirama Arumuga Nainar1906a002015-06-29 10:30:41 -0700125 return SavedStrings->save(OutputFile.c_str());
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700126}
127
Stephen Hines9ae18b22014-06-10 23:53:00 -0700128typedef std::list<std::pair<const char*, const char*> > NamePairList;
129
130/*
131 * Compile the Inputs.
132 *
133 * Returns 0 on success and nonzero on failure.
134 *
135 * IOFiles - list of (foo.rs, foo.bc) pairs of input/output files.
136 * IOFiles32 - list of input/output pairs for 32-bit compilation.
137 * Inputs - input filenames.
138 * Opts - options controlling compilation.
139 * DiagEngine - Clang diagnostic engine (for creating diagnostics).
140 * DiagClient - Slang diagnostic consumer (collects and displays diagnostics).
141 * SavedStrings - expanded strings copied from argv source input files.
142 *
143 * We populate IOFiles dynamically while working through the list of Inputs.
144 * On any 64-bit compilation, we pass back in the 32-bit pairs of files as
145 * IOFiles32. This allows the 64-bit compiler to later bundle up both the
146 * 32-bit and 64-bit bitcode outputs to be included in the final reflected
147 * source code that is emitted.
148 */
Jean-Luc Brouillet5309b0c2015-05-01 16:13:11 -0700149static void makeFileList(NamePairList *IOFiles, NamePairList *DepFiles,
Stephen Hines9ae18b22014-06-10 23:53:00 -0700150 const llvm::SmallVector<const char*, 16> &Inputs, slang::RSCCOptions &Opts,
Jean-Luc Brouilletc2473632015-05-14 13:07:47 -0700151 StringSet *SavedStrings) {
Stephen Hines9ae18b22014-06-10 23:53:00 -0700152 std::string PathSuffix = "";
Stephen Hines9ae18b22014-06-10 23:53:00 -0700153 // In our mixed 32/64-bit path, we need to suffix our files differently for
154 // both 32-bit and 64-bit versions.
155 if (Opts.mEmit3264) {
156 if (Opts.mBitWidth == 64) {
157 PathSuffix = "bc64";
158 } else {
159 PathSuffix = "bc32";
160 }
161 }
162
163 for (int i = 0, e = Inputs.size(); i != e; i++) {
164 const char *InputFile = Inputs[i];
165
166 const char *BCOutputFile = DetermineOutputFile(Opts.mBitcodeOutputDir,
167 PathSuffix, InputFile,
Yang Ni76837a12015-04-28 17:30:51 -0700168 Opts.mOutputType,
Jean-Luc Brouilletc2473632015-05-14 13:07:47 -0700169 SavedStrings);
Stephen Hines9ae18b22014-06-10 23:53:00 -0700170 const char *OutputFile = BCOutputFile;
171
172 if (Opts.mEmitDependency) {
173 // The dependency file is always emitted without a PathSuffix.
174 // Collisions between 32-bit and 64-bit files don't make a difference,
175 // because they share the same sources/dependencies.
176 const char *DepOutputFile =
177 DetermineOutputFile(Opts.mDependencyOutputDir, "", InputFile,
Jean-Luc Brouilletc2473632015-05-14 13:07:47 -0700178 slang::Slang::OT_Dependency, SavedStrings);
Stephen Hines9ae18b22014-06-10 23:53:00 -0700179 if (Opts.mOutputType == slang::Slang::OT_Dependency) {
180 OutputFile = DepOutputFile;
181 }
182
Jean-Luc Brouillet5309b0c2015-05-01 16:13:11 -0700183 DepFiles->push_back(std::make_pair(BCOutputFile, DepOutputFile));
Stephen Hines9ae18b22014-06-10 23:53:00 -0700184 }
185
186 IOFiles->push_back(std::make_pair(InputFile, OutputFile));
187 }
Stephen Hines9ae18b22014-06-10 23:53:00 -0700188}
189
Stephen Hinesba7c6dc2011-09-07 19:57:04 -0700190#define str(s) #s
191#define wrap_str(s) str(s)
192static void llvm_rs_cc_VersionPrinter() {
193 llvm::raw_ostream &OS = llvm::outs();
194 OS << "llvm-rs-cc: Renderscript compiler\n"
195 << " (http://developer.android.com/guide/topics/renderscript)\n"
196 << " based on LLVM (http://llvm.org):\n";
Stephen Hinesba7c6dc2011-09-07 19:57:04 -0700197 OS << " Target APIs: " << SLANG_MINIMUM_TARGET_API << " - "
198 << SLANG_MAXIMUM_TARGET_API;
199 OS << "\n Build type: " << wrap_str(TARGET_BUILD_VARIANT);
200#ifndef __DISABLE_ASSERTS
201 OS << " with assertions";
202#endif
203 OS << ".\n";
Stephen Hinesba7c6dc2011-09-07 19:57:04 -0700204}
Stephen Hines8f4d9722011-12-05 14:14:42 -0800205#undef wrap_str
206#undef str
Stephen Hinesba7c6dc2011-09-07 19:57:04 -0700207
Jean-Luc Brouillet5309b0c2015-05-01 16:13:11 -0700208static void LLVMErrorHandler(void *UserData, const std::string &Message,
209 bool GenCrashDialog) {
210 clang::DiagnosticsEngine *DiagEngine =
211 static_cast<clang::DiagnosticsEngine *>(UserData);
212
213 DiagEngine->Report(clang::diag::err_fe_error_backend) << Message;
214
215 // Run the interrupt handlers to make sure any special cleanups get done, in
216 // particular that we remove files registered with RemoveFileOnSignal.
217 llvm::sys::RunInterruptHandlers();
218
219 exit(1);
220}
221
Steven Moreland722b5242019-09-24 18:39:00 -0700222// TODO(b/37755219): detect leaks
223extern "C" const char *__asan_default_options() {
224 return "detect_leaks=0";
225}
226
Xusong Wang19ea0eb2020-11-10 11:25:05 -0800227static void emitDeprecationWarning(clang::DiagnosticsEngine *DiagEngine) {
228 DiagEngine->Report(clang::diag::warn_deprecated_message)
229 << "Renderscript"
230 << "Please refer to the migration guide "
231 "(https://developer.android.com/guide/topics/renderscript/migration-guide) "
232 "for the proposed alternatives.";
233}
234
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700235int main(int argc, const char **argv) {
Jean-Luc Brouillet5309b0c2015-05-01 16:13:11 -0700236 llvm::llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
237 LLVMInitializeARMTargetInfo();
238 LLVMInitializeARMTarget();
239 LLVMInitializeARMAsmPrinter();
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700240
Jean-Luc Brouillet5309b0c2015-05-01 16:13:11 -0700241 StringSet SavedStrings; // Keeps track of strings to be destroyed at the end.
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700242
Jean-Luc Brouillet5309b0c2015-05-01 16:13:11 -0700243 // Parse the command line arguments and respond to show help & version
244 // commands.
245 llvm::SmallVector<const char *, 16> Inputs;
Jean-Luc Brouilletc2473632015-05-14 13:07:47 -0700246 slang::RSCCOptions Opts;
Jean-Luc Brouillet5309b0c2015-05-01 16:13:11 -0700247 llvm::IntrusiveRefCntPtr<clang::DiagnosticOptions> DiagOpts =
248 new clang::DiagnosticOptions();
249 if (!slang::ParseArguments(llvm::makeArrayRef(argv, argc), Inputs, Opts,
Pirama Arumuga Nainar98cfae42016-03-03 23:56:27 -0800250 *DiagOpts, SavedStrings.getStringSaver())) {
Jean-Luc Brouillet5309b0c2015-05-01 16:13:11 -0700251 // Exits when there's any error occurred during parsing the arguments
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700252 return 1;
Stephen Hines8f4d9722011-12-05 14:14:42 -0800253 }
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700254 if (Opts.mShowHelp) {
Stephen Hines7ac9d0d2014-07-15 16:50:03 -0700255 std::unique_ptr<llvm::opt::OptTable> OptTbl(slang::createRSCCOptTable());
Jean-Luc Brouillet5309b0c2015-05-01 16:13:11 -0700256 const std::string Argv0 = llvm::sys::path::stem(argv[0]);
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700257 OptTbl->PrintHelp(llvm::outs(), Argv0.c_str(),
Stephen Hinesb7d12692011-09-02 18:16:19 -0700258 "Renderscript source compiler");
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700259 return 0;
260 }
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700261 if (Opts.mShowVersion) {
Stephen Hinesba7c6dc2011-09-07 19:57:04 -0700262 llvm_rs_cc_VersionPrinter();
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700263 return 0;
264 }
265
Jean-Luc Brouillet5309b0c2015-05-01 16:13:11 -0700266 // Initialize the diagnostic objects
267 llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> DiagIDs(
268 new clang::DiagnosticIDs());
269 slang::DiagnosticBuffer DiagsBuffer;
270 clang::DiagnosticsEngine DiagEngine(DiagIDs, &*DiagOpts, &DiagsBuffer, false);
271 clang::ProcessWarningOptions(DiagEngine, *DiagOpts);
272 (void)DiagEngine.setSeverityForGroup(clang::diag::Flavor::WarningOrError,
273 "implicit-function-declaration",
274 clang::diag::Severity::Error);
Xusong Wang19ea0eb2020-11-10 11:25:05 -0800275 emitDeprecationWarning(&DiagEngine);
Jean-Luc Brouillet5309b0c2015-05-01 16:13:11 -0700276
277 // Report error if no input file
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700278 if (Inputs.empty()) {
Logan Chien9207a2e2011-10-21 15:39:28 +0800279 DiagEngine.Report(clang::diag::err_drv_no_input_files);
Jean-Luc Brouillet5309b0c2015-05-01 16:13:11 -0700280 llvm::errs() << DiagsBuffer.str();
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700281 return 1;
282 }
283
Jean-Luc Brouillet5309b0c2015-05-01 16:13:11 -0700284 llvm::install_fatal_error_handler(LLVMErrorHandler, &DiagEngine);
Zonr Changcf6af6a2010-10-12 12:38:51 +0800285
David Grossd80e58b2017-07-24 11:41:12 -0700286 slang::ReflectionState Reflection;
287
Jean-Luc Brouillet5309b0c2015-05-01 16:13:11 -0700288 // Compile the 32 bit version
289 NamePairList IOFiles32;
290 NamePairList DepFiles32;
291 makeFileList(&IOFiles32, &DepFiles32, Inputs, Opts, &SavedStrings);
292
Miao Wangbfbdd982015-06-25 15:04:35 -0700293 int CompileFailed = 0;
294 // Handle 32-bit case for Java and C++ reflection.
295 // For Java, both 32bit and 64bit will be generated.
296 // For C++, either 64bit or 32bit will be generated based on the target.
297 if (Opts.mEmit3264 || Opts.mBitWidth == 32) {
298 std::unique_ptr<slang::Slang> Compiler(
299 new slang::Slang(32, &DiagEngine, &DiagsBuffer));
300 CompileFailed =
David Grossd80e58b2017-07-24 11:41:12 -0700301 !Compiler->compile(IOFiles32, IOFiles32, DepFiles32, Opts, *DiagOpts, &Reflection);
Miao Wangbfbdd982015-06-25 15:04:35 -0700302 }
Zonr Chang641558f2010-10-12 21:07:06 +0800303
Stephen Hines9ae18b22014-06-10 23:53:00 -0700304 // Handle the 64-bit case too!
Miao Wangbfbdd982015-06-25 15:04:35 -0700305 bool needEmit64 = Opts.mEmit3264 || Opts.mBitWidth == 64;
306 if (needEmit64 && !CompileFailed) {
Stephen Hines9ae18b22014-06-10 23:53:00 -0700307 Opts.mBitWidth = 64;
Jean-Luc Brouillet5309b0c2015-05-01 16:13:11 -0700308 NamePairList IOFiles64;
309 NamePairList DepFiles64;
310 makeFileList(&IOFiles64, &DepFiles64, Inputs, Opts, &SavedStrings);
311
312 std::unique_ptr<slang::Slang> Compiler(
313 new slang::Slang(64, &DiagEngine, &DiagsBuffer));
314 CompileFailed =
David Grossd80e58b2017-07-24 11:41:12 -0700315 !Compiler->compile(IOFiles64, IOFiles32, DepFiles64, Opts, *DiagOpts, &Reflection);
Zonr Changcf6af6a2010-10-12 12:38:51 +0800316 }
317
Jean-Luc Brouillet5309b0c2015-05-01 16:13:11 -0700318 llvm::errs() << DiagsBuffer.str();
319 llvm::remove_fatal_error_handler();
Stephen Hinesd7f0ea22011-02-22 17:45:19 -0800320 return CompileFailed;
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700321}