1#!/usr/bin/env python
2
3"""
4Install.py tool to download, unpack, build, and link to the ScaFaCoS library
5used to automate the steps described in the README file in this dir
6"""
7
8from __future__ import print_function
9import sys, os, subprocess, shutil, tarfile
10from argparse import ArgumentParser
11
12sys.path.append('..')
13from install_helpers import fullpath, geturl, get_cpus, checkmd5sum
14
15parser = ArgumentParser(prog='Install.py',
16                        description="LAMMPS library build wrapper script")
17
18# settings
19
20version = "1.0.1"
21url = "https://github.com/scafacos/scafacos/releases/download/v%s/scafacos-%s.tar.gz" % (version, version)
22
23# known checksums for different ScaFaCoS versions. used to validate the download.
24checksums = { \
25        '1.0.1' : 'bd46d74e3296bd8a444d731bb10c1738' \
26        }
27
28# extra help message
29
30HELP = """
31Syntax from src dir: make lib-scafacos args="-b"
32                 or: make lib-scafacos args="-p /usr/local/scafacos"
33Syntax from lib dir: python Install.py -b
34                 or: python Install.py -p /usr/local/scafacos
35
36Example:
37
38make lib-scafacos args="-b"   # download/build in lib/scafacos/scafacos
39make lib-scafacos args="-p $HOME/scafacos" # use existing ScaFaCoS installation in $HOME
40"""
41
42# parse and process arguments
43
44pgroup = parser.add_mutually_exclusive_group()
45pgroup.add_argument("-b", "--build", action="store_true",
46                    help="download and build the ScaFaCoS library")
47pgroup.add_argument("-p", "--path",
48                    help="specify folder of existing ScaFaCoS installation")
49parser.add_argument("-v", "--version", default=version,
50                    help="set version of ScaFaCoS to download and build (default: %s)" % version)
51
52args = parser.parse_args()
53
54# print help message and exit, if neither build nor path options are given
55if not args.build and not args.path:
56  parser.print_help()
57  sys.exit(HELP)
58
59buildflag = args.build
60pathflag = args.path is not None
61version = args.version
62
63homepath = fullpath(".")
64scafacospath = os.path.join(homepath, "scafacos-%s" % version)
65
66if pathflag:
67  scafacospath = args.path
68  if not os.path.isdir(os.path.join(scafacospath, "include")):
69    sys.exit("ScaFaCoS include path for %s does not exist" % scafacospath)
70  if (not os.path.isdir(os.path.join(scafacospath, "lib64"))) \
71     and (not os.path.isdir(os.path.join(scafacospath, "lib"))):
72    sys.exit("ScaFaCoS lib path for %s does not exist" % scafacospath)
73  scafacospath = fullpath(scafacospath)
74
75# download and unpack ScaFaCoS tarball
76
77if buildflag:
78  print("Downloading ScaFaCoS ...")
79  geturl(url, "%s/scafacos-%s.tar.gz" % (homepath, version))
80
81  # verify downloaded archive integrity via md5 checksum, if known.
82  if version in checksums:
83    if not checkmd5sum(checksums[version], '%s/scafacos-%s.tar.gz' % (homepath, version)):
84      sys.exit("Checksum for ScaFaCoS library does not match")
85
86  print("Unpacking ScaFaCoS tarball ...")
87  if os.path.exists(scafacospath):
88    shutil.rmtree(scafacospath)
89  tarname = os.path.join(homepath, "%s.tar.gz" % scafacospath)
90  if tarfile.is_tarfile(tarname):
91    tgz = tarfile.open(tarname)
92    tgz.extractall(path=homepath)
93    os.remove(tarname)
94  else:
95    sys.exit("File %s is not a supported archive" % tarname)
96
97  # build ScaFaCoS
98  print("Building ScaFaCoS ...")
99  n_cpu = get_cpus()
100  cmd = 'cd "%s"; ./configure --prefix="%s" --disable-doc --enable-fcs-solvers=fmm,p2nfft,direct,ewald,p3m --with-internal-fftw --with-internal-pfft --with-internal-pnfft CC=mpicc FC=mpif90 CXX=mpicxx F77=; make -j%d; make install' % (scafacospath, os.path.join(homepath, 'build'), n_cpu)
101  try:
102    txt = subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=True)
103    print(txt.decode('UTF-8'))
104  except subprocess.CalledProcessError as e:
105    sys.exit("Make failed with:\n %s" % e.output.decode('UTF-8'))
106
107# create 2 links in lib/scafacos to ScaFaCoS include/lib dirs
108
109print("Creating links to ScaFaCoS include and lib files")
110if os.path.isfile("includelink") or os.path.islink("includelink"):
111  os.remove("includelink")
112if os.path.isfile("liblink") or os.path.islink("liblink"):
113  os.remove("liblink")
114if buildflag:
115  os.symlink(os.path.join(homepath, 'build', 'include'), 'includelink')
116  os.symlink(os.path.join(homepath, 'build', 'lib'), 'liblink')
117else:
118  os.symlink(os.path.join(scafacospath, 'include'), 'includelink')
119  if os.path.isdir(os.path.join(scafacospath, "lib64")):
120    os.symlink(os.path.join(scafacospath, 'lib64'), 'liblink')
121  else:
122    os.symlink(os.path.join(scafacospath, 'lib'), 'liblink')
123