[vm/tool] Teach precompiler2 script to build ELF binaries.

Usage: pkg/vm/tool/precompiler2 --build-elf a.dart liba.so

Change-Id: I06f673453c8fc6f103c0ccc345b8586edaf6d1a0
Reviewed-on: https://dart-review.googlesource.com/56503
Reviewed-by: Alexander Markov <alexmarkov@google.com>
Commit-Queue: Vyacheslav Egorov <vegorov@google.com>
diff --git a/pkg/vm/tool/precompiler2 b/pkg/vm/tool/precompiler2
index 64908b5..d707213 100755
--- a/pkg/vm/tool/precompiler2
+++ b/pkg/vm/tool/precompiler2
@@ -17,6 +17,7 @@
 OPTIONS=()
 GEN_KERNEL_OPTIONS=()
 PACKAGES=
+BUILD_ELF=0
 
 ARGV=()
 for arg in "$@"; do
@@ -35,6 +36,9 @@
     -D* )
     GEN_KERNEL_OPTIONS+=("$arg")
     ;;
+    --build-elf)
+    BUILD_ELF=1
+    ;;
     --*)
     OPTIONS+=("$arg")
     ;;
@@ -52,6 +56,13 @@
 SOURCE_FILE="${ARGV[0]}"
 SNAPSHOT_FILE="${ARGV[1]}"
 
+if [ $BUILD_ELF -eq 1 ]; then
+  DART_BOOTSTRAP_OUT="${SNAPSHOT_FILE}.S"
+else
+  OPTIONS=("--use-blobs" "${OPTIONS[@]}")
+  DART_BOOTSTRAP_OUT="${SNAPSHOT_FILE}"
+fi
+
 function follow_links() {
   file="$1"
   while [ -h "$file" ]; do
@@ -95,12 +106,16 @@
      "$SOURCE_FILE"
 
 # Step 2: Generate snapshot from the Kernel binary.
-exec "$BIN_DIR"/dart_bootstrap                                                 \
+"$BIN_DIR"/dart_bootstrap                                                      \
      --strong                                                                  \
      --reify-generic-functions                                                 \
      --limit-ints-to-64-bits                                                   \
      --snapshot-kind=app-aot                                                   \
-     --use-blobs                                                               \
-     --snapshot="$SNAPSHOT_FILE"                                               \
+     --snapshot="$DART_BOOTSTRAP_OUT"                                          \
      "${OPTIONS[@]}"                                                           \
      "$SNAPSHOT_FILE.dill"
+
+# Step 3: Assemble the assembly file into an ELF object.
+if [ $BUILD_ELF -eq 1 ]; then
+    gcc -shared -o "$SNAPSHOT_FILE" "$DART_BOOTSTRAP_OUT"
+fi
\ No newline at end of file