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