Salome HOME
Add the makeinstall command. Add the script compilation in make command
[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 
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     # Instantiate the class that manages all the construction commands
126     # like cmake, make, make install, make test, environment management, etc...
127     builder = src.compilation.Builder(config, logger, p_info)
128     
129     # Prepare the environment
130     log_step(logger, header, "PREPARE ENV")
131     res_prepare = builder.prepare()
132     log_res_step(logger, res_prepare)
133     
134     # Execute buildconfigure, configure if the product is autotools
135     # Execute cmake if the product is cmake
136     len_end_line = 20
137     res = 0
138     if not src.product.product_has_script(p_info):
139         nb_proc, make_opt_without_j = get_nb_proc(p_info, config, make_option)
140         log_step(logger, header, "MAKE -j" + str(nb_proc))
141         res_m = builder.make(nb_proc, make_opt_without_j)
142         log_res_step(logger, res_m)
143         res += res_m
144     else:
145         scrit_path_display = src.printcolors.printcLabel(p_info.compil_script)
146         log_step(logger, header, "SCRIPT " + scrit_path_display)
147         len_end_line += len(scrit_path_display)
148         res_s = builder.do_script_build(p_info.compil_script)
149         log_res_step(logger, res_s)
150         res += res_s
151     
152     # Log the result
153     if res > 0:
154         logger.write("\r%s%s" % (header, " " * len_end_line), 3)
155         logger.write("\r" + header + src.printcolors.printcError("KO"))
156         logger.write("==== %(KO)s in make of %(name)s \n" %
157             { "name" : p_name , "KO" : src.printcolors.printcInfo("ERROR")}, 4)
158         logger.flush()
159     else:
160         logger.write("\r%s%s" % (header, " " * len_end_line), 3)
161         logger.write("\r" + header + src.printcolors.printcSuccess("OK"))
162         logger.write("==== %s \n" % src.printcolors.printcInfo("OK"), 4)
163         logger.write("==== Make of %(name)s %(OK)s \n" %
164             { "name" : p_name , "OK" : src.printcolors.printcInfo("OK")}, 4)
165         logger.flush()
166     logger.write("\n", 3, False)
167
168     return res
169
170 def get_nb_proc(product_info, config, make_option):
171     
172     opt_nb_proc = None
173     new_make_option = make_option
174     if "-j" in make_option:
175         oExpr = re.compile("-j[0-9]+")
176         found = oExpr.search(make_option)
177         opt_nb_proc = int(re.findall('\d+', found.group())[0])
178         new_make_option = make_option.replace(found.group(), "")
179     
180     nbproc = -1
181     if "nb_proc" in product_info:
182         # nb proc is specified in module definition
183         nbproc = product_info.nb_proc
184         if opt_nb_proc and opt_nb_proc < product_info.nb_proc:
185             # use command line value only if it is lower than module definition
186             nbproc = opt_nb_proc
187     else:
188         # nb proc is not specified in module definition
189         if opt_nb_proc:
190             nbproc = opt_nb_proc
191         else:
192             nbproc = config.VARS.nb_proc
193     
194     assert nbproc > 0
195     return nbproc, new_make_option
196
197 def description():
198     '''method that is called when salomeTools is called with --help option.
199     
200     :return: The text to display for the make command description.
201     :rtype: str
202     '''
203     return _("The make command executes the \"make\" command in"
204              " the build directory")
205   
206 def run(args, runner, logger):
207     '''method that is called when salomeTools is called with make parameter.
208     '''
209     
210     # Parse the options
211     (options, args) = parser.parse_args(args)
212
213     # check that the command has been called with an application
214     src.check_config_has_application( runner.cfg )
215
216     # Get the list of products to threat
217     products_infos = get_products_list(options, runner.cfg, logger)
218     
219     # Print some informations
220     logger.write(_('Executing the make command in the build '
221                                 'directories of the application %s\n') % 
222                 src.printcolors.printcLabel(runner.cfg.VARS.application), 1)
223     
224     info = [(_("BUILD directory"),
225              os.path.join(runner.cfg.APPLICATION.workdir, 'BUILD'))]
226     src.print_info(logger, info)
227     
228     # Call the function that will loop over all the products and execute
229     # the right command(s)
230     res = make_all_products(runner.cfg, products_infos, options.option, logger)
231     
232     # Print the final state
233     nb_products = len(products_infos)
234     if res == 0:
235         final_status = "OK"
236     else:
237         final_status = "KO"
238    
239     logger.write(_("\nMake: %(status)s (%(valid_result)d/%(nb_products)d)\n") % \
240         { 'status': src.printcolors.printc(final_status), 
241           'valid_result': nb_products - res,
242           'nb_products': nb_products }, 1)    
243     
244     return res