Skip to content

Commit

Permalink
fix: build script
Browse files Browse the repository at this point in the history
  • Loading branch information
krypton-byte committed Jan 12, 2024
1 parent 5550dcf commit 1a9d616
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 9 deletions.
21 changes: 19 additions & 2 deletions neonize/_binder.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
import ctypes
import os
from platform import system

import platform
from typing import Dict
def arch_normalizer(arch_: str) -> str:
arch: Dict[str, str] = {
"aarch64":"arm64",
"x86_64":"amd64",
}
return arch.get(arch_, arch_)
def generated_name(os_name = "", arch_name =""):
os_name = os_name or platform.system().lower()
arch_name = arch_normalizer(arch_name or platform.machine().lower())
if os_name== "windows":
ext = "dll"
elif os_name == "linux":
ext = "so"
else:
ext = "so"
return f"neonize-{os_name}-{arch_name}.{ext}"

file_ext = "dll" if system() == "Windows" else "so"
root_dir = os.path.abspath(os.path.dirname(__file__))
gocode = ctypes.CDLL(f"{root_dir}/neonize.{file_ext}")
gocode = ctypes.CDLL(f"{root_dir}/{generated_name()}")
func_string = ctypes.CFUNCTYPE(None, ctypes.c_void_p)
func = ctypes.CFUNCTYPE(None, ctypes.c_void_p)
func_bytes = ctypes.CFUNCTYPE(None, ctypes.c_void_p, ctypes.c_int)
Expand Down
106 changes: 99 additions & 7 deletions neonize/goneonize/build.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,105 @@
import os
import platform
import shlex
import argparse
import subprocess
import sys
import shutil
from pathlib import Path
from typing import Dict
cwd = os.path.dirname(__file__)
shell = [
"protoc --go_out=. Neonize.proto def.proto",
"protoc --python_out=../proto --mypy_out=../proto def.proto Neonize.proto",
"protoc --go_out=. --go-grpc_out=. -I . Neonize.proto def.proto",
]
def arch_normalizer(arch_: str) -> str:
arch: Dict[str, str] = {
"aarch64":"arm64",
"x86_64":"amd64",
}
return arch.get(arch_, arch_)
def generated_name(os_name = "", arch_name =""):
os_name = os_name or platform.system().lower()
arch_name = arch_normalizer(arch_name or platform.machine().lower())
if os_name== "windows":
ext = "dll"
elif os_name == "linux":
ext = "so"
else:
ext = "so"
return f"neonize-{os_name}-{arch_name}.{ext}"

def __build():
args = argparse.ArgumentParser()
args.add_argument("--os", default=platform.system().lower())
args.add_argument("--arch", default=platform.machine().lower())
parse = args.parse_args()
filename = generated_name(parse.os, parse.arch)
for sh in shell:
subprocess.call(shlex.split(sh), cwd=cwd)
if (Path(cwd) / "defproto").exists():
shutil.rmtree(f"{cwd}/defproto")
os.mkdir(f"{cwd}/defproto")
os.rename(f"{cwd}/github.com/krypton-byte/neonize/defproto/", f"{cwd}/defproto")
shutil.rmtree(f"{cwd}/github.com")
subprocess.call(shlex.split(f"go build -buildmode=c-shared -ldflags=-s -o {filename} main.go"), cwd=cwd,env=os.environ.update({"CGO_ENABLED":"1"}))
if (Path(cwd).parent / filename).exists():
os.remove(os.path.dirname(cwd) +"/"+ filename)
os.rename(f"{cwd}/{filename}", os.path.dirname(cwd) +"/"+ filename)

def build_proto():
for sh in shell:
subprocess.call(shlex.split(sh), cwd=cwd)
if (Path(cwd) / "defproto").exists():
shutil.rmtree(f"{cwd}/defproto")
os.mkdir(f"{cwd}/defproto")
os.rename(f"{cwd}/github.com/krypton-byte/neonize/defproto/", f"{cwd}/defproto")
shutil.rmtree(f"{cwd}/github.com")

def build_neonize():
os_name=os.environ.get('GOOS') or platform.system().lower()
arch_name=os.environ.get('GOARCH') or platform.machine().lower()
print(f'os: {os_name}, arch: {arch_name}')
filename = generated_name(os_name, arch_name)
subprocess.call(shlex.split(f"go build -buildmode=c-shared -ldflags=-s -o {filename} main.go"), cwd=cwd,env=os.environ.update({"CGO_ENABLED":"1"}))
if (Path(cwd).parent / filename).exists():
os.remove(os.path.dirname(cwd) +"/"+ filename)
os.rename(f"{cwd}/{filename}", os.path.dirname(cwd) +"/"+ filename)

def build():
command = shlex.split("build.bat" if os.name == "nt" else "bash build.sh")
subprocess.call(
command,
cwd=os.path.dirname(__file__),
env=os.environ.update({"build_neonize": "1"}),
shell=os.name == "nt",
)
args = argparse.ArgumentParser()
sub=args.add_subparsers(dest="build", required=True)
sub.add_parser("goneonize")
sub.add_parser("proto")
sub.add_parser("all")
parse = args.parse_args()
match parse.build:
case "goneonize":
build_neonize()
case "proto":
build_proto()
case "all":
build_proto()
build_neonize()
def build_android():
filename = generated_name("android", "aarch4")
for sh in shell:
subprocess.call(shlex.split(sh), cwd=cwd)
if (Path(cwd) / "defproto").exists():
shutil.rmtree(f"{cwd}/defproto")
os.mkdir(f"{cwd}/defproto")
os.rename(f"{cwd}/github.com/krypton-byte/neonize/defproto/", f"{cwd}/defproto")
shutil.rmtree(f"{cwd}/github.com")
os.environ.update({"CGO_ENABLED":"1", "CC":"/home/krypton-byte/Pictures/android-ndk-r26b/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android28-clang","CXX":"/home/krypton-byte/Pictures/android-ndk-r26b/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android28-clang++"})
subprocess.call(shlex.split(f"go build -buildmode=c-shared -ldflags=-s -o {filename} main.go"), cwd=cwd,env=os.environ)
if (Path(cwd).parent / filename).exists():
os.remove(os.path.dirname(cwd) +"/"+ filename)
os.rename(f"{cwd}/{filename}", os.path.dirname(cwd) +"/"+ filename)
# command = shlex.split("build.bat " if os.name == "nt" else "bash build.sh "+platform.machine())
# subprocess.call(
# command,
# cwd=os.path.dirname(__file__),
# env=os.environ.update({"build_neonize": "1"}),
# shell=os.name == "nt",
# )

0 comments on commit 1a9d616

Please sign in to comment.