You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

318 lines
12 KiB

#!/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"
# argv[1]: online/offline
# print(sys.argv)
is_online = False
# 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):
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 if it is offline install or online install
for i in range(1, len(sys.argv)):
if('-online' == sys.argv[i]) or ('-on' == sys.argv[i]) or ('-o' == sys.argv[i]):
is_online = True
print("[1]: Online install" if is_online else "[1]: Offline install")
# check install environment
install_platform = -1
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
#########################################
if (install_platform == windows_msys2):
if (is_online == True):
print("[1]: Windows Msys2 do not support online install!!!")
exit()
if (None == os.environ.get("PHYTIUM_DEV_PATH")):
print("[1]: Please set 'PHYTIUM_DEV_PATH' fisrt!!!")
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)
print("[2]: Reset environment")
## STEP 3: install cross-platform compiler
cc_install_path = standalone_sdk_path + '/' + 'tools' + '/'
# set cc package name, download url and install dst dir
if (install_platform == linux_x86):
aarch32_cc = 'gcc-arm-x86_64-none-eabi-10-2020-q4-major'
aarch64_cc = 'gcc-arm-x86_64-aarch64-none-elf-10.2-2020.11'
# cc package name
aarch32_cc_pack = aarch32_cc + '.tar.xz'
aarch64_cc_pack = aarch64_cc + '.tar.xz'
aarch32_cc_url = 'https://gitee.com/phytium_embedded/phytium-standalone-sdk/attach_files/776847/download/gcc-arm-x86_64-none-eabi-10-2020-q4-major.tar.xz'
aarch64_cc_url = 'https://gitee.com/phytium_embedded/phytium-standalone-sdk/attach_files/776846/download/gcc-arm-x86_64-aarch64-none-elf-10.2-2020.11.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-aarch64-none-eabi-10-2020-q4-major'
aarch64_cc = 'gcc-arm-10.2-2020.11-aarch64-aarch64-none-elf'
# cc package name
aarch32_cc_pack = aarch32_cc + '.tar.xz'
aarch64_cc_pack = aarch64_cc + '.tar.xz'
aarch32_cc_url = 'https://gitee.com/phytium_embedded/phytium-standalone-sdk/attach_files/779742/download/gcc-arm-aarch64-none-eabi-10-2020-q4-major.tar.xz'
aarch64_cc_url = 'https://gitee.com/phytium_embedded/phytium-standalone-sdk/attach_files/779743/download/gcc-arm-10.2-2020.11-aarch64-aarch64-none-elf.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
# features for msys2 platform, copy cc package from DEV PATH to SDK PATH
#########################################
if (install_platform == windows_msys2) and (not os.path.exists(aarch32_cc_dl_dst)):
aarch32_cc_dev_path = os.environ.get('PHYTIUM_DEV_PATH') + '\\' + aarch32_cc_pack
if (os.path.exists(aarch32_cc_dev_path)):
print("[3] Copy AARCH32 CC from {}".format(aarch32_cc_dev_path))
shutil.copy2(aarch32_cc_dev_path, aarch32_cc_dl_dst)
if (install_platform == windows_msys2) and (not os.path.exists(aarch64_cc_dl_dst)):
aarch64_cc_dev_path = os.environ.get('PHYTIUM_DEV_PATH') + '\\' + aarch64_cc_pack
if (os.path.exists(aarch64_cc_dev_path)):
print("[3] Copy AARCH64 CC from {}".format(aarch32_cc_dev_path))
shutil.copy2(aarch64_cc_dev_path, aarch64_cc_dl_dst)
#########################################
if is_online: #online
import wget
# download aarch32 cc package
if not aarch32_cc_is_installed:
print("[3] Download AARCH32 CC from {}...".format(aarch32_cc_url))
cc_file = wget.download(aarch32_cc_url, aarch32_cc_dl_dst)
if not os.path.exists(cc_file) or not (cc_file == aarch32_cc_dl_dst):
print("[3]: Download AARCH32 CC failed!!!")
exit()
else:
print("[3]: Download AARCH32 CC at {}".format(aarch32_cc_dl_dst))
# download aarch64 cc package
if not aarch64_cc_is_installed:
print("[3] Download AARCH64 CC from {}...".format(aarch64_cc_url))
cc_file = wget.download(aarch64_cc_url, aarch64_cc_dl_dst)
if not os.path.exists(cc_file) or not (cc_file == aarch64_cc_dl_dst):
print("[3]: Download AARCH64 CC failed!!!")
exit()
else:
print("[3]: Download AARCH64 CC at {}".format(aarch64_cc_dl_dst))
else: #offline, assert cc package exists
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()
# untar aarch32 cc
if not aarch32_cc_is_installed:
if os.path.exists(aarch32_cc_dl_dst):
print("[3]: 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("[3]: AARCH32 CC install success at {}".format(aarch32_cc_install_path))
else:
print("[3]: AARCH32 CC install failed !!!")
exit()
# untar aarch64 cc
if not aarch64_cc_is_installed:
if os.path.exists(aarch64_cc_dl_dst):
print("[3]: 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("[3]: AARCH64 CC install success at {}".format(aarch64_cc_install_path))
else:
print("[3]: AARCH64 CC install failed !!!")
exit()
## STEP 4: 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))