#!/usr/bin/env python3 | |
""" | |
py_build_sm64_wiiu.py: python script to help building sm64-port for the wiiu | |
This is free and unencumbered software released into the public domain. | |
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means. | |
In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
""" | |
""" | |
examples: | |
py_build_sm64.py -v sm64ex -b nightly --add-build-opts BETTERCAM=1 NODRAWINGDISTANCE=1 TEXTURE_FIX=1 | |
Build sm64ex Version, use nightly-branch and use BETTERCAM=1, NODRAWINGDISTANCE=1 and TEXTURE_FIX=1 as | |
additional build options. | |
py_build_sm64.py | |
Build sm64-port Version, master branch, baserom file is in same directory as the python script | |
py_build_sm64.py --install-dependencies --install-devkitpro-pacman | |
Same as above, but try to install devkitpro-pacman and needed packages | |
""" | |
import argparse, os, subprocess, shutil | |
from urllib import request | |
# consts | |
GIT_BASE_URL="https://github.com/aboood40091/" | |
# first entry is default for parser version argument | |
VERSIONS=["sm64-port","sm64ex"] | |
DEVKITPRO_PACMAN_URL="https://github.com/devkitPro/pacman/releases/download/v1.0.2/devkitpro-pacman.amd64.deb" | |
BUILD_DEPENDENCIES = "git build-essential" | |
PACMAN_DEPENDENCIES = "wiiu-dev wiiu-sdl2-libs wiiu-portlibs" | |
class Shell: | |
""" | |
shell cmd helper class | |
""" | |
sh = None | |
def __init__(self,cmd,capture_output=False,sudocmd=False): | |
self.cap_out = capture_output | |
self.sudocmd = sudocmd | |
if isinstance(cmd,str): | |
self.cmd = cmd.split(" ") | |
elif isinstance(cmd,list): | |
self.cmd = cmd | |
else: | |
print("%s needs to be a string or list not %s" %(cmd,type(cmd))) | |
raise(TypeError) | |
def __enter__(self): | |
return(self) | |
def run(self): | |
try: | |
# sudo doesnt work with "subprocess.run" use call instead | |
if self.sudocmd: | |
self.sh = subprocess.call(self.cmd) | |
else: | |
self.sh = subprocess.run(self.cmd,capture_output=self.cap_out) | |
except Exception as e: | |
print("ERROR : %s" %(e)) | |
self.sh = e | |
self.sh.returncode = e.errno | |
def get_result(self): | |
return(self.sh) | |
def __exit__(self, typ, val, traceback): | |
print("command was: %s, result: %i" %(self.cmd, self.sh.returncode if self.sudocmd is False else self.sh)) | |
class File_Download: | |
""" | |
simple file download helper | |
""" | |
def __init__(self,url,out_file): | |
self.url = url | |
self.out_file = out_file | |
self._open_request() | |
self._save_result() | |
def _open_request(self): | |
print("trying to load '%s'" %(self.url)) | |
self.req = request.urlopen(self.url) | |
def _save_result(self): | |
with open(self.out_file,"wb") as f: | |
f.write(self.req.read()) | |
self.req.close() | |
print("saved '%s' to file '%s'" %(self.req.url,self.out_file)) | |
def simple_git_clone(url,branch,out_dir,depth=1): | |
with Shell("git clone --depth="+str(depth)+" "+url+" -b "+branch+" "+out_dir) as git: | |
git.run() | |
if git.get_result().returncode != 0: | |
exit(git.sh.returncode) | |
def create_parser(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument("--install-devkitpro-pacman", | |
nargs="?", | |
const=True, | |
metavar='', | |
help="Try to install dkp-pacman .deb package") | |
parser.add_argument("--install-dependencies", | |
nargs="?", | |
const=True, | |
metavar='', | |
help="Try to install needed build dependencies") | |
parser.add_argument("--skip-compilation", | |
nargs="?", | |
const=True, | |
metavar='', | |
help="Dont compile source") | |
parser.add_argument("--skip-download", | |
nargs="?", | |
const=True, | |
metavar='', | |
help="Skip download") | |
parser.add_argument("-b", "--branch", | |
default="master", | |
help="Specifiy branch to use (default: master)") | |
parser.add_argument("-v","--version", | |
default=VERSIONS[0], | |
choices=VERSIONS, | |
help="Specifiy which version to use (sm64-port or sm64ex default: "+VERSIONS[0]+")") | |
parser.add_argument("-d", "--directory", | |
default="/tmp/SM64", | |
help="Used directory (default: /tmp/SM64)") | |
parser.add_argument("-r", "--romfile", | |
default="baserom.us.z64", | |
help="Base N64 Super Mario 64 Rom file (default baserom.us.z64). use proper naming") | |
parser.add_argument("--add-build-opts", | |
nargs="+", | |
metavar='', | |
help="Specifiy build options (e.g. BETTERCAMERA=1 NODRAWINGDISTANCE=1 TEXTURE_FIX=1).\n(Check Makefile after download or online for available options of version and branch)") | |
return(parser.parse_args()) | |
def install_devkitpro_pacman(): | |
#TODO | |
print("not properly implemented yet") | |
# install gdebi-core | |
with Shell("sudo apt-get update",False,True) as apt_up: | |
apt_up.run() | |
with Shell("sudo apt-get install gdebi-core",False,True) as gdebi_inst: | |
gdebi_inst.run() | |
# check /etc/mtab | |
if os.path.exists("/etc/mtab"): | |
pass | |
else: | |
print("no /etc/mtab found, symlink /proc/self/mounts") | |
# use ugly sudo shell cmd workaround for symlink | |
Shell("sudo ln -s /proc/self/mounts /etc/mtab",False,True).run() | |
dl = File_Download(DEVKITPRO_PACMAN_URL,"/tmp/"+DEVKITPRO_PACMAN_URL.split("/")[-1]) | |
with Shell("sudo gdebi "+dl.out_file,False,True) as dpkg_inst: | |
dpkg_inst.run() | |
def test_pacman(pacman): | |
with Shell(pacman+" -h", True) as pac_test: | |
pac_test.run() | |
if pac_test.get_result().returncode == 0: | |
return(True) | |
else: | |
return(False) | |
def install_dependencies(): | |
#TODO | |
print("not properly implemented yet") | |
# test pacman | |
pacs, pacman = ["dkp-pacman","pacman"], None | |
for pac in pacs: | |
if test_pacman(pac): | |
pacman = pac | |
break | |
if pacman is None: | |
print("no pacman found!") | |
raise FileNotFoundError | |
# update apt pkg list | |
with Shell("sudo apt-get update",False,True) as apt_up: | |
apt_up.run() | |
# install apt pkgs | |
with Shell("sudo apt-get install "+BUILD_DEPENDENCIES,False,True) as apt_inst: | |
apt_inst.run() | |
# update package db | |
with Shell("sudo "+pacman+" -Sy",False,True) as pac_up: | |
pac_up.run() | |
# install dkp pkgs | |
with Shell("sudo "+pacman+" -S "+PACMAN_DEPENDENCIES,False,True) as dkp_deps: | |
dkp_deps.run() | |
def check_env(): | |
#if "DEVKITPRO" and "DEVKITARM" and "DEVKITPPC" in os.environ.keys(): | |
if "DEVKITPRO" and "DEVKITPPC" in os.environ.keys(): | |
return(True) | |
else: | |
try: | |
with open("/etc/profile.d/devkit-env.sh") as f: | |
txt = f.read() | |
env_li = (line for line in txt.replace("export ","").splitlines() if line != "") | |
env_dic = {} | |
for val in env_li: | |
kv = val.split("=") | |
env_dic[kv[0]]=kv[1] | |
return(env_dic) | |
except FileNotFoundError: | |
print("/etc/profile.d/devkit-env.sh not found.\nIs devkit-env installed ?") | |
exit(2) | |
except Exception as e: | |
print(str(e)) | |
exit(e.errno) | |
def set_env(env_dic): | |
for k,v in env_dic.items(): | |
if k == "PATH": | |
os.environ["PATH"] += v | |
print("%s added to PATH" %v) | |
else: | |
print("setting %s = %s" %(k,v)) | |
os.environ.putenv(k,v) | |
def compile_source(out_base_directory, baserom, add_build_opts=None): | |
if os.getuid() == 0: | |
print("dont compile as root") | |
exit(1) | |
env = check_env() | |
if env is not True: | |
print("setup environment") | |
set_env(env) | |
os.chdir(out_base_directory) | |
shutil.copyfile(baserom,os.path.basename(baserom)) | |
with Shell("nproc",True) as nproc: | |
nproc.run() | |
num_processor = nproc.sh.stdout.decode().strip("\n") | |
ver = os.path.basename(baserom)[8:10] | |
build_cmd = ["make","TARGET_WII_U=1","VERSION="+ver,"-j"+num_processor] if add_build_opts is None else ["make"]+add_build_opts+["TARGET_WII_U=1","VERSION="+ver,"-j"+num_processor] | |
with Shell(build_cmd) as sh: | |
sh.run() | |
if sh.sh.returncode == 0: | |
print("build success\noutput is in %s/build/%s_wiiu" %(out_base_directory,ver)) | |
if __name__ == "__main__": | |
args = create_parser() | |
for k,v in vars(args).items(): | |
print("%s = %s" %(k,v)) | |
if args.install_devkitpro_pacman is not None: | |
install_devkitpro_pacman() | |
if args.install_dependencies is not None: | |
install_dependencies() | |
if args.skip_download is not True: | |
simple_git_clone(GIT_BASE_URL+args.version,args.branch,args.directory) | |
if args.skip_compilation is not True: | |
compile_source(args.directory,os.path.abspath(args.romfile),args.add_build_opts) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment