blob: e7300d2610fcd07c8fd8d81cb06c28e6b2e2cc3f [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"
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070020#include "clang/Frontend/TextDiagnosticPrinter.h"
Stephen Hines8f4d9722011-12-05 14:14:42 -080021#include "clang/Frontend/Utils.h"
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070022
Stephen Hinese639eb52010-11-08 19:27:20 -080023#include "llvm/ADT/SmallVector.h"
Loganbe274822011-02-16 22:02:54 +080024#include "llvm/ADT/IntrusiveRefCntPtr.h"
25#include "llvm/ADT/OwningPtr.h"
Stephen Hinese639eb52010-11-08 19:27:20 -080026
Stephen Hinesa1f95ee2013-08-09 01:26:08 -070027#include "llvm/Option/OptTable.h"
Stephen Hinese639eb52010-11-08 19:27:20 -080028#include "llvm/Support/CommandLine.h"
29#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"
Loganbe274822011-02-16 22:02:54 +080033#include "llvm/Support/system_error.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.h"
41#include "slang_rs_reflect_utils.h"
42
Stephen Hines8b5c5c62014-06-06 18:03:18 -070043#include <list>
44#include <set>
45#include <string>
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070046
47// SaveStringInSet, ExpandArgsFromBuf and ExpandArgv are all copied from
48// $(CLANG_ROOT)/tools/driver/driver.cpp for processing argc/argv passed in
49// main().
50static inline const char *SaveStringInSet(std::set<std::string> &SavedStrings,
51 llvm::StringRef S) {
52 return SavedStrings.insert(S).first->c_str();
53}
54static void ExpandArgsFromBuf(const char *Arg,
55 llvm::SmallVectorImpl<const char*> &ArgVector,
56 std::set<std::string> &SavedStrings);
57static void ExpandArgv(int argc, const char **argv,
58 llvm::SmallVectorImpl<const char*> &ArgVector,
59 std::set<std::string> &SavedStrings);
60
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070061static const char *DetermineOutputFile(const std::string &OutputDir,
Stephen Hines9ae18b22014-06-10 23:53:00 -070062 const std::string &PathSuffix,
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070063 const char *InputFile,
Stephen Hinese639eb52010-11-08 19:27:20 -080064 slang::Slang::OutputType OutputType,
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070065 std::set<std::string> &SavedStrings) {
Stephen Hinese639eb52010-11-08 19:27:20 -080066 if (OutputType == slang::Slang::OT_Nothing)
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070067 return "/dev/null";
68
69 std::string OutputFile(OutputDir);
70
Stephen Hines7f5704e2014-06-10 18:06:50 -070071 // Append '/' to Opts.mBitcodeOutputDir if not presents
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070072 if (!OutputFile.empty() &&
Raphael8d5a2f62011-02-08 00:15:05 -080073 (OutputFile[OutputFile.size() - 1]) != OS_PATH_SEPARATOR)
74 OutputFile.append(1, OS_PATH_SEPARATOR);
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070075
Stephen Hines9ae18b22014-06-10 23:53:00 -070076 if (!PathSuffix.empty()) {
77 OutputFile.append(PathSuffix);
78 OutputFile.append(1, OS_PATH_SEPARATOR);
79 }
80
Stephen Hinese639eb52010-11-08 19:27:20 -080081 if (OutputType == slang::Slang::OT_Dependency) {
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070082 // The build system wants the .d file name stem to be exactly the same as
83 // the source .rs file, instead of the .bc file.
Stephen Hinese639eb52010-11-08 19:27:20 -080084 OutputFile.append(slang::RSSlangReflectUtils::GetFileNameStem(InputFile));
85 } else {
86 OutputFile.append(
87 slang::RSSlangReflectUtils::BCFileNameFromRSFileName(InputFile));
88 }
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070089
Stephen Hinese639eb52010-11-08 19:27:20 -080090 switch (OutputType) {
91 case slang::Slang::OT_Dependency: {
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070092 OutputFile.append(".d");
93 break;
94 }
Stephen Hinese639eb52010-11-08 19:27:20 -080095 case slang::Slang::OT_Assembly: {
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070096 OutputFile.append(".S");
97 break;
98 }
Stephen Hinese639eb52010-11-08 19:27:20 -080099 case slang::Slang::OT_LLVMAssembly: {
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700100 OutputFile.append(".ll");
101 break;
102 }
Stephen Hinese639eb52010-11-08 19:27:20 -0800103 case slang::Slang::OT_Object: {
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700104 OutputFile.append(".o");
105 break;
106 }
Stephen Hinese639eb52010-11-08 19:27:20 -0800107 case slang::Slang::OT_Bitcode: {
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700108 OutputFile.append(".bc");
109 break;
110 }
Stephen Hinese639eb52010-11-08 19:27:20 -0800111 case slang::Slang::OT_Nothing:
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700112 default: {
Stephen Hines6e6578a2011-02-07 18:05:48 -0800113 slangAssert(false && "Invalid output type!");
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700114 }
115 }
116
117 return SaveStringInSet(SavedStrings, OutputFile);
118}
119
Stephen Hines9ae18b22014-06-10 23:53:00 -0700120typedef std::list<std::pair<const char*, const char*> > NamePairList;
121
122/*
123 * Compile the Inputs.
124 *
125 * Returns 0 on success and nonzero on failure.
126 *
127 * IOFiles - list of (foo.rs, foo.bc) pairs of input/output files.
128 * IOFiles32 - list of input/output pairs for 32-bit compilation.
129 * Inputs - input filenames.
130 * Opts - options controlling compilation.
131 * DiagEngine - Clang diagnostic engine (for creating diagnostics).
132 * DiagClient - Slang diagnostic consumer (collects and displays diagnostics).
133 * SavedStrings - expanded strings copied from argv source input files.
134 *
135 * We populate IOFiles dynamically while working through the list of Inputs.
136 * On any 64-bit compilation, we pass back in the 32-bit pairs of files as
137 * IOFiles32. This allows the 64-bit compiler to later bundle up both the
138 * 32-bit and 64-bit bitcode outputs to be included in the final reflected
139 * source code that is emitted.
140 */
141static int compileFiles(NamePairList *IOFiles, NamePairList *IOFiles32,
142 const llvm::SmallVector<const char*, 16> &Inputs, slang::RSCCOptions &Opts,
143 clang::DiagnosticsEngine *DiagEngine, slang::DiagnosticBuffer *DiagClient,
144 std::set<std::string> *SavedStrings) {
145 NamePairList DepFiles;
146 std::string PathSuffix = "";
147
148 // In our mixed 32/64-bit path, we need to suffix our files differently for
149 // both 32-bit and 64-bit versions.
150 if (Opts.mEmit3264) {
151 if (Opts.mBitWidth == 64) {
152 PathSuffix = "bc64";
153 } else {
154 PathSuffix = "bc32";
155 }
156 }
157
158 for (int i = 0, e = Inputs.size(); i != e; i++) {
159 const char *InputFile = Inputs[i];
160
161 const char *BCOutputFile = DetermineOutputFile(Opts.mBitcodeOutputDir,
162 PathSuffix, InputFile,
163 slang::Slang::OT_Bitcode,
164 *SavedStrings);
165 const char *OutputFile = BCOutputFile;
166
167 if (Opts.mEmitDependency) {
168 // The dependency file is always emitted without a PathSuffix.
169 // Collisions between 32-bit and 64-bit files don't make a difference,
170 // because they share the same sources/dependencies.
171 const char *DepOutputFile =
172 DetermineOutputFile(Opts.mDependencyOutputDir, "", InputFile,
173 slang::Slang::OT_Dependency, *SavedStrings);
174 if (Opts.mOutputType == slang::Slang::OT_Dependency) {
175 OutputFile = DepOutputFile;
176 }
177
178 DepFiles.push_back(std::make_pair(BCOutputFile, DepOutputFile));
179 }
180
181 IOFiles->push_back(std::make_pair(InputFile, OutputFile));
182 }
183
184 llvm::OwningPtr<slang::SlangRS> Compiler(new slang::SlangRS());
185 Compiler->init(Opts.mBitWidth, DiagEngine, DiagClient);
186 int CompileFailed = !Compiler->compile(*IOFiles, *IOFiles32, DepFiles, Opts);
187 Compiler->reset();
188 return CompileFailed;
189}
190
Stephen Hinesba7c6dc2011-09-07 19:57:04 -0700191#define str(s) #s
192#define wrap_str(s) str(s)
193static void llvm_rs_cc_VersionPrinter() {
194 llvm::raw_ostream &OS = llvm::outs();
195 OS << "llvm-rs-cc: Renderscript compiler\n"
196 << " (http://developer.android.com/guide/topics/renderscript)\n"
197 << " based on LLVM (http://llvm.org):\n";
198 OS << " Built " << __DATE__ << " (" << __TIME__ ").\n";
199 OS << " Target APIs: " << SLANG_MINIMUM_TARGET_API << " - "
200 << SLANG_MAXIMUM_TARGET_API;
201 OS << "\n Build type: " << wrap_str(TARGET_BUILD_VARIANT);
202#ifndef __DISABLE_ASSERTS
203 OS << " with assertions";
204#endif
205 OS << ".\n";
Stephen Hinesba7c6dc2011-09-07 19:57:04 -0700206}
Stephen Hines8f4d9722011-12-05 14:14:42 -0800207#undef wrap_str
208#undef str
Stephen Hinesba7c6dc2011-09-07 19:57:04 -0700209
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700210int main(int argc, const char **argv) {
211 std::set<std::string> SavedStrings;
212 llvm::SmallVector<const char*, 256> ArgVector;
Stephen Hines8b5c5c62014-06-06 18:03:18 -0700213 slang::RSCCOptions Opts;
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700214 llvm::SmallVector<const char*, 16> Inputs;
215 std::string Argv0;
216
Stephen Hines9ae18b22014-06-10 23:53:00 -0700217 llvm::llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700218
219 ExpandArgv(argc, argv, ArgVector, SavedStrings);
220
221 // Argv0
Logan Chien6f4e0a92011-03-10 01:10:25 +0800222 Argv0 = llvm::sys::path::stem(ArgVector[0]);
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700223
224 // Setup diagnostic engine
Stephen Hines8f4d9722011-12-05 14:14:42 -0800225 slang::DiagnosticBuffer *DiagClient = new slang::DiagnosticBuffer();
Loganbe274822011-02-16 22:02:54 +0800226
227 llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> DiagIDs(
228 new clang::DiagnosticIDs());
229
Stephen Hines23c43582013-01-09 20:02:04 -0800230 llvm::IntrusiveRefCntPtr<clang::DiagnosticOptions> DiagOpts(
231 new clang::DiagnosticOptions());
232 clang::DiagnosticsEngine DiagEngine(DiagIDs, &*DiagOpts, DiagClient, true);
Logan Chien9207a2e2011-10-21 15:39:28 +0800233
Stephen Hinese639eb52010-11-08 19:27:20 -0800234 slang::Slang::GlobalInitialization();
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700235
Stephen Hines8b5c5c62014-06-06 18:03:18 -0700236 slang::ParseArguments(ArgVector, Inputs, Opts, DiagEngine);
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700237
238 // Exits when there's any error occurred during parsing the arguments
Stephen Hines8f4d9722011-12-05 14:14:42 -0800239 if (DiagEngine.hasErrorOccurred()) {
240 llvm::errs() << DiagClient->str();
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700241 return 1;
Stephen Hines8f4d9722011-12-05 14:14:42 -0800242 }
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700243
244 if (Opts.mShowHelp) {
Stephen Hines8b5c5c62014-06-06 18:03:18 -0700245 llvm::OwningPtr<llvm::opt::OptTable> OptTbl(slang::createRSCCOptTable());
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700246 OptTbl->PrintHelp(llvm::outs(), Argv0.c_str(),
Stephen Hinesb7d12692011-09-02 18:16:19 -0700247 "Renderscript source compiler");
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700248 return 0;
249 }
250
251 if (Opts.mShowVersion) {
Stephen Hinesba7c6dc2011-09-07 19:57:04 -0700252 llvm_rs_cc_VersionPrinter();
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700253 return 0;
254 }
255
256 // No input file
257 if (Inputs.empty()) {
Logan Chien9207a2e2011-10-21 15:39:28 +0800258 DiagEngine.Report(clang::diag::err_drv_no_input_files);
Stephen Hines8f4d9722011-12-05 14:14:42 -0800259 llvm::errs() << DiagClient->str();
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700260 return 1;
261 }
262
Zonr Changcf6af6a2010-10-12 12:38:51 +0800263 // Prepare input data for RS compiler.
Stephen Hines9ae18b22014-06-10 23:53:00 -0700264 NamePairList IOFiles64;
265 NamePairList IOFiles32;
Zonr Changcf6af6a2010-10-12 12:38:51 +0800266
Stephen Hines9ae18b22014-06-10 23:53:00 -0700267 int CompileFailed = compileFiles(&IOFiles32, &IOFiles32, Inputs, Opts,
268 &DiagEngine, DiagClient, &SavedStrings);
Zonr Chang641558f2010-10-12 21:07:06 +0800269
Stephen Hines9ae18b22014-06-10 23:53:00 -0700270 // Handle the 64-bit case too!
271 if (Opts.mEmit3264 && !CompileFailed) {
272 Opts.mBitWidth = 64;
273 CompileFailed = compileFiles(&IOFiles64, &IOFiles32, Inputs, Opts,
274 &DiagEngine, DiagClient, &SavedStrings);
Zonr Changcf6af6a2010-10-12 12:38:51 +0800275 }
276
Stephen Hinesd7f0ea22011-02-22 17:45:19 -0800277 return CompileFailed;
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700278}
279
280///////////////////////////////////////////////////////////////////////////////
281
282// ExpandArgsFromBuf -
283static void ExpandArgsFromBuf(const char *Arg,
284 llvm::SmallVectorImpl<const char*> &ArgVector,
285 std::set<std::string> &SavedStrings) {
286 const char *FName = Arg + 1;
Stephen Hines75d47182014-05-28 09:43:18 -0700287 std::unique_ptr<llvm::MemoryBuffer> MemBuf;
Loganbe274822011-02-16 22:02:54 +0800288 if (llvm::MemoryBuffer::getFile(FName, MemBuf)) {
289 // Unable to open the file
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700290 ArgVector.push_back(SaveStringInSet(SavedStrings, Arg));
291 return;
292 }
293
294 const char *Buf = MemBuf->getBufferStart();
295 char InQuote = ' ';
296 std::string CurArg;
297
298 for (const char *P = Buf; ; ++P) {
299 if (*P == '\0' || (isspace(*P) && InQuote == ' ')) {
300 if (!CurArg.empty()) {
301 if (CurArg[0] != '@') {
302 ArgVector.push_back(SaveStringInSet(SavedStrings, CurArg));
303 } else {
304 ExpandArgsFromBuf(CurArg.c_str(), ArgVector, SavedStrings);
305 }
306
307 CurArg = "";
308 }
309 if (*P == '\0')
310 break;
311 else
312 continue;
313 }
314
315 if (isspace(*P)) {
316 if (InQuote != ' ')
317 CurArg.push_back(*P);
318 continue;
319 }
320
321 if (*P == '"' || *P == '\'') {
322 if (InQuote == *P)
323 InQuote = ' ';
324 else if (InQuote == ' ')
325 InQuote = *P;
326 else
327 CurArg.push_back(*P);
328 continue;
329 }
330
331 if (*P == '\\') {
332 ++P;
333 if (*P != '\0')
334 CurArg.push_back(*P);
335 continue;
336 }
337 CurArg.push_back(*P);
338 }
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700339}
340
341// ExpandArgsFromBuf -
342static void ExpandArgv(int argc, const char **argv,
343 llvm::SmallVectorImpl<const char*> &ArgVector,
344 std::set<std::string> &SavedStrings) {
345 for (int i = 0; i < argc; ++i) {
346 const char *Arg = argv[i];
347 if (Arg[0] != '@') {
348 ArgVector.push_back(SaveStringInSet(SavedStrings, std::string(Arg)));
349 continue;
350 }
351
352 ExpandArgsFromBuf(Arg, ArgVector, SavedStrings);
353 }
354}