1#!/usr/bin/python
2
3# Copyright 2014 Google Inc.
4#
5# Use of this source code is governed by a BSD-style license that can be
6# found in the LICENSE file.
7
8"""
9Functions for creating an Android.mk from already created dictionaries.
10"""
11
12import os
13
14def write_group(f, name, items, append):
15  """Helper function to list all names passed to a variable.
16
17  Args:
18    f: File open for writing (Android.mk)
19    name: Name of the makefile variable (e.g. LOCAL_CFLAGS)
20    items: list of strings to be passed to the variable.
21    append: Whether to append to the variable or overwrite it.
22  """
23  if not items:
24    return
25
26  # Copy the list so we can prepend it with its name.
27  items_to_write = list(items)
28
29  if append:
30    items_to_write.insert(0, '%s +=' % name)
31  else:
32    items_to_write.insert(0, '%s :=' % name)
33
34  f.write(' \\\n\t'.join(items_to_write))
35
36  f.write('\n\n')
37
38
39def write_local_vars(f, var_dict, append, name):
40  """Helper function to write all the members of var_dict to the makefile.
41
42  Args:
43    f: File open for writing (Android.mk)
44    var_dict: VarsDict holding the unique values for one configuration.
45    append: Whether to append to each makefile variable or overwrite it.
46    name: If not None, a string to be appended to each key.
47  """
48  for key in var_dict.keys():
49    _key = key
50    _items = var_dict[key]
51    if key == 'LOCAL_CFLAGS':
52      # Always append LOCAL_CFLAGS. This allows us to define some early on in
53      # the makefile and not overwrite them.
54      _append = True
55    elif key == 'DEFINES':
56      # For DEFINES, we want to append to LOCAL_CFLAGS.
57      _append = True
58      _key = 'LOCAL_CFLAGS'
59      _items_with_D = []
60      for define in _items:
61        _items_with_D.append('-D' + define)
62      _items = _items_with_D
63    elif key == 'KNOWN_TARGETS':
64      # KNOWN_TARGETS are not needed in the final make file.
65      continue
66    else:
67      _append = append
68    if name:
69      _key += '_' + name
70    write_group(f, _key, _items, _append)
71
72
73AUTOGEN_WARNING = (
74"""
75###############################################################################
76#
77# THIS FILE IS AUTOGENERATED BY GYP_TO_ANDROID.PY. DO NOT EDIT.
78#
79# For bugs, please contact scroggo@google.com or djsollen@google.com
80#
81###############################################################################
82
83"""
84)
85
86DEBUGGING_HELP = (
87"""
88###############################################################################
89#
90# PROBLEMS WITH SKIA DEBUGGING?? READ THIS...
91#
92# The debug build results in changes to the Skia headers. This means that those
93# using libskia must also be built with the debug version of the Skia headers.
94# There are a few scenarios where this comes into play:
95#
96# (1) You're building debug code that depends on libskia.
97#   (a) If libskia is built in release, then define SK_RELEASE when building
98#       your sources.
99#   (b) If libskia is built with debugging (see step 2), then no changes are
100#       needed since your sources and libskia have been built with SK_DEBUG.
101# (2) You're building libskia in debug mode.
102#   (a) RECOMMENDED: You can build the entire system in debug mode. Do this by
103#       updating your build/core/config.mk to include -DSK_DEBUG on the line
104#       that defines COMMON_GLOBAL_CFLAGS
105#   (b) You can update all the users of libskia to define SK_DEBUG when they are
106#       building their sources.
107#
108# NOTE: If neither SK_DEBUG or SK_RELEASE are defined then Skia checks NDEBUG to
109#       determine which build type to use.
110###############################################################################
111
112"""
113)
114
115SKIA_TOOLS = (
116"""
117#############################################################
118# Build the skia tools
119#
120
121# benchmark (timings)
122include $(BASE_PATH)/bench/Android.mk
123
124# diamond-master (one test to rule them all)
125include $(BASE_PATH)/dm/Android.mk
126"""
127)
128
129
130class VarsDictData(object):
131  """Helper class to keep a VarsDict along with a name and optional condition.
132  """
133  def __init__(self, vars_dict, name, condition=None):
134    """Create a new VarsDictData.
135
136    Args:
137      vars_dict: A VarsDict. Can be accessed via self.vars_dict.
138      name: Name associated with the VarsDict. Can be accessed via
139        self.name.
140      condition: Optional string representing a condition. If not None,
141        used to create a conditional inside the makefile.
142    """
143    self.vars_dict = vars_dict
144    self.condition = condition
145    self.name = name
146
147def write_local_path(f):
148  """Add the LOCAL_PATH line to the makefile.
149
150  Args:
151    f: File open for writing.
152  """
153  f.write('LOCAL_PATH:= $(call my-dir)\n')
154
155def write_clear_vars(f):
156  """Add the CLEAR_VARS line to the makefile.
157
158  Args:
159    f: File open for writing.
160  """
161  f.write('include $(CLEAR_VARS)\n')
162
163def write_android_mk(target_dir, common, deviations_from_common):
164  """Given all the variables, write the final make file.
165
166  Args:
167    target_dir: The full path to the directory to write Android.mk, or None
168      to use the current working directory.
169    common: VarsDict holding variables definitions common to all
170      configurations.
171    deviations_from_common: List of VarsDictData, one for each possible
172      configuration. VarsDictData.name will be appended to each key before
173      writing it to the makefile. VarsDictData.condition, if not None, will be
174      written to the makefile as a condition to determine whether to include
175      VarsDictData.vars_dict.
176  """
177  target_file = 'Android.mk'
178  if target_dir:
179    target_file = os.path.join(target_dir, target_file)
180  with open(target_file, 'w') as f:
181    f.write(AUTOGEN_WARNING)
182    f.write('BASE_PATH := $(call my-dir)\n')
183    write_local_path(f)
184
185    f.write(DEBUGGING_HELP)
186
187    write_clear_vars(f)
188
189    # need flags to enable feedback driven optimization (FDO) when requested
190    # by the build system.
191    f.write('LOCAL_FDO_SUPPORT := true\n')
192    f.write('ifneq ($(strip $(TARGET_FDO_CFLAGS)),)\n')
193    f.write('\t# This should be the last -Oxxx specified in LOCAL_CFLAGS\n')
194    f.write('\tLOCAL_CFLAGS += -O2\n')
195    f.write('endif\n\n')
196
197    f.write('LOCAL_ARM_MODE := thumb\n')
198
199    # need a flag to tell the C side when we're on devices with large memory
200    # budgets (i.e. larger than the low-end devices that initially shipped)
201    # On arm, only define the flag if it has VFP. For all other architectures,
202    # always define the flag.
203    f.write('ifeq ($(TARGET_ARCH),arm)\n')
204    f.write('\tifeq ($(ARCH_ARM_HAVE_VFP),true)\n')
205    f.write('\t\tLOCAL_CFLAGS += -DANDROID_LARGE_MEMORY_DEVICE\n')
206    f.write('\tendif\n')
207    f.write('else\n')
208    f.write('\tLOCAL_CFLAGS += -DANDROID_LARGE_MEMORY_DEVICE\n')
209    f.write('endif\n\n')
210
211    f.write('# used for testing\n')
212    f.write('#LOCAL_CFLAGS += -g -O0\n\n')
213
214    f.write('ifeq ($(NO_FALLBACK_FONT),true)\n')
215    f.write('\tLOCAL_CFLAGS += -DNO_FALLBACK_FONT\n')
216    f.write('endif\n\n')
217
218    write_local_vars(f, common, False, None)
219
220    for data in deviations_from_common:
221      if data.condition:
222        f.write('ifeq ($(%s), true)\n' % data.condition)
223      write_local_vars(f, data.vars_dict, True, data.name)
224      if data.condition:
225        f.write('endif\n\n')
226
227    f.write('include $(BUILD_SHARED_LIBRARY)\n')
228    f.write(SKIA_TOOLS)
229
230