Rewrite the command line tool "bcc."

Get rid of deprecated BCC C APIs using in bcc command.

For the usage, see "/system/bin/bcc -help" for detail.

Change-Id: I26dccd63034624509cb7311c72a45b908fc6d92c
diff --git a/tools/bcc/Android.mk b/tools/bcc/Android.mk
index 54482f6..cc2b30e 100644
--- a/tools/bcc/Android.mk
+++ b/tools/bcc/Android.mk
@@ -29,9 +29,11 @@
 LOCAL_STATIC_LIBRARIES := libLLVMSupport
 LOCAL_SHARED_LIBRARIES := libbcc
 LOCAL_LDLIBS = -ldl
-LOCAL_SRC_FILES := main.cpp
+LOCAL_SRC_FILES := Main.cpp
 
 include $(LIBBCC_HOST_BUILD_MK)
+include $(LIBBCC_GEN_CONFIG_MK)
+include $(LLVM_HOST_BUILD_MK)
 include $(BUILD_HOST_EXECUTABLE)
 
 # Executable for target
@@ -42,10 +44,12 @@
 LOCAL_MODULE_TAGS := optional
 LOCAL_MODULE_CLASS := EXECUTABLES
 
-LOCAL_SRC_FILES := main.cpp
+LOCAL_SRC_FILES := Main.cpp
 
-LOCAL_SHARED_LIBRARIES := libdl libstlport libbcinfo libbcc
+LOCAL_SHARED_LIBRARIES := libdl libstlport libbcinfo libbcc libutils libcutils
 
 include external/stlport/libstlport.mk
 include $(LIBBCC_DEVICE_BUILD_MK)
+include $(LIBBCC_GEN_CONFIG_MK)
+include $(LLVM_DEVICE_BUILD_MK)
 include $(BUILD_EXECUTABLE)
