import os import sys import subprocess import time import glob import shutil from lib.ui import bordered, underlined, progressbar def install_lib(lib, src_dir, work_dir, inst_dir, comp_cc, comp_cxx, comp_fc, build_threads, disable_shared_libs, verbose): print(bordered("Installing " + lib['name'] + " v." + lib['version'])) if not os.path.exists(src_dir): os.makedirs(src_dir) if not os.path.exists(work_dir): os.makedirs(work_dir) if not os.path.exists(inst_dir): os.makedirs(inst_dir) os.chdir(src_dir) lib_name = lib['name'] lib_version = lib['version'] if not verbose: logfile_path = work_dir + "/" + lib_name + "-" + lib_version + ".log" if os.path.exists(logfile_path): os.remove(logfile_path) logfile = open(logfile_path, 'a') else: logfile = None # download lib src if not present if not os.path.exists(lib['name']+"-"+lib['version']): print("Downloading Source Code") load_command = lib['download'].replace("$VERSION_UNDERSCORE", lib_version.replace(".", "_")).replace("$VERSION", lib_version) err = subprocess.call(load_command, stdout=logfile, stderr=logfile, shell=True) if err != 0: print("Error: Download of "+lib_name+" v."+lib_version+" failed!") print("See "+logfile_path+" for details") sys.exit(0) # configure library os.chdir(work_dir) if os.path.exists(work_dir+"/"+lib_name+"-"+lib_version): shutil.rmtree(work_dir+"/"+lib_name+"-"+lib_version) shutil.copytree(src_dir+"/"+lib_name+"-"+lib_version, work_dir+"/"+lib_name+"-"+lib_version) os.chdir(work_dir+"/"+lib_name+"-"+lib_version) if disable_shared_libs: shared_option = "--disable-shared" else: shared_option = "" config_command = lib['configure'].replace("$PREFIX", inst_dir).replace("$SHARED",shared_option) # TODO: THIS IS UGLY AND NEEDS TO BE MOVED TO EITHER LIB OR COMPILER CONFIGURATION # workaround for bad autotools reconization of flang if (lib_name == "hdf5" or lib_name == "netcdf-f") and "aocc" in inst_dir: config_command = config_command + " FCFLAGS=-fPIC" if verbose: print(underlined("\nConfiguring Library")) else: print("Configuring library...") err = subprocess.call(config_command, stdout=logfile, stderr=logfile, shell=True) if err != 0: print("Error: Configuring of "+lib_name+" v."+lib_version+" failed!") print("See "+logfile_path+" for details") sys.exit(1) # TODO: THIS IS UGLY AND NEEDS TO BE MOVED TO EITHER LIB OR COMPILER CONFIGURATION # workaround for bad autotools reconization of flang if (lib_name == "hdf5" or lib_name == "netcdf-f") and "aocc" in inst_dir: err = subprocess.call("sed -i -e 's/wl=\"\"/wl=\"-Wl,\"/g' libtool", shell=True) # build library build_command = lib['build'].replace("$BUILDTHREADS", build_threads) if verbose: print(underlined("\nBuilding Library")) err = subprocess.call(build_command, stdout=logfile, stderr=logfile, shell=True) else: build_task = subprocess.Popen(build_command, stdout=logfile, stderr=logfile, shell=True) num_objects = int(lib['object files']) finished = False while build_task.poll() is None: if not finished: bar_width = 50 for i in progressbar(range(num_objects), "Building library: ", bar_width): while len(glob.glob('**/*.o', recursive=True)) < i: time.sleep(0.5) if build_task.poll is not None: break finished = True build_task.wait() err = build_task.returncode if err != 0: print("Error: Building "+lib_name+" v."+lib_version+" failed!") print("See "+logfile_path+" for details") sys.exit(1) # install library install_command = lib['install'].replace("$BUILDTHREADS", build_threads) if verbose: print(underlined("\nInstalling Library")) else: print("Installing library...") err = subprocess.call(install_command, stdout=logfile, stderr=logfile, shell=True) if err != 0: print("Error: Installing "+lib_name+" v."+lib_version+" failed!") print("See "+logfile_path+" for details") sys.exit(1) print("Library "+lib_name+" has been installed successfully!\n") # copy logfile to install location if not verbose: build_output_path = inst_dir+"/"+"build_info" if not os.path.exists(build_output_path): os.makedirs(build_output_path) shutil.copyfile(logfile_path, build_output_path+"/"+lib_name+".log")