commit ab7826ee4ee5f2920b015d01aa538c3ebf50dccd
Author: Maxim Blumenthal <maxim.blumenthal@intel.com>
Date:   Mon Jul 27 20:15:01 2015 +0300

    Check xxd

diff --git a/configure b/configure
index 6d7152e..faad566 100755
--- a/configure
+++ b/configure
@@ -674,6 +674,8 @@ LDFLAGS
 CFLAGS
 CC
 EXTRA_CONFIGARGS_LIBJAVA
+python_present
+xxd_present
 extra_liboffloadmic_configure_flags
 target_subdir
 host_subdir
@@ -3131,6 +3133,92 @@ fi
 
 
 
+# Check if xxd is present in the system
+# when the target is intelmic or intelmicemul.
+case "${target}" in
+  *-intelmic-* | *-intelmicemul-*)
+    # Extract the first word of "xxd", so it can be a program name with args.
+set dummy xxd; ac_word=$2
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5
+$as_echo_n "checking for $ac_word... " >&6; }
+if test "${ac_cv_prog_xxd_present+set}" = set; then :
+  $as_echo_n "(cached) " >&6
+else
+  if test -n "$xxd_present"; then
+  ac_cv_prog_xxd_present="$xxd_present" # Let the user override the test.
+else
+as_save_IFS=$IFS; IFS=$PATH_SEPARATOR
+for as_dir in $PATH
+do
+  IFS=$as_save_IFS
+  test -z "$as_dir" && as_dir=.
+    for ac_exec_ext in '' $ac_executable_extensions; do
+  if { test -f "$as_dir/$ac_word$ac_exec_ext" && $as_test_x "$as_dir/$ac_word$ac_exec_ext"; }; then
+    ac_cv_prog_xxd_present=""yes""
+    $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5
+    break 2
+  fi
+done
+  done
+IFS=$as_save_IFS
+
+  test -z "$ac_cv_prog_xxd_present" && ac_cv_prog_xxd_present=""no""
+fi
+fi
+xxd_present=$ac_cv_prog_xxd_present
+if test -n "$xxd_present"; then
+  { $as_echo "$as_me:${as_lineno-$LINENO}: result: $xxd_present" >&5
+$as_echo "$xxd_present" >&6; }
+else
+  { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
+$as_echo "no" >&6; }
+fi
+
+
+    # Extract the first word of "python", so it can be a program name with args.
+set dummy python; ac_word=$2
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5
+$as_echo_n "checking for $ac_word... " >&6; }
+if test "${ac_cv_prog_python_present+set}" = set; then :
+  $as_echo_n "(cached) " >&6
+else
+  if test -n "$python_present"; then
+  ac_cv_prog_python_present="$python_present" # Let the user override the test.
+else
+as_save_IFS=$IFS; IFS=$PATH_SEPARATOR
+for as_dir in $PATH
+do
+  IFS=$as_save_IFS
+  test -z "$as_dir" && as_dir=.
+    for ac_exec_ext in '' $ac_executable_extensions; do
+  if { test -f "$as_dir/$ac_word$ac_exec_ext" && $as_test_x "$as_dir/$ac_word$ac_exec_ext"; }; then
+    ac_cv_prog_python_present=""yes""
+    $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5
+    break 2
+  fi
+done
+  done
+IFS=$as_save_IFS
+
+  test -z "$ac_cv_prog_python_present" && ac_cv_prog_python_present=""no""
+fi
+fi
+python_present=$ac_cv_prog_python_present
+if test -n "$python_present"; then
+  { $as_echo "$as_me:${as_lineno-$LINENO}: result: $python_present" >&5
+$as_echo "$python_present" >&6; }
+else
+  { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
+$as_echo "no" >&6; }
+fi
+
+
+    if test "$xxd_present$python_present" = "nono"; then
+      as_fn_error "cannot find neither xxd nor python" "$LINENO" 5
+    fi
+    ;;
+esac
+
 # Save it here so that, even in case of --enable-libgcj, if the Java
 # front-end isn't enabled, we still get libgcj disabled.
 libgcj_saved=$libgcj
diff --git a/configure.ac b/configure.ac
index fbc49ce..f429a1f 100644
--- a/configure.ac
+++ b/configure.ac
@@ -494,6 +494,18 @@ else
 fi])
 AC_SUBST(extra_liboffloadmic_configure_flags)
 
+# Check if xxd is present in the system
+# when the target is intelmic or intelmicemul.
+case "${target}" in
+  *-intelmic-* | *-intelmicemul-*)
+    AC_CHECK_PROG(xxd_present, xxd, "yes", "no")
+    AC_CHECK_PROG(python_present, python, "yes", "no")
+    if test "$xxd_present$python_present" = "nono"; then
+      AC_MSG_ERROR([cannot find neither xxd nor python])
+    fi
+    ;;
+esac
+
 # Save it here so that, even in case of --enable-libgcj, if the Java
 # front-end isn't enabled, we still get libgcj disabled.
 libgcj_saved=$libgcj
