|
|
|
#!/usr/bin/env python3
|
|
|
|
#
|
|
|
|
# : Copyright (c) 2021 Phytium Information Technology, Inc.
|
|
|
|
|
|
|
|
# SPDX-License-Identifier: Apache-2.0.
|
|
|
|
|
|
|
|
# Date: 2021-10-14 08:19:30
|
|
|
|
# LastEditTime: 2021-10-14 08:59:24
|
|
|
|
# Description: This files is for install phytiunm standalone sdk
|
|
|
|
|
|
|
|
# Modify History:
|
|
|
|
# Ver Who Date Changes
|
|
|
|
# ----- ------ -------- --------------------------------------
|
|
|
|
# 1.0 Zhu Gengyu 2021/9/30 init
|
|
|
|
#
|
|
|
|
|
|
|
|
# Before run this script, Run 'pip download -r requirment.txt' to make sure all depended module exists
|
|
|
|
# Run 'pip freeze' to output requirment.txt
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import pwd
|
|
|
|
import stat
|
|
|
|
import platform
|
|
|
|
import getpass
|
|
|
|
import tarfile
|
|
|
|
import re
|
|
|
|
import shutil
|
|
|
|
|
|
|
|
### platform constant
|
|
|
|
platform_tags = ["Linux_X86_64" "Linux_AARCH64" "Msys2"]
|
|
|
|
linux_x86 = 0
|
|
|
|
linux_aarch64 = 1
|
|
|
|
windows_msys2 = 2
|
|
|
|
|
|
|
|
### environment constant
|
|
|
|
sdk_profile_path = "/etc/profile.d/phytium_standalone_sdk.sh"
|
|
|
|
|
|
|
|
# check file attributes
|
|
|
|
def is_readable(path, user):
|
|
|
|
user_info = pwd.getpwnam(user)
|
|
|
|
uid = user_info.pw_uid
|
|
|
|
gid = user_info.pw_gid
|
|
|
|
s = os.stat(path)
|
|
|
|
mode = s[stat.ST_MODE]
|
|
|
|
return (((s[stat.ST_UID] == uid) and (mode & stat.S_IRUSR > 0)) or ((s[stat.ST_GID] == gid) and (mode & stat.S_IRGRP > 0)) or (mode & stat.S_IROTH > 0))
|
|
|
|
|
|
|
|
def is_writable(path, user):
|
|
|
|
user_info = pwd.getpwnam(user)
|
|
|
|
uid = user_info.pw_uid
|
|
|
|
gid = user_info.pw_gid
|
|
|
|
s = os.stat(path)
|
|
|
|
mode = s[stat.ST_MODE]
|
|
|
|
return (((s[stat.ST_UID] == uid) and (mode & stat.S_IWUSR > 0)) or((s[stat.ST_GID] == gid) and (mode & stat.S_IWGRP > 0)) or (mode & stat.S_IWOTH > 0))
|
|
|
|
|
|
|
|
def is_executable(path, user):
|
|
|
|
user_info = pwd.getpwnam(user)
|
|
|
|
uid = user_info.pw_uid
|
|
|
|
gid = user_info.pw_gid
|
|
|
|
s = os.stat(path)
|
|
|
|
mode = s[stat.ST_MODE]
|
|
|
|
return (((s[stat.ST_UID] == uid) and (mode & stat.S_IXUSR > 0)) or ((s[stat.ST_GID] == gid) and (mode & stat.S_IXGRP > 0)) or (mode & stat.S_IXOTH > 0))
|
|
|
|
|
|
|
|
def un_tar(src_path, dst_dir):
|
|
|
|
print("- untar {} in progress...".format(src_path))
|
|
|
|
tar = tarfile.open(name=src_path)
|
|
|
|
|
|
|
|
for member_info in tar.getmembers():
|
|
|
|
print("- extracting: " + member_info.name)
|
|
|
|
tar.extract(member=member_info, path=dst_dir)
|
|
|
|
|
|
|
|
tar.close()
|
|
|
|
|
|
|
|
#################################################################
|
|
|
|
# STEP 1: Check environment
|
|
|
|
|
|
|
|
# check install environment
|
|
|
|
if (platform.system() == 'Linux' ) and (platform.processor() == 'x86_64'):
|
|
|
|
install_platform = linux_x86
|
|
|
|
elif (platform.system() == 'Linux' ) and (platform.processor() == 'aarch64'):
|
|
|
|
install_platform = linux_aarch64
|
|
|
|
elif (re.search('MSYS_NT', platform.system()).span() == (0, len('MSYS_NT'))):
|
|
|
|
install_platform = windows_msys2
|
|
|
|
else:
|
|
|
|
print("[1]: Platform not support !!! ")
|
|
|
|
exit()
|
|
|
|
|
|
|
|
# features for msys2 platform, windows platform do not support online
|
|
|
|
phytium_dev_path = os.environ.get("PHYTIUM_DEV_PATH")
|
|
|
|
# to compatible with Windows path, replace \\ with /
|
|
|
|
phytium_dev_path = '/'.join(phytium_dev_path.split('\\'))
|
|
|
|
if (None == phytium_dev_path):
|
|
|
|
print("[1]: Please set 'PHYTIUM_DEV_PATH' first!!!")
|
|
|
|
exit()
|
|
|
|
|
|
|
|
# create '/etc/profile.d/phytium_standalone_sdk.sh' need sudo right, ask user to create it first
|
|
|
|
if not os.path.exists(sdk_profile_path):
|
|
|
|
if (install_platform == linux_x86) or (install_platform == linux_aarch64):
|
|
|
|
print("[1]: Please create sdk profile with 'sudo touch {}' first".format(sdk_profile_path))
|
|
|
|
print("then 'sudo chmod 666 {}'".format(sdk_profile_path))
|
|
|
|
else: # for Windows msys2
|
|
|
|
print("[1]: Please create sdk profile with 'touch {}' first".format(sdk_profile_path))
|
|
|
|
print("then 'chmod 666 {}'".format(sdk_profile_path))
|
|
|
|
|
|
|
|
exit()
|
|
|
|
|
|
|
|
# get current user to install, profile depends on user
|
|
|
|
usr = getpass.getuser()
|
|
|
|
if (install_platform == windows_msys2):
|
|
|
|
# arch is not able to get for msys2
|
|
|
|
print("[1]: Usr: {}, OS: {}, Type: {}".format(usr, platform.system(), install_platform))
|
|
|
|
else:
|
|
|
|
print("[1]: Usr: {}, OS: {}, Arch: {}, Type: {}".format(usr, platform.system(), platform.processor(), install_platform))
|
|
|
|
|
|
|
|
print("[1]: Enviroment variables will set at {}".format(sdk_profile_path))
|
|
|
|
|
|
|
|
# get absoulte path current pwd to install sdk
|
|
|
|
install_path, install_script = os.path.split(os.path.abspath(__file__))
|
|
|
|
curr_path = os.getcwd()
|
|
|
|
standalone_sdk_path = ''
|
|
|
|
|
|
|
|
# in case user call this script not from current path
|
|
|
|
if (curr_path != install_path):
|
|
|
|
print("[1]: Please cd to install script path first !!!")
|
|
|
|
exit()
|
|
|
|
|
|
|
|
# get absolute path of sdk install dir
|
|
|
|
standalone_sdk_path = install_path
|
|
|
|
print("[1]: Standalone SDK at {}".format(standalone_sdk_path))
|
|
|
|
|
|
|
|
# make sure sdk scripts are executable
|
|
|
|
os.system("chmod +x ./*.sh --silent ")
|
|
|
|
os.system("chmod +x ./scripts/*.sh --silent ")
|
|
|
|
os.system("chmod +x ./make/*.mk --silent ")
|
|
|
|
os.system("chmod +x ./lib/Kconfiglib/*.py --silent ")
|
|
|
|
|
|
|
|
## STEP 2: reset environment
|
|
|
|
# remove environment variables
|
|
|
|
try:
|
|
|
|
sdk_profile = open(sdk_profile_path, "w")
|
|
|
|
sdk_profile.truncate()
|
|
|
|
except Exception as ex:
|
|
|
|
print(ex)
|
|
|
|
print("[1]: Create SDK profile {} failed !!!!".format(sdk_profile_path))
|
|
|
|
exit()
|
|
|
|
|
|
|
|
# remove environment from old profile for compatible
|
|
|
|
old_profile_path = os.environ.get('HOME') + '/.profile'
|
|
|
|
os.system("sed -i '/### PHYTIUM STANDALONE SDK SETTING START/d' "+ old_profile_path)
|
|
|
|
os.system("sed -i '/export AARCH32_CROSS_PATH=/d' " + old_profile_path)
|
|
|
|
os.system("sed -i '/export PATH=\$PATH:\$AARCH32_CROSS_PATH/d' " + old_profile_path)
|
|
|
|
os.system("sed -i '/export AARCH64_CROSS_PATH=/d' " + old_profile_path)
|
|
|
|
os.system("sed -i '/export PATH=\$PATH:\$AARCH64_CROSS_PATH/d' " + old_profile_path)
|
|
|
|
os.system("sed -i '/export STANDALONE_SDK_ROOT=/d' " + old_profile_path)
|
|
|
|
os.system("sed -i '/### PHYTIUM STANDALONE SDK SETTING END/d' "+ old_profile_path)
|
|
|
|
|
|
|
|
# remove environment variables
|
|
|
|
# os.system("sed -i '/### PHYTIUM STANDALONE SDK SETTING START/d' "+ sdk_profile_path)
|
|
|
|
# os.system("sed -i '/export AARCH32_CROSS_PATH=/d' " + sdk_profile_path)
|
|
|
|
# os.system("sed -i '/export PATH=\$PATH:\$AARCH32_CROSS_PATH/d' " + sdk_profile_path)
|
|
|
|
# os.system("sed -i '/export AARCH64_CROSS_PATH=/d' " + sdk_profile_path)
|
|
|
|
# os.system("sed -i '/export PATH=\$PATH:\$AARCH64_CROSS_PATH/d' " + sdk_profile_path)
|
|
|
|
# os.system("sed -i '/export STANDALONE_SDK_ROOT=/d' " + sdk_profile_path)
|
|
|
|
# os.system("sed -i '/### PHYTIUM STANDALONE SDK SETTING END/d' "+ sdk_profile_path)
|
|
|
|
|
|
|
|
print("[2]: Reset environment")
|
|
|
|
|
|
|
|
## STEP 3: get cross-platform compiler
|
|
|
|
cc_install_path = phytium_dev_path + '/' + 'cross_tool' + '/'
|
|
|
|
|
|
|
|
# set cc package name, download url and install dst dir
|
|
|
|
if (install_platform == linux_x86):
|
|
|
|
|
|
|
|
aarch32_cc = 'gcc-arm-10.3-2021.07-x86_64-arm-none-eabi'
|
|
|
|
aarch64_cc = 'gcc-arm-10.3-2021.07-x86_64-aarch64-none-elf'
|
|
|
|
|
|
|
|
# cc package name
|
|
|
|
aarch32_cc_pack = aarch32_cc + '.tar.xz'
|
|
|
|
aarch64_cc_pack = aarch64_cc + '.tar.xz'
|
|
|
|
|
|
|
|
aarch32_cc_install_path = cc_install_path + aarch32_cc
|
|
|
|
aarch64_cc_install_path = cc_install_path + aarch64_cc
|
|
|
|
|
|
|
|
elif (install_platform == linux_aarch64):
|
|
|
|
|
|
|
|
aarch32_cc = 'gcc-arm-10.3-2021.07-aarch64-arm-none-eabi'
|
|
|
|
aarch64_cc = 'gcc-arm-10.3-2021.07-aarch64-aarch64-none-elf'
|
|
|
|
|
|
|
|
# cc package name
|
|
|
|
aarch32_cc_pack = aarch32_cc + '.tar.xz'
|
|
|
|
aarch64_cc_pack = aarch64_cc + '.tar.xz'
|
|
|
|
|
|
|
|
aarch32_cc_install_path = cc_install_path + aarch32_cc
|
|
|
|
aarch64_cc_install_path = cc_install_path + aarch64_cc
|
|
|
|
|
|
|
|
elif (install_platform == windows_msys2):
|
|
|
|
|
|
|
|
aarch32_cc = 'gcc-arm-10.3-2021.07-mingw-w64-i686-arm-none-eabi'
|
|
|
|
aarch64_cc = 'gcc-arm-10.3-2021.07-mingw-w64-i686-aarch64-none-elf'
|
|
|
|
|
|
|
|
# cc package name
|
|
|
|
aarch32_cc_pack = aarch32_cc + '.tar.xz'
|
|
|
|
aarch64_cc_pack = aarch64_cc + '.tar.xz'
|
|
|
|
|
|
|
|
aarch32_cc_install_path = cc_install_path + aarch32_cc
|
|
|
|
aarch64_cc_install_path = cc_install_path + aarch64_cc
|
|
|
|
|
|
|
|
|
|
|
|
print("[3]: Download and install CC....")
|
|
|
|
|
|
|
|
# check if cc has already been installed
|
|
|
|
aarch32_cc_is_installed = os.path.exists(aarch32_cc_install_path)
|
|
|
|
aarch64_cc_is_installed = os.path.exists(aarch64_cc_install_path)
|
|
|
|
|
|
|
|
# cc download target
|
|
|
|
# aarch32_cc_dl_dst = cc_install_path + aarch32_cc_pack
|
|
|
|
# aarch64_cc_dl_dst = cc_install_path + aarch64_cc_pack
|
|
|
|
|
|
|
|
aarch32_cc_dl_dst = phytium_dev_path + '/' + aarch32_cc_pack
|
|
|
|
aarch64_cc_dl_dst = phytium_dev_path + '/' + aarch64_cc_pack
|
|
|
|
|
|
|
|
#########################################
|
|
|
|
if not os.path.exists(aarch32_cc_dl_dst):
|
|
|
|
print("[3]: Failed, AARCH32 CC package {} non found !!!".format(aarch32_cc_dl_dst))
|
|
|
|
exit()
|
|
|
|
|
|
|
|
if not os.path.exists(aarch64_cc_dl_dst):
|
|
|
|
print("[3]: Failed, AARCH64 CC package {} non found !!!".format(aarch64_cc_dl_dst))
|
|
|
|
exit()
|
|
|
|
|
|
|
|
## STEP 4:install cc
|
|
|
|
# untar aarch32 cc
|
|
|
|
if not aarch32_cc_is_installed:
|
|
|
|
if os.path.exists(aarch32_cc_dl_dst):
|
|
|
|
print("[4]: Install AARCH32 CC...")
|
|
|
|
un_tar(aarch32_cc_dl_dst, cc_install_path)
|
|
|
|
|
|
|
|
# write aarch32 cc path
|
|
|
|
if os.path.exists(aarch32_cc_install_path):
|
|
|
|
print("[4]: AARCH32 CC install success at {}".format(aarch32_cc_install_path))
|
|
|
|
else:
|
|
|
|
print("[4]: AARCH32 CC install failed !!!")
|
|
|
|
exit()
|
|
|
|
|
|
|
|
# untar aarch64 cc
|
|
|
|
if not aarch64_cc_is_installed:
|
|
|
|
if os.path.exists(aarch64_cc_dl_dst):
|
|
|
|
print("[4]: Install AARCH64 CC...")
|
|
|
|
un_tar(aarch64_cc_dl_dst, cc_install_path)
|
|
|
|
|
|
|
|
# write aarch64 cc path
|
|
|
|
if os.path.exists(aarch64_cc_install_path):
|
|
|
|
print("[4]: AARCH64 CC install success at {}".format(aarch64_cc_install_path))
|
|
|
|
else:
|
|
|
|
print("[4]: AARCH64 CC install failed !!!")
|
|
|
|
exit()
|
|
|
|
|
|
|
|
print("[4]: GNU CC version: 10.3.1-2021.07")
|
|
|
|
|
|
|
|
## STEP 5: write environment variables
|
|
|
|
os.environ['STANDALONE_SDK_ROOT'] = standalone_sdk_path
|
|
|
|
os.environ['AARCH32_CROSS_PATH'] = aarch32_cc_install_path
|
|
|
|
os.environ['AARCH64_CROSS_PATH'] = aarch64_cc_install_path
|
|
|
|
|
|
|
|
os.system("echo \"### PHYTIUM STANDALONE SDK SETTING START\" >> {}".format(sdk_profile_path))
|
|
|
|
os.system("echo \"export AARCH32_CROSS_PATH={}\" >> {}".format(aarch32_cc_install_path, sdk_profile_path))
|
|
|
|
os.system("echo \"export PATH=\$PATH:\$AARCH32_CROSS_PATH/bin\">> {}".format(sdk_profile_path))
|
|
|
|
os.system("echo \"export AARCH64_CROSS_PATH={}\" >> {}".format(aarch64_cc_install_path, sdk_profile_path))
|
|
|
|
os.system("echo \"export PATH=\$PATH:\$AARCH64_CROSS_PATH/bin\">> {}".format(sdk_profile_path))
|
|
|
|
os.system("echo \"export STANDALONE_SDK_ROOT={}\" >> {}".format(standalone_sdk_path, sdk_profile_path))
|
|
|
|
os.system("echo \"### PHYTIUM STANDALONE SDK SETTING END\" >> {}".format(sdk_profile_path))
|
|
|
|
|
|
|
|
## STEP 5: display success message and enable environment
|
|
|
|
print("[5]: Success!!! Standalone SDK is Install at {}".format(standalone_sdk_path))
|
|
|
|
print("[5]: SDK Environment Variables is in {}".format(sdk_profile_path))
|
|
|
|
print("[5]: Phytium Standalone SDK Setup Done for {}!!!".format(usr))
|
|
|
|
print("[5]: Input 'source {}' or Reboot System to Active SDK".format(sdk_profile_path))
|