@ -1,27 +1,34 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
# install.py tool to do a generic build of a library
|
||||
# soft linked to by many of the lib/Install.py files
|
||||
# used to automate the steps described in the corresponding lib/README
|
||||
# Install.py tool to do automate build of Colvars
|
||||
|
||||
import sys,commands,os
|
||||
from __future__ import print_function
|
||||
import sys,os,subprocess
|
||||
|
||||
# help message
|
||||
|
||||
help = """
|
||||
Syntax: python Install.py -m machine -e suffix
|
||||
specify -m and optionally -e, order does not matter
|
||||
Syntax from src dir: make lib-colvars args="-m machine -e suffix"
|
||||
Syntax from lib/colvars dir: python Install.py -m machine -e suffix
|
||||
|
||||
specify -m and optionally -e, order does not matter
|
||||
|
||||
-m = peform a clean followed by "make -f Makefile.machine"
|
||||
machine = suffix of a lib/Makefile.* file
|
||||
machine = suffix of a lib/colvars/Makefile.* or of a
|
||||
src/MAKE/MACHINES/Makefile.* file
|
||||
-e = set EXTRAMAKE variable in Makefile.machine to Makefile.lammps.suffix
|
||||
does not alter existing Makefile.machine
|
||||
|
||||
Examples:
|
||||
|
||||
make lib-colvars args="-m g++" # build COLVARS lib with GNU g++ compiler
|
||||
"""
|
||||
|
||||
# print error message or help
|
||||
|
||||
def error(str=None):
|
||||
if not str: print help
|
||||
else: print "ERROR",str
|
||||
if not str: print(help)
|
||||
else: print("ERROR"),str
|
||||
sys.exit()
|
||||
|
||||
# parse args
|
||||
@ -31,17 +38,17 @@ nargs = len(args)
|
||||
if nargs == 0: error()
|
||||
|
||||
machine = None
|
||||
extraflag = 0
|
||||
extraflag = False
|
||||
|
||||
iarg = 0
|
||||
while iarg < nargs:
|
||||
if args[iarg] == "-m":
|
||||
if iarg+2 > nargs: error()
|
||||
if iarg+2 > len(args): error()
|
||||
machine = args[iarg+1]
|
||||
iarg += 2
|
||||
elif args[iarg] == "-e":
|
||||
if iarg+2 > nargs: error()
|
||||
extraflag = 1
|
||||
if iarg+2 > len(args): error()
|
||||
extraflag = True
|
||||
suffix = args[iarg+1]
|
||||
iarg += 2
|
||||
else: error()
|
||||
@ -51,32 +58,85 @@ while iarg < nargs:
|
||||
cwd = os.getcwd()
|
||||
lib = os.path.basename(cwd)
|
||||
|
||||
# create Makefile.auto as copy of Makefile.machine
|
||||
# reset EXTRAMAKE if requested
|
||||
|
||||
def get_lammps_machine_flags(machine):
|
||||
"""Parse Makefile.machine from LAMMPS, return dictionary of compiler flags"""
|
||||
if not os.path.exists("../../src/MAKE/MACHINES/Makefile.%s" % machine):
|
||||
error("Cannot locate src/MAKE/MACHINES/Makefile.%s" % machine)
|
||||
lines = open("../../src/MAKE/MACHINES/Makefile.%s" % machine,
|
||||
'r').readlines()
|
||||
machine_flags = {}
|
||||
for line in lines:
|
||||
line = line.partition('#')[0]
|
||||
line = line.rstrip()
|
||||
words = line.split()
|
||||
if (len(words) > 2):
|
||||
if ((words[0] == 'CC') or (words[0] == 'CCFLAGS') or
|
||||
(words[0] == 'SHFLAGS') or (words[0] == 'ARCHIVE') or
|
||||
(words[0] == 'ARFLAGS') or (words[0] == 'SHELL')):
|
||||
machine_flags[words[0]] = ' '.join(words[2:])
|
||||
return machine_flags
|
||||
|
||||
def gen_colvars_makefile_machine(machine, machine_flags):
|
||||
"""Generate Makefile.machine for Colvars given the compiler flags"""
|
||||
machine_makefile = open("Makefile.%s" % machine, 'w')
|
||||
machine_makefile.write('''# -*- makefile -*- to build Colvars module with %s
|
||||
|
||||
COLVARS_LIB = libcolvars.a
|
||||
COLVARS_OBJ_DIR =
|
||||
|
||||
CXX = %s
|
||||
CXXFLAGS = %s %s
|
||||
AR = %s
|
||||
ARFLAGS = %s
|
||||
SHELL = %s
|
||||
|
||||
include Makefile.common
|
||||
|
||||
.PHONY: default clean
|
||||
|
||||
default: $(COLVARS_LIB) Makefile.lammps
|
||||
|
||||
clean:
|
||||
-rm -f $(COLVARS_OBJS) $(COLVARS_LIB)
|
||||
''' % (machine, machine_flags['CC'],
|
||||
machine_flags['CCFLAGS'], machine_flags['SHFLAGS'] ,
|
||||
machine_flags['ARCHIVE'], machine_flags['ARFLAGS'],
|
||||
machine_flags['SHELL']))
|
||||
|
||||
if not os.path.exists("Makefile.%s" % machine):
|
||||
machine_flags = get_lammps_machine_flags(machine)
|
||||
gen_colvars_makefile_machine(machine, machine_flags)
|
||||
if not os.path.exists("Makefile.%s" % machine):
|
||||
error("lib/%s/Makefile.%s does not exist" % (lib,machine))
|
||||
|
||||
# create Makefile.auto as copy of Makefile.machine
|
||||
# reset EXTRAMAKE if requested
|
||||
|
||||
lines = open("Makefile.%s" % machine,'r').readlines()
|
||||
fp = open("Makefile.auto",'w')
|
||||
|
||||
for line in lines:
|
||||
words = line.split()
|
||||
if len(words) == 3 and extraflag and \
|
||||
words[0] == "EXTRAMAKE" and words[1] == '=':
|
||||
line = line.replace(words[2],"Makefile.lammps.%s" % suffix)
|
||||
print >>fp,line,
|
||||
|
||||
fp.write(line)
|
||||
fp.close()
|
||||
|
||||
# make the library via Makefile.auto
|
||||
|
||||
print "Building lib%s.a ..." % lib
|
||||
cmd = "make -f Makefile.auto clean; make -f Makefile.auto"
|
||||
txt = commands.getoutput(cmd)
|
||||
print txt
|
||||
try:
|
||||
import multiprocessing
|
||||
n_cpus = multiprocessing.cpu_count()
|
||||
except:
|
||||
n_cpus = 1
|
||||
|
||||
if os.path.exists("lib%s.a" % lib): print "Build was successful"
|
||||
print("Building lib%s.a ..." % lib)
|
||||
cmd = ["make -f Makefile.auto clean"]
|
||||
print(subprocess.check_output(cmd, shell=True).decode())
|
||||
cmd = ["make -f Makefile.auto -j%d" % n_cpus]
|
||||
print(subprocess.check_output(cmd, shell=True).decode())
|
||||
|
||||
if os.path.exists("lib%s.a" % lib): print("Build was successful")
|
||||
else: error("Build of lib/%s/lib%s.a was NOT successful" % (lib,lib))
|
||||
if not os.path.exists("Makefile.lammps"):
|
||||
print "lib/%s/Makefile.lammps was NOT created" % lib
|
||||
print("lib/%s/Makefile.lammps was NOT created" % lib)
|
||||
|
||||
Reference in New Issue
Block a user