Skip to content

Commit a0528fc

Browse files
committed
build: implement JSON-driven Android patch system
Replace hardcoded Android patch application with JSON configuration to improve maintainability and enable multiple patches. The current patch_android() function only applies a single hardcoded patch, making it difficult to add new patches needed for Android builds. Recent contributions show multiple patches are required for proper Android cross-compilation support. This change: - Creates android-patches.json for patch configuration - Refactors patch_android() to read from JSON - Enables platform-specific patch filtering - Maintains 100% backward compatibility - Prepares infrastructure for additional patches Implements TODO comment in android_configure.py line 5 Refs: #57748
1 parent ae0aaec commit a0528fc

File tree

2 files changed

+95
-4
lines changed

2 files changed

+95
-4
lines changed

android-patches.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"format_version": "1.0",
3+
"patches": [
4+
{
5+
"name": "V8 Trap Handler Fix",
6+
"target_file": "./deps/v8/src/trap-handler/trap-handler.h",
7+
"patch_file": "./android-patches/trap-handler.h.patch",
8+
"platforms": ["Linux"],
9+
"description": "Related to https://github.com/nodejs/node/issues/36287",
10+
"required": true
11+
}
12+
]
13+
}

android_configure.py

Lines changed: 82 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,91 @@
11
import platform
22
import sys
33
import os
4+
import json
45

5-
# TODO: In next version, it will be a JSON file listing all the patches, and then it will iterate through to apply them.
66
def patch_android():
77
print("- Patches List -")
8-
print("[1] [deps/v8/src/trap-handler/trap-handler.h] related to https://github.com/nodejs/node/issues/36287")
9-
if platform.system() == "Linux":
10-
os.system('patch -f ./deps/v8/src/trap-handler/trap-handler.h < ./android-patches/trap-handler.h.patch')
8+
9+
try:
10+
with open('android-patches.json', 'r') as f:
11+
config = json.load(f)
12+
except FileNotFoundError:
13+
print("\033[91mError: \033[0m" + "android-patches.json not found. This file is required for Android patch management.")
14+
return
15+
except json.JSONDecodeError as e:
16+
print("\033[91mError: \033[0m" + f"Invalid JSON in android-patches.json: {e}")
17+
return
18+
19+
patches = config.get('patches', [])
20+
if not patches:
21+
print("\033[93mWarning: \033[0m" + "No patches found in configuration.")
22+
print("\033[92mInfo: \033[0m" + "Tried to patch.")
23+
return
24+
25+
current_platform = platform.system()
26+
patch_number = 1
27+
patches_applied = 0
28+
29+
for patch in patches:
30+
try:
31+
name = patch.get('name', 'Unknown Patch')
32+
target_file = patch.get('target_file', '')
33+
patch_file = patch.get('patch_file', '')
34+
platforms = patch.get('platforms', [])
35+
description = patch.get('description', '')
36+
required = patch.get('required', True)
37+
38+
# Display patch information
39+
if description:
40+
print(f"[{patch_number}] [{target_file}] {description}")
41+
else:
42+
print(f"[{patch_number}] [{target_file}] {name}")
43+
44+
# Check if patch applies to current platform
45+
if platforms and current_platform not in platforms:
46+
print(f" Skipping: Not applicable to {current_platform}")
47+
patch_number += 1
48+
continue
49+
50+
# Check if patch file exists
51+
if not os.path.exists(patch_file):
52+
error_msg = f"Patch file not found: {patch_file}"
53+
if required:
54+
print(f"\033[91mError: \033[0m{error_msg}")
55+
return
56+
else:
57+
print(f"\033[93mWarning: \033[0m{error_msg}")
58+
patch_number += 1
59+
continue
60+
61+
# Check if target file exists
62+
if not os.path.exists(target_file):
63+
error_msg = f"Target file not found: {target_file}"
64+
if required:
65+
print(f"\033[91mError: \033[0m{error_msg}")
66+
return
67+
else:
68+
print(f"\033[93mWarning: \033[0m{error_msg}")
69+
patch_number += 1
70+
continue
71+
72+
# Apply the patch
73+
result = os.system(f'patch -f {target_file} < {patch_file}')
74+
if result == 0:
75+
patches_applied += 1
76+
elif required:
77+
print(f"\033[91mError: \033[0mFailed to apply required patch: {name}")
78+
return
79+
80+
patch_number += 1
81+
82+
except KeyError as e:
83+
print(f"\033[91mError: \033[0mMissing required field in patch configuration: {e}")
84+
if patch.get('required', True):
85+
return
86+
patch_number += 1
87+
continue
88+
1189
print("\033[92mInfo: \033[0m" + "Tried to patch.")
1290

1391
if platform.system() != "Linux" and platform.system() != "Darwin":

0 commit comments

Comments
 (0)