Refactor ABCCompilerDriver to allow target-dependent settings.
Change-Id: Ie46a8838c0b911d5dedf82bc09616dcd728c663d
diff --git a/include/bcc/AndroidBitcode/ABCCompilerDriver.h b/include/bcc/AndroidBitcode/ABCCompilerDriver.h
index 0fa38fe..f43111a 100644
--- a/include/bcc/AndroidBitcode/ABCCompilerDriver.h
+++ b/include/bcc/AndroidBitcode/ABCCompilerDriver.h
@@ -50,11 +50,24 @@
bool link(const Script &pScript, const std::string &input_relocatable,
int pOutputFd);
-public:
- ABCCompilerDriver(const std::string &pTriple,
- const std::string &pAndroidSysroot);
+protected:
+ ABCCompilerDriver(const std::string &pTriple);
- ~ABCCompilerDriver();
+public:
+ static ABCCompilerDriver *Create(const std::string &pTriple);
+
+ virtual ~ABCCompilerDriver();
+
+ inline const std::string &getAndroidSysroot() const {
+ return mAndroidSysroot;
+ }
+ inline void setAndroidSysroot(const std::string &pAndroidSysroot) {
+ mAndroidSysroot = pAndroidSysroot;
+ }
+
+ inline const std::string &getTriple() const {
+ return mTriple;
+ }
// Compile the bitcode and link the shared object
bool build(int pInputFd, int pOutputFd);
diff --git a/lib/AndroidBitcode/ABCCompilerDriver.cpp b/lib/AndroidBitcode/ABCCompilerDriver.cpp
index 84b6583..59d6dda 100644
--- a/lib/AndroidBitcode/ABCCompilerDriver.cpp
+++ b/lib/AndroidBitcode/ABCCompilerDriver.cpp
@@ -20,6 +20,10 @@
#include <llvm/Support/MemoryBuffer.h>
#include <llvm/Support/raw_ostream.h>
+#include "ARMABCCompilerDriver.h"
+#include "MipsABCCompilerDriver.h"
+#include "X86ABCCompilerDriver.h"
+
#include "bcc/Config/Config.h"
#include "bcc/Script.h"
#include "bcc/Source.h"
@@ -34,11 +38,10 @@
namespace bcc {
-ABCCompilerDriver::ABCCompilerDriver(const std::string &pTriple,
- const std::string &pAndroidSysroot)
+ABCCompilerDriver::ABCCompilerDriver(const std::string &pTriple)
: mContext(), mCompiler(), mLinker(),
mCompilerConfig(NULL), mLinkerConfig(NULL),
- mTriple(pTriple), mAndroidSysroot(pAndroidSysroot) {
+ mTriple(pTriple), mAndroidSysroot("/") {
}
ABCCompilerDriver::~ABCCompilerDriver() {
@@ -211,6 +214,38 @@
//------------------------------------------------------------------------------
+ABCCompilerDriver *ABCCompilerDriver::Create(const std::string &pTriple) {
+ std::string error;
+ const llvm::Target *target =
+ llvm::TargetRegistry::lookupTarget(pTriple, error);
+
+ if (target == NULL) {
+ ALOGE("Unsupported target '%s' (detail: %s)!", pTriple.c_str(),
+ error.c_str());
+ return NULL;
+ }
+
+ switch (llvm::Triple::getArchTypeForLLVMName(target->getName())) {
+ case llvm::Triple::arm:
+ case llvm::Triple::thumb: {
+ return new ARMABCCompilerDriver(pTriple);
+ }
+ case llvm::Triple::mipsel: {
+ return new MipsABCCompilerDriver(pTriple);
+ }
+ case llvm::Triple::x86: {
+ return new X86ABCCompilerDriver(pTriple);
+ }
+ default: {
+ ALOGE("Unknown architecture '%s' supplied in %s!", target->getName(),
+ pTriple.c_str());
+ break;
+ }
+ }
+
+ return NULL;
+}
+
bool ABCCompilerDriver::build(int pInputFd, int pOutputFd) {
//===--------------------------------------------------------------------===//
// Prepare the input.
diff --git a/lib/AndroidBitcode/ARMABCCompilerDriver.h b/lib/AndroidBitcode/ARMABCCompilerDriver.h
new file mode 100644
index 0000000..ff1c91e
--- /dev/null
+++ b/lib/AndroidBitcode/ARMABCCompilerDriver.h
@@ -0,0 +1,34 @@
+/*
+ * 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.
+ */
+
+#ifndef BCC_ARM_ABC_COMPILER_DRIVER_H
+#define BCC_ARM_ABC_COMPILER_DRIVER_H
+
+#include "bcc/AndroidBitcode/ABCCompilerDriver.h"
+
+namespace bcc {
+
+class ARMABCCompilerDriver : public ABCCompilerDriver {
+public:
+ ARMABCCompilerDriver(const std::string &pTriple)
+ : ABCCompilerDriver(pTriple) { }
+
+ virtual ~ARMABCCompilerDriver() { }
+};
+
+} // end namespace bcc
+
+#endif // BCC_ARM_ABC_COMPILER_DRIVER_H
diff --git a/lib/AndroidBitcode/MipsABCCompilerDriver.h b/lib/AndroidBitcode/MipsABCCompilerDriver.h
new file mode 100644
index 0000000..dea58ea
--- /dev/null
+++ b/lib/AndroidBitcode/MipsABCCompilerDriver.h
@@ -0,0 +1,34 @@
+/*
+ * 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.
+ */
+
+#ifndef BCC_MIPS_ABC_COMPILER_DRIVER_H
+#define BCC_MIPS_ABC_COMPILER_DRIVER_H
+
+#include "bcc/AndroidBitcode/ABCCompilerDriver.h"
+
+namespace bcc {
+
+class MipsABCCompilerDriver : public ABCCompilerDriver {
+public:
+ MipsABCCompilerDriver(const std::string &pTriple)
+ : ABCCompilerDriver(pTriple) { }
+
+ virtual ~MipsABCCompilerDriver() { }
+};
+
+} // end namespace bcc
+
+#endif // BCC_MIPS_ABC_COMPILER_DRIVER_H
diff --git a/lib/AndroidBitcode/X86ABCCompilerDriver.h b/lib/AndroidBitcode/X86ABCCompilerDriver.h
new file mode 100644
index 0000000..4d76b5f
--- /dev/null
+++ b/lib/AndroidBitcode/X86ABCCompilerDriver.h
@@ -0,0 +1,34 @@
+/*
+ * 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.
+ */
+
+#ifndef BCC_X86_ABC_COMPILER_DRIVER_H
+#define BCC_X86_ABC_COMPILER_DRIVER_H
+
+#include "bcc/AndroidBitcode/ABCCompilerDriver.h"
+
+namespace bcc {
+
+class X86ABCCompilerDriver : public ABCCompilerDriver {
+public:
+ X86ABCCompilerDriver(const std::string &pTriple)
+ : ABCCompilerDriver(pTriple) { }
+
+ virtual ~X86ABCCompilerDriver() { }
+};
+
+} // end namespace bcc
+
+#endif // BCC_X86_ABC_COMPILER_DRIVER_H
diff --git a/tools/abcc/Main.cpp b/tools/abcc/Main.cpp
index 7fe99eb..44ceadd 100644
--- a/tools/abcc/Main.cpp
+++ b/tools/abcc/Main.cpp
@@ -120,8 +120,19 @@
static bool Build(int input_fd, int output_fd,
const char *triple, const char *sysroot) {
- ABCCompilerDriver driver(triple, sysroot);
- return driver.build(input_fd, output_fd);;
+ ABCCompilerDriver *driver = ABCCompilerDriver::Create(triple);
+
+ if (driver == NULL) {
+ return false;
+ }
+
+ driver->setAndroidSysroot(sysroot);
+
+ bool build_result = driver->build(input_fd, output_fd);;
+
+ delete driver;
+
+ return build_result;
}
static int ProcessFromFd(const char *input, const char *output,