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 10 import os 11 import shutil 12 13 14 AUTOGEN_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 28 BUILD_GUARD = 'SkUserConfig_Android_DEFINED' 29 30 31 def generate_user_config(original_sk_user_config, require_sk_user_config, 32 target_dir, ordered_set): 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 ordered_set: A vars_dict_lib.OrderedSet, containing a list of defines to 52 be appended to SkUserConfig. 53 54 Raises: 55 AssertionError: If original_sk_user_config does not exist. 56 """ 57 58 sk_user_config_exists = os.path.exists(original_sk_user_config) 59 if require_sk_user_config: 60 assert sk_user_config_exists 61 62 dst_filename = os.path.basename(original_sk_user_config) 63 if target_dir: 64 dst_filename = os.path.join(target_dir, dst_filename) 65 66 with open(dst_filename, 'w') as dst: 67 dst.write(AUTOGEN_WARNING) 68 69 # Copy the original exactly. This is merely for reference. Many of the 70 # defines written to the file below, either manually or generated from the 71 # gyp files, have explanations in the original SkUserConfig.h 72 if sk_user_config_exists: 73 with open(original_sk_user_config, 'r') as original: 74 shutil.copyfileobj(original, dst) 75 76 # Now add the defines specific to Android. Write a custom build guard to 77 # ensure they don't get defined more than once. 78 dst.write('\n// Android defines:\n') 79 dst.write('#ifndef ' + BUILD_GUARD + '\n') 80 dst.write('#define ' + BUILD_GUARD + '\n') 81 82 # Add conditional defines manually: 83 84 # do this build check for other tools that still read this header 85 dst.write('#ifdef ANDROID\n') 86 dst.write(' #include <utils/misc.h>\n') 87 dst.write('#endif\n\n') 88 89 dst.write('#if __BYTE_ORDER == __BIG_ENDIAN\n') 90 dst.write(' #define SK_CPU_BENDIAN\n') 91 dst.write(' #undef SK_CPU_LENDIAN\n') 92 dst.write('#else\n') 93 dst.write(' #define SK_CPU_LENDIAN\n') 94 dst.write(' #undef SK_CPU_BENDIAN\n') 95 dst.write('#endif\n\n') 96 97 # Now add the defines from the gyp files. 98 for item in ordered_set: 99 # Although our defines may have '=' in them, when written to the header 100 # there should be a space between the macro and what it replaces. 101 dst.write('#define ' + item.replace('=', ' ') + '\n') 102 103 dst.write('\n#endif // ' + BUILD_GUARD + '\n') 104