1#!/usr/bin/env python
2
3"""
4Install.py tool to do a generic build of a library
5soft linked to by many of the lib/Install.py files
6used to automate the steps described in the corresponding lib/README
7"""
8
9from __future__ import print_function
10import sys, os, subprocess
11from argparse import ArgumentParser
12
13sys.path.append('..')
14from install_helpers import get_cpus, fullpath
15
16parser = ArgumentParser(prog='Install.py',
17                        description="LAMMPS library build wrapper script")
18
19HELP = """
20Syntax from src dir: make lib-libname args="-m machine -e suffix"
21Syntax from lib dir: python Install.py -m machine -e suffix
22
23libname = name of lib dir (e.g. atc, h5md, meam, poems, etc)
24specify -m and optionally -e, order does not matter
25
26Examples:
27
28make lib-poems args="-m serial" # build POEMS lib with same settings as in the serial Makefile in src
29make lib-colvars args="-m mpi"  # build COLVARS lib with same settings as in the mpi Makefile in src
30make lib-meam args="-m ifort"   # build MEAM lib with custom Makefile.ifort (using Intel Fortran)
31"""
32
33# parse and process arguments
34
35parser.add_argument("-m", "--machine",
36                    help="suffix of a <libname>/Makefile.* file used for compiling this library")
37parser.add_argument("-e", "--extramake",
38                    help="set EXTRAMAKE variable in <libname>/Makefile.<machine> to Makefile.lammps.<extramake>")
39
40args = parser.parse_args()
41
42# print help message and exit, if neither build nor path options are given
43if not args.machine and not args.extramake:
44  parser.print_help()
45  sys.exit(HELP)
46
47machine = args.machine
48extraflag = args.extramake
49if extraflag:
50    suffix = args.extramake
51else:
52    suffix = 'empty'
53
54# set lib from working dir
55
56cwd = fullpath('.')
57lib = os.path.basename(cwd)
58
59# create Makefile.auto as copy of Makefile.machine
60# reset EXTRAMAKE if requested
61
62if not os.path.exists("Makefile.%s" % machine):
63  sys.exit("lib/%s/Makefile.%s does not exist" % (lib, machine))
64
65lines = open("Makefile.%s" % machine, 'r').readlines()
66fp = open("Makefile.auto", 'w')
67
68has_extramake = False
69for line in lines:
70  words = line.split()
71  if len(words) == 3 and words[0] == "EXTRAMAKE" and words[1] == '=':
72    has_extramake = True
73    if extraflag:
74      line = line.replace(words[2], "Makefile.lammps.%s" % suffix)
75  fp.write(line)
76
77fp.close()
78
79# make the library via Makefile.auto optionally with parallel make
80n_cpus = get_cpus()
81
82print("Building lib%s.a ..." % lib)
83cmd = "make -f Makefile.auto clean; make -f Makefile.auto -j%d" % n_cpus
84try:
85  txt = subprocess.check_output(cmd, shell=True, stderr=subprocess.STDOUT)
86  print(txt.decode('UTF-8'))
87except subprocess.CalledProcessError as e:
88  print("Make failed with:\n %s" % e.output.decode('UTF-8'))
89  sys.exit(1)
90
91if os.path.exists("lib%s.a" % lib):
92  print("Build was successful")
93else:
94  sys.exit("Build of lib/%s/lib%s.a was NOT successful" % (lib, lib))
95
96if has_extramake and not os.path.exists("Makefile.lammps"):
97  print("WARNING: lib/%s/Makefile.lammps was NOT created" % lib)
98