SDK: Add a Makefile to build the C++ stuff.

R=vardhan@google.com

Review URL: https://codereview.chromium.org/1748073002 .
diff --git a/sdk_build/data/cpp/Makefile b/sdk_build/data/cpp/Makefile
new file mode 100644
index 0000000..a3023c1
--- /dev/null
+++ b/sdk_build/data/cpp/Makefile
@@ -0,0 +1,151 @@
+# Copyright 2016 The Chromium Authors. All rights reserved. 
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+# This is a simplistic sample Makefile for building a Mojo application. It
+# produces output in the out subdirectory (which it creates if necessary), and
+# produces "debug" builds. It assumes that the mojom tool and clang have been
+# obtained by running mojo_sdk_setup/setup.sh.
+#
+# Note: It is very primitive, and doesn't try to correctly handle dependencies.
+#
+# TODO(vtl): Even so, it could use a lot of improvements.
+# TODO(vtl): Should probably support other toolchains, when appropriate.
+
+# Default target ---------------------------------------------------------------
+
+all: default
+
+# Build directories ------------------------------------------------------------
+
+OUT_DIR := out
+GEN_DIR := $(OUT_DIR)/gen
+OBJ_DIR := $(OUT_DIR)/obj
+
+$(OUT_DIR):
+	mkdir -p $(OUT_DIR)
+
+$(GEN_DIR): | $(OUT_DIR)
+	mkdir -p $(GEN_DIR)
+
+$(OBJ_DIR): | $(OUT_DIR)
+	mkdir -p $(OBJ_DIR)
+
+# Tools ------------------------------------------------------------------------
+
+MOJOM_BINDINGS_GENERATOR := \
+		mojo/public/tools/bindings/mojom_bindings_generator.py
+
+CC := toolchain/clang/bin/clang
+CFLAGS := -m64 -march=x86-64 -fPIC -fvisibility=hidden -fno-strict-aliasing \
+		-pthread -O0 -g2 -Wall -Werror -I. -I$(GEN_DIR)
+
+CXX := toolchain/clang/bin/clang++
+CXXFLAGS := $(CFLAGS) -std=c++11 -fno-rtti -fno-exceptions
+
+CXX_MAKE_SO_FLAGS := $(CXXFLAGS) -shared -ldl -Wl,--fatal-warnings \
+		-Wl,-z,noexecstack -Wl,-z,now -Wl,-z,relro -Wl,-z,defs
+
+# Build patterns ---------------------------------------------------------------
+
+$(GEN_DIR)/%.mojom.cc $(GEN_DIR)/%.mojom.h $(GEN_DIR)/%.mojom-internal.h: \
+		%.mojom | $(GEN_DIR)
+	$(MOJOM_BINDINGS_GENERATOR) --use_bundled_pylibs -d . -I . -o $(GEN_DIR) \
+			--no-gen-imports --no-generate-type-info $<
+
+$(OBJ_DIR)/%.o: %.c
+	mkdir -p $(@D)
+	$(CC) $(CFLAGS) -o $@ -c $< 
+
+$(OBJ_DIR)/%.o: %.cc
+	mkdir -p $(@D)
+	$(CXX) $(CXXFLAGS) -o $@ -c $< 
+
+# "Functions" (to use with $(call function_name,...)) --------------------------
+
+# Function to get a list of generated .cc files from a list of .mojom files.
+cc_files_from_mojom_files = $(patsubst %.mojom,$(GEN_DIR)/%.mojom.cc,$(1))
+
+# Function to get a list of generated .o files from a list of .c files.
+o_files_from_c_files = $(patsubst %.c,$(OBJ_DIR)/%.o,$(1))
+
+# Function to get a list of generated .o files from a list of .cc files.
+o_files_from_cc_files = $(patsubst %.cc,$(OBJ_DIR)/%.o,$(1))
+
+# Target: mojo_public.a --------------------------------------------------------
+
+MOJO_PUBLIC_MOJOM_FILES := \
+		$(wildcard mojo/public/interfaces/application/*.mojom) \
+		$(wildcard mojo/public/interfaces/bindings/*.mojom) \
+		$(wildcard mojo/public/interfaces/network/*.mojom)
+MOJO_PUBLIC_MOJOM_CC_FILES := \
+		$(call cc_files_from_mojom_files,$(MOJO_PUBLIC_MOJOM_FILES))
+MOJO_PUBLIC_C_FILES := \
+		$(wildcard mojo/public/c/bindings/lib/*.c)
+MOJO_PUBLIC_CC_FILES := \
+		$(wildcard mojo/public/cpp/application/lib/*.cc) \
+		$(wildcard mojo/public/cpp/bindings/lib/*.cc) \
+		$(wildcard mojo/public/cpp/environment/lib/*.cc) \
+		$(wildcard mojo/public/cpp/system/lib/*.cc) \
+		$(wildcard mojo/public/cpp/utility/lib/*.cc)
+# We have to choose *one* implementation of |Environment|, so remove the
+# logging-only environment.
+MOJO_PUBLIC_CC_FILES := \
+		$(filter-out mojo/public/cpp/environment/lib/logging_only_environment.cc, \
+				$(MOJO_PUBLIC_CC_FILES))
+
+$(MOJO_PUBLIC_CC_FILES): $(MOJO_PUBLIC_MOJOM_CC_FILES)
+
+# TODO(vtl): We could really update the archive instead of re-creating it each
+# time.
+$(OUT_DIR)/mojo_public.a: \
+		$(MOJO_PUBLIC_MOJOM_CC_FILES) \
+		$(call o_files_from_cc_files,$(MOJO_PUBLIC_MOJOM_CC_FILES)) \
+		$(call o_files_from_c_files,$(MOJO_PUBLIC_C_FILES)) \
+		$(call o_files_from_cc_files,$(MOJO_PUBLIC_CC_FILES)) \
+		| $(OUT_DIR)
+	rm -f $@
+	ar rc $@ $(filter %.o,$^)
+
+# Target: mojo_system_thunks.o -------------------------------------------------
+
+# TODO(vtl): Support other thunk libraries.
+
+$(OBJ_DIR)/mojo_system_thunks.o: \
+		$(OBJ_DIR)/mojo/public/platform/native/system_thunks.o
+	cp $^ $@
+
+# Targets: hello_mojo_client.mojo and hello_mojo_server.mojo -------------------
+
+EXAMPLES_HELLO_MOJO_MOJOM_FILES := \
+		$(wildcard examples/hello_mojo/*.mojom)
+EXAMPLES_HELLO_MOJO_MOJOM_CC_FILES := \
+		$(call cc_files_from_mojom_files,$(EXAMPLES_HELLO_MOJO_MOJOM_FILES))
+
+EXAMPLES_HELLO_MOJO_CLIENT_CC_FILES := \
+		examples/hello_mojo/hello_mojo_client.cc
+$(EXAMPLES_HELLO_MOJO_CLIENT_CC_FILES): $(EXAMPLES_HELLO_MOJO_MOJOM_CC_FILES)
+$(OUT_DIR)/hello_mojo_client.mojo: \
+		$(OUT_DIR)/mojo_public.a \
+		$(OBJ_DIR)/mojo_system_thunks.o \
+		$(call o_files_from_cc_files,$(EXAMPLES_HELLO_MOJO_MOJOM_CC_FILES)) \
+		$(call o_files_from_cc_files,$(EXAMPLES_HELLO_MOJO_CLIENT_CC_FILES))
+	$(CXX) $(CXX_MAKE_SO_FLAGS) \
+			-Wl,--whole-archive $(filter %.o,$^) -Wl,-no-whole-archive \
+			$(filter %.a,$^) -o $@
+
+EXAMPLES_HELLO_MOJO_SERVER_CC_FILES := \
+		examples/hello_mojo/hello_mojo_server.cc
+$(EXAMPLES_HELLO_MOJO_SERVER_CC_FILES): $(EXAMPLES_HELLO_MOJO_MOJOM_CC_FILES)
+$(OUT_DIR)/hello_mojo_server.mojo: \
+		$(OUT_DIR)/mojo_public.a \
+		$(OBJ_DIR)/mojo_system_thunks.o \
+		$(call o_files_from_cc_files,$(EXAMPLES_HELLO_MOJO_MOJOM_CC_FILES)) \
+		$(call o_files_from_cc_files,$(EXAMPLES_HELLO_MOJO_SERVER_CC_FILES))
+	$(CXX) $(CXX_MAKE_SO_FLAGS) \
+			-Wl,--whole-archive $(filter %.o,$^) -Wl,-no-whole-archive \
+			$(filter %.a,$^) -o $@
+
+# Default targets --------------------------------------------------------------
+
+default: $(OUT_DIR)/hello_mojo_client.mojo $(OUT_DIR)/hello_mojo_server.mojo
diff --git a/sdk_build/data/cpp/cpp.sdk b/sdk_build/data/cpp/cpp.sdk
index 0a4b46b..64a1e63 100644
--- a/sdk_build/data/cpp/cpp.sdk
+++ b/sdk_build/data/cpp/cpp.sdk
@@ -4,7 +4,7 @@
 
 # This file contains steps for "building" a C/C++ SDK. It is processed by
 # //mojo/sdk_build/build_sdk.py.
-# TODO(vtl): This isn't done yet.
+# TODO(vtl): This isn't done yet. (Or is it?)
 
 import re
 
@@ -66,7 +66,8 @@
 # Scripts to download binaries.
 CopyFiles(["sdk_build/data/common/download_file_from_google_storage.py",
            "sdk_build/data/common/download_mojom_tool.sh",
-           "sdk_build/data/cpp/download_clang.sh"],
+           "sdk_build/data/cpp/download_clang.sh",
+           "sdk_build/data/cpp/setup.sh"],
           "mojo_sdk_setup")
 
 # Figure out the version of clang, and include that. (This is a little janky,
@@ -86,3 +87,6 @@
         "examples/hello_mojo",
         recursive=True,
         exclude_file_patterns=EXCLUDE_FILES)
+
+# Put in a simple example Makefile (in the root directory).
+CopyFiles("sdk_build/data/cpp/Makefile", "")
diff --git a/sdk_build/data/cpp/setup.sh b/sdk_build/data/cpp/setup.sh
new file mode 100755
index 0000000..47fdd19
--- /dev/null
+++ b/sdk_build/data/cpp/setup.sh
@@ -0,0 +1,10 @@
+#!/bin/bash
+# Copyright 2016 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+# Note: In the SDK, this script lives in mojo_sdk_setup.
+SCRIPT_DIR=$(dirname $0)
+
+${SCRIPT_DIR}/download_mojom_tool.sh
+${SCRIPT_DIR}/download_clang.sh