#!/usr/bin/env python
#
# Copyright 2016 Google Inc.
#
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# Generate Android.bp for Skia from GN configuration.

import json
import os
import pprint
import string
import subprocess
import tempfile

import gn_to_bp_utils

# First we start off with a template for Android.bp,
# with holes for source lists and include directories.
bp = string.Template('''// This file is autogenerated by gn_to_bp.py.

cc_library_static {
    name: "libskia",
    cflags: [
        $cflags
    ],

    cppflags:[
        $cflags_cc
    ],

    export_include_dirs: [
        $export_includes
    ],

    local_include_dirs: [
        $local_includes
    ],

    srcs: [
        $srcs
    ],

    arch: {
        arm: {
            srcs: [
                $arm_srcs
            ],

            neon: {
                srcs: [
                    $arm_neon_srcs
                ],
            },
        },

        arm64: {
            srcs: [
                $arm64_srcs
            ],
        },

        mips: {
            srcs: [
                $none_srcs
            ],
        },

        mips64: {
            srcs: [
                $none_srcs
            ],
        },

        x86: {
            srcs: [
                $x86_srcs
            ],
            cflags: [
                // Clang seems to think new/malloc will only be 4-byte aligned
                // on x86 Android. We're pretty sure it's actually 8-byte
                // alignment. tests/OverAlignedTest.cpp has more information,
                // and should fail if we're wrong.
                "-Wno-over-aligned"
            ],
        },

        x86_64: {
            srcs: [
                $x86_srcs
            ],
        },
    },

    defaults: ["skia_deps",
               "skia_pgo",
    ],
}

// Build libskia with PGO by default.
// Location of PGO profile data is defined in build/soong/cc/pgo.go
// and is separate from skia.
// To turn it off, set ANDROID_PGO_NO_PROFILE_USE environment variable
// or set enable_profile_use property to false.
cc_defaults {
    name: "skia_pgo",
    pgo: {
        instrumentation: true,
        profile_file: "hwui/hwui.profdata",
        benchmarks: ["hwui", "skia"],
        enable_profile_use: true,
    },
}

// "defaults" property to disable profile use for Skia tools and benchmarks.
cc_defaults {
    name: "skia_pgo_no_profile_use",
    defaults: [
        "skia_pgo",
    ],
    pgo: {
        enable_profile_use: false,
    },
}

cc_defaults {
    name: "skia_deps",
    shared_libs: [
        "libEGL",
        "libGLESv2",
        "libdng_sdk",
        "libexpat",
        "libft2",
        "libheif",
        "libicui18n",
        "libicuuc",
        "libjpeg",
        "liblog",
        "libpiex",
        "libpng",
        "libvulkan",
        "libz",
        "libcutils",
        "libnativewindow",
    ],
    static_libs: [
        "libarect",
        "libsfntly",
        "libwebp-decode",
        "libwebp-encode",
    ],
    group_static_libs: true,
}

cc_defaults {
    name: "skia_tool_deps",
    defaults: [
        "skia_deps",
        "skia_pgo_no_profile_use"
    ],
    static_libs: [
        "libjsoncpp",
        "libskia",
    ],
    cflags: [
        "-Wno-unused-parameter",
        "-Wno-unused-variable",
    ],
}

cc_test {
    name: "skia_dm",

    defaults: [
        "skia_tool_deps"
    ],

    local_include_dirs: [
        $dm_includes
    ],

    srcs: [
        $dm_srcs
    ],

    shared_libs: [
        "libbinder",
        "libutils",
    ],
}

cc_test {
    name: "skia_nanobench",

    defaults: [
        "skia_tool_deps"
    ],

    local_include_dirs: [
        $nanobench_includes
    ],

    srcs: [
        $nanobench_srcs
    ],

    data: [
        "resources/*",
    ],
}''')

# We'll run GN to get the main source lists and include directories for Skia.
gn_args = {
  'is_official_build':   'true',
  'skia_enable_tools':   'true',
  'skia_enable_skottie': 'false', # requires rapidjson third-party
  'skia_use_libheif':    'true',
  'skia_use_vulkan':     'true',
  'target_cpu':          '"none"',
  'target_os':           '"android"',
  'skia_vulkan_header':  '"Skia_Vulkan_Android.h"',
}

js = gn_to_bp_utils.GenerateJSONFromGN(gn_args)

def strip_slashes(lst):
  return {str(p.lstrip('/')) for p in lst}

srcs            = strip_slashes(js['targets']['//:skia']['sources'])
cflags          = strip_slashes(js['targets']['//:skia']['cflags'])
cflags_cc       = strip_slashes(js['targets']['//:skia']['cflags_cc'])
local_includes  = strip_slashes(js['targets']['//:skia']['include_dirs'])
export_includes = strip_slashes(js['targets']['//:public']['include_dirs'])
defines      = [str(d) for d in js['targets']['//:skia']['defines']]

