Skip to content

Commit

Permalink
chore: fix cython compile script (#396)
Browse files Browse the repository at this point in the history
* chore: fix cython compile script

* chore: fix cython compile script
  • Loading branch information
sunxilin authored Dec 15, 2024
1 parent f395b74 commit bd03da3
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions core/src/ten_runtime/binding/python/tools/cython_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,21 @@
# step1. replace the 'source_file_list' with the actual source files.
# step2. run the following command:
#
# python3 cython_compiler.py build_ext --inplace
# python3 cython_compiler.py -r <compile_root_dir>
#
# step3. the compiled dynamic library will be generated in __current__
# directory.
# step3. the compiled dynamic library will be generated in <compile_root_dir>.

import argparse
import os
import glob
from setuptools import Extension, setup


class ArgumentInfo(argparse.Namespace):
def __init__(self):
self.compile_root_dir: str


def install_cython_if_needed():
try:
from Cython.Build import cythonize
Expand All @@ -36,6 +41,18 @@ def install_cython_if_needed():


if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Cython compiler")
parser.add_argument(
"-r",
"--compile-root-dir",
type=str,
required=False,
default=os.path.dirname(os.path.abspath(__file__)),
)

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

install_rc = install_cython_if_needed()
if not install_rc:
print("Failed to install Cython")
Expand All @@ -44,13 +61,12 @@ def install_cython_if_needed():
from Cython.Build import cythonize

# Get the directory name of the directory.
current_dir = os.path.dirname(os.path.abspath(__file__))
dir_name = os.path.basename(current_dir)
dir_name = os.path.basename(args.compile_root_dir)

# Recursively find all .pyx files in the current directory and its
# subdirectories.
pyx_files = glob.glob(
os.path.join(current_dir, "**", "*.pyx"), recursive=True
os.path.join(args.compile_root_dir, "**", "*.pyx"), recursive=True
)

if not pyx_files:
Expand All @@ -61,7 +77,7 @@ def install_cython_if_needed():
for pyx_file in pyx_files:
# Calculate the relative module name by removing the base directory and
# extension.
rel_path = os.path.relpath(pyx_file, current_dir)
rel_path = os.path.relpath(pyx_file, args.compile_root_dir)
module_name = rel_path.replace(os.path.sep, ".").rsplit(".", 1)[0]

# Create an Extension object for each .pyx file.
Expand All @@ -74,5 +90,5 @@ def install_cython_if_needed():
# The package_dir parameter is used to where the packed package will be
# placed. In this case, the package will be placed in the current
# directory.
package_dir={dir_name: ""},
package_dir={dir_name: args.compile_root_dir},
)

0 comments on commit bd03da3

Please sign in to comment.