1#!/usr/bin/env python
2
3"""
4Install.py tool to download, unpack, and point to the Eigen library
5used to automate the steps described in the README file in this dir
6"""
7
8from __future__ import print_function
9import sys, os, glob, shutil, tarfile
10from argparse import ArgumentParser
11
12sys.path.append('..')
13from install_helpers import fullpath, geturl, checkmd5sum
14
15parser = ArgumentParser(prog='Install.py',
16                        description="LAMMPS library build wrapper script")
17
18# settings
19
20version = '3.3.9'
21tarball = "eigen.tar.gz"
22
23# known checksums for different Eigen versions. used to validate the download.
24checksums = { \
25              '3.3.9' : '609286804b0f79be622ccf7f9ff2b660', \
26              '3.3.7' : '9e30f67e8531477de4117506fe44669b' \
27}
28
29
30# help message
31
32HELP = """
33Syntax from src dir: make lib-smd args="-b"
34                 or: make lib-smd args="-p /usr/include/eigen3"
35
36Syntax from lib dir: python Install.py -b
37                 or: python Install.py -p /usr/include/eigen3"
38                 or: python Install.py -v 3.3.7 -b
39
40Example:
41
42make lib-smd args="-b"   # download/build in default lib/smd/eigen-eigen-*
43make lib-smd args="-p /usr/include/eigen3" # use existing Eigen installation in /usr/include/eigen3
44"""
45
46pgroup = parser.add_mutually_exclusive_group()
47pgroup.add_argument("-b", "--build", action="store_true",
48                    help="download and build the Eigen3 library")
49pgroup.add_argument("-p", "--path",
50                    help="specify folder of existing Eigen installation")
51parser.add_argument("-v", "--version", default=version,
52                    help="set version of Eigen to download and build (default: %s)" % version)
53
54args = parser.parse_args()
55
56# print help message and exit, if neither build nor path options are given
57if not args.build and not args.path:
58  parser.print_help()
59  sys.exit(HELP)
60
61homepath = fullpath(".")
62eigenpath = os.path.join(homepath, "eigen3")
63
64buildflag = args.build
65pathflag = args.path is not None
66version = args.version
67
68if pathflag:
69  eigenpath = args.path
70  if not os.path.isdir(eigenpath):
71    sys.exit("Eigen path %s does not exist" % eigenpath)
72  eigenpath = fullpath(eigenpath)
73
74# download and unpack Eigen tarball
75# use glob to find name of dir it unpacks to
76
77if buildflag:
78  print("Downloading Eigen ...")
79  eigentar = os.path.join(homepath, tarball)
80  url = "https://gitlab.com/libeigen/eigen/-/archive/%s/eigen-%s.tar.gz" %  (version,version)
81  geturl(url, eigentar)
82
83  # verify downloaded archive integrity via md5 checksum, if known.
84  if version in checksums:
85    print("checking version %s\n" % version)
86    if not checkmd5sum(checksums[version], eigentar):
87      sys.exit("Checksum for Eigen library does not match")
88
89
90  print("Cleaning up old folders ...")
91  edir = glob.glob(os.path.join(homepath, "eigen-*"))
92  edir.append(eigenpath)
93  for one in edir:
94    if os.path.isdir(one):
95      shutil.rmtree(one)
96
97  print("Unpacking Eigen tarball ...")
98  if tarfile.is_tarfile(eigentar):
99    tgz = tarfile.open(eigentar)
100    tgz.extractall(path=homepath)
101    os.remove(eigentar)
102  else:
103    sys.exit("File %s is not a supported archive" % eigentar)
104  edir = os.path.join(homepath, "eigen-%s" % version)
105  os.rename(edir, eigenpath)
106
107# create link in lib/smd to Eigen src dir
108
109print("Creating link to Eigen include folder")
110if os.path.isfile("includelink") or os.path.islink("includelink"):
111  os.remove("includelink")
112linkdir = eigenpath
113os.symlink(linkdir, 'includelink')
114