PLUTO
make_problem.py
Go to the documentation of this file.
1 import os
2 import sys
3 import shutil
4 import menu
5 import pluto_files_IO as pfIO
6 from define_problem import DefineProblem
7 
8 class MakeProblem(object):
9  def __init__(self, work_dir, pluto_dir, auto_update_def, auto_update_mkfl):
10  """Create the makefile for the PLUTO code from the defintions header file.
11 
12  This class creates a makefile with all necessary information
13  that would compile the code based on the problem defined by user
14  in defintions.h file.
15  In case the makefile is already present, this class will
16  read those default values to re-create the file.
17 
18  **Inputs**:
19  1. work_dir = Path to PLUTO code working directory
20  2. pluto_dir = Path to PLUTO source directory
21  3. auto-update_def = Boolean that indicates auto-update of defintions.h.
22  4. auto-update_mkfl = Boolean that indicates auto-update of makefile.
23 
24  **Output**:
25  It generates a makefile for compilation.
26  """
27  self.work_dir = work_dir
28  self.pluto_dir = pluto_dir
29  self.auto_update = auto_update_mkfl
30  self.mkfl_name = self.work_dir+'/makefile'
31  Dp = DefineProblem(self.work_dir, self.pluto_dir, auto_update_def)
32  self.additional_files = Dp.additional_files
33  self.additional_flags = Dp.additional_flags
34  self.header_files = Dp.header_files
35  self.pluto_path = Dp.pluto_path
36  self.chomboflag = Dp.flag_dict['WITH-CHOMBO']
37 
38  self.SetArch()
39  self.row = 1
40  menu.Print ("> Generating makefile... ["+self.arch+"]", row=self.row,sleep=1)
41 
42  if self.chomboflag:
43  self.ChomboMakeVars()
44  self.makefile_template = '/Src/Templates/makefile.chombo'
45  else:
46  self.makefile_template = '/Src/Templates/makefile'
47 
48  self.UpdateMkflTemplate()
49 
50 
51  def SetArch(self):
52  """Sets the Architecture for compilation of code.
53 
54  This attribute of the MakeProblem class looks for the
55  'ARCH' keyword in makefile and if not defined will define it
56  based on user's choice of the Makefile configuration
57  from the Config/ folder. If already defined then will use
58  that architecture for compilation of Makefile.
59  """
60  mkfl_exits = os.path.exists(self.mkfl_name)
61  if mkfl_exits:
62  pf = pfIO.PlutoFiles(self.mkfl_name)
63  scrh = pf.LocateString('ARCH')
64 
65  if self.auto_update == 0 or not mkfl_exits or len(scrh[0]) == 0:
66  def_list = []
67  entries = os.listdir(self.pluto_dir + '/Config')
68  for def_file in entries:
69  if (def_file.endswith('.defs')): def_list.append(def_file)
70 
71  def_list.sort()
72  menu.SetTitle("Change makefile")
73  self.arch = menu.Browse(def_list)
74  self.arch_string = 'ARCH = '+ self.arch + '\n'
75  else:
76  self.arch_string = scrh[0][1]
77  self.arch = self.arch_string.split()[2]
78 
79  def ChomboMakeVars(self):
80  """Adds CHOMBO specific vars in the Makefile.
81 
82  This method of the MakeProblem class does necessary
83  modification to the makefile so as to accomodate
84  compilation of chombo (AMR) specific variables.
85  """
86  pf = pfIO.PlutoFiles(self.work_dir+'/definitions.h')
87  scrh = pf.LocateString('DIMENSIONS')
88  dims = scrh[0][1].split()[2]
89  chombo_config_string = 'DIM='+dims
90 
91  if '--with-chombo:' in sys.argv:
92  i = sys.argv.index('--with-chombo:') + 1
93  try:
94  sys.argv[1:]
95  except IndexError:
96  print "Additional Configration Details Required for '--with-chombo:' flag"
97  sys.exit()
98  else:
99  for y in sys.argv[i:]:
100  chombo_config_string += ' '+y
101 
102  self.row += 1
103  menu.Print(" - Chombo config string: "+chombo_config_string,row=self.row)
104  self.row += 1
105  menu.Print(" - creating make.vars...",row=self.row,sleep=0)
106  os.chdir(self.pluto_dir+"/Lib/Chombo-3.2/lib")
107  os.system("make "+chombo_config_string+" vars > make.vars\n")
108  os.system("cp make.vars "+self.work_dir+"\n")
109  os.chdir(self.work_dir)
110 
111 
113  """
114  Updates Makefile with additional flags, files and modular makefile paths.
115  """
116  shutil.copy(self.pluto_dir + self.makefile_template, self.mkfl_name)
117  pf = pfIO.PlutoFiles(self.mkfl_name)
118  pf.ReplaceWord('PLUTO_DIR','PLUTO_DIR = '+self.pluto_dir+'\n',DelOld=True)
119  pf.ReplaceWord('ARCH',self.arch_string, DelOld=True)
120 
121  scrh = pf.LocateString('Additional_header_files_here')
122  ipos = scrh[0][0] + 3
123  for x in self.header_files:
124  pf.InsertLine('HEADERS += '+ x +'\n',ipos)
125  ipos = ipos + 1
126 
127  scrh = pf.LocateString('Additional_object_files_here')
128  ipos = scrh[0][0] + 3
129  for x in self.additional_files:
130  pf.InsertLine('OBJ += '+x + '\n', ipos)
131  ipos = ipos + 1
132 
133  for x in self.pluto_path:
134  pf.InsertLine('include $(SRC)/' + x + 'makefile' + '\n',ipos)
135  ipos = ipos + 1
136 
137  for x in self.additional_flags:
138  pf.InsertLine('CFLAGS += '+x+'\n', ipos)
139  ipos = ipos + 1
140 
def __init__(self, work_dir, pluto_dir, auto_update_def, auto_update_mkfl)
Definition: make_problem.py:9
def Print
Print a message on the screen.
Definition: menu.py:60
def SetTitle
Definition: menu.py:50
def Browse
Browse a menu with entries (1st column) and default (2nd column, optional) Note: with Python > 2...
Definition: menu.py:181