Skip to content

Commit 1d4829e

Browse files
[Android] ndk static build (#15215)
ndk static build
1 parent 5dc25af commit 1d4829e

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

python/tvm/contrib/ndk.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020

2121
import subprocess
2222
import os
23+
import shutil
2324
from .._ffi.base import py_str
25+
from . import utils as _utils, tar as _tar
2426
from .cc import get_target_by_dump_machine
2527

2628

@@ -68,3 +70,56 @@ def create_shared(output, objects, options=None):
6870
create_shared.get_target_triple = (
6971
get_target_by_dump_machine(os.environ["TVM_NDK_CC"]) if "TVM_NDK_CC" in os.environ else None
7072
)
73+
74+
75+
def create_staticlib(output, inputs):
76+
"""Create static library:
77+
78+
Parameters
79+
----------
80+
output : str
81+
The target static library.
82+
83+
inputs : list
84+
List of object files or tar files
85+
"""
86+
if "TVM_NDK_CC" not in os.environ:
87+
raise RuntimeError(
88+
"Require environment variable TVM_NDK_CC" " to be the NDK standalone compiler"
89+
)
90+
output_name = os.path.basename(output)
91+
92+
temp = _utils.tempdir()
93+
tmp_output = temp.relpath("lib" + output_name)
94+
objects = _tar.normalize_file_list_by_unpacking_tars(temp, inputs)
95+
96+
compiler = os.environ["TVM_NDK_CC"]
97+
base_path = os.path.dirname(compiler)
98+
ar_path = os.path.join(base_path, "llvm-ar")
99+
cmd = [ar_path]
100+
cmd += ["qcs", tmp_output]
101+
cmd += objects
102+
103+
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
104+
(out, _) = proc.communicate()
105+
if proc.returncode != 0:
106+
msg = "AR error:\n"
107+
msg += py_str(out)
108+
msg += "\nCommand line: " + " ".join(cmd)
109+
raise RuntimeError(msg)
110+
111+
ranlib_path = os.path.join(base_path, "llvm-ranlib")
112+
cmd = [ranlib_path]
113+
cmd += [tmp_output]
114+
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
115+
(out, _) = proc.communicate()
116+
if proc.returncode != 0:
117+
msg = "Ranlib error:\n"
118+
msg += py_str(out)
119+
msg += "\nCommand line: " + " ".join(cmd)
120+
raise RuntimeError(msg)
121+
122+
shutil.move(tmp_output, output)
123+
124+
125+
create_staticlib.output_format = "a"

0 commit comments

Comments
 (0)