diff --git a/liboffloadmic/plugin/Makefile.am b/liboffloadmic/plugin/Makefile.am
index 19d69ab..cb93309 100644
--- a/liboffloadmic/plugin/Makefile.am
+++ b/liboffloadmic/plugin/Makefile.am
@@ -49,6 +49,10 @@ target_prefix_dir = $(libdir)/gcc/$(accel_target)/$(gcc_version)$(MULTISUBDIR)
 target_build_dir = $(accel_search_dir)/$(accel_target)$(MULTISUBDIR)/liboffloadmic/plugin
 target_install_dir = $(accel_search_dir)/lib/gcc/$(accel_target)/$(gcc_version)$(MULTISUBDIR)
 
+XXD_PY = $(source_dir)/xxd.py
+xxd_path=`which xxd`
+python_path=`which python`
+
 if PLUGIN_HOST
   toolexeclib_LTLIBRARIES = libgomp-plugin-intelmic.la
   libgomp_plugin_intelmic_la_SOURCES = libgomp-plugin-intelmic.cpp
@@ -73,7 +77,7 @@ main_target_image.h: offload_target_main
 	@echo "};" >> $@
 	@echo "extern \"C\" const MainTargetImage main_target_image = {" >> $@
 	@echo "  image_size, \"offload_target_main\"," >> $@
-	@cat $< | xxd -include >> $@
+	@if test "x$(xxd_path)" != "x"; then cat $< | $(xxd_path) -include >> $@; else $(python_path) $(XXD_PY) $< >> $@; fi;
 	@echo "};" >> $@
 
 offload_target_main: $(liboffload_dir)/ofldbegin.o offload_target_main.o $(liboffload_dir)/ofldend.o
diff --git a/liboffloadmic/plugin/Makefile.in b/liboffloadmic/plugin/Makefile.in
index 19a1a96..12fe5ca 100644
--- a/liboffloadmic/plugin/Makefile.in
+++ b/liboffloadmic/plugin/Makefile.in
@@ -318,6 +318,9 @@ libsubincludedir = $(libdir)/gcc/$(target_alias)/$(gcc_version)$(MULTISUBDIR)/in
 target_prefix_dir = $(libdir)/gcc/$(accel_target)/$(gcc_version)$(MULTISUBDIR)
 target_build_dir = $(accel_search_dir)/$(accel_target)$(MULTISUBDIR)/liboffloadmic/plugin
 target_install_dir = $(accel_search_dir)/lib/gcc/$(accel_target)/$(gcc_version)$(MULTISUBDIR)
+XXD_PY = $(source_dir)/xxd.py
+xxd_path = `which xxd`
+python_path = `which python`
 @PLUGIN_HOST_TRUE@toolexeclib_LTLIBRARIES = libgomp-plugin-intelmic.la
 @PLUGIN_HOST_TRUE@libgomp_plugin_intelmic_la_SOURCES = libgomp-plugin-intelmic.cpp
 @PLUGIN_HOST_TRUE@libgomp_plugin_intelmic_la_CPPFLAGS = $(CPPFLAGS) -DLINUX -DCOI_LIBRARY_VERSION=2 -DMYO_SUPPORT -DOFFLOAD_DEBUG=1 -DSEP_SUPPORT -DTIMING_SUPPORT -DHOST_LIBRARY=1 -I$(coi_inc_dir) -I$(myo_inc_dir) -I$(liboffload_src_dir) -I$(libgomp_src_dir) -I$(libgomp_dir) -I$(target_prefix_dir)/include -I$(target_build_dir) -I$(target_install_dir)/include
@@ -719,7 +722,7 @@ main_target_image.h: offload_target_main
 	@echo "};" >> $@
 	@echo "extern \"C\" const MainTargetImage main_target_image = {" >> $@
 	@echo "  image_size, \"offload_target_main\"," >> $@
-	@cat $< | xxd -include >> $@
+	@if test "x$(xxd_path)" != "x"; then cat $< | $(xxd_path) -include >> $@; else $(python_path) $(XXD_PY) $< >> $@; fi;
 	@echo "};" >> $@
 
 offload_target_main: $(liboffload_dir)/ofldbegin.o offload_target_main.o $(liboffload_dir)/ofldend.o
diff --git a/liboffloadmic/plugin/xxd.py b/liboffloadmic/plugin/xxd.py
new file mode 100644
index 0000000..fd9d7f0
--- /dev/null
+++ b/liboffloadmic/plugin/xxd.py
@@ -0,0 +1,39 @@
+#!/usr/bin/python
+import sys
+
+if sys.version_info[0] == 2:
+    # Python 2:
+    # 'block' below is an instance of str; iterating over it gives us
+    # str instances of len 1.
+    def get_byte(ch):
+        return ord(ch)
+else:
+    # Python 3:
+    # 'block' below is an instance of bytes; iterating over it gives us
+    # instances of int, in the range 0-255.
+    def get_byte(ch):
+        return ch
+
+with open(sys.argv[1],"rb") as f:
+    nextblock = f.read(12)
+    while 1:
+        block = nextblock
+        nextblock = f.read(12)
+        if not block:
+            break
+        str = ""
+        for item in block:
+            byte = get_byte(item)
+            if str == "":
+                str = "  "
+            else:
+                str += ", "
+            if byte < 10:
+                str += "0x0" + chr(ord('0')+byte)
+            elif byte < 16:
+                str += "0x0" + chr(ord('a')+byte-10)
+            else:
+                str += hex(byte)
+        if nextblock:
+            str += ","
+        print(str)
