aboutsummaryrefslogtreecommitdiffhomepage
path: root/platform_tools/android/gyp_gen/makefile_writer.py
blob: 1b977ca1e65efd323d36a57a91156d010e28d5a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/python

# Copyright 2014 Google Inc.
#
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""
Functions for creating an Android.mk from already created dictionaries.
"""

import os

def write_group(f, name, items, append):
  """
  Helper function to list all names passed to a variable.
  @param f File open for writing (Android.mk)
  @param name Name of the makefile variable (e.g. LOCAL_CFLAGS)
  @param items list of strings to be passed to the variable.
  @param append Whether to append to the variable or overwrite it.
  """
  if not items:
    return

  # Copy the list so we can prepend it with its name.
  items_to_write = list(items)

  if append:
    items_to_write.insert(0, '%s +=' % name)
  else:
    items_to_write.insert(0, '%s :=' % name)

  f.write(' \\\n\t'.join(items_to_write))

  f.write('\n\n')


def write_local_vars(f, var_dict, append, name):
  """
  Helper function to write all the members of var_dict to the makefile.
  @param f File open for writing (Android.mk)
  @param var_dict VarsDict holding the unique values for one configuration.
  @param append Whether to append to each makefile variable or overwrite it.
  @param name If not None, a string to be appended to each key.
  """
  for key in var_dict.keys():
    _key = key
    _items = var_dict[key]
    if key == 'LOCAL_CFLAGS':
      # Always append LOCAL_CFLAGS. This allows us to define some early on in
      # the makefile and not overwrite them.
      _append = True
    elif key == 'DEFINES':
      # For DEFINES, we want to append to LOCAL_CFLAGS.
      _append = True
      _key = 'LOCAL_CFLAGS'
      _items_with_D = []
      for define in _items:
        _items_with_D.append('-D' + define)
      _items = _items_with_D
    elif key == 'KNOWN_TARGETS':
      # KNOWN_TARGETS are not needed in the final make file.
      continue
    else:
      _append = append
    if name:
      _key += '_' + name
    write_group(f, _key, _items, _append)


AUTOGEN_WARNING = (
"""
###############################################################################
#
# THIS FILE IS AUTOGENERATED BY GYP_TO_ANDROID.PY. DO NOT EDIT.
#
###############################################################################

"""
)

DEBUGGING_HELP = (
"""
###############################################################################
#
# PROBLEMS WITH SKIA DEBUGGING?? READ THIS...
#
# The debug build results in changes to the Skia headers. This means that those
# using libskia must also be built with the debug version of the Skia headers.
# There are a few scenarios where this comes into play:
#
# (1) You're building debug code that depends on libskia.
#   (a) If libskia is built in release, then define SK_RELEASE when building
#       your sources.
#   (b) If libskia is built with debugging (see step 2), then no changes are
#       needed since your sources and libskia have been built with SK_DEBUG.
# (2) You're building libskia in debug mode.
#   (a) RECOMMENDED: You can build the entire system in debug mode. Do this by
#       updating your build/config.mk to include -DSK_DEBUG on the line that
#       defines COMMON_GLOBAL_CFLAGS
#   (b) You can update all the users of libskia to define SK_DEBUG when they are
#       building their sources.
#
# NOTE: If neither SK_DEBUG or SK_RELEASE are defined then Skia checks NDEBUG to
#       determine which build type to use.
###############################################################################

"""
)

SKIA_TOOLS = (
"""
#############################################################
# Build the skia tools
#

# benchmark (timings)
#include $(BASE_PATH)/bench/Android.mk

# golden-master (fidelity / regression test)
#include $(BASE_PATH)/gm/Android.mk

# unit-tests
#include $(BASE_PATH)/tests/Android.mk

# pathOps unit-tests
# TODO include those sources!
"""
)


class VarsDictData(object):
  """
  Helper class for keeping a VarsDict along with a name and an optional
  condition.
  """
  def __init__(self, vars_dict, name, condition=None):
    """
    Create a new VarsDictData.
    @param vars_dict A VarsDict. Can be accessed via self.vars_dict.
    @param name Name associated with the VarsDict. Can be accessed via
                self.name.
    @param condition Optional string representing a condition. If not None,
                     used to create a conditional inside the makefile.
    """
    self.vars_dict = vars_dict
    self.condition = condition
    self.name = name

def write_android_mk(target_dir, common, deviations_from_common):
  """
  Given all the variables, write the final make file.
  @param target_dir The full path to the directory to write Android.mk, or None
                    to use the current working directory.
  @param common VarsDict holding variables definitions common to all
                configurations.
  @param deviations_from_common List of VarsDictData, one for each possible
                                configuration. VarsDictData.name will be
                                appended to each key before writing it to the
                                makefile. VarsDictData.condition, if not None,
                                will be written to the makefile as a condition
                                to determine whether to include
                                VarsDictData.vars_dict.
  """
  target_file = 'Android.mk'
  if target_dir:
    target_file = os.path.join(target_dir, target_file)
  with open(target_file, 'w') as f:
    f.write(AUTOGEN_WARNING)
    f.write('BASE_PATH := $(call my-dir)\n')
    f.write('LOCAL_PATH:= $(call my-dir)\n')

    f.write(DEBUGGING_HELP)

    f.write('include $(CLEAR_VARS)\n')

    f.write('LOCAL_ARM_MODE := thumb\n')

    # need a flag to tell the C side when we're on devices with large memory
    # budgets (i.e. larger than the low-end devices that initially shipped)
    # On arm, only define the flag if it has VFP. For all other architectures,
    # always define the flag.
    f.write('ifeq ($(TARGET_ARCH),arm)\n')
    f.write('\tifeq ($(ARCH_ARM_HAVE_VFP),true)\n')
    f.write('\t\tLOCAL_CFLAGS += -DANDROID_LARGE_MEMORY_DEVICE\n')
    f.write('\tendif\n')
    f.write('else\n')
    f.write('\tLOCAL_CFLAGS += -DANDROID_LARGE_MEMORY_DEVICE\n')
    f.write('endif\n\n')

    f.write('# used for testing\n')
    f.write('#LOCAL_CFLAGS += -g -O0\n\n')

    f.write('ifeq ($(NO_FALLBACK_FONT),true)\n')
    f.write('\tLOCAL_CFLAGS += -DNO_FALLBACK_FONT\n')
    f.write('endif\n\n')

    write_local_vars(f, common, False, None)

    for data in deviations_from_common:
      if data.condition:
        f.write('ifeq ($(%s), true)\n' % data.condition)
      write_local_vars(f, data.vars_dict, True, data.name)
      if data.condition:
        f.write('endif\n\n')

    f.write('include external/stlport/libstlport.mk\n')
    f.write('LOCAL_MODULE:= libskia\n')
    f.write('include $(BUILD_SHARED_LIBRARY)\n')
    f.write(SKIA_TOOLS)