CMakeLists for Cross-compiling

Similar to http://layers.openembedded.org/layerindex/recipe/86424/

Bug: b/148877100

Test: verified successful cross compilation
Change-Id: I3923767f3748d9d3ed297a801c25e661a11036c7
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 124bd0d..198af83 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -176,13 +176,13 @@
              "${_gRPC_PROTO_GENS_DIR}/${RELFIL_WE}_mock.grpc.pb.h"
              "${_gRPC_PROTO_GENS_DIR}/${RELFIL_WE}.pb.cc"
              "${_gRPC_PROTO_GENS_DIR}/${RELFIL_WE}.pb.h"
-      COMMAND ${_gRPC_PROTOBUF_PROTOC_EXECUTABLE}
+      COMMAND ${gRPC_PROTOBUF_PROTOC_EXECUTABLE}
       ARGS --grpc_out=generate_mock_code=true:${_gRPC_PROTO_GENS_DIR}
            --cpp_out=${_gRPC_PROTO_GENS_DIR}
-           --plugin=protoc-gen-grpc=$<TARGET_FILE:grpc_cpp_plugin>
+           --plugin=protoc-gen-grpc=${gRPC_CPP_PLUGIN}
            ${_protobuf_include_path}
            ${REL_FIL}
-      DEPENDS ${ABS_FIL} ${_gRPC_PROTOBUF_PROTOC} grpc_cpp_plugin
+      DEPENDS ${ABS_FIL} ${gRPC_PROTOBUF_PROTOC} ${gRPC_CPP_PLUGIN}
       WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
       COMMENT "Running gRPC C++ protocol buffer compiler on ${FIL}"
       VERBATIM)
@@ -191,6 +191,17 @@
   endforeach()
 endfunction()
 
+#if cross-compiling or nativesdk, find host plugin
+if(CMAKE_CROSSCOMPILING)
+    find_program(gRPC_CPP_PLUGIN protoc-gen-grpc-cpp-plugin)  # Android GRPC plugin
+    find_program(gRPC_PROTOBUF_PROTOC_EXECUTABLE aprotoc)  # Android Protoc
+    set(gRPC_PROTOBUF_PROTOC ${gRPC_PROTOBUF_PROTOC_EXECUTABLE})
+else()
+    set(gRPC_CPP_PLUGIN $<TARGET_FILE:grpc_cpp_plugin>)
+    set(gRPC_PROTOBUF_PROTOC ${_gRPC_PROTOBUF_PROTOC})
+    set(gRPC_PROTOBUF_PROTOC_EXECUTABLE ${_gRPC_PROTOBUF_PROTOC_EXECUTABLE})
+endif()
+
 add_custom_target(plugins
   DEPENDS
   grpc_cpp_plugin
diff --git a/src/core/lib/iomgr/unix_sockets_posix.cc b/src/core/lib/iomgr/unix_sockets_posix.cc
index 49ced55..cf93a7d 100644
--- a/src/core/lib/iomgr/unix_sockets_posix.cc
+++ b/src/core/lib/iomgr/unix_sockets_posix.cc
@@ -30,6 +30,7 @@
 
 #include "src/core/lib/iomgr/sockaddr.h"
 
+#include <cstdio>
 #include <string.h>
 #include <sys/stat.h>
 #include <sys/types.h>