dm_srcs         = strip_slashes(js['targets']['//:dm']['sources'])
dm_includes     = strip_slashes(js['targets']['//:dm']['include_dirs'])

nanobench_target = js['targets']['//:nanobench']
nanobench_srcs     = strip_slashes(nanobench_target['sources'])
nanobench_includes = strip_slashes(nanobench_target['include_dirs'])

gn_to_bp_utils.GrabDependentValues(js, '//:skia', 'sources', srcs, None)
gn_to_bp_utils.GrabDependentValues(js, '//:dm', 'sources', dm_srcs, 'skia')
gn_to_bp_utils.GrabDependentValues(js, '//:nanobench', 'sources',
                                   nanobench_srcs, 'skia')

# skcms is a little special, kind of a second-party library.
srcs          .add("third_party/skcms/skcms.c")
local_includes.add("third_party/skcms")
dm_includes   .add("third_party/skcms")

# No need to list headers.
srcs            = {s for s in srcs           if not s.endswith('.h')}
dm_srcs         = {s for s in dm_srcs        if not s.endswith('.h')}
nanobench_srcs  = {s for s in nanobench_srcs if not s.endswith('.h')}

cflags = gn_to_bp_utils.CleanupCFlags(cflags)
cflags_cc = gn_to_bp_utils.CleanupCCFlags(cflags_cc)

# We need to add the include path to the vulkan defines and header file set in
# then skia_vulkan_header gn arg that is used for framework builds.
local_includes.add("platform_tools/android/vulkan")
export_includes.add("platform_tools/android/vulkan")

here = os.path.dirname(__file__)
defs = gn_to_bp_utils.GetArchSources(os.path.join(here, 'opts.gni'))

gn_to_bp_utils.WriteUserConfig('include/config/SkUserConfig.h', defines)

# Turn a list of strings into the style bpfmt outputs.
def bpfmt(indent, lst, sort=True):
  if sort:
    lst = sorted(lst)
  return ('\n' + ' '*indent).join('"%s",' % v for v in lst)

# OK!  We have everything to fill in Android.bp...
with open('Android.bp', 'w') as f:
  print >>f, bp.substitute({
    'export_includes': bpfmt(8, export_includes),
    'local_includes':  bpfmt(8, local_includes),
    'srcs':            bpfmt(8, srcs),
    'cflags':          bpfmt(8, cflags, False),
    'cflags_cc':       bpfmt(8, cflags_cc),

    'arm_srcs':      bpfmt(16, defs['armv7']),
    'arm_neon_srcs': bpfmt(20, defs['neon']),
    'arm64_srcs':    bpfmt(16, defs['arm64'] +
                               defs['crc32']),
    'none_srcs':     bpfmt(16, defs['none']),
    'x86_srcs':      bpfmt(16, defs['sse2'] +
                               defs['ssse3'] +
                               defs['sse41'] +
                               defs['sse42'] +
                               defs['avx'  ] +
                               defs['hsw'  ]),

    'dm_includes'       : bpfmt(8, dm_includes),
    'dm_srcs'           : bpfmt(8, dm_srcs),

    'nanobench_includes'    : bpfmt(8, nanobench_includes),
    'nanobench_srcs'        : bpfmt(8, nanobench_srcs),
  })

