|
20 | 20 |
|
21 | 21 | import subprocess |
22 | 22 | import os |
| 23 | +import shutil |
23 | 24 | from .._ffi.base import py_str |
| 25 | +from . import utils as _utils, tar as _tar |
24 | 26 | from .cc import get_target_by_dump_machine |
25 | 27 |
|
26 | 28 |
|
@@ -68,3 +70,56 @@ def create_shared(output, objects, options=None): |
68 | 70 | create_shared.get_target_triple = ( |
69 | 71 | get_target_by_dump_machine(os.environ["TVM_NDK_CC"]) if "TVM_NDK_CC" in os.environ else None |
70 | 72 | ) |
| 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