blob: a51e672c712eff32856bf8fbcd018e041396b7c3 [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
Stephen Hines8b5c5c62014-06-06 18:03:18 -070017#include "clang/Basic/DiagnosticOptions.h"
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070018#include "clang/Driver/DriverDiagnostic.h"
Stephen Hinesa1f95ee2013-08-09 01:26:08 -070019#include "clang/Driver/Options.h"
Jean-Luc Brouillet5309b0c2015-05-01 16:13:11 -070020#include "clang/Frontend/CompilerInvocation.h"
21#include "clang/Frontend/FrontendDiagnostic.h"
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070022#include "clang/Frontend/TextDiagnosticPrinter.h"
Stephen Hines8f4d9722011-12-05 14:14:42 -080023#include "clang/Frontend/Utils.h"
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070024
Stephen Hinese639eb52010-11-08 19:27:20 -080025#include "llvm/ADT/SmallVector.h"
Loganbe274822011-02-16 22:02:54 +080026#include "llvm/ADT/IntrusiveRefCntPtr.h"
Stephen Hinese639eb52010-11-08 19:27:20 -080027
Stephen Hinesa1f95ee2013-08-09 01:26:08 -070028#include "llvm/Option/OptTable.h"
Pirama Arumuga Nainar1906a002015-06-29 10:30:41 -070029#include "llvm/Support/Allocator.h"
Stephen Hinese639eb52010-11-08 19:27:20 -080030#include "llvm/Support/ManagedStatic.h"
31#include "llvm/Support/MemoryBuffer.h"
Loganbe274822011-02-16 22:02:54 +080032#include "llvm/Support/Path.h"
Stephen Hinesba7c6dc2011-09-07 19:57:04 -070033#include "llvm/Support/raw_ostream.h"
Jean-Luc Brouillet5309b0c2015-05-01 16:13:11 -070034#include "llvm/Support/Signals.h"
Pirama Arumuga Nainar1906a002015-06-29 10:30:41 -070035#include "llvm/Support/StringSaver.h"
Pirama Arumuga Nainarb6a14352016-07-26 11:39:47 -070036#include "llvm/Support/TargetSelect.h"
mkopec1c460b372012-01-09 11:21:50 -050037#include "llvm/Target/TargetMachine.h"
Stephen Hinese639eb52010-11-08 19:27:20 -080038
Elliott Hughes9534de12015-07-29 14:07:37 -070039#include "os_sep.h"
Stephen Hines8b5c5c62014-06-06 18:03:18 -070040#include "rs_cc_options.h"
Stephen Hinese639eb52010-11-08 19:27:20 -080041#include "slang.h"
Stephen Hines6e6578a2011-02-07 18:05:48 -080042#include "slang_assert.h"
Stephen Hines8f4d9722011-12-05 14:14:42 -080043#include "slang_diagnostic_buffer.h"
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070044#include "slang_rs_reflect_utils.h"
David Grossd80e58b2017-07-24 11:41:12 -070045#include "slang_rs_reflection_state.h"
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070046
Stephen Hines8b5c5c62014-06-06 18:03:18 -070047#include <list>
48#include <set>
49#include <string>
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070050
Jean-Luc Brouilletc2473632015-05-14 13:07:47 -070051namespace {
Pirama Arumuga Nainar98cfae42016-03-03 23:56:27 -080052class StringSet {
Jean-Luc Brouilletc2473632015-05-14 13:07:47 -070053public:
Pirama Arumuga Nainar98cfae42016-03-03 23:56:27 -080054 const char *save(const char *Str) {
55 return Strings.save(Str);
Jean-Luc Brouilletc2473632015-05-14 13:07:47 -070056 }
57
Pirama Arumuga Nainar98cfae42016-03-03 23:56:27 -080058 StringSet() : Strings(A), A() {}
59
60 llvm::StringSaver & getStringSaver() { return Strings; }
Pirama Arumuga Nainar1906a002015-06-29 10:30:41 -070061
Jean-Luc Brouilletc2473632015-05-14 13:07:47 -070062private:
Pirama Arumuga Nainar98cfae42016-03-03 23:56:27 -080063 llvm::StringSaver Strings;
Pirama Arumuga Nainar1906a002015-06-29 10:30:41 -070064 llvm::BumpPtrAllocator A;
Jean-Luc Brouilletc2473632015-05-14 13:07:47 -070065};
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070066}
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070067
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070068static const char *DetermineOutputFile(const std::string &OutputDir,
Stephen Hines9ae18b22014-06-10 23:53:00 -070069 const std::string &PathSuffix,
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070070 const char *InputFile,
Stephen Hinese639eb52010-11-08 19:27:20 -080071 slang::Slang::OutputType OutputType,
Jean-Luc Brouilletc2473632015-05-14 13:07:47 -070072 StringSet *SavedStrings) {
Stephen Hinese639eb52010-11-08 19:27:20 -080073 if (OutputType == slang::Slang::OT_Nothing)
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070074 return "/dev/null";
75
76 std::string OutputFile(OutputDir);
77
Stephen Hines7f5704e2014-06-10 18:06:50 -070078 // Append '/' to Opts.mBitcodeOutputDir if not presents
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070079 if (!OutputFile.empty() &&
Raphael8d5a2f62011-02-08 00:15:05 -080080 (OutputFile[OutputFile.size() - 1]) != OS_PATH_SEPARATOR)
81 OutputFile.append(1, OS_PATH_SEPARATOR);
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070082
Stephen Hines9ae18b22014-06-10 23:53:00 -070083 if (!PathSuffix.empty()) {
84 OutputFile.append(PathSuffix);
85 OutputFile.append(1, OS_PATH_SEPARATOR);
86 }
87
Stephen Hinese639eb52010-11-08 19:27:20 -080088 if (OutputType == slang::Slang::OT_Dependency) {
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070089 // The build system wants the .d file name stem to be exactly the same as
90 // the source .rs file, instead of the .bc file.
Stephen Hinese639eb52010-11-08 19:27:20 -080091 OutputFile.append(slang::RSSlangReflectUtils::GetFileNameStem(InputFile));
92 } else {
93 OutputFile.append(
94 slang::RSSlangReflectUtils::BCFileNameFromRSFileName(InputFile));
95 }
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070096
Stephen Hinese639eb52010-11-08 19:27:20 -080097 switch (OutputType) {
98 case slang::Slang::OT_Dependency: {
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070099 OutputFile.append(".d");
100 break;
101 }
Stephen Hinese639eb52010-11-08 19:27:20 -0800102 case slang::Slang::OT_Assembly: {
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700103 OutputFile.append(".S");
104 break;
105 }
Stephen Hinese639eb52010-11-08 19:27:20 -0800106 case slang::Slang::OT_LLVMAssembly: {
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700107 OutputFile.append(".ll");
108 break;
109 }
Stephen Hinese639eb52010-11-08 19:27:20 -0800110 case slang::Slang::OT_Object: {
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700111 OutputFile.append(".o");
112 break;
113 }
Stephen Hinese639eb52010-11-08 19:27:20 -0800114 case slang::Slang::OT_Bitcode: {
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700115 OutputFile.append(".bc");
116 break;
117 }
Stephen Hinese639eb52010-11-08 19:27:20 -0800118 case slang::Slang::OT_Nothing:
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700119 default: {
Stephen Hines6e6578a2011-02-07 18:05:48 -0800120 slangAssert(false && "Invalid output type!");
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700121 }
122 }
123
Pirama Arumuga Nainar1906a002015-06-29 10:30:41 -0700124 return SavedStrings->save(OutputFile.c_str());
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700125}
126
Stephen Hines9ae18b22014-06-10 23:53:00 -0700127typedef std::list<std::pair<const char*, const char*> > NamePairList;
128
129/*
130 * Compile the Inputs.
131 *
132 * Returns 0 on success and nonzero on failure.
133 *
134 * IOFiles - list of (foo.rs, foo.bc) pairs of input/output files.
135 * IOFiles32 - list of input/output pairs for 32-bit compilation.
136 * Inputs - input filenames.
137 * Opts - options controlling compilation.
138 * DiagEngine - Clang diagnostic engine (for creating diagnostics).
139 * DiagClient - Slang diagnostic consumer (collects and displays diagnostics).
140 * SavedStrings - expanded strings copied from argv source input files.
141 *
142 * We populate IOFiles dynamically while working through the list of Inputs.
143 * On any 64-bit compilation, we pass back in the 32-bit pairs of files as
144 * IOFiles32. This allows the 64-bit compiler to later bundle up both the
145 * 32-bit and 64-bit bitcode outputs to be included in the final reflected
146 * source code that is emitted.
147 */
Jean-Luc Brouillet5309b0c2015-05-01 16:13:11 -0700148static void makeFileList(NamePairList *IOFiles, NamePairList *DepFiles,
Stephen Hines9ae18b22014-06-10 23:53:00 -0700149 const llvm::SmallVector<const char*, 16> &Inputs, slang::RSCCOptions &Opts,
Jean-Luc Brouilletc2473632015-05-14 13:07:47 -0700150 StringSet *SavedStrings) {
Stephen Hines9ae18b22014-06-10 23:53:00 -0700151 std::string PathSuffix = "";
Stephen Hines9ae18b22014-06-10 23:53:00 -0700152 // In our mixed 32/64-bit path, we need to suffix our files differently for
153 // both 32-bit and 64-bit versions.
154 if (Opts.mEmit3264) {
155 if (Opts.mBitWidth == 64) {
156 PathSuffix = "bc64";
157 } else {
158 PathSuffix = "bc32";
159 }
160 }
161
162 for (int i = 0, e = Inputs.size(); i != e; i++) {
163 const char *InputFile = Inputs[i];
164
165 const char *BCOutputFile = DetermineOutputFile(Opts.mBitcodeOutputDir,
166 PathSuffix, InputFile,
Yang Ni76837a12015-04-28 17:30:51 -0700167 Opts.mOutputType,
Jean-Luc Brouilletc2473632015-05-14 13:07:47 -0700168 SavedStrings);
Stephen Hines9ae18b22014-06-10 23:53:00 -0700169 const char *OutputFile = BCOutputFile;
170
171 if (Opts.mEmitDependency) {
172 // The dependency file is always emitted without a PathSuffix.
173 // Collisions between 32-bit and 64-bit files don't make a difference,
174 // because they share the same sources/dependencies.
175 const char *DepOutputFile =
176 DetermineOutputFile(Opts.mDependencyOutputDir, "", InputFile,
Jean-Luc Brouilletc2473632015-05-14 13:07:47 -0700177 slang::Slang::OT_Dependency, SavedStrings);
Stephen Hines9ae18b22014-06-10 23:53:00 -0700178 if (Opts.mOutputType == slang::Slang::OT_Dependency) {
179 OutputFile = DepOutputFile;
180 }
181
Jean-Luc Brouillet5309b0c2015-05-01 16:13:11 -0700182 DepFiles->push_back(std::make_pair(BCOutputFile, DepOutputFile));
Stephen Hines9ae18b22014-06-10 23:53:00 -0700183 }
184
185 IOFiles->push_back(std::make_pair(InputFile, OutputFile));
186 }
Stephen Hines9ae18b22014-06-10 23:53:00 -0700187}
188
Stephen Hinesba7c6dc2011-09-07 19:57:04 -0700189#define str(s) #s
190#define wrap_str(s) str(s)
191static void llvm_rs_cc_VersionPrinter() {
192 llvm::raw_ostream &OS = llvm::outs();
193 OS << "llvm-rs-cc: Renderscript compiler\n"
194 << " (http://developer.android.com/guide/topics/renderscript)\n"
195 << " based on LLVM (http://llvm.org):\n";
Stephen Hinesba7c6dc2011-09-07 19:57:04 -0700196 OS << " Target APIs: " << SLANG_MINIMUM_TARGET_API << " - "
197 << SLANG_MAXIMUM_TARGET_API;
198 OS << "\n Build type: " << wrap_str(TARGET_BUILD_VARIANT);
199#ifndef __DISABLE_ASSERTS
200 OS << " with assertions";
201#endif
202 OS << ".\n";
Stephen Hinesba7c6dc2011-09-07 19:57:04 -0700203}
Stephen Hines8f4d9722011-12-05 14:14:42 -0800204#undef wrap_str
205#undef str
Stephen Hinesba7c6dc2011-09-07 19:57:04 -0700206
Jean-Luc Brouillet5309b0c2015-05-01 16:13:11 -0700207static void LLVMErrorHandler(void *UserData, const std::string &Message,
208 bool GenCrashDialog) {
209 clang::DiagnosticsEngine *DiagEngine =
210 static_cast<clang::DiagnosticsEngine *>(UserData);
211
212 DiagEngine->Report(clang::diag::err_fe_error_backend) << Message;
213
214 // Run the interrupt handlers to make sure any special cleanups get done, in
215 // particular that we remove files registered with RemoveFileOnSignal.
216 llvm::sys::RunInterruptHandlers();
217
218 exit(1);
219}
220
Steven Moreland722b5242019-09-24 18:39:00 -0700221// TODO(b/37755219): detect leaks
222extern "C" const char *__asan_default_options() {
223 return "detect_leaks=0";
224}
225
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700226int main(int argc, const char **argv) {
Jean-Luc Brouillet5309b0c2015-05-01 16:13:11 -0700227 llvm::llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
228 LLVMInitializeARMTargetInfo();
229 LLVMInitializeARMTarget();
230 LLVMInitializeARMAsmPrinter();
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700231
Jean-Luc Brouillet5309b0c2015-05-01 16:13:11 -0700232 StringSet SavedStrings; // Keeps track of strings to be destroyed at the end.
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700233
Jean-Luc Brouillet5309b0c2015-05-01 16:13:11 -0700234 // Parse the command line arguments and respond to show help & version
235 // commands.
236 llvm::SmallVector<const char *, 16> Inputs;
Jean-Luc Brouilletc2473632015-05-14 13:07:47 -0700237 slang::RSCCOptions Opts;
Jean-Luc Brouillet5309b0c2015-05-01 16:13:11 -0700238 llvm::IntrusiveRefCntPtr<clang::DiagnosticOptions> DiagOpts =
239 new clang::DiagnosticOptions();
240 if (!slang::ParseArguments(llvm::makeArrayRef(argv, argc), Inputs, Opts,
Pirama Arumuga Nainar98cfae42016-03-03 23:56:27 -0800241 *DiagOpts, SavedStrings.getStringSaver())) {
Jean-Luc Brouillet5309b0c2015-05-01 16:13:11 -0700242 // Exits when there's any error occurred during parsing the arguments
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700243 return 1;
Stephen Hines8f4d9722011-12-05 14:14:42 -0800244 }
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700245 if (Opts.mShowHelp) {
Stephen Hines7ac9d0d2014-07-15 16:50:03 -0700246 std::unique_ptr<llvm::opt::OptTable> OptTbl(slang::createRSCCOptTable());
Jean-Luc Brouillet5309b0c2015-05-01 16:13:11 -0700247 const std::string Argv0 = llvm::sys::path::stem(argv[0]);
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700248 OptTbl->PrintHelp(llvm::outs(), Argv0.c_str(),
Stephen Hinesb7d12692011-09-02 18:16:19 -0700249 "Renderscript source compiler");
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700250 return 0;
251 }
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700252 if (Opts.mShowVersion) {
Stephen Hinesba7c6dc2011-09-07 19:57:04 -0700253 llvm_rs_cc_VersionPrinter();
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700254 return 0;
255 }
256
Jean-Luc Brouillet5309b0c2015-05-01 16:13:11 -0700257 // Initialize the diagnostic objects
258 llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> DiagIDs(
259 new clang::DiagnosticIDs());
260 slang::DiagnosticBuffer DiagsBuffer;
261 clang::DiagnosticsEngine DiagEngine(DiagIDs, &*DiagOpts, &DiagsBuffer, false);
262 clang::ProcessWarningOptions(DiagEngine, *DiagOpts);
263 (void)DiagEngine.setSeverityForGroup(clang::diag::Flavor::WarningOrError,
264 "implicit-function-declaration",
265 clang::diag::Severity::Error);
266
267 // Report error if no input file
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700268 if (Inputs.empty()) {
Logan Chien9207a2e2011-10-21 15:39:28 +0800269 DiagEngine.Report(clang::diag::err_drv_no_input_files);
Jean-Luc Brouillet5309b0c2015-05-01 16:13:11 -0700270 llvm::errs() << DiagsBuffer.str();
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700271 return 1;
272 }
273
Jean-Luc Brouillet5309b0c2015-05-01 16:13:11 -0700274 llvm::install_fatal_error_handler(LLVMErrorHandler, &DiagEngine);
Zonr Changcf6af6a2010-10-12 12:38:51 +0800275
David Grossd80e58b2017-07-24 11:41:12 -0700276 slang::ReflectionState Reflection;
277
Jean-Luc Brouillet5309b0c2015-05-01 16:13:11 -0700278 // Compile the 32 bit version
279 NamePairList IOFiles32;
280 NamePairList DepFiles32;
281 makeFileList(&IOFiles32, &DepFiles32, Inputs, Opts, &SavedStrings);
282
Miao Wangbfbdd982015-06-25 15:04:35 -0700283 int CompileFailed = 0;
284 // Handle 32-bit case for Java and C++ reflection.
285 // For Java, both 32bit and 64bit will be generated.
286 // For C++, either 64bit or 32bit will be generated based on the target.
287 if (Opts.mEmit3264 || Opts.mBitWidth == 32) {
288 std::unique_ptr<slang::Slang> Compiler(
289 new slang::Slang(32, &DiagEngine, &DiagsBuffer));
290 CompileFailed =
David Grossd80e58b2017-07-24 11:41:12 -0700291 !Compiler->compile(IOFiles32, IOFiles32, DepFiles32, Opts, *DiagOpts, &Reflection);
Miao Wangbfbdd982015-06-25 15:04:35 -0700292 }
Zonr Chang641558f2010-10-12 21:07:06 +0800293
Stephen Hines9ae18b22014-06-10 23:53:00 -0700294 // Handle the 64-bit case too!
Miao Wangbfbdd982015-06-25 15:04:35 -0700295 bool needEmit64 = Opts.mEmit3264 || Opts.mBitWidth == 64;
296 if (needEmit64 && !CompileFailed) {
Stephen Hines9ae18b22014-06-10 23:53:00 -0700297 Opts.mBitWidth = 64;
Jean-Luc Brouillet5309b0c2015-05-01 16:13:11 -0700298 NamePairList IOFiles64;
299 NamePairList DepFiles64;
300 makeFileList(&IOFiles64, &DepFiles64, Inputs, Opts, &SavedStrings);
301
302 std::unique_ptr<slang::Slang> Compiler(
303 new slang::Slang(64, &DiagEngine, &DiagsBuffer));
304 CompileFailed =
David Grossd80e58b2017-07-24 11:41:12 -0700305 !Compiler->compile(IOFiles64, IOFiles32, DepFiles64, Opts, *DiagOpts, &Reflection);
Zonr Changcf6af6a2010-10-12 12:38:51 +0800306 }
307
Jean-Luc Brouillet5309b0c2015-05-01 16:13:11 -0700308 llvm::errs() << DiagsBuffer.str();
309 llvm::remove_fatal_error_handler();
Stephen Hinesd7f0ea22011-02-22 17:45:19 -0800310 return CompileFailed;
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700311}