Salome HOME
Add the script command, change the compile command in order to take the script comman...
[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 can be'
28     ' passed several time to configure several products.'))
29 parser.add_option('o', 'option', 'string', 'option',
30     _('Optional: Option to add to the make command.'), "")
31
32 def get_products_list(options, cfg, logger):
33     '''method that gives the product list with their informations from 
34        configuration regarding the passed options.
35     
36     :param options Options: The Options instance that stores the commands 
37                             arguments
38     :param cfg Config: The global configuration
39     :param logger Logger: The logger instance to use for the display and 
40                           logging
41     :return: The list of (product name, product_informations).
42     :rtype: List
43     '''
44     # Get the products to be prepared, regarding the options
45     if options.products is None:
46         # No options, get all products sources
47         products = cfg.APPLICATION.products
48     else:
49         # if option --products, check that all products of the command line
50         # are present in the application.
51         products = options.products
52         for p in products:
53             if p not in cfg.APPLICATION.products:
54                 raise src.SatException(_("Product %(product)s "
55                             "not defined in application %(application)s") %
56                         { 'product': p, 'application': cfg.VARS.application} )
57     
58     # Construct the list of tuple containing 
59     # the products name and their definition
60     products_infos = src.product.get_products_infos(products, cfg)
61     
62     products_infos = [pi for pi in products_infos if not(
63                                      src.product.product_is_native(pi[1]) or 
64                                      src.product.product_is_fixed(pi[1]))]
65     
66     return products_infos
67
68 def log_step(logger, header, step):
69     logger.write("\r%s%s" % (header, " " * 20), 3)
70     logger.write("\r%s%s" % (header, step), 3)
71     logger.write("\n==== %s \n" % src.printcolors.printcInfo(step), 4)
72     logger.flush()
73
74 def log_res_step(logger, res):
75     if res == 0:
76         logger.write("%s \n" % src.printcolors.printcSuccess("OK"), 4)
77         logger.flush()
78     else:
79         logger.write("%s \n" % src.printcolors.printcError("KO"), 4)
80         logger.flush()
81
82 def make_all_products(config, products_infos, make_option, logger):
83     '''Execute the proper configuration commands 
84        in each product build directory.
85
86     :param config Config: The global configuration
87     :param products_info list: List of 
88                                  (str, Config) => (product_name, product_info)
89     :param make_option str: The options to add to the command
90     :param logger Logger: The logger instance to use for the display and logging
91     :return: the number of failing commands.
92     :rtype: int
93     '''
94     res = 0
95     for p_name_info in products_infos:
96         res_prod = make_product(p_name_info, make_option, config, logger)
97         if res_prod != 0:
98             res += 1 
99     return res
100
101 def make_product(p_name_info, make_option, config, logger):
102     '''Execute the proper configuration command(s) 
103        in the product build directory.
104     
105     :param p_name_info tuple: (str, Config) => (product_name, product_info)
106     :param make_option str: The options to add to the command
107     :param config Config: The global configuration
108     :param logger Logger: The logger instance to use for the display 
109                           and logging
110     :return: 1 if it fails, else 0.
111     :rtype: int
112     '''
113     
114     p_name, p_info = p_name_info
115     
116     # Logging
117     logger.write("\n", 4, False)
118     logger.write("################ ", 4)
119     header = _("Make of %s") % src.printcolors.printcLabel(p_name)
120     header += " %s " % ("." * (20 - len(p_name)))
121     logger.write(header, 3)
122     logger.write("\n", 4, False)
123     logger.flush()
124
125     # Do nothing if he product is not compilable
126     if ("properties" in p_info and "compilation" in p_info.properties and 
127                                         p_info.properties.compilation == "no"):
128         log_step(logger, header, "ignored")
129         logger.write("\n", 3, False)
130         return 0
131
132     # Instantiate the class that manages all the construction commands
133     # like cmake, make, make install, make test, environment management, etc...
134     builder = src.compilation.Builder(config, logger, p_info)
135     
136     # Prepare the environment
137     log_step(logger, header, "PREPARE ENV")
138     res_prepare = builder.prepare()
139     log_res_step(logger, res_prepare)
140     
141     # Execute buildconfigure, configure if the product is autotools
142     # Execute cmake if the product is cmake
143     len_end_line = 20
144     res = 0
145     nb_proc, make_opt_without_j = get_nb_proc(p_info, config, make_option)
146     log_step(logger, header, "MAKE -j" + str(nb_proc))
147     res_m = builder.make(nb_proc, make_opt_without_j)
148     log_res_step(logger, res_m)
149     res += res_m
150     
151     # Log the result
152     if res > 0:
153         logger.write("\r%s%s" % (header, " " * len_end_line), 3)
154         logger.write("\r" + header + src.printcolors.printcError("KO"))
155         logger.write("==== %(KO)s in make of %(name)s \n" %
156             { "name" : p_name , "KO" : src.printcolors.printcInfo("ERROR")}, 4)
157         logger.flush()
158     else:
159         logger.write("\r%s%s" % (header, " " * len_end_line), 3)
160         logger.write("\r" + header + src.printcolors.printcSuccess("OK"))
161         logger.write("==== %s \n" % src.printcolors.printcInfo("OK"), 4)
162         logger.write("==== Make of %(name)s %(OK)s \n" %
163             { "name" : p_name , "OK" : src.printcolors.printcInfo("OK")}, 4)
164         logger.flush()
165     logger.write("\n", 3, False)
166
167     return res
168
169 def get_nb_proc(product_info, config, make_option):
170     
171     opt_nb_proc = None
172     new_make_option = make_option
173     if "-j" in make_option:
174         oExpr = re.compile("-j[0-9]+")
175         found = oExpr.search(make_option)
176         opt_nb_proc = int(re.findall('\d+', found.group())[0])
177         new_make_option = make_option.replace(found.group(), "")
178     
179     nbproc = -1
180     if "nb_proc" in product_info:
181         # nb proc is specified in module definition
182         nbproc = product_info.nb_proc
183         if opt_nb_proc and opt_nb_proc < product_info.nb_proc:
184             # use command line value only if it is lower than module definition
185             nbproc = opt_nb_proc
186     else:
187         # nb proc is not specified in module definition
188         if opt_nb_proc:
189             nbproc = opt_nb_proc
190         else:
191             nbproc = config.VARS.nb_proc
192     
193     assert nbproc > 0
194     return nbproc, new_make_option
195
196 def description():
197     '''method that is called when salomeTools is called with --help option.
198     
199     :return: The text to display for the make command description.
200     :rtype: str
201     '''
202     return _("The make command executes the \"make\" command in"
203              " the build directory.\nIn case of a product that is constructed "
204              "using a script (build_source :  \"script\"), then the make "
205              "command executes the script.\n\nexample:\nsat make SALOME-master "
206              "--products Python,KERNEL,GUI")
207   
208 def run(args, runner, logger):
209     '''method that is called when salomeTools is called with make parameter.
210     '''
211     
212     # Parse the options
213     (options, args) = parser.parse_args(args)
214
215     # check that the command has been called with an application
216     src.check_config_has_application( runner.cfg )
217
218     # Get the list of products to treat
219     products_infos = get_products_list(options, runner.cfg, logger)
220     
221     # Print some informations
222     logger.write(_('Executing the make command in the build '
223                                 'directories of the application %s\n') % 
224                 src.printcolors.printcLabel(runner.cfg.VARS.application), 1)
225     
226     info = [(_("BUILD directory"),
227              os.path.join(runner.cfg.APPLICATION.workdir, 'BUILD'))]
228     src.print_info(logger, info)
229     
230     # Call the function that will loop over all the products and execute
231     # the right command(s)
232     if options.option is None:
233         options.option = ""
234     res = make_all_products(runner.cfg, products_infos, options.option, logger)
235     
236     # Print the final state
237     nb_products = len(products_infos)
238     if res == 0:
239         final_status = "OK"
240     else:
241         final_status = "KO"
242    
243     logger.write(_("\nMake: %(status)s (%(valid_result)d/%(nb_products)d)\n") % \
244         { 'status': src.printcolors.printc(final_status), 
245           'valid_result': nb_products - res,
246           'nb_products': nb_products }, 1)    
247     
248     return res