Skip to content

Commit

Permalink
(WIP) Make C24 ATV media stack build in C25
Browse files Browse the repository at this point in the history
b/327287075

Change-Id: Id64c88979358090596ad5684c7c667382b666184
  • Loading branch information
xiaomings committed Aug 29, 2024
1 parent dd1af1d commit 87a00ce
Show file tree
Hide file tree
Showing 6 changed files with 701 additions and 0 deletions.
1 change: 1 addition & 0 deletions starboard/android/shared/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,7 @@ static_library("starboard_platform") {
public_deps = [
":game_activity_sources",
":starboard_base_symbolize",
"//starboard/android/shared/media_2500:media_2500",
"//starboard/common",
"//starboard/shared/starboard/media:media_util",
"//starboard/shared/starboard/player/filter:filter_based_player_sources",
Expand Down
68 changes: 68 additions & 0 deletions starboard/tools/media/find_dependency.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/usr/bin/env python3
# Copyright 2024 The Cobalt Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
'''Create a snapshot of an Starboard Android TV implementation under
'starboard/android/shared/media_<version>/'. This helps with running
multiple Starboard media implementations side by side.'''

import gn_utils
import os
import source_utils
import utils

_GN_TARGETS = [
'//starboard/common:common',
'//starboard/android/shared:starboard_platform',
'//starboard/shared/starboard/media:media_util',
'//starboard/shared/starboard/player/filter:filter_based_player_sources',
]


def find_inbound_dependencies(project_root_dir, ninja_output_pathname):
project_root_dir = os.path.abspath(os.path.expanduser(project_root_dir))
assert os.path.isdir(project_root_dir)
assert os.path.isdir(os.path.join(project_root_dir, ninja_output_pathname))

source_files = []

for target in _GN_TARGETS:
source_files += gn_utils.get_source_pathnames(project_root_dir,
ninja_output_pathname, target)

source_files.sort()

non_media_files = [f for f in source_files if not utils.is_media_file(f)]

inbound_dependencies = {}

for file in non_media_files:
with open(file, encoding='utf-8') as f:
content = f.read()

headers = source_utils.extract_project_includes(content)
for header in headers:
if utils.is_media_file(header):
if header in inbound_dependencies:
inbound_dependencies[header].append(file)
else:
inbound_dependencies[header] = [file]

for header, sources in inbound_dependencies.items():
print(header)
for source in sources:
print(' ', source)
print()


find_inbound_dependencies('~/cobalt', 'out/android-arm_devel')
166 changes: 166 additions & 0 deletions starboard/tools/media/gn_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
#!/usr/bin/env python3
# Copyright 2024 The Cobalt Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
'''Utility to work with gn.'''

from datetime import datetime
from textwrap import dedent

import os
import subprocess

_COPYRIGHT_HEADER = '''\
# Copyright {0} The Cobalt Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
'''

_GN_CONTENT = '''##########################################################
# Configuration to extract GameActivity native files.
##########################################################
game_activity_aar_file = "//starboard/android/apk/app/src/main/java/dev/cobalt/libraries/game_activity/games-activity-2.0.2.aar"
game_activity_source_files = [
"$target_gen_dir/game_activity/prefab/modules/game-activity/include/game-activity/GameActivity.cpp",
"$target_gen_dir/game_activity/prefab/modules/game-activity/include/game-activity/GameActivity.h",
"$target_gen_dir/game_activity/prefab/modules/game-activity/include/game-activity/GameActivityEvents.cpp",
"$target_gen_dir/game_activity/prefab/modules/game-activity/include/game-activity/GameActivityEvents.h",
"$target_gen_dir/game_activity/prefab/modules/game-activity/include/game-activity/GameActivityLog.h",
"$target_gen_dir/game_activity/prefab/modules/game-activity/include/game-text-input/gamecommon.h",
"$target_gen_dir/game_activity/prefab/modules/game-activity/include/game-text-input/gametextinput.cpp",
"$target_gen_dir/game_activity/prefab/modules/game-activity/include/game-text-input/gametextinput.h",
]
game_activity_include_dirs =
[ "$target_gen_dir/game_activity/prefab/modules/game-activity/include" ]
action("game_activity_sources") {{
script = "//starboard/tools/unzip_file.py"
sources = [ game_activity_aar_file ]
outputs = game_activity_source_files
args = [
"--zip_file",
rebase_path(game_activity_aar_file, root_build_dir),
"--output_dir",
rebase_path(target_gen_dir, root_build_dir) + "/game_activity",
]
}}
static_library("{0}") {{
check_includes = false
sources = [
{1}
]
configs += [ "//starboard/build/config:starboard_implementation" ]
include_dirs = game_activity_include_dirs
public_deps = [
":game_activity_sources",
"//starboard/common",
]
deps = [
"//third_party/opus",
]
}}
'''


def _get_copyright_header():
return dedent(_COPYRIGHT_HEADER).format(datetime.now().year)


def create_gn_file(project_root_dir, gn_pathname, library_name, file_pathnames):
abs_project_root_dir = os.path.abspath(project_root_dir)
abs_gn_pathname = os.path.abspath(gn_pathname)

# The gn file should reside in the project dir.
assert abs_gn_pathname.find(abs_project_root_dir) == 0

source_list = []

for file_pathname in file_pathnames:
abs_file_pathname = os.path.abspath(file_pathname)
# The file should reside in the directory containing the gn file.
assert abs_file_pathname.find(os.path.dirname(abs_gn_pathname)) == 0
rel_file_pathname = os.path.relpath(abs_file_pathname, abs_project_root_dir)
source_list.append('\"//' + rel_file_pathname + '",')

source_list.sort()

with open(abs_gn_pathname, 'w+', encoding='utf-8') as f:
f.write(_get_copyright_header() + '\n' +
_GN_CONTENT.format(library_name, '\n '.join(source_list)))


def _get_full_pathname(project_root_dir, pathname_in_gn_format):
''' Transform a pathname in gn format to unix format
project_root_dir: The project root directory in unix format, e.g.
'/home/.../cobalt'
pathname_in_gn_format: A pathname in gn format, e.g. '//starboard/media.h'
return: the full path name as '/home/.../cobalt/starboard/media.h'
'''
assert pathname_in_gn_format.find('//') == 0
pathname_in_gn_format = pathname_in_gn_format[2:]
pathname = os.path.join(project_root_dir, pathname_in_gn_format)
if pathname.find('game-activity') < 0:
assert os.path.isfile(pathname), pathname
return pathname


def get_source_pathnames(project_root_dir, ninja_root_dir, target_name):
''' Return a list of source files built for a particular ninja target
project_root_dir: The project root directory, e.g. '/home/.../cobalt'
ninja_root_dir: The output directory, e.g. 'out/android-arm'
target_name: The name of the ninja target, e.g. '//cobalt/base:base'.
'''
saved_python_path = os.environ['PYTHONPATH']
os.environ['PYTHONPATH'] = os.path.abspath(
project_root_dir) + ':' + os.environ['PYTHONPATH']
gn_desc = subprocess.check_output(['gn', 'desc', ninja_root_dir, target_name],
cwd=project_root_dir).decode('utf-8')
os.environ['PYTHONPATH'] = saved_python_path

# gn_desc is in format:
# ...
# sources
# //path/name1
# //path/name2
# <empty line>
# ...
lines = gn_desc.split('\n')
sources_index = lines.index('sources')
assert sources_index >= 0
sources_index += 1
sources = []
while sources_index < len(lines) and lines[sources_index]:
sources.append(
_get_full_pathname(project_root_dir, lines[sources_index].strip()))
sources_index += 1
return sources
Loading

0 comments on commit 87a00ce

Please sign in to comment.