[((434, 3856), 'string.Template', 'string.Template', (['"""// This file is autogenerated by gn_to_bp.py.\n\ncc_library_static {\n    name: "libskia",\n    cflags: [\n        $cflags\n    ],\n\n    cppflags:[\n        $cflags_cc\n    ],\n\n    export_include_dirs: [\n        $export_includes\n    ],\n\n    local_include_dirs: [\n        $local_includes\n    ],\n\n    srcs: [\n        $srcs\n    ],\n\n    arch: {\n        arm: {\n            srcs: [\n                $arm_srcs\n            ],\n\n            neon: {\n                srcs: [\n                    $arm_neon_srcs\n                ],\n            },\n        },\n\n        arm64: {\n            srcs: [\n                $arm64_srcs\n            ],\n        },\n\n        mips: {\n            srcs: [\n                $none_srcs\n            ],\n        },\n\n        mips64: {\n            srcs: [\n                $none_srcs\n            ],\n        },\n\n        x86: {\n            srcs: [\n                $x86_srcs\n            ],\n            cflags: [\n                // Clang seems to think new/malloc will only be 4-byte aligned\n                // on x86 Android. We\'re pretty sure it\'s actually 8-byte\n                // alignment. tests/OverAlignedTest.cpp has more information,\n                // and should fail if we\'re wrong.\n                "-Wno-over-aligned"\n            ],\n        },\n\n        x86_64: {\n            srcs: [\n                $x86_srcs\n            ],\n        },\n    },\n\n    defaults: ["skia_deps",\n               "skia_pgo",\n    ],\n}\n\n// Build libskia with PGO by default.\n// Location of PGO profile data is defined in build/soong/cc/pgo.go\n// and is separate from skia.\n// To turn it off, set ANDROID_PGO_NO_PROFILE_USE environment variable\n// or set enable_profile_use property to false.\ncc_defaults {\n    name: "skia_pgo",\n    pgo: {\n        instrumentation: true,\n        profile_file: "hwui/hwui.profdata",\n        benchmarks: ["hwui", "skia"],\n        enable_profile_use: true,\n    },\n}\n\n// "defaults" property to disable profile use for Skia tools and benchmarks.\ncc_defaults {\n    name: "skia_pgo_no_profile_use",\n    defaults: [\n        "skia_pgo",\n    ],\n    pgo: {\n        enable_profile_use: false,\n    },\n}\n\ncc_defaults {\n    name: "skia_deps",\n    shared_libs: [\n        "libEGL",\n        "libGLESv2",\n        "libdng_sdk",\n        "libexpat",\n        "libft2",\n        "libheif",\n        "libicui18n",\n        "libicuuc",\n        "libjpeg",\n        "liblog",\n        "libpiex",\n        "libpng",\n        "libvulkan",\n        "libz",\n        "libcutils",\n        "libnativewindow",\n    ],\n    static_libs: [\n        "libarect",\n        "libsfntly",\n        "libwebp-decode",\n        "libwebp-encode",\n    ],\n    group_static_libs: true,\n}\n\ncc_defaults {\n    name: "skia_tool_deps",\n    defaults: [\n        "skia_deps",\n        "skia_pgo_no_profile_use"\n    ],\n    static_libs: [\n        "libjsoncpp",\n        "libskia",\n    ],\n    cflags: [\n        "-Wno-unused-parameter",\n        "-Wno-unused-variable",\n    ],\n}\n\ncc_test {\n    name: "skia_dm",\n\n    defaults: [\n        "skia_tool_deps"\n    ],\n\n    local_include_dirs: [\n        $dm_includes\n    ],\n\n    srcs: [\n        $dm_srcs\n    ],\n\n    shared_libs: [\n        "libbinder",\n        "libutils",\n    ],\n}\n\ncc_test {\n    name: "skia_nanobench",\n\n    defaults: [\n        "skia_tool_deps"\n    ],\n\n    local_include_dirs: [\n        $nanobench_includes\n    ],\n\n    srcs: [\n        $nanobench_srcs\n    ],\n\n    data: [\n        "resources/*",\n    ],\n}"""'], {}), '(\n    """// This file is autogenerated by gn_to_bp.py.\n\ncc_library_static {\n    name: "libskia",\n    cflags: [\n        $cflags\n    ],\n\n    cppflags:[\n        $cflags_cc\n    ],\n\n    export_include_dirs: [\n        $export_includes\n    ],\n\n    local_include_dirs: [\n        $local_includes\n    ],\n\n    srcs: [\n        $srcs\n    ],\n\n    arch: {\n        arm: {\n            srcs: [\n                $arm_srcs\n            ],\n\n            neon: {\n                srcs: [\n                    $arm_neon_srcs\n                ],\n            },\n        },\n\n        arm64: {\n            srcs: [\n                $arm64_srcs\n            ],\n        },\n\n        mips: {\n            srcs: [\n                $none_srcs\n            ],\n        },\n\n        mips64: {\n            srcs: [\n                $none_srcs\n            ],\n        },\n\n        x86: {\n            srcs: [\n                $x86_srcs\n            ],\n            cflags: [\n                // Clang seems to think new/malloc will only be 4-byte aligned\n                // on x86 Android. We\'re pretty sure it\'s actually 8-byte\n                // alignment. tests/OverAlignedTest.cpp has more information,\n                // and should fail if we\'re wrong.\n                "-Wno-over-aligned"\n            ],\n        },\n\n        x86_64: {\n            srcs: [\n                $x86_srcs\n            ],\n        },\n    },\n\n    defaults: ["skia_deps",\n               "skia_pgo",\n    ],\n}\n\n// Build libskia with PGO by default.\n// Location of PGO profile data is defined in build/soong/cc/pgo.go\n// and is separate from skia.\n// To turn it off, set ANDROID_PGO_NO_PROFILE_USE environment variable\n// or set enable_profile_use property to false.\ncc_defaults {\n    name: "skia_pgo",\n    pgo: {\n        instrumentation: true,\n        profile_file: "hwui/hwui.profdata",\n        benchmarks: ["hwui", "skia"],\n        enable_profile_use: true,\n    },\n}\n\n// "defaults" property to disable profile use for Skia tools and benchmarks.\ncc_defaults {\n    name: "skia_pgo_no_profile_use",\n    defaults: [\n        "skia_pgo",\n    ],\n    pgo: {\n        enable_profile_use: false,\n    },\n}\n\ncc_defaults {\n    name: "skia_deps",\n    shared_libs: [\n        "libEGL",\n        "libGLESv2",\n        "libdng_sdk",\n        "libexpat",\n        "libft2",\n        "libheif",\n        "libicui18n",\n        "libicuuc",\n        "libjpeg",\n        "liblog",\n        "libpiex",\n        "libpng",\n        "libvulkan",\n        "libz",\n        "libcutils",\n        "libnativewindow",\n    ],\n    static_libs: [\n        "libarect",\n        "libsfntly",\n        "libwebp-decode",\n        "libwebp-encode",\n    ],\n    group_static_libs: true,\n}\n\ncc_defaults {\n    name: "skia_tool_deps",\n    defaults: [\n        "skia_deps",\n        "skia_pgo_no_profile_use"\n    ],\n    static_libs: [\n        "libjsoncpp",\n        "libskia",\n    ],\n    cflags: [\n        "-Wno-unused-parameter",\n        "-Wno-unused-variable",\n    ],\n}\n\ncc_test {\n    name: "skia_dm",\n\n    defaults: [\n        "skia_tool_deps"\n    ],\n\n    local_include_dirs: [\n        $dm_includes\n    ],\n\n    srcs: [\n        $dm_srcs\n    ],\n\n    shared_libs: [\n        "libbinder",\n        "libutils",\n    ],\n}\n\ncc_test {\n    name: "skia_nanobench",\n\n    defaults: [\n        "skia_tool_deps"\n    ],\n\n    local_include_dirs: [\n        $nanobench_includes\n    ],\n\n    srcs: [\n        $nanobench_srcs\n    ],\n\n    data: [\n        "resources/*",\n    ],\n}"""\n    )\n', (449, 3856), False, 'import string\n'), ((4270, 4312), 'gn_to_bp_utils.GenerateJSONFromGN', 'gn_to_bp_utils.GenerateJSONFromGN', (['gn_args'], {}), '(gn_args)\n', (4303, 4312), False, 'import gn_to_bp_utils\n'), ((5133, 5205), 'gn_to_bp_utils.GrabDependentValues', 'gn_to_bp_utils.GrabDependentValues', (['js', '"""//:skia"""', '"""sources"""', 'srcs', 'None'], {}), "(js, '//:skia', 'sources', srcs, None)\n", (5167, 5205), False, 'import gn_to_bp_utils\n'), ((5206, 5281), 'gn_to_bp_utils.GrabDependentValues', 'gn_to_bp_utils.GrabDependentValues', (['js', '"""//:dm"""', '"""sources"""', 'dm_srcs', '"""skia"""'], {}), "(js, '//:dm', 'sources', dm_srcs, 'skia')\n", (5240, 5281), False, 'import gn_to_bp_utils\n'), ((5282, 5375), 'gn_to_bp_utils.GrabDependentValues', 'gn_to_bp_utils.GrabDependentValues', (['js', '"""//:nanobench"""', '"""sources"""', 'nanobench_srcs', '"""skia"""'], {}), "(js, '//:nanobench', 'sources',\n    nanobench_srcs, 'skia')\n", (5316, 5375), False, 'import gn_to_bp_utils\n'), ((5845, 5881), 'gn_to_bp_utils.CleanupCFlags', 'gn_to_bp_utils.CleanupCFlags', (['cflags'], {}), '(cflags)\n', (5873, 5881), False, 'import gn_to_bp_utils\n'), ((5894, 5934), 'gn_to_bp_utils.CleanupCCFlags', 'gn_to_bp_utils.CleanupCCFlags', (['cflags_cc'], {}), '(cflags_cc)\n', (5923, 5934), False, 'import gn_to_bp_utils\n'), ((6196, 6221), 'os.path.dirname', 'os.path.dirname', (['__file__'], {}), '(__file__)\n', (6211, 6221), False, 'import os\n'), ((6292, 6364), 'gn_to_bp_utils.WriteUserConfig', 'gn_to_bp_utils.WriteUserConfig', (['"""include/config/SkUserConfig.h"""', 'defines'], {}), "('include/config/SkUserConfig.h', defines)\n", (6322, 6364), False, 'import gn_to_bp_utils\n'), ((6259, 6289), 'os.path.join', 'os.path.join', (['here', '"""opts.gni"""'], {}), "(here, 'opts.gni')\n", (6271, 6289), False, 'import os\n')]