aboutsummaryrefslogtreecommitdiffhomepage
path: root/platform_tools/android/gyp_gen/generate_user_config.py
blob: 6858d9372fc824e499052a7b2b85507488068ff5 (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
#!/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.

"""Function for generating the SkUserConfig file, customized for Android."""

import os
import shutil


AUTOGEN_WARNING = (
"""
///////////////////////////////////////////////////////////////////////////////
//
// THIS FILE IS AUTOGENERATED BY GYP_TO_ANDROID.PY. DO NOT EDIT.
//
// This file contains Skia's upstream include/config/SkUserConfig.h as a
// reference, followed by the actual defines set for Android.
//
///////////////////////////////////////////////////////////////////////////////

"""
)

BUILD_GUARD = 'SkUserConfig_Android_DEFINED'


def generate_user_config(original_sk_user_config, require_sk_user_config,
                         target_dir, defines):
  """Generate the SkUserConfig file specific to the Android framework.

  Android needs its #defines in its skia/include/core directory, so that other
  libraries which use Skia's headers get the right definitions. This function
  takes the existing sample version of SkUserConfig, checked into Skia, and
  appends the defines from ordered_set, which is expected to be a
  vars_dict_lib.OrderedSet containing the defines. The result is written to
  target_dir/SkUserConfig.h

  Args:
      original_sk_user_config: Path to original SkUserConfig.h
      require_sk_user_config: If True, raise an AssertionError if
          SkUserConfig.h does not exist. Either way, if it does exist, copy it
          into the new file.
      target_dir: Directory within which the modified SkUserConfig.h will be
          written. Its name will be the same basename as
          original_sk_user_config. If None, the new file will be written to the
          working directory.
      defines: Iterable of defines to be appended to SkUserConfig.

  Raises:
      AssertionError: If original_sk_user_config does not exist.
  """

  sk_user_config_exists = os.path.exists(original_sk_user_config)
  if require_sk_user_config:
    assert sk_user_config_exists

  dst_filename = os.path.basename(original_sk_user_config)
  if target_dir:
    dst_filename = os.path.join(target_dir, dst_filename)

  with open(dst_filename, 'w') as dst:
    dst.write(AUTOGEN_WARNING)

    # Copy the original exactly. This is merely for reference. Many of the
    # defines written to the file below, either manually or generated from the
    # gyp files, have explanations in the original SkUserConfig.h
    if sk_user_config_exists:
      with open(original_sk_user_config, 'r') as original:
        shutil.copyfileobj(original, dst)

    # Now add the defines specific to Android. Write a custom build guard to
    # ensure they don't get defined more than once.
    dst.write('\n// Android defines:\n')
    dst.write('#ifndef ' + BUILD_GUARD + '\n')
    dst.write('#define ' + BUILD_GUARD + '\n')

    # Add conditional defines manually:

    # do this build check for other tools that still read this header
    dst.write('#ifdef ANDROID\n')
    dst.write('    #include <utils/misc.h>\n')
    dst.write('#endif\n\n')

    dst.write('#if __BYTE_ORDER == __BIG_ENDIAN\n')
    dst.write('    #define SK_CPU_BENDIAN\n')
    dst.write('    #undef  SK_CPU_LENDIAN\n')
    dst.write('#else\n')
    dst.write('    #define SK_CPU_LENDIAN\n')
    dst.write('    #undef  SK_CPU_BENDIAN\n')
    dst.write('#endif\n\n')

    # Now add the defines from the gyp files.
    for item in sorted(defines):
      # Although our defines may have '=' in them, when written to the header
      # there should be a space between the macro and what it replaces.
      dst.write('#define ' + item.replace('=', ' ') + '\n')

    dst.write('\n#endif // ' + BUILD_GUARD + '\n')