diff --git a/tools/bcc/Main.cpp b/tools/bcc/Main.cpp
new file mode 100644
index 0000000..44da31c
--- /dev/null
+++ b/tools/bcc/Main.cpp
@@ -0,0 +1,435 @@
+/*
+ * Copyright 2012, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <string>
+#include <vector>
+
+#include <stdlib.h>
+
+#include <llvm/ADT/STLExtras.h>
+#include <llvm/ADT/SmallString.h>
+#include <llvm/Config/config.h>
+#include <llvm/Support/CommandLine.h>
+#include <llvm/Support/FileSystem.h>
+#include <llvm/Support/Path.h>
+#include <llvm/Support/raw_ostream.h>
+#include <llvm/Support/system_error.h>
+
+#include <bcc/BCCContext.h>
+#include <bcc/Compiler.h>
+#include <bcc/Config/BuildInfo.h>
+#include <bcc/Config/Config.h>
+#include <bcc/ExecutionEngine/BCCRuntimeSymbolResolver.h>
+#include <bcc/ExecutionEngine/ObjectLoader.h>
+#include <bcc/ExecutionEngine/SymbolResolverProxy.h>
+#include <bcc/ExecutionEngine/SymbolResolvers.h>
+#include <bcc/Script.h>
+#include <bcc/Source.h>
+#include <bcc/Support/CompilerConfig.h>
+#include <bcc/Support/Initialization.h>
+#include <bcc/Support/InputFile.h>
+#include <bcc/Support/OutputFile.h>
+#include <bcc/Support/TargetCompilerConfigs.h>
+
+using namespace bcc;
+
+//===----------------------------------------------------------------------===//
+// General Options
+//===----------------------------------------------------------------------===//
+namespace {
+
+llvm::cl::list<std::string>
+OptInputFilenames(llvm::cl::Positional, llvm::cl::OneOrMore,
+                  llvm::cl::desc("<input bitcode files>"));
+
+llvm::cl::opt<std::string>
+OptOutputFilename("o", llvm::cl::desc("Specify the output filename"),
+                  llvm::cl::value_desc("filename"));
+
+#ifdef TARGET_BUILD
+const std::string OptTargetTriple(DEFAULT_TARGET_TRIPLE_STRING);
+#else
+llvm::cl::opt<std::string>
+OptTargetTriple("mtriple",
+                llvm::cl::desc("Specify the target triple (default: "
+                               DEFAULT_TARGET_TRIPLE_STRING ")"),
+                llvm::cl::init(DEFAULT_TARGET_TRIPLE_STRING),
+                llvm::cl::value_desc("triple"));
+
+llvm::cl::alias OptTargetTripleC("C", llvm::cl::NotHidden,
+                                 llvm::cl::desc("Alias for -mtriple"),
+                                 llvm::cl::aliasopt(OptTargetTriple));
+#endif
+
+//===----------------------------------------------------------------------===//
+// Compiler Options
+//===----------------------------------------------------------------------===//
+llvm::cl::opt<bool>
+OptPIC("fPIC", llvm::cl::desc("Generate fully relocatable, position independent"
+                              " code"));
+
+llvm::cl::opt<char>
+OptOptLevel("O", llvm::cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
+                                "(default: -O2)"),
+            llvm::cl::Prefix, llvm::cl::ZeroOrMore, llvm::cl::init('2'));
+
+llvm::cl::opt<bool>
+OptC("c", llvm::cl::desc("Compile and assemble, but do not link."));
+
+//===----------------------------------------------------------------------===//
+// Linker Options
+//===----------------------------------------------------------------------===//
+// FIXME: this option will be removed in the future when MCLinker is capable
+//        of generating shared library directly from given bitcode. It only
+//        takes effect when -shared is supplied.
+llvm::cl::opt<std::string>
+OptImmObjectOutput("or", llvm::cl::desc("Specify the filename for output the "
+                                        "intermediate relocatable when linking "
+                                        "the input bitcode to the shared "
+                                        "library"), llvm::cl::ValueRequired);
+
+llvm::cl::opt<bool>
+OptShared("shared", llvm::cl::desc("Create a shared library from input bitcode "
+                                   "files"));
+
+
+//===----------------------------------------------------------------------===//
+// Loader Options
+//===----------------------------------------------------------------------===//
+llvm::cl::opt<bool>
+OptRunEntry("R", llvm::cl::desc("Run the entry method after successfully load "
+                                "and compile."));
+
+llvm::cl::opt<std::string>
+OptEntryFunction("entry-function", llvm::cl::desc("Specify the entry function "
+                                                  "for -R (default: main)"),
+                 llvm::cl::value_desc("function"), llvm::cl::init("main"));
+
+llvm::cl::opt<bool>
+OptEnableGDB("enable-gdb", llvm::cl::desc("Enable GDB JIT debugging when "
+                                          "runs the entry method"));
+
+llvm::cl::list<std::string>
+OptRuntimeLibs("load", llvm::cl::desc("Specify the shared libraries for "
+                                      "execution (e.g., -load=c will search "
+                                      "and load libc.so for execution)"),
+               llvm::cl::ZeroOrMore, llvm::cl::value_desc("namespec"));
+
+// Override "bcc -version" since the LLVM version information is not correct on
+// Android build.
+void BCCVersionPrinter() {
+  llvm::raw_ostream &os = llvm::outs();
+  os << "libbcc (The Android Open Source Project, http://www.android.com/):\n"
+     << "  Build time: " << BuildInfo::GetBuildTime() << "\n"
+     << "  Build revision: " << BuildInfo::GetBuildRev() << "\n"
+     << "  Build source blob: " << BuildInfo::GetBuildSourceBlob() << "\n"
+     << "  Default target: " << DEFAULT_TARGET_TRIPLE_STRING << "\n";
+
+  os << "\n";
+
+  os << "LLVM (http://llvm.org/):\n"
+     << "  Version: " << PACKAGE_VERSION << "\n";
+  return;
+}
+
+} // end anonymous namespace
+
+static inline
+Script *PrepareScript(BCCContext &pContext,
+                      const llvm::cl::list<std::string> &pBitcodeFiles) {
+  Script *result = NULL;
+
+  for (unsigned i = 0; i < pBitcodeFiles.size(); i++) {
+    const std::string &input_bitcode = pBitcodeFiles[i];
+    Source *source = Source::CreateFromFile(pContext, input_bitcode);
+    if (source == NULL) {
+      llvm::errs() << "Failed to load llvm module from file `" << input_bitcode
+                   << "'!\n";
+      return NULL;
+    }
+
+    if (result != NULL) {
+      if (!result->mergeSource(*source, /* pPreserveSource */false)) {
+        llvm::errs() << "Failed to merge the llvm module `" << input_bitcode
+                     << "' to compile!\n";
+        delete source;
+        return NULL;
+      }
+    } else {
+      result = new (std::nothrow) Script(*source);
+      if (result == NULL) {
+        llvm::errs() << "Out of memory when create script for file `"
+                     << input_bitcode << "'!\n";
+        delete source;
+        return NULL;
+      }
+    }
+  }
+
+  return result;
+}
+
+static inline
+bool ConfigCompiler(Compiler &pCompiler) {
+  CompilerConfig *config = NULL;
+
+#ifdef TARGET_BUILD
+  config = new (std::nothrow) DefaultCompilerConfig();
+#else
+  config = new (std::nothrow) CompilerConfig(OptTargetTriple);
+#endif
+  if (config == NULL) {
+    llvm::errs() << "Out of memory when create the compiler configuration!\n";
+    return false;
+  }
+
+  // Setup the config according to the valud of command line option.
+  if (OptPIC) {
+    config->setRelocationModel(llvm::Reloc::PIC_);
+  }
+  switch (OptOptLevel) {
+    case '0': config->setOptimizationLevel(llvm::CodeGenOpt::None); break;
+    case '1': config->setOptimizationLevel(llvm::CodeGenOpt::Less); break;
+    case '3': config->setOptimizationLevel(llvm::CodeGenOpt::Aggressive); break;
+    case '2':
+    default: {
+      config->setOptimizationLevel(llvm::CodeGenOpt::Default);
+      break;
+    }
+  }
+
+  Compiler::ErrorCode result = pCompiler.config(*config);
+
+  delete config;
+
+  if (result != Compiler::kSuccess) {
+    llvm::errs() << "Failed to configure the compiler! (detail: "
+                 << Compiler::GetErrorString(result) << ")\n";
+    return false;
+  }
+
+  return true;
+}
+
+#define DEFAULT_OUTPUT_PATH   "/sdcard/a.out"
+static inline
+std::string DetermineOutputFilename(const std::string &pOutputPath) {
+  if (!pOutputPath.empty()) {
+    return pOutputPath;
+  }
+
+  // User doesn't specify the value to -o.
+  if (OptInputFilenames.size() > 1) {
+    llvm::errs() << "Use " DEFAULT_OUTPUT_PATH " for output file!\n";
+    return DEFAULT_OUTPUT_PATH;
+  }
+
+  // There's only one input bitcode file.
+  const std::string &input_path = OptInputFilenames[0];
+  llvm::SmallString<200> output_path(input_path);
+
+  llvm::error_code err = llvm::sys::fs::make_absolute(output_path);
+  if (err != llvm::errc::success) {
+    llvm::errs() << "Failed to determine the absolute path of `" << input_path
+                 << "'! (detail: " << err.message() << ")\n";
+    return "";
+  }
+
+  if (OptC) {
+    // -c was specified. Replace the extension to .o.
+    llvm::sys::path::replace_extension(output_path, "o");
+  } else {
+    // Use a.out under current working directory when compile executable or
+    // shared library.
+    llvm::sys::path::remove_filename(output_path);
+    llvm::sys::path::append(output_path, "a.out");
+  }
+
+  return output_path.c_str();
+}
+
+static inline
+bool CompileScript(Compiler &pCompiler, Script &pScript,
+                   const std::string &pOutputPath) {
+  // Open the output file.
+  OutputFile output_file(pOutputPath);
+
+  if (output_file.hasError()) {
+    llvm::errs() << "Failed to open the output file `" << pOutputPath
+                 << "'! (detail: " << output_file.getErrorMessage() << ")\n";
+    return false;
+  }
+
+  // Run the compiler.
+  Compiler::ErrorCode result = pCompiler.compile(pScript, output_file);
+  if (result != Compiler::kSuccess) {
+    llvm::errs() << "Fatal error during compilation (detail: "
+                 << Compiler::GetErrorString(result) << ".)\n";
+    return false;
+  }
+
+  return true;
+}
+
+static inline
+bool PrepareRuntimes(std::vector<SymbolResolverInterface *> &pRuntimes) {
+  llvm::SmallVector<const char *, 2> search_paths;
+
+#ifdef TARGET_BUILD
+  search_paths.push_back("/system/lib/");
+#else
+  search_paths.push_back("/lib/");
+  search_paths.push_back("/usr/lib/");
+#endif
+
+  // Most of the following lines comes from llvm/tools/llvm-ld.cpp.
+  for (unsigned i = 0; i < OptRuntimeLibs.size(); i++) {
+    const std::string &lib = OptRuntimeLibs[i];
+    llvm::sys::Path lib_path;
+    for (llvm::SmallVectorImpl<const char *>::const_iterator
+             search_path_iter = search_paths.begin(),
+             search_path_end = search_paths.end();
+         search_path_iter != search_path_end; search_path_iter++) {
+
+      lib_path = *search_path_iter;
+      lib_path.appendComponent("lib" + lib);
+      lib_path.appendSuffix(llvm::sys::Path::GetDLLSuffix());
+
+      if (lib_path.isEmpty()) {
+        if (!lib_path.isDynamicLibrary()) {
+          lib_path = llvm::sys::Path();
+        } else {
+          break;
+        }
+      }
+    } // for each search_paths
+    if (lib_path.isEmpty()) {
+      // FIXME: llvm::sys::Path::FindLibrary(...) is able to consume
+      //        'const std::string &' instead of 'std::string &'.
+      std::string lib_tmp = lib;
+      lib_path = llvm::sys::Path::FindLibrary(lib_tmp);
+    }
+    if (lib_path.isEmpty()) {
+      llvm::errs() << "Unable to find `lib" << lib << "' for execution!\n";
+      llvm::DeleteContainerPointers(pRuntimes);
+      return false;
+    } else {
+      DyldSymbolResolver *dyld_resolver =
+          new (std::nothrow) DyldSymbolResolver(lib_path.str().c_str());
+
+      if (dyld_resolver != NULL) {
+        pRuntimes.push_back(dyld_resolver);
+      } else {
+        llvm::errs() << "Out of memory when load `" << lib_path.str() << "'!\n";
+        llvm::DeleteContainerPointers(pRuntimes);
+        return false;
+      }
+    }
+  } // for each OptRuntimeLibs
+
+  return true;
+}
+
+static inline
+bool LoadAndRun(const std::string &pOutputExecutable) {
+  SymbolResolverProxy runtime_resolver;
+
+  // Include compiler runtime.
+  BCCRuntimeSymbolResolver bcc_runtimes;
+  runtime_resolver.chainResolver(bcc_runtimes);
+
+  // Open the output file for execution.
+  InputFile input_exec(pOutputExecutable);
+  if (input_exec.hasError()) {
+    llvm::errs() << "Failed to open the executable `" << pOutputExecutable
+                 << "'! (detail: " << input_exec.getErrorMessage() << ")\n";
+    return false;
+  }
+
+  // Load the runtime libraries given in command line.
+  std::vector<SymbolResolverInterface *> lib_runtimes;
+  if (!PrepareRuntimes(lib_runtimes)) {
+    return false;
+  }
+
+  for (std::vector<SymbolResolverInterface *>::const_iterator
+           librt_iter = lib_runtimes.begin(), librt_end = lib_runtimes.end();
+       librt_iter != librt_end; librt_iter++) {
+    runtime_resolver.chainResolver(*(*librt_iter));
+  }
+
+  // Load the output file.
+  ObjectLoader *loader = ObjectLoader::Load(input_exec, runtime_resolver,
+                                            OptEnableGDB);
+  if (loader == NULL) {
+    llvm::errs() << "Failed to load `" << pOutputExecutable << "'!\n";
+    llvm::DeleteContainerPointers(lib_runtimes);
+    return false;
+  }
+
+  // Retrieve the address of entry function.
+  void *entry = loader->getSymbolAddress(OptEntryFunction.c_str());
+  if (entry == NULL) {
+    llvm::errs() << "Couldn't find entry method `" << OptEntryFunction
+                 << "' in " << pOutputExecutable << "' for execution!\n";
+    delete loader;
+    llvm::DeleteContainerPointers(lib_runtimes);
+    return false;
+  }
+
+  // Execute the entry function.
+  int run_result = reinterpret_cast<int (*)()>(entry)();
+  llvm::errs() << "result: " << run_result << "\n";
+
+  // Clean up.
+  delete loader;
+  llvm::DeleteContainerPointers(lib_runtimes);
+
+  return true;
+}
+
+int main(int argc, char **argv) {
+  llvm::cl::SetVersionPrinter(BCCVersionPrinter);
+  llvm::cl::ParseCommandLineOptions(argc, argv);
+  init::Initialize();
+
+  BCCContext context;
+  Compiler compiler;
+
+  Script *script = PrepareScript(context, OptInputFilenames);
+  if (script == NULL) {
+    return EXIT_FAILURE;
+  }
+
+  if (!ConfigCompiler(compiler)) {
+    return EXIT_FAILURE;
+  }
+
+  std::string OutputFilename = DetermineOutputFilename(OptOutputFilename);
+  if (OutputFilename.empty()) {
+    return EXIT_FAILURE;
+  }
+
+  if (!CompileScript(compiler, *script, OutputFilename)) {
+    return EXIT_FAILURE;
+  }
+
+  if (OptRunEntry && !LoadAndRun(OutputFilename)) {
+    return EXIT_FAILURE;
+  }
+
+  return EXIT_SUCCESS;
+}
diff --git a/tools/bcc/main.cpp b/tools/bcc/main.cpp
deleted file mode 100644
index a0d4e5a..0000000
--- a/tools/bcc/main.cpp
+++ /dev/null
@@ -1,384 +0,0 @@
-/*
- * Copyright 2010-2012, The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include <ctype.h>
-#include <dlfcn.h>
-#include <stdint.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include <errno.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-
-#include <bcc/bcc.h>
-
-#define DEFAULT_OUTPUT_FILENAME "a.out"
-
-typedef int (*RootPtr)();
-
-// This is a separate function so it can easily be set by breakpoint in gdb.
-static int run(RootPtr rootFunc) {
-  return rootFunc();
-}
-
-enum OutputType {
-  OT_Executable,
-  OT_Relocatable,
-  OT_SharedObject
-};
-
-enum OutputType OutType = OT_Executable;
-enum bccRelocModelEnum OutRelocModel = bccRelocDefault;
-const char *InFile = NULL;
-const char *OutFile = NULL;
-const char *IntermediateOutFile = NULL;
-bool RunRoot = false;
-
-struct OptionInfo {
-  const char *option_name;
-
-  // min_option_argc is the minimum number of arguments this option should
-  // have. This is for sanity check before invoking processing function.
-  unsigned min_option_argc;
-
-  const char *argument_desc;
-  const char *help_message;
-
-  // The function to process this option. Return the number of arguments it
-  // consumed or < 0 if there's an error during the processing.
-  int (*process)(int argc, char **arg);
-};
-
-// forward declaration of option processing functions
-#if defined(__HOST__)
-static int optSetTriple(int, char **);
-#endif
-static int optSetInput(int, char **);
-static int optSetOutput(int, char **);
-static int optSetIntermediateOutput(int, char **);
-static int optOutputReloc(int, char **);
-static int optSetOutputPIC(int, char **);
-static int optSetOutputShared(int, char **);
-static int optRunRoot(int, char **);
-static int optHelp(int, char **);
-
-static const struct OptionInfo Options[] = {
-#if defined(__HOST__)
-  { "C", 1, "triple", "set the triple string.",
-    optSetTriple },
-#endif
-
-  { "c", 0, NULL, "compile and assemble, but do not link.",
-    optOutputReloc },
-
-  { "fPIC", 0, NULL,  "Generate position-independent code, if possible.",
-    optSetOutputPIC },
-
-  { "o", 1, "output", "write the native result to an output file.",
-    optSetOutput },
-
-  // FIXME: this option will be removed in the future when MCLinker is capable
-  //        of generating shared library directly from given bitcode. It only
-  //        takes effect when -shared is supplied.
-  { "or", 1, NULL, "set the output filename for the intermediate relocatable.",
-    optSetIntermediateOutput },
-
-
-  { "shared", 0, NULL, "create a shared library.",
-    optSetOutputShared },
-
-  { "R", 0, NULL, "run root() method after a successful load and compile.",
-    optRunRoot },
-
-  { "h", 0, NULL, "print this help.",
-    optHelp },
-};
-#define NUM_OPTIONS (sizeof(Options) / sizeof(struct OptionInfo))
-
-static int parseOption(int argc, char** argv) {
-  if (argc <= 1) {
-    optHelp(argc, argv);
-    return 0; // unreachable
-  }
-
-  // argv[i] is the current processing argument from command line
-  int i = 1;
-  while (i < argc) {
-    const unsigned left_argc = argc - i - 1;
-
-    if (argv[i][0] == '-') {
-      // Find the corresponding OptionInfo object
-      unsigned opt_idx = 0;
-      while (opt_idx < NUM_OPTIONS) {
-        if (::strcmp(&argv[i][1], Options[opt_idx].option_name) == 0) {
-          const struct OptionInfo *cur_option = &Options[opt_idx];
-          if (left_argc < cur_option->min_option_argc) {
-            fprintf(stderr, "%s: '%s' requires at least %u arguments", argv[0],
-                    cur_option->option_name, cur_option->min_option_argc);
-            return 1;
-          }
-
-          int result = cur_option->process(left_argc, &argv[i]);
-          if (result >= 0) {
-            // consume the used arguments
-            i += result;
-          } else {
-            // error occurs
-            return 1;
-          }
-
-          break;
-        }
-        ++opt_idx;
-      }
-      if (opt_idx >= NUM_OPTIONS) {
-        fprintf(stderr, "%s: unrecognized option '%s'", argv[0], argv[i]);
-        return 1;
-      }
-    } else {
-      if (InFile == NULL) {
-        optSetInput(left_argc, &argv[i]);
-      } else {
-        fprintf(stderr, "%s: only a single input file is allowed currently.",
-                argv[0]);
-        return 1;
-      }
-    }
-    i++;
-  }
-
-  return 0;
-}
-
-static BCCScriptRef loadScript() {
-  if (!InFile) {
-    fprintf(stderr, "input file required.\n");
-    return NULL;
-  }
-
-  BCCScriptRef script = bccCreateScript();
-
-  if (bccReadFile(script, InFile, /* flags */BCC_SKIP_DEP_SHA1) != 0) {
-    fprintf(stderr, "bcc: FAILS to read bitcode.");
-    bccDisposeScript(script);
-    return NULL;
-  }
-
-  char *output = NULL;
-
-  if (OutFile != NULL) {
-    // Copy the outFile since we're going to modify it
-    size_t outFileLen = strlen(OutFile);
-    output = new char [outFileLen + 1];
-    strncpy(output, OutFile, outFileLen);
-  } else {
-    if (OutType == OT_Executable) {
-      output = new char [(sizeof(DEFAULT_OUTPUT_FILENAME) - 1) + 1];
-      strncpy(output, DEFAULT_OUTPUT_FILENAME,
-                  sizeof(DEFAULT_OUTPUT_FILENAME) - 1);
-    } else {
-      size_t inFileLen = strlen(InFile);
-      output = new char [inFileLen + 3 /* ensure there's room for .so */ + 1];
-      strncpy(output, InFile, inFileLen);
-
-      char *fileExtension = strrchr(output, '.');
-      if (fileExtension == NULL) {
-        // append suffix
-        fileExtension = output + inFileLen;
-        *fileExtension = '.';
-      }
-
-      fileExtension++;  // skip '.'
-      if (OutType == OT_Relocatable) {
-        *fileExtension++ = 'o';
-      } else /* must be OT_SharedObject */{
-        *fileExtension++ = 's';
-        *fileExtension++ = 'o';
-      }
-      *fileExtension++ = '\0';
-    }
-  }
-
-  int bccResult = 0;
-  const char *errMsg = NULL;
-  switch (OutType) {
-    case OT_Executable: {
-      bccResult = 1;
-      errMsg = "generation of executable is unsupported currently.";
-      break;
-    }
-    case OT_Relocatable: {
-      bccResult = bccPrepareRelocatable(script, output, OutRelocModel, 0);
-      errMsg = "failed to generate relocatable.";
-      break;
-    }
-    case OT_SharedObject: {
-      if (IntermediateOutFile != NULL) {
-        bccResult =
-            bccPrepareRelocatable(script, IntermediateOutFile, bccRelocPIC, 0);
-        errMsg = "failed to generate intermediate relocatable.";
-      }
-
-      if (bccResult == 0) {
-        bccResult =
-            bccPrepareSharedObject(script, IntermediateOutFile, output, 0);
-        errMsg = "failed to generate shared library.";
-      }
-      break;
-    }
-  }
-
-  delete [] output;
-
-  if (bccResult == 0) {
-    return script;
-  } else {
-    fprintf(stderr, "bcc: %s\n", errMsg);
-    bccDisposeScript(script);
-    return NULL;
-  }
-}
-
-static int runRoot(BCCScriptRef script) {
-  RootPtr rootPointer =
-      reinterpret_cast<RootPtr>(bccGetFuncAddr(script, "main"));
-
-  if (!rootPointer) {
-    rootPointer = reinterpret_cast<RootPtr>(bccGetFuncAddr(script, "root"));
-  }
-  if (!rootPointer) {
-    rootPointer = reinterpret_cast<RootPtr>(bccGetFuncAddr(script, "_Z4rootv"));
-  }
-  if (!rootPointer) {
-    fprintf(stderr, "Could not find root or main or mangled root.\n");
-    return 1;
-  }
-
-  fprintf(stderr, "Executing compiled code:\n");
-
-  int result = run(rootPointer);
-  fprintf(stderr, "result: %d\n", result);
-
-  return 0;
-}
-
-int main(int argc, char** argv) {
-  if(parseOption(argc, argv)) {
-    return 1;
-  }
-
-  BCCScriptRef script;
-
-  if((script = loadScript()) == NULL) {
-    return 2;
-  }
-
-  if(RunRoot && runRoot(script)) {
-    return 6;
-  }
-
-  bccDisposeScript(script);
-
-  return 0;
-}
-
-/*
- * Functions to process the command line option.
- */
-#if defined(__HOST__)
-static int optSetTriple(int, char **) {
-  // FIXME: Will relax this contraint to let user designate the triple on the
-  //        host later.
-  //TARGET_TRIPLE_STRING = arg[1];
-  return 1;
-}
-#endif
-
-static int optSetInput(int, char **arg) {
-  // Check the input file path
-  struct stat statInFile;
-  if (stat(arg[0], &statInFile) < 0) {
-    fprintf(stderr, "Unable to stat input file: %s\n", strerror(errno));
-    return -1;
-  }
-
-  if (!S_ISREG(statInFile.st_mode)) {
-    fprintf(stderr, "Input file should be a regular file.\n");
-    return -1;
-  }
-
-  InFile = arg[0];
-  return 0;
-}
-
-static int optSetOutput(int, char **arg) {
-  char *lastSlash = strrchr(arg[1], '/');
-  if ((lastSlash != NULL) && *(lastSlash + 1) == '\0') {
-    fprintf(stderr, "bcc: output file should not be a directory.");
-    return -1;
-  }
-
-  OutFile = arg[1];
-  return 1;
-}
-
-static int optSetIntermediateOutput(int, char **arg) {
-  char *lastSlash = strrchr(arg[1], '/');
-  if ((lastSlash != NULL) && *(lastSlash + 1) == '\0') {
-    fprintf(stderr, "bcc: output intermediate file should not be a directory.");
-    return -1;
-  }
-
-  IntermediateOutFile = arg[1];
-  return 1;
-}
-
-static int optOutputReloc(int, char **) {
-  OutType = OT_Relocatable;
-  return 0;
-}
-
-static int optSetOutputShared(int, char **) {
-  OutType = OT_SharedObject;
-  return 0;
-}
-
-static int optSetOutputPIC(int, char **) {
-  OutRelocModel = bccRelocPIC;
-  return 0;
-}
-
-static int optRunRoot(int, char **) {
-  RunRoot = true;
-  return 0;
-}
-
-static int optHelp(int, char **) {
-  printf("Usage: bcc [OPTION]... [input file]\n\n");
-  for (unsigned i = 0; i < NUM_OPTIONS; i++) {
-    const struct OptionInfo *opt = &Options[i];
-
-    printf("\t-%s", opt->option_name);
-    if (opt->argument_desc)
-      printf(" %s ", opt->argument_desc);
-    else
-      printf(" \t ");
-    printf("\t%s\n", opt->help_message);
-  }
-  exit(0);
-}