include.mk revision ed88f7de96091c1ba4c8e863d97dd41bfdf2be6f
1# Copyright 2014 Google Inc. All rights reserved. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15# This file contains utility functions for Android projects using Flatbuffers. 16# To use this file, include it in your project's Android.mk by calling near the 17# top of your android makefile like so: 18# 19# include $(FLATBUFFERS_DIR)/android/jni/include.mk 20# 21# You will also need to import the flatbuffers module using the standard 22# import-module function. 23# 24# The main functionality this file provides are the following functions: 25# flatbuffers_fbs_to_h: Converts flatbuffer schema paths to header paths. 26# flatbuffers_header_build_rule: 27# Creates a build rule for a schema's generated header. This build rule 28# has a dependency on the flatc compiler which will be built if necessary. 29# flatbuffers_header_build_rules: 30# Creates build rules for generated headers for each schema listed and sets 31# up depenedendies. 32# 33# More information and example usage can be found in the comments preceeding 34# each function. 35 36# Targets to build the Flatbuffers compiler as well as some utility definitions 37ifeq (,$(FLATBUFFERS_INCLUDE_MK_)) 38FLATBUFFERS_INCLUDE_MK_ := 1 39 40PROJECT_OS := $(OS) 41ifeq (,$(OS)) 42PROJECT_OS := $(shell uname -s) 43else 44ifneq ($(findstring Windows,$(PROJECT_OS)),) 45PROJECT_OS := Windows 46endif 47endif 48 49# The following block generates build rules which result in headers being 50# rebuilt from flatbuffers schemas. 51 52FLATBUFFERS_CMAKELISTS_DIR := \ 53 $(realpath $(dir $(lastword $(MAKEFILE_LIST)))/../..) 54 55# Directory that contains the FlatBuffers compiler. 56ifeq (Windows,$(PROJECT_OS)) 57FLATBUFFERS_FLATC_PATH?=$(CURDIR)/bin 58FLATBUFFERS_FLATC := $(FLATBUFFERS_FLATC_PATH)/Debug/flatc.exe 59endif 60ifeq (Linux,$(PROJECT_OS)) 61FLATBUFFERS_FLATC_PATH?=$(CURDIR)/bin 62FLATBUFFERS_FLATC := $(FLATBUFFERS_FLATC_PATH)/flatc 63endif 64ifeq (Darwin,$(PROJECT_OS)) 65FLATBUFFERS_FLATC_PATH?=$(FLATBUFFERS_CMAKELISTS_DIR) 66FLATBUFFERS_FLATC := $(FLATBUFFERS_FLATC_PATH)/Debug/flatc 67endif 68 69FLATBUFFERS_FLATC_ARGS?= 70 71# Search for cmake. 72CMAKE_ROOT := $(realpath $(LOCAL_PATH)/../../../../../../prebuilts/cmake) 73ifeq (,$(CMAKE)) 74ifeq (Linux,$(PROJECT_OS)) 75CMAKE := $(wildcard $(CMAKE_ROOT)/linux-x86/current/bin/cmake*) 76endif 77ifeq (Darwin,$(PROJECT_OS)) 78CMAKE := \ 79 $(wildcard $(CMAKE_ROOT)/darwin-x86_64/current/*.app/Contents/bin/cmake) 80endif 81ifeq (Windows,$(PROJECT_OS)) 82CMAKE := $(wildcard $(CMAKE_ROOT)/windows/current/bin/cmake*) 83endif 84endif 85ifeq (,$(CMAKE)) 86CMAKE := cmake 87endif 88 89# Windows friendly portable local path. 90# GNU-make doesn't like : in paths, must use relative paths on Windows. 91ifeq (Windows,$(PROJECT_OS)) 92PORTABLE_LOCAL_PATH = 93else 94PORTABLE_LOCAL_PATH = $(LOCAL_PATH)/ 95endif 96 97# Generate a host build rule for the flatbuffers compiler. 98ifeq (Windows,$(PROJECT_OS)) 99define build_flatc_recipe 100 cd & jni\build_flatc.bat $(CMAKE) 101endef 102endif 103ifeq (Linux,$(PROJECT_OS)) 104define build_flatc_recipe 105 mkdir -p bin && cd bin && $(CMAKE) $(FLATBUFFERS_CMAKELISTS_DIR) \ 106 && $(MAKE) flatc 107endef 108endif 109ifeq (Darwin,$(PROJECT_OS)) 110define build_flatc_recipe 111 cd $(FLATBUFFERS_CMAKELISTS_DIR) && "$(CMAKE)" -GXcode . && \ 112 xcodebuild -target flatc 113endef 114endif 115ifeq (,$(build_flatc_recipe)) 116ifeq (,$(FLATBUFFERS_FLATC)) 117$(error flatc binary not found!) 118endif 119endif 120 121# Generate a build rule for flatc. 122ifeq ($(strip $(FLATBUFFERS_FLATC)),) 123flatc_target := build_flatc 124.PHONY: $(flatc_target) 125else 126flatc_target := $(FLATBUFFERS_FLATC) 127endif 128$(flatc_target): 129 $(call build_flatc_recipe) 130 131# $(flatbuffers_fbs_to_h schema_dir,output_dir,path) 132# 133# Convert the specified schema path to a Flatbuffers generated header path. 134# For example: 135# 136# $(call flatbuffers_fbs_to_h,$(MY_PROJ_DIR)/schemas,\ 137# $(MY_PROJ_DIR)/gen/include,$(MY_PROJ_DIR)/schemas/example.fbs) 138# 139# This will convert the file path `$(MY_PROJ_DIR)/schemas/example.fbs)` to 140# `$(MY_PROJ_DIR)/gen/include/example_generated.h` 141define flatbuffers_fbs_to_h 142$(subst $(1),$(2),$(patsubst %.fbs,%_generated.h,$(3))) 143endef 144 145# $(flatbuffers_header_build_rule schema_file,schema_dir,output_dir,\ 146# schema_include_dirs) 147# 148# Generate a build rule that will convert a Flatbuffers schema to a generated 149# header derived from the schema filename using flatbuffers_fbs_to_h. For 150# example: 151# 152# $(call flatbuffers_header_build_rule,$(MY_PROJ_DIR)/schemas/example.fbs,\ 153# $(MY_PROJ_DIR)/schemas,$(MY_PROJ_DIR)/gen/include) 154# 155# The final argument, schema_include_dirs, is optional and is only needed when 156# the schema files depend on other schema files outside their own directory. 157define flatbuffers_header_build_rule 158$(eval \ 159 $(call flatbuffers_fbs_to_h,$(2),$(3),$(1)): $(1) $(flatc_target) 160 $(call host-echo-build-step,generic,Generate) \ 161 $(subst $(LOCAL_PATH)/,,$(call flatbuffers_fbs_to_h,$(2),$(3),$(1))) 162 $(hide) $$(FLATBUFFERS_FLATC) $(FLATBUFFERS_FLATC_ARGS) \ 163 $(foreach include,$(4),-I $(include)) -o $$(dir $$@) -c $$<) 164endef 165 166# $(flatbuffers_header_build_rules schema_files,schema_dir,output_dir,\ 167# schema_include_dirs,src_files,[build_target],[dependencies])) 168# 169# $(1) schema_files: Space separated list of flatbuffer schema files. 170# $(2) schema_dir: Directory containing the flatbuffer schemas. 171# $(3) output_dir: Where to place the generated files. 172# $(4) schema_include_dirs: Directories to include when generating schemas. 173# $(5) src_files: Files that should depend upon the headers generated from the 174# flatbuffer schemas. 175# $(6) build_target: Name of a build target that depends upon all generated 176# headers. 177# $(7) dependencies: Space seperated list of additional build targets src_files 178# should depend upon. 179# 180# Use this in your own Android.mk file to generate build rules that will 181# generate header files for your flatbuffer schemas as well as automatically 182# set your source files to be dependent on the generated headers. For example: 183# 184# $(call flatbuffers_header_build_rules,$(MY_PROJ_SCHEMA_FILES),\ 185# $(MY_PROJ_SCHEMA_DIR),$(MY_PROJ_GENERATED_OUTPUT_DIR), 186# $(MY_PROJ_SCHEMA_INCLUDE_DIRS),$(LOCAL_SRC_FILES)) 187define flatbuffers_header_build_rules 188$(foreach schema,$(1),\ 189 $(call flatbuffers_header_build_rule,\ 190 $(schema),$(strip $(2)),$(strip $(3)),$(strip $(4))))\ 191$(foreach src,$(strip $(5)),\ 192 $(eval $(PORTABLE_LOCAL_PATH)$$(src): \ 193 $(foreach schema,$(strip $(1)),\ 194 $(call flatbuffers_fbs_to_h,$(strip $(2)),$(strip $(3)),$(schema)))))\ 195$(if $(6),\ 196 $(foreach schema,$(strip $(1)),\ 197 $(eval $(6): \ 198 $(call flatbuffers_fbs_to_h,$(strip $(2)),$(strip $(3)),$(schema)))),)\ 199$(if $(7),\ 200 $(foreach src,$(strip $(5)),\ 201 $(eval $(PORTABLE_LOCAL_PATH)$$(src): $(strip $(7)))),)\ 202$(if $(7),\ 203 $(foreach dependency,$(strip $(7)),\ 204 $(eval $(6): $(dependency))),) 205endef 206 207endif # FLATBUFFERS_INCLUDE_MK_ 208 209