blob: 905f2e751ce4ebeece7aea1a399108125249dc13 [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 Brouillet666b0a72015-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"
Stephen Hinese639eb52010-11-08 19:27:20 -080029#include "llvm/Support/ManagedStatic.h"
30#include "llvm/Support/MemoryBuffer.h"
Loganbe274822011-02-16 22:02:54 +080031#include "llvm/Support/Path.h"
Stephen Hinesba7c6dc2011-09-07 19:57:04 -070032#include "llvm/Support/raw_ostream.h"
Jean-Luc Brouillet666b0a72015-05-01 16:13:11 -070033#include "llvm/Support/Signals.h"
mkopec1c460b372012-01-09 11:21:50 -050034#include "llvm/Target/TargetMachine.h"
Stephen Hinese639eb52010-11-08 19:27:20 -080035
Stephen Hines8b5c5c62014-06-06 18:03:18 -070036#include "rs_cc_options.h"
Stephen Hinese639eb52010-11-08 19:27:20 -080037#include "slang.h"
Stephen Hines6e6578a2011-02-07 18:05:48 -080038#include "slang_assert.h"
Stephen Hines8f4d9722011-12-05 14:14:42 -080039#include "slang_diagnostic_buffer.h"
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070040#include "slang_rs_reflect_utils.h"
41
Stephen Hines8b5c5c62014-06-06 18:03:18 -070042#include <list>
43#include <set>
44#include <string>
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070045
Jean-Luc Brouillet21f7c382015-05-14 13:07:47 -070046namespace {
47class StringSet : public llvm::cl::StringSaver {
48public:
49 const char *SaveString(const char *Str) override {
50 return Strings.insert(Str).first->c_str();
51 }
52
53private:
54 std::set<std::string> Strings;
55};
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070056}
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070057
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070058static const char *DetermineOutputFile(const std::string &OutputDir,
Stephen Hines9ae18b22014-06-10 23:53:00 -070059 const std::string &PathSuffix,
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070060 const char *InputFile,
Stephen Hinese639eb52010-11-08 19:27:20 -080061 slang::Slang::OutputType OutputType,
Jean-Luc Brouillet21f7c382015-05-14 13:07:47 -070062 StringSet *SavedStrings) {
Stephen Hinese639eb52010-11-08 19:27:20 -080063 if (OutputType == slang::Slang::OT_Nothing)
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070064 return "/dev/null";
65
66 std::string OutputFile(OutputDir);
67
Stephen Hines7f5704e2014-06-10 18:06:50 -070068 // Append '/' to Opts.mBitcodeOutputDir if not presents
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070069 if (!OutputFile.empty() &&
Raphael8d5a2f62011-02-08 00:15:05 -080070 (OutputFile[OutputFile.size() - 1]) != OS_PATH_SEPARATOR)
71 OutputFile.append(1, OS_PATH_SEPARATOR);
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070072
Stephen Hines9ae18b22014-06-10 23:53:00 -070073 if (!PathSuffix.empty()) {
74 OutputFile.append(PathSuffix);
75 OutputFile.append(1, OS_PATH_SEPARATOR);
76 }
77
Stephen Hinese639eb52010-11-08 19:27:20 -080078 if (OutputType == slang::Slang::OT_Dependency) {
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070079 // The build system wants the .d file name stem to be exactly the same as
80 // the source .rs file, instead of the .bc file.
Stephen Hinese639eb52010-11-08 19:27:20 -080081 OutputFile.append(slang::RSSlangReflectUtils::GetFileNameStem(InputFile));
82 } else {
83 OutputFile.append(
84 slang::RSSlangReflectUtils::BCFileNameFromRSFileName(InputFile));
85 }
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070086
Stephen Hinese639eb52010-11-08 19:27:20 -080087 switch (OutputType) {
88 case slang::Slang::OT_Dependency: {
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070089 OutputFile.append(".d");
90 break;
91 }
Stephen Hinese639eb52010-11-08 19:27:20 -080092 case slang::Slang::OT_Assembly: {
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070093 OutputFile.append(".S");
94 break;
95 }
Stephen Hinese639eb52010-11-08 19:27:20 -080096 case slang::Slang::OT_LLVMAssembly: {
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070097 OutputFile.append(".ll");
98 break;
99 }
Stephen Hinese639eb52010-11-08 19:27:20 -0800100 case slang::Slang::OT_Object: {
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700101 OutputFile.append(".o");
102 break;
103 }
Stephen Hinese639eb52010-11-08 19:27:20 -0800104 case slang::Slang::OT_Bitcode: {
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700105 OutputFile.append(".bc");
106 break;
107 }
Stephen Hinese639eb52010-11-08 19:27:20 -0800108 case slang::Slang::OT_Nothing:
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700109 default: {
Stephen Hines6e6578a2011-02-07 18:05:48 -0800110 slangAssert(false && "Invalid output type!");
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700111 }
112 }
113
Jean-Luc Brouillet21f7c382015-05-14 13:07:47 -0700114 return SavedStrings->SaveString(OutputFile.c_str());
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700115}
116
Stephen Hines9ae18b22014-06-10 23:53:00 -0700117typedef std::list<std::pair<const char*, const char*> > NamePairList;
118
119/*
120 * Compile the Inputs.
121 *
122 * Returns 0 on success and nonzero on failure.
123 *
124 * IOFiles - list of (foo.rs, foo.bc) pairs of input/output files.
125 * IOFiles32 - list of input/output pairs for 32-bit compilation.
126 * Inputs - input filenames.
127 * Opts - options controlling compilation.
128 * DiagEngine - Clang diagnostic engine (for creating diagnostics).
129 * DiagClient - Slang diagnostic consumer (collects and displays diagnostics).
130 * SavedStrings - expanded strings copied from argv source input files.
131 *
132 * We populate IOFiles dynamically while working through the list of Inputs.
133 * On any 64-bit compilation, we pass back in the 32-bit pairs of files as
134 * IOFiles32. This allows the 64-bit compiler to later bundle up both the
135 * 32-bit and 64-bit bitcode outputs to be included in the final reflected
136 * source code that is emitted.
137 */
Jean-Luc Brouillet666b0a72015-05-01 16:13:11 -0700138static void makeFileList(NamePairList *IOFiles, NamePairList *DepFiles,
Stephen Hines9ae18b22014-06-10 23:53:00 -0700139 const llvm::SmallVector<const char*, 16> &Inputs, slang::RSCCOptions &Opts,
Jean-Luc Brouillet21f7c382015-05-14 13:07:47 -0700140 StringSet *SavedStrings) {
Stephen Hines9ae18b22014-06-10 23:53:00 -0700141 std::string PathSuffix = "";
Stephen Hines9ae18b22014-06-10 23:53:00 -0700142 // In our mixed 32/64-bit path, we need to suffix our files differently for
143 // both 32-bit and 64-bit versions.
144 if (Opts.mEmit3264) {
145 if (Opts.mBitWidth == 64) {
146 PathSuffix = "bc64";
147 } else {
148 PathSuffix = "bc32";
149 }
150 }
151
152 for (int i = 0, e = Inputs.size(); i != e; i++) {
153 const char *InputFile = Inputs[i];
154
155 const char *BCOutputFile = DetermineOutputFile(Opts.mBitcodeOutputDir,
156 PathSuffix, InputFile,
Yang Ni18d52a62015-04-28 17:30:51 -0700157 Opts.mOutputType,
Jean-Luc Brouillet21f7c382015-05-14 13:07:47 -0700158 SavedStrings);
Stephen Hines9ae18b22014-06-10 23:53:00 -0700159 const char *OutputFile = BCOutputFile;
160
161 if (Opts.mEmitDependency) {
162 // The dependency file is always emitted without a PathSuffix.
163 // Collisions between 32-bit and 64-bit files don't make a difference,
164 // because they share the same sources/dependencies.
165 const char *DepOutputFile =
166 DetermineOutputFile(Opts.mDependencyOutputDir, "", InputFile,
Jean-Luc Brouillet21f7c382015-05-14 13:07:47 -0700167 slang::Slang::OT_Dependency, SavedStrings);
Stephen Hines9ae18b22014-06-10 23:53:00 -0700168 if (Opts.mOutputType == slang::Slang::OT_Dependency) {
169 OutputFile = DepOutputFile;
170 }
171
Jean-Luc Brouillet666b0a72015-05-01 16:13:11 -0700172 DepFiles->push_back(std::make_pair(BCOutputFile, DepOutputFile));
Stephen Hines9ae18b22014-06-10 23:53:00 -0700173 }
174
175 IOFiles->push_back(std::make_pair(InputFile, OutputFile));
176 }
Stephen Hines9ae18b22014-06-10 23:53:00 -0700177}
178
Stephen Hinesba7c6dc2011-09-07 19:57:04 -0700179#define str(s) #s
180#define wrap_str(s) str(s)
181static void llvm_rs_cc_VersionPrinter() {
182 llvm::raw_ostream &OS = llvm::outs();
183 OS << "llvm-rs-cc: Renderscript compiler\n"
184 << " (http://developer.android.com/guide/topics/renderscript)\n"
185 << " based on LLVM (http://llvm.org):\n";
186 OS << " Built " << __DATE__ << " (" << __TIME__ ").\n";
187 OS << " Target APIs: " << SLANG_MINIMUM_TARGET_API << " - "
188 << SLANG_MAXIMUM_TARGET_API;
189 OS << "\n Build type: " << wrap_str(TARGET_BUILD_VARIANT);
190#ifndef __DISABLE_ASSERTS
191 OS << " with assertions";
192#endif
193 OS << ".\n";
Stephen Hinesba7c6dc2011-09-07 19:57:04 -0700194}
Stephen Hines8f4d9722011-12-05 14:14:42 -0800195#undef wrap_str
196#undef str
Stephen Hinesba7c6dc2011-09-07 19:57:04 -0700197
Jean-Luc Brouillet666b0a72015-05-01 16:13:11 -0700198static void LLVMErrorHandler(void *UserData, const std::string &Message,
199 bool GenCrashDialog) {
200 clang::DiagnosticsEngine *DiagEngine =
201 static_cast<clang::DiagnosticsEngine *>(UserData);
202
203 DiagEngine->Report(clang::diag::err_fe_error_backend) << Message;
204
205 // Run the interrupt handlers to make sure any special cleanups get done, in
206 // particular that we remove files registered with RemoveFileOnSignal.
207 llvm::sys::RunInterruptHandlers();
208
209 exit(1);
210}
211
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700212int main(int argc, const char **argv) {
Jean-Luc Brouillet666b0a72015-05-01 16:13:11 -0700213 llvm::llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
214 LLVMInitializeARMTargetInfo();
215 LLVMInitializeARMTarget();
216 LLVMInitializeARMAsmPrinter();
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700217
Jean-Luc Brouillet666b0a72015-05-01 16:13:11 -0700218 StringSet SavedStrings; // Keeps track of strings to be destroyed at the end.
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700219
Jean-Luc Brouillet666b0a72015-05-01 16:13:11 -0700220 // Parse the command line arguments and respond to show help & version
221 // commands.
222 llvm::SmallVector<const char *, 16> Inputs;
Jean-Luc Brouillet21f7c382015-05-14 13:07:47 -0700223 slang::RSCCOptions Opts;
Jean-Luc Brouillet666b0a72015-05-01 16:13:11 -0700224 llvm::IntrusiveRefCntPtr<clang::DiagnosticOptions> DiagOpts =
225 new clang::DiagnosticOptions();
226 if (!slang::ParseArguments(llvm::makeArrayRef(argv, argc), Inputs, Opts,
227 *DiagOpts, SavedStrings)) {
228 // Exits when there's any error occurred during parsing the arguments
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700229 return 1;
Stephen Hines8f4d9722011-12-05 14:14:42 -0800230 }
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700231 if (Opts.mShowHelp) {
Stephen Hines7ac9d0d2014-07-15 16:50:03 -0700232 std::unique_ptr<llvm::opt::OptTable> OptTbl(slang::createRSCCOptTable());
Jean-Luc Brouillet666b0a72015-05-01 16:13:11 -0700233 const std::string Argv0 = llvm::sys::path::stem(argv[0]);
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700234 OptTbl->PrintHelp(llvm::outs(), Argv0.c_str(),
Stephen Hinesb7d12692011-09-02 18:16:19 -0700235 "Renderscript source compiler");
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700236 return 0;
237 }
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700238 if (Opts.mShowVersion) {
Stephen Hinesba7c6dc2011-09-07 19:57:04 -0700239 llvm_rs_cc_VersionPrinter();
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700240 return 0;
241 }
242
Jean-Luc Brouillet666b0a72015-05-01 16:13:11 -0700243 // Initialize the diagnostic objects
244 llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> DiagIDs(
245 new clang::DiagnosticIDs());
246 slang::DiagnosticBuffer DiagsBuffer;
247 clang::DiagnosticsEngine DiagEngine(DiagIDs, &*DiagOpts, &DiagsBuffer, false);
248 clang::ProcessWarningOptions(DiagEngine, *DiagOpts);
249 (void)DiagEngine.setSeverityForGroup(clang::diag::Flavor::WarningOrError,
250 "implicit-function-declaration",
251 clang::diag::Severity::Error);
252
253 // Report error if no input file
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700254 if (Inputs.empty()) {
Logan Chien9207a2e2011-10-21 15:39:28 +0800255 DiagEngine.Report(clang::diag::err_drv_no_input_files);
Jean-Luc Brouillet666b0a72015-05-01 16:13:11 -0700256 llvm::errs() << DiagsBuffer.str();
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700257 return 1;
258 }
259
Jean-Luc Brouillet666b0a72015-05-01 16:13:11 -0700260 llvm::install_fatal_error_handler(LLVMErrorHandler, &DiagEngine);
Zonr Changcf6af6a2010-10-12 12:38:51 +0800261
Jean-Luc Brouillet666b0a72015-05-01 16:13:11 -0700262 // Compile the 32 bit version
263 NamePairList IOFiles32;
264 NamePairList DepFiles32;
265 makeFileList(&IOFiles32, &DepFiles32, Inputs, Opts, &SavedStrings);
266
Miao Wangbb68ab32015-06-25 15:04:35 -0700267 int CompileFailed = 0;
268 // Handle 32-bit case for Java and C++ reflection.
269 // For Java, both 32bit and 64bit will be generated.
270 // For C++, either 64bit or 32bit will be generated based on the target.
271 if (Opts.mEmit3264 || Opts.mBitWidth == 32) {
272 std::unique_ptr<slang::Slang> Compiler(
273 new slang::Slang(32, &DiagEngine, &DiagsBuffer));
274 CompileFailed =
275 !Compiler->compile(IOFiles32, IOFiles32, DepFiles32, Opts, *DiagOpts);
276 }
Zonr Chang641558f2010-10-12 21:07:06 +0800277
Stephen Hines9ae18b22014-06-10 23:53:00 -0700278 // Handle the 64-bit case too!
Miao Wangbb68ab32015-06-25 15:04:35 -0700279 bool needEmit64 = Opts.mEmit3264 || Opts.mBitWidth == 64;
280 if (needEmit64 && !CompileFailed) {
Stephen Hines9ae18b22014-06-10 23:53:00 -0700281 Opts.mBitWidth = 64;
Jean-Luc Brouillet666b0a72015-05-01 16:13:11 -0700282 NamePairList IOFiles64;
283 NamePairList DepFiles64;
284 makeFileList(&IOFiles64, &DepFiles64, Inputs, Opts, &SavedStrings);
285
286 std::unique_ptr<slang::Slang> Compiler(
287 new slang::Slang(64, &DiagEngine, &DiagsBuffer));
288 CompileFailed =
289 !Compiler->compile(IOFiles64, IOFiles32, DepFiles64, Opts, *DiagOpts);
Zonr Changcf6af6a2010-10-12 12:38:51 +0800290 }
291
Jean-Luc Brouillet666b0a72015-05-01 16:13:11 -0700292 llvm::errs() << DiagsBuffer.str();
293 llvm::remove_fatal_error_handler();
Stephen Hinesd7f0ea22011-02-22 17:45:19 -0800294 return CompileFailed;
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700295}