Salome HOME
b3e6aaf54829f54df7b6589d1f1dccae38adbd12
[tools/sat.git] / commands / make.py
1 #!/usr/bin/env python
2 #-*- coding:utf-8 -*-
3 #  Copyright (C) 2010-2012  CEA/DEN
4 #
5 #  This library is free software; you can redistribute it and/or
6 #  modify it under the terms of the GNU Lesser General Public
7 #  License as published by the Free Software Foundation; either
8 #  version 2.1 of the License.
9 #
10 #  This library is distributed in the hope that it will be useful,
11 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 #  Lesser General Public License for more details.
14 #
15 #  You should have received a copy of the GNU Lesser General Public
16 #  License along with this library; if not, write to the Free Software
17 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18
19 import os
20 import re
21
22 import src
23
24 # Define all possible option for the make command :  sat make <options>
25 parser = src.options.Options()
26 parser.add_option('p', 'products', 'list2', 'products',
27     _('Optional: products to configure. This option accepts a comma separated list.'))
28 parser.add_option('o', 'option', 'string', 'option',
29     _('Optional: Option to add to the make command.'), "")
30
31
32 def log_step(logger, header, step):
33     logger.write("\r%s%s" % (header, " " * 20), 3)
34     logger.write("\r%s%s" % (header, step), 3)
35     logger.write("\n==== %s \n" % src.printcolors.printcInfo(step), 4)
36     logger.flush()
37
38 def log_res_step(logger, res):
39     if res == 0:
40         logger.write("%s \n" % src.printcolors.printcSuccess("OK"), 4)
41         logger.flush()
42     else:
43         logger.write("%s \n" % src.printcolors.printcError("KO"), 4)
44         logger.flush()
45
46 def make_all_products(config, products_infos, make_option, logger):
47     '''Execute the proper configuration commands 
48        in each product build directory.
49
50     :param config Config: The global configuration
51     :param products_info list: List of 
52                                  (str, Config) => (product_name, product_info)
53     :param make_option str: The options to add to the command
54     :param logger Logger: The logger instance to use for the display and logging
55     :return: the number of failing commands.
56     :rtype: int
57     '''
58     res = 0
59     for p_name_info in products_infos:
60         res_prod = make_product(p_name_info, make_option, config, logger)
61         if res_prod != 0:
62             res += 1 
63     return res
64
65 def make_product(p_name_info, make_option, config, logger):
66     '''Execute the proper configuration command(s) 
67        in the product build directory.
68     
69     :param p_name_info tuple: (str, Config) => (product_name, product_info)
70     :param make_option str: The options to add to the command
71     :param config Config: The global configuration
72     :param logger Logger: The logger instance to use for the display 
73                           and logging
74     :return: 1 if it fails, else 0.
75     :rtype: int
76     '''
77     
78     p_name, p_info = p_name_info
79     
80     # Logging
81     logger.write("\n", 4, False)
82     logger.write("################ ", 4)
83     header = _("Make of %s") % src.printcolors.printcLabel(p_name)
84     header += " %s " % ("." * (20 - len(p_name)))
85     logger.write(header, 3)
86     logger.write("\n", 4, False)
87     logger.flush()
88
89     # Do nothing if he product is not compilable
90     if ("properties" in p_info and "compilation" in p_info.properties and 
91                                         p_info.properties.compilation == "no"):
92         log_step(logger, header, "ignored")
93         logger.write("\n", 3, False)
94         return 0
95
96     # Instantiate the class that manages all the construction commands
97     # like cmake, make, make install, make test, environment management, etc...
98     builder = src.compilation.Builder(config, logger, p_info)
99     
100     # Prepare the environment
101     log_step(logger, header, "PREPARE ENV")
102     res_prepare = builder.prepare()
103     log_res_step(logger, res_prepare)
104     
105     # Execute buildconfigure, configure if the product is autotools
106     # Execute cmake if the product is cmake
107     len_end_line = 20
108
109     nb_proc, make_opt_without_j = get_nb_proc(p_info, config, make_option)
110     log_step(logger, header, "MAKE -j" + str(nb_proc))
111     if src.architecture.is_windows():
112         res = builder.wmake(nb_proc, make_opt_without_j)
113     else:
114         res = builder.make(nb_proc, make_opt_without_j)
115     log_res_step(logger, res)
116     
117     # Log the result
118     if res > 0:
119         logger.write("\r%s%s" % (header, " " * len_end_line), 3)
120         logger.write("\r" + header + src.printcolors.printcError("KO"))
121         logger.write("==== %(KO)s in make of %(name)s \n" %
122             { "name" : p_name , "KO" : src.printcolors.printcInfo("ERROR")}, 4)
123         logger.flush()
124     else:
125         logger.write("\r%s%s" % (header, " " * len_end_line), 3)
126         logger.write("\r" + header + src.printcolors.printcSuccess("OK"))
127         logger.write("==== %s \n" % src.printcolors.printcInfo("OK"), 4)
128         logger.write("==== Make of %(name)s %(OK)s \n" %
129             { "name" : p_name , "OK" : src.printcolors.printcInfo("OK")}, 4)
130         logger.flush()
131     logger.write("\n", 3, False)
132
133     return res
134
135 def get_nb_proc(product_info, config, make_option):
136     
137     opt_nb_proc = None
138     new_make_option = make_option
139     if "-j" in make_option:
140         oExpr = re.compile("-j[0-9]+")
141         found = oExpr.search(make_option)
142         opt_nb_proc = int(re.findall('\d+', found.group())[0])
143         new_make_option = make_option.replace(found.group(), "")
144     
145     nbproc = -1
146     if "nb_proc" in product_info:
147         # nb proc is specified in module definition
148         nbproc = product_info.nb_proc
149         if opt_nb_proc and opt_nb_proc < product_info.nb_proc:
150             # use command line value only if it is lower than module definition
151             nbproc = opt_nb_proc
152     else:
153         # nb proc is not specified in module definition
154         if opt_nb_proc:
155             nbproc = opt_nb_proc
156         else:
157             nbproc = config.VARS.nb_proc
158     
159     assert nbproc > 0
160     return nbproc, new_make_option
161
162 def description():
163     '''method that is called when salomeTools is called with --help option.
164     
165     :return: The text to display for the make command description.
166     :rtype: str
167     '''
168     return _("The make command executes the \"make\" command in"
169              " the build directory.\nexample:\nsat make SALOME-master "
170              "--products Python,KERNEL,GUI")
171   
172 def run(args, runner, logger):
173     '''method that is called when salomeTools is called with make parameter.
174     '''
175     
176     # Parse the options
177     (options, args) = parser.parse_args(args)
178
179     # check that the command has been called with an application
180     src.check_config_has_application( runner.cfg )
181
182     # Get the list of products to treat
183     products_infos = src.product.get_products_list(options, runner.cfg, logger)
184     
185     # Print some informations
186     logger.write(_('Executing the make command in the build '
187                                 'directories of the application %s\n') % 
188                 src.printcolors.printcLabel(runner.cfg.VARS.application), 1)
189     
190     info = [(_("BUILD directory"),
191              os.path.join(runner.cfg.APPLICATION.workdir, 'BUILD'))]
192     src.print_info(logger, info)
193     
194     # Call the function that will loop over all the products and execute
195     # the right command(s)
196     if options.option is None:
197         options.option = ""
198     res = make_all_products(runner.cfg, products_infos, options.option, logger)
199     
200     # Print the final state
201     nb_products = len(products_infos)
202     if res == 0:
203         final_status = "OK"
204     else:
205         final_status = "KO"
206    
207     logger.write(_("\nMake: %(status)s (%(valid_result)d/%(nb_products)d)\n") % \
208         { 'status': src.printcolors.printc(final_status), 
209           'valid_result': nb_products - res,
210           'nb_products': nb_products }, 1)    
211     
212     return res