Skip to content

Commit

Permalink
feat: refine build script (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
halajohn authored Jan 22, 2025
1 parent 1cc8a13 commit 3af639b
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 105 deletions.
133 changes: 69 additions & 64 deletions .gnfiles/build/feature/ten_package.gni
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,11 @@ template("ten_package") {
}
}

# Specify the necessary steps which are used in group("<target_name>") in the
# end.
# In the `ten_package` template, those internal gn targets used to create the
# package need to be detectable by the outside targets if they are rebuilt.
# This ensures that other targets dependent on the `ten_package` targets can
# also be rebuilt as a result. Therefore, it is necessary to collect these
# internal targets and expose them via `public_deps` in the end.
package_deps = []

# Check if we need to build the package.
Expand Down Expand Up @@ -100,13 +103,6 @@ template("ten_package") {
output_name = "${build_output_name}"
output_dir = "${build_output_dir}"

forward_variables_from(invoker,
[
"deps",
"public_deps",
"data_deps",
])

sources = []
if (defined(invoker.sources) && invoker.sources != []) {
sources += invoker.sources
Expand Down Expand Up @@ -185,17 +181,17 @@ template("ten_package") {
if (invoker.package_kind == "app") {
app_base_dir = get_path_info(_target_name, "dir")

used_system_pkgs =
exec_script("//.gnfiles/build/scripts/get_used_ten_pkgs.py",
[
"--pkg-base-dir",
rebase_path("${app_base_dir}"),
"--app-base-dir",
rebase_path("${app_base_dir}"),
"--pkg-type",
"system",
],
"list lines")
used_system_pkgs = exec_script(
"//.gnfiles/build/scripts/get_dependent_and_buildable_pkgs.py",
[
"--pkg-base-dir",
rebase_path("${app_base_dir}"),
"--app-base-dir",
rebase_path("${app_base_dir}"),
"--pkg-type",
"system",
],
"list lines")

foreach(used_pkg, used_system_pkgs) {
configs += [ "${used_pkg}:config_for_app" ]
Expand All @@ -204,33 +200,40 @@ template("ten_package") {
pkg_base_dir = get_path_info(_target_name, "dir")
app_base_dir = "${pkg_base_dir}/../../../"

used_system_pkgs =
exec_script("//.gnfiles/build/scripts/get_used_ten_pkgs.py",
[
"--pkg-base-dir",
rebase_path("${pkg_base_dir}"),
"--app-base-dir",
rebase_path("${app_base_dir}"),
"--pkg-type",
"system",
],
"list lines")
used_system_pkgs = exec_script(
"//.gnfiles/build/scripts/get_dependent_and_buildable_pkgs.py",
[
"--pkg-base-dir",
rebase_path("${pkg_base_dir}"),
"--app-base-dir",
rebase_path("${app_base_dir}"),
"--pkg-type",
"system",
],
"list lines")

foreach(used_pkg, used_system_pkgs) {
configs += [ "${app_base_dir}${used_pkg}:config_for_ten_packages" ]
}
}

forward_variables_from(invoker,
[
"public_deps",
"deps",
])
}

# Copy the build results to the final destination.
unique_copy_build_result_target_name = "${_target_name}_copy_build_result"
unique_copy_build_result_timestamp_proxy_file =
unique_copy_build_result_tg_timestamp_proxy_file =
"${target_gen_dir}/${unique_copy_build_result_target_name}"

action("${unique_copy_build_result_target_name}") {
script = "//.gnfiles/build/scripts/copy_fs_entry.py"

inputs = [ "${build_output_dir}/${real_build_output_file_name}" ]
outputs = [ unique_copy_build_result_timestamp_proxy_file ]
outputs = [ unique_copy_build_result_tg_timestamp_proxy_file ]

target_dir = get_label_info(":${unique_build_target_name}", "dir")

Expand All @@ -241,13 +244,16 @@ template("ten_package") {
rebase_path(
"${target_dir}/${output_dir_related_to_package_output_root_dir}/${real_build_output_file_name}"),
"--tg-timestamp-proxy-file",
rebase_path(unique_copy_build_result_timestamp_proxy_file),
rebase_path(unique_copy_build_result_tg_timestamp_proxy_file),
"--files-only",
]

deps = [ ":${unique_build_target_name}" ]
}

# If this action is rebuilt, it means the final build result may have been
# modified. Other targets that depend on this `ten_package` need to be aware
# of this, so it is added to `package_deps`.
package_deps += [ ":${unique_copy_build_result_target_name}" ]
}

Expand Down Expand Up @@ -288,25 +294,31 @@ template("ten_package") {
action("${unique_resource_action_target_name}") {
script = "//.gnfiles/build/scripts/copy_fs_entry.py"

resource_output_path =
"${package_output_root_dir}/${resource_dest_path}"

inputs = [ resource_src_path ]
outputs = [ "${package_output_root_dir}/${resource_dest_path}" ]
outputs = [ resource_output_path ]

args = [
"--source",
rebase_path(resource_src_path),
"--destination",
rebase_path("${package_output_root_dir}/${resource_dest_path}"),
rebase_path(resource_output_path),
"--files-only",
]

forward_variables_from(invoker,
[
"deps",
"public_deps",
"data_deps",
])
}

# If this action is rebuilt, it means that the resources ultimately
# packaged into this `ten_package` might have been modified. Other targets
# outside that depend on this `ten_package` need to be aware of this, so
# it should be included in `package_deps`.
package_deps += [ ":${unique_resource_action_target_name}" ]

resource_index += 1
Expand All @@ -327,38 +339,31 @@ template("ten_package") {
if (invoker.package_kind == "app") {
app_base_dir = rebase_path(get_path_info(_target_name, "dir"))

installed_buildable_addons =
exec_script("//.gnfiles/build/scripts/get_used_ten_pkgs.py",
[
"--pkg-base-dir",
"${app_base_dir}",
"--app-base-dir",
"${app_base_dir}",
"--pkg-type",
"extension",
"--pkg-type",
"protocol",
"--pkg-type",
"addon_loader",
],
"list lines")

foreach(installed_buildable_addon, installed_buildable_addons) {
dependent_and_buildable_pkgs =
exec_script(
"//.gnfiles/build/scripts/get_dependent_and_buildable_pkgs.py",
[
"--pkg-base-dir",
"${app_base_dir}",
"--app-base-dir",
"${app_base_dir}",
"--pkg-type",
"extension",
"--pkg-type",
"protocol",
"--pkg-type",
"addon_loader",
],
"list lines")

foreach(dependent_and_buildable_pkg, dependent_and_buildable_pkgs) {
# The addition of the following deps will cause the app's building process
# to also build the installed and buildable C++ addons.
package_deps += [ "${installed_buildable_addon}" ]
# to also build the dependent and buildable C++ addons.
package_deps += [ "${dependent_and_buildable_pkg}" ]
}
}

group(_target_name) {
forward_variables_from(invoker,
[
"deps",
"public_deps",
"data_deps",
"public_configs",
])

if (defined(public_deps)) {
public_deps += package_deps
} else {
Expand Down
30 changes: 14 additions & 16 deletions .gnfiles/build/feature/ten_package_test.gni
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ template("ten_package_test") {
[
"deps",
"public_deps",
"data_deps",
])

sources = []
Expand Down Expand Up @@ -91,17 +90,17 @@ template("ten_package_test") {
pkg_base_dir = get_path_info(_target_name, "dir")
app_base_dir = "${pkg_base_dir}/../../../"

used_system_pkgs =
exec_script("//.gnfiles/build/scripts/get_used_ten_pkgs.py",
[
"--pkg-base-dir",
rebase_path("${pkg_base_dir}"),
"--app-base-dir",
rebase_path("${app_base_dir}"),
"--pkg-type",
"system",
],
"list lines")
used_system_pkgs = exec_script(
"//.gnfiles/build/scripts/get_dependent_and_buildable_pkgs.py",
[
"--pkg-base-dir",
rebase_path("${pkg_base_dir}"),
"--app-base-dir",
rebase_path("${app_base_dir}"),
"--pkg-type",
"system",
],
"list lines")

foreach(used_pkg, used_system_pkgs) {
configs +=
Expand All @@ -112,13 +111,13 @@ template("ten_package_test") {

# Copy the build results to the final destination.
unique_copy_build_result_target_name = "${_target_name}_copy_build_result"
unique_copy_build_result_timestamp_proxy_file =
unique_copy_build_result_tg_timestamp_proxy_file =
"${target_gen_dir}/${unique_copy_build_result_target_name}"
action("${unique_copy_build_result_target_name}") {
script = "//.gnfiles/build/scripts/copy_fs_entry.py"

inputs = [ "${build_output_dir}/${real_build_output_file_name}" ]
outputs = [ unique_copy_build_result_timestamp_proxy_file ]
outputs = [ unique_copy_build_result_tg_timestamp_proxy_file ]

target_dir = get_label_info(":${unique_build_target_name}", "dir")

Expand All @@ -129,7 +128,7 @@ template("ten_package_test") {
rebase_path(
"${target_dir}/${output_dir_related_to_package_output_root_dir}/${real_build_output_file_name}"),
"--tg-timestamp-proxy-file",
rebase_path(unique_copy_build_result_timestamp_proxy_file),
rebase_path(unique_copy_build_result_tg_timestamp_proxy_file),
"--files-only",
]

Expand All @@ -143,7 +142,6 @@ template("ten_package_test") {
[
"deps",
"public_deps",
"data_deps",
"public_configs",
])

Expand Down
2 changes: 1 addition & 1 deletion .gnfiles/build/scripts/copy_fs_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def main():
try:
# Ensure the destination folder, if specified, does exist.
if os.path.dirname(args.destination) != "":
fs_utils.mkdir_p(os.path.dirname(args.destination))
os.makedirs(os.path.dirname(args.destination), exist_ok=True)
except Exception as e:
raise Exception(f"Failed to create destination directory: {str(e)}")

Expand Down
10 changes: 1 addition & 9 deletions .gnfiles/build/scripts/fs_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
# Licensed under the Apache License, Version 2.0, with certain conditions.
# Refer to the "LICENSE" file in the root directory for more information.
#
import sys
import stat
import os
import shutil
Expand All @@ -13,13 +12,6 @@
from build.scripts import log


def mkdir_p(path: str) -> None:
if sys.version > "3":
os.makedirs(path, exist_ok=True)
else:
raise Exception("tgn supports Python 3 only")


def remove_readonly(func, path, excinfo):
if not os.access(path, os.W_OK):
os.chmod(path, stat.S_IWUSR)
Expand Down Expand Up @@ -125,7 +117,7 @@ def copy_file(src_file: str, dst_file: str, rm_dst=False) -> None:
dst_file = os.path.abspath(dst_file)

try:
mkdir_p(os.path.dirname(dst_file))
os.makedirs(os.path.dirname(dst_file), exist_ok=True)
except Exception as exc:
log.error(
inspect.cleandoc(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,7 @@ def load_manifest(path: str) -> dict:
return {}


if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--app-base-dir", type=str, required=True)
parser.add_argument("--pkg-base-dir", type=str, required=True)
parser.add_argument("--pkg-type", type=str, required=True, action="append")
arg_info = ArgumentInfo()
args = parser.parse_args(namespace=arg_info)

def get_dependent_and_buildable_pkgs(args: ArgumentInfo) -> None:
dependencies = load_manifest_dependencies(args.pkg_base_dir)

ten_pkg_type_dirs_info = {
Expand Down Expand Up @@ -127,3 +120,16 @@ def load_manifest(path: str) -> dict:
# Print the matching sub-folders.
for ten_pkg_dir in matching_folders:
print(ten_pkg_dir)


if __name__ == "__main__":
parser = argparse.ArgumentParser()

parser.add_argument("--app-base-dir", type=str, required=True)
parser.add_argument("--pkg-base-dir", type=str, required=True)
parser.add_argument("--pkg-type", type=str, required=True, action="append")

arg_info = ArgumentInfo()
args = parser.parse_args(namespace=arg_info)

get_dependent_and_buildable_pkgs(args)
4 changes: 2 additions & 2 deletions .gnfiles/build/scripts/npm_run_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def sync_source(self, tsconfig_info) -> None:
for src in sources:
dst = os.path.relpath(src, tsconfig_dir)
dst = os.path.join(self.args.out_dir, dst) # build/../src/**/*
fs_utils.mkdir_p(os.path.dirname(dst))
os.makedirs(os.path.dirname(dst), exist_ok=True)
fs_utils.copy(src, dst)

fs_utils.copy(
Expand Down Expand Up @@ -140,7 +140,7 @@ def generate_path_json(self):

def run(self):
# Create the output directory if it does not exist.
fs_utils.mkdir_p(os.path.join(self.args.out_dir, "build"))
os.makedirs(os.path.join(self.args.out_dir, "build"), exist_ok=True)

# Record the current working directory path and switch to the output
# directory.
Expand Down
Loading

0 comments on commit 3af639b

Please sign in to comment.