3 # Copyright (C) 2010-2012 CEA/DEN
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.
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.
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
23 # Compatibility python 2/3 for input function
24 # input stays input for python 3 and input = raw_input for python 2
30 # Define all possible option for the compile command : sat compile <options>
31 parser = src.options.Options()
32 parser.add_option('p', 'products', 'list2', 'products',
33 _('products to configure. This option can be'
34 ' passed several time to configure several products.'))
35 parser.add_option('', 'with_fathers', 'boolean', 'fathers',
36 _("build all necessary products to the given product (KERNEL is build before"
37 " building GUI)."), False)
38 parser.add_option('', 'with_children', 'boolean', 'children',
39 _("build all products using the given product (all SMESH plugins are build "
40 "after SMESH)."), False)
41 parser.add_option('', 'clean_all', 'boolean', 'clean_all',
42 _("clean BUILD dir and INSTALL dir before building product."), False)
43 parser.add_option('', 'clean_install', 'boolean', 'clean_install',
44 _("clean INSTALL dir before building product."), False)
45 parser.add_option('', 'make_flags', 'string', 'makeflags',
46 _("add extra options to the 'make' command."))
47 parser.add_option('', 'show', 'boolean', 'no_compile',
48 _("DO NOT COMPILE just show if products are installed or not."), False)
49 parser.add_option('', 'stop_first_fail', 'boolean', 'stop_first_fail', _("Stop"
50 "s the command at first product compilation fail."), False)
52 def get_products_list(options, cfg, logger):
53 '''method that gives the product list with their informations from
54 configuration regarding the passed options.
56 :param options Options: The Options instance that stores the commands
58 :param cfg Config: The global configuration
59 :param logger Logger: The logger instance to use for the display and
61 :return: The list of (product name, product_informations).
64 # Get the products to be prepared, regarding the options
65 if options.products is None:
66 # No options, get all products sources
67 products = cfg.APPLICATION.products
69 # if option --products, check that all products of the command line
70 # are present in the application.
71 products = options.products
73 if p not in cfg.APPLICATION.products:
74 raise src.SatException(_("Product %(product)s "
75 "not defined in application %(application)s") %
76 { 'product': p, 'application': cfg.VARS.application} )
78 # Construct the list of tuple containing
79 # the products name and their definition
80 products_infos = src.product.get_products_infos(products, cfg)
82 products_infos = [pi for pi in products_infos if not(
83 src.product.product_is_native(pi[1]) or
84 src.product.product_is_fixed(pi[1]))]
88 def get_children(config, p_name_p_info):
90 p_name, __ = p_name_p_info
91 # Get all products of the application
92 products = config.APPLICATION.products
93 products_infos = src.product.get_products_infos(products, config)
94 for p_name_potential_child, p_info_potential_child in products_infos:
95 if ("depend" in p_info_potential_child and
96 p_name in p_info_potential_child.depend):
97 l_res.append(p_name_potential_child)
100 def get_recursive_children(config, p_name_p_info, without_native_fixed=False):
101 """ Get the recursive list of the product that depend on
102 the product defined by prod_info
104 :param config Config: The global configuration
105 :param prod_info Config: The specific config of the product
106 :param without_native_fixed boolean: If true, do not include the fixed
107 or native products in the result
108 :return: The list of product_informations.
111 p_name, __ = p_name_p_info
112 # Initialization of the resulting list
115 # Get the direct children (not recursive)
116 l_direct_children = get_children(config, p_name_p_info)
117 # Minimal case : no child
118 if l_direct_children == []:
120 # Add the children and call the function to get the children of the
122 for child_name in l_direct_children:
123 l_children_name = [pn_pi[0] for pn_pi in l_children]
124 if child_name not in l_children_name:
125 if child_name not in config.APPLICATION.products:
126 msg = _("The product %(child_name)s that is in %(product_nam"
127 "e)s children is not present in application "
128 "%(appli_name)s" % {"child_name" : child_name,
129 "product_name" : p_name.name,
130 "appli_name" : config.VARS.application})
131 raise src.SatException(msg)
132 prod_info_child = src.product.get_product_config(config,
134 pname_pinfo_child = (prod_info_child.name, prod_info_child)
135 # Do not append the child if it is native or fixed and
136 # the corresponding parameter is called
137 if without_native_fixed:
138 if not(src.product.product_is_native(prod_info_child) or
139 src.product.product_is_fixed(prod_info_child)):
140 l_children.append(pname_pinfo_child)
142 l_children.append(pname_pinfo_child)
143 # Get the children of the children
144 l_grand_children = get_recursive_children(config,
146 without_native_fixed = without_native_fixed)
147 l_children += l_grand_children
150 def get_recursive_fathers(config, p_name_p_info, without_native_fixed=False):
151 """ Get the recursive list of the dependencies of the product defined by
154 :param config Config: The global configuration
155 :param prod_info Config: The specific config of the product
156 :param without_native_fixed boolean: If true, do not include the fixed
157 or native products in the result
158 :return: The list of product_informations.
161 p_name, p_info = p_name_p_info
162 # Initialization of the resulting list
164 # Minimal case : no dependencies
165 if "depend" not in p_info or p_info.depend == []:
167 # Add the dependencies and call the function to get the dependencies of the
169 for father_name in p_info.depend:
170 l_fathers_name = [pn_pi[0] for pn_pi in l_fathers]
171 if father_name not in l_fathers_name:
172 if father_name not in config.APPLICATION.products:
173 msg = _("The product %(father_name)s that is in %(product_nam"
174 "e)s dependencies is not present in application "
175 "%(appli_name)s" % {"father_name" : father_name,
176 "product_name" : p_name.name,
177 "appli_name" : config.VARS.application})
178 raise src.SatException(msg)
179 prod_info_father = src.product.get_product_config(config,
181 pname_pinfo_father = (prod_info_father.name, prod_info_father)
182 # Do not append the father if it is native or fixed and
183 # the corresponding parameter is called
184 if without_native_fixed:
185 if not(src.product.product_is_native(prod_info_father) or
186 src.product.product_is_fixed(prod_info_father)):
187 l_fathers.append(pname_pinfo_father)
189 l_fathers.append(pname_pinfo_father)
190 # Get the dependencies of the dependency
191 l_grand_fathers = get_recursive_fathers(config,
193 without_native_fixed = without_native_fixed)
194 l_fathers += l_grand_fathers
197 def sort_products(config, p_infos):
198 """ Sort the p_infos regarding the dependencies between the products
200 :param config Config: The global configuration
201 :param p_infos list: List of (str, Config) => (product_name, product_info)
203 l_prod_sorted = deepcopy_list(p_infos)
205 l_fathers = get_recursive_fathers(config,
207 without_native_fixed=True)
208 l_fathers = [father for father in l_fathers if father in p_infos]
211 for p_sorted in l_prod_sorted:
212 if p_sorted in l_fathers:
213 l_fathers.remove(p_sorted)
215 l_prod_sorted.remove(prod)
216 l_prod_sorted.insert(l_prod_sorted.index(p_sorted)+1, prod)
221 def deepcopy_list(input_list):
222 """ Do a deep copy of a list
224 :param input_list List: The list to copy
225 :return: The copy of the list
229 for elem in input_list:
233 def extend_with_fathers(config, p_infos):
234 p_infos_res = deepcopy_list(p_infos)
235 for p_name_p_info in p_infos:
236 fathers = get_recursive_fathers(config,
238 without_native_fixed=True)
239 for p_name_p_info_father in fathers:
240 if p_name_p_info_father not in p_infos_res:
241 p_infos_res.append(p_name_p_info_father)
244 def extend_with_children(config, p_infos):
245 p_infos_res = deepcopy_list(p_infos)
246 for p_name_p_info in p_infos:
247 children = get_recursive_children(config,
249 without_native_fixed=True)
250 for p_name_p_info_child in children:
251 if p_name_p_info_child not in p_infos_res:
252 p_infos_res.append(p_name_p_info_child)
255 def log_step(logger, header, step):
256 logger.write("\r%s%s" % (header, " " * 30), 3)
257 logger.write("\r%s%s" % (header, step), 3)
258 logger.write("\n==== %s \n" % src.printcolors.printcInfo(step), 4)
261 def log_res_step(logger, res):
263 logger.write("%s \n" % src.printcolors.printcSuccess("OK"), 4)
266 logger.write("%s \n" % src.printcolors.printcError("KO"), 4)
269 def compile_all_products(sat, config, options, products_infos, logger):
270 '''Execute the proper configuration commands
271 in each product build directory.
273 :param config Config: The global configuration
274 :param products_info list: List of
275 (str, Config) => (product_name, product_info)
276 :param logger Logger: The logger instance to use for the display and logging
277 :return: the number of failing commands.
281 for p_name_info in products_infos:
283 p_name, p_info = p_name_info
286 logger.write("\n", 4, False)
287 logger.write("################ ", 4)
288 header = _("Compilation of %s") % src.printcolors.printcLabel(p_name)
289 header += " %s " % ("." * (30 - len(p_name)))
290 logger.write(header, 3)
291 logger.write("\n", 4, False)
294 # Clean the build and the install directories
295 # if the corresponding options was called
296 if options.clean_all:
297 log_step(logger, header, "CLEAN BUILD AND INSTALL")
298 sat.clean(config.VARS.application +
299 " --products " + p_name +
300 " --build --install", batch=True, verbose=0)
302 # Clean the the install directory
303 # if the corresponding option was called
304 if options.clean_install and not options.clean_all:
305 log_step(logger, header, "CLEAN INSTALL")
306 sat.clean(config.VARS.application +
307 " --products " + p_name +
308 " --install", batch=True, verbose=0)
310 # Check if it was already successfully installed
311 if src.product.check_installation(p_info):
312 logger.write(_("Already installed\n"))
315 if options.no_compile:
316 logger.write(_("Not installed\n"))
319 res_prod = compile_product(sat, p_name_info, config, options, logger, header)
322 if options.stop_first_fail:
327 def compile_product(sat, p_name_info, config, options, logger, header):
328 '''Execute the proper configuration command(s)
329 in the product build directory.
331 :param p_name_info tuple: (str, Config) => (product_name, product_info)
332 :param config Config: The global configuration
333 :param logger Logger: The logger instance to use for the display
335 :return: 1 if it fails, else 0.
339 p_name, p_info = p_name_info
341 # Execute "sat configure", "sat make" and "sat install"
345 # Logging and sat command call for configure step
346 log_step(logger, header, "CONFIGURE")
347 res_c = sat.configure(config.VARS.application + " --products " + p_name,
349 log_res_step(logger, res_c)
352 # Logging and sat command call for make step
353 # Logging take account of the fact that the product has a compilation
355 if src.product.product_has_script(p_info):
356 # if the product has a compilation script,
357 # it is executed during make step
358 scrit_path_display = src.printcolors.printcLabel(p_info.compil_script)
359 log_step(logger, header, "SCRIPT " + scrit_path_display)
360 len_end_line = len(scrit_path_display)
362 log_step(logger, header, "MAKE")
363 make_arguments = config.VARS.application + " --products " + p_name
364 # Get the make_flags option if there is any
365 if options.makeflags:
366 make_arguments += " --option -j" + options.makeflags
367 res_c = sat.make(make_arguments,
369 log_res_step(logger, res_c)
372 # Logging and sat command call for make install step
373 log_step(logger, header, "MAKE INSTALL")
374 res_c = sat.makeinstall(config.VARS.application + " --products " + p_name,
376 log_res_step(logger, res_c)
381 logger.write("\r%s%s" % (header, " " * len_end_line), 3)
382 logger.write("\r" + header + src.printcolors.printcError("KO"))
383 logger.write("\n==== %(KO)s in compile of %(name)s \n" %
384 { "name" : p_name , "KO" : src.printcolors.printcInfo("ERROR")}, 4)
387 logger.write("\r%s%s" % (header, " " * len_end_line), 3)
388 logger.write("\r" + header + src.printcolors.printcSuccess("OK"))
389 logger.write(_("\nINSTALL directory = %s" %
390 src.printcolors.printcInfo(p_info.install_dir)), 3)
391 logger.write("\n==== %s \n" % src.printcolors.printcInfo("OK"), 4)
392 logger.write("\n==== Compilation of %(name)s %(OK)s \n" %
393 { "name" : p_name , "OK" : src.printcolors.printcInfo("OK")}, 4)
395 logger.write("\n", 3, False)
400 '''method that is called when salomeTools is called with --help option.
402 :return: The text to display for the compile command description.
405 return _("The compile command constructs the products of the application")
407 def run(args, runner, logger):
408 '''method that is called when salomeTools is called with compile parameter.
412 (options, args) = parser.parse_args(args)
414 # Warn the user if he invoked the clean_all option
415 # without --products option
416 if (options.clean_all and
417 options.products is None and
418 not runner.options.batch):
419 rep = raw_input(_("You used --clean_all without specifying a product"
420 " are you sure you want to continue? [Yes/No] "))
421 if rep.upper() != _("YES").upper():
424 # check that the command has been called with an application
425 src.check_config_has_application( runner.cfg )
427 # Get the list of products to treat
428 products_infos = get_products_list(options, runner.cfg, logger)
431 # Extend the list with all recursive dependencies of the given products
432 products_infos = extend_with_fathers(runner.cfg, products_infos)
435 # Extend the list with all products that use the given products
436 products_infos = extend_with_children(runner.cfg, products_infos)
438 # Sort the list regarding the dependencies of the products
439 products_infos = sort_products(runner.cfg, products_infos)
441 # Print some informations
442 logger.write(_('Executing the compile command in the build '
443 'directories of the application %s\n') %
444 src.printcolors.printcLabel(runner.cfg.VARS.application), 1)
447 (_("SOURCE directory"),
448 os.path.join(runner.cfg.APPLICATION.workdir, 'SOURCES')),
449 (_("BUILD directory"),
450 os.path.join(runner.cfg.APPLICATION.workdir, 'BUILD'))
452 src.print_info(logger, info)
454 # Call the function that will loop over all the products and execute
455 # the right command(s)
456 res = compile_all_products(runner, runner.cfg, options, products_infos, logger)
458 # Print the final state
459 nb_products = len(products_infos)
465 logger.write(_("\nCompilation: %(status)s (%(valid_result)d/%(nb_products)d)\n") % \
466 { 'status': src.printcolors.printc(final_status),
467 'valid_result': nb_products - res,
468 'nb_products': nb_products }, 1)