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"""Function for generating the SkUserConfig file, customized for Android."""
9
10import os
11import shutil
12
13
14AUTOGEN_WARNING = (
15"""
16///////////////////////////////////////////////////////////////////////////////
17//
18// THIS FILE IS AUTOGENERATED BY GYP_TO_ANDROID.PY. DO NOT EDIT.
19//
20// This file contains Skia's upstream include/config/SkUserConfig.h as a
21// reference, followed by the actual defines set for Android.
22//
23///////////////////////////////////////////////////////////////////////////////
24
25"""
26)
27
28BUILD_GUARD = 'SkUserConfig_Android_DEFINED'
29
30
31def generate_user_config(original_sk_user_config, require_sk_user_config,
32                         target_dir, defines):
33  """Generate the SkUserConfig file specific to the Android framework.
34
35  Android needs its #defines in its skia/include/core directory, so that other
36  libraries which use Skia's headers get the right definitions. This function
37  takes the existing sample version of SkUserConfig, checked into Skia, and
38  appends the defines from ordered_set, which is expected to be a
39  vars_dict_lib.OrderedSet containing the defines. The result is written to
40  target_dir/SkUserConfig.h
41
42  Args:
43      original_sk_user_config: Path to original SkUserConfig.h
44      require_sk_user_config: If True, raise an AssertionError if
45          SkUserConfig.h does not exist. Either way, if it does exist, copy it
46          into the new file.
47      target_dir: Directory within which the modified SkUserConfig.h will be
48          written. Its name will be the same basename as
49          original_sk_user_config. If None, the new file will be written to the
50          working directory.
51      defines: Iterable of defines to be appended to SkUserConfig.
52
53  Raises:
54      AssertionError: If original_sk_user_config does not exist.
55  """
56
57  sk_user_config_exists = os.path.exists(original_sk_user_config)
58  if require_sk_user_config:
59    assert sk_user_config_exists
60
61  dst_filename = os.path.basename(original_sk_user_config)
62  if target_dir:
63    dst_filename = os.path.join(target_dir, dst_filename)
64
65  with open(dst_filename, 'w') as dst:
66    dst.write(AUTOGEN_WARNING)
67
68    # Copy the original exactly. This is merely for reference. Many of the
69    # defines written to the file below, either manually or generated from the
70    # gyp files, have explanations in the original SkUserConfig.h
71    if sk_user_config_exists:
72      with open(original_sk_user_config, 'r') as original:
73        shutil.copyfileobj(original, dst)
74
75    # Now add the defines specific to Android. Write a custom build guard to
76    # ensure they don't get defined more than once.
77    dst.write('\n// Android defines:\n')
78    dst.write('#ifndef ' + BUILD_GUARD + '\n')
79    dst.write('#define ' + BUILD_GUARD + '\n')
80
81    # Add conditional defines manually:
82
83    # do this build check for other tools that still read this header
84    dst.write('#ifdef ANDROID\n')
85    dst.write('    #include <utils/misc.h>\n')
86    dst.write('#endif\n\n')
87
88    dst.write('#if __BYTE_ORDER == __BIG_ENDIAN\n')
89    dst.write('    #define SK_CPU_BENDIAN\n')
90    dst.write('    #undef  SK_CPU_LENDIAN\n')
91    dst.write('#else\n')
92    dst.write('    #define SK_CPU_LENDIAN\n')
93    dst.write('    #undef  SK_CPU_BENDIAN\n')
94    dst.write('#endif\n\n')
95
96    # Now add the defines from the gyp files.
97    for item in sorted(defines):
98      # Although our defines may have '=' in them, when written to the header
99      # there should be a space between the macro and what it replaces.
100      dst.write('#define ' + item.replace('=', ' ') + '\n')
101
102    dst.write('\n#endif // ' + BUILD_GUARD + '\n')
103