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 _('Optional: products to configure. This option can be'
34 ' passed several time to configure several products.'))
35 parser.add_option('', 'with_fathers', 'boolean', 'fathers',
36 _("Optional: build all necessary products to the given product (KERNEL is "
37 "build before building GUI)."), False)
38 parser.add_option('', 'with_children', 'boolean', 'children',
39 _("Optional: build all products using the given product (all SMESH plugins"
40 " are build after SMESH)."), False)
41 parser.add_option('', 'clean_all', 'boolean', 'clean_all',
42 _("Optional: clean BUILD dir and INSTALL dir before building product."),
44 parser.add_option('', 'clean_install', 'boolean', 'clean_install',
45 _("Optional: clean INSTALL dir before building product."), False)
46 parser.add_option('', 'make_flags', 'string', 'makeflags',
47 _("Optional: add extra options to the 'make' command."))
48 parser.add_option('', 'show', 'boolean', 'no_compile',
49 _("Optional: DO NOT COMPILE just show if products are installed or not."),
51 parser.add_option('', 'stop_first_fail', 'boolean', 'stop_first_fail', _(
52 "Optional: Stops the command at first product compilation"
55 def get_products_list(options, cfg, logger):
56 '''method that gives the product list with their informations from
57 configuration regarding the passed options.
59 :param options Options: The Options instance that stores the commands
61 :param cfg Config: The global configuration
62 :param logger Logger: The logger instance to use for the display and
64 :return: The list of (product name, product_informations).
67 # Get the products to be prepared, regarding the options
68 if options.products is None:
69 # No options, get all products sources
70 products = cfg.APPLICATION.products
72 # if option --products, check that all products of the command line
73 # are present in the application.
74 products = options.products
76 if p not in cfg.APPLICATION.products:
77 raise src.SatException(_("Product %(product)s "
78 "not defined in application %(application)s") %
79 { 'product': p, 'application': cfg.VARS.application} )
81 # Construct the list of tuple containing
82 # the products name and their definition
83 products_infos = src.product.get_products_infos(products, cfg)
85 products_infos = [pi for pi in products_infos if not(
86 src.product.product_is_fixed(pi[1]))]
90 def get_children(config, p_name_p_info):
92 p_name, __ = p_name_p_info
93 # Get all products of the application
94 products = config.APPLICATION.products
95 products_infos = src.product.get_products_infos(products, config)
96 for p_name_potential_child, p_info_potential_child in products_infos:
97 if ("depend" in p_info_potential_child and
98 p_name in p_info_potential_child.depend):
99 l_res.append(p_name_potential_child)
102 def get_recursive_children(config, p_name_p_info, without_native_fixed=False):
103 """ Get the recursive list of the product that depend on
104 the product defined by prod_info
106 :param config Config: The global configuration
107 :param prod_info Config: The specific config of the product
108 :param without_native_fixed boolean: If true, do not include the fixed
109 or native products in the result
110 :return: The list of product_informations.
113 p_name, __ = p_name_p_info
114 # Initialization of the resulting list
117 # Get the direct children (not recursive)
118 l_direct_children = get_children(config, p_name_p_info)
119 # Minimal case : no child
120 if l_direct_children == []:
122 # Add the children and call the function to get the children of the
124 for child_name in l_direct_children:
125 l_children_name = [pn_pi[0] for pn_pi in l_children]
126 if child_name not in l_children_name:
127 if child_name not in config.APPLICATION.products:
128 msg = _("The product %(child_name)s that is in %(product_nam"
129 "e)s children is not present in application "
130 "%(appli_name)s" % {"child_name" : child_name,
131 "product_name" : p_name.name,
132 "appli_name" : config.VARS.application})
133 raise src.SatException(msg)
134 prod_info_child = src.product.get_product_config(config,
136 pname_pinfo_child = (prod_info_child.name, prod_info_child)
137 # Do not append the child if it is native or fixed and
138 # the corresponding parameter is called
139 if without_native_fixed:
140 if not(src.product.product_is_native(prod_info_child) or
141 src.product.product_is_fixed(prod_info_child)):
142 l_children.append(pname_pinfo_child)
144 l_children.append(pname_pinfo_child)
145 # Get the children of the children
146 l_grand_children = get_recursive_children(config,
148 without_native_fixed = without_native_fixed)
149 l_children += l_grand_children
152 def get_recursive_fathers(config, p_name_p_info, without_native_fixed=False):
153 """ Get the recursive list of the dependencies of the product defined by
156 :param config Config: The global configuration
157 :param prod_info Config: The specific config of the product
158 :param without_native_fixed boolean: If true, do not include the fixed
159 or native products in the result
160 :return: The list of product_informations.
163 p_name, p_info = p_name_p_info
164 # Initialization of the resulting list
166 # Minimal case : no dependencies
167 if "depend" not in p_info or p_info.depend == []:
169 # Add the dependencies and call the function to get the dependencies of the
171 for father_name in p_info.depend:
172 l_fathers_name = [pn_pi[0] for pn_pi in l_fathers]
173 if father_name not in l_fathers_name:
174 if father_name not in config.APPLICATION.products:
175 msg = _("The product %(father_name)s that is in %(product_nam"
176 "e)s dependencies is not present in application "
177 "%(appli_name)s" % {"father_name" : father_name,
178 "product_name" : p_name,
179 "appli_name" : config.VARS.application})
180 raise src.SatException(msg)
181 prod_info_father = src.product.get_product_config(config,
183 pname_pinfo_father = (prod_info_father.name, prod_info_father)
184 # Do not append the father if it is native or fixed and
185 # the corresponding parameter is called
186 if without_native_fixed:
187 if not(src.product.product_is_native(prod_info_father) or
188 src.product.product_is_fixed(prod_info_father)):
189 l_fathers.append(pname_pinfo_father)
191 l_fathers.append(pname_pinfo_father)
192 # Get the dependencies of the dependency
193 l_grand_fathers = get_recursive_fathers(config,
195 without_native_fixed = without_native_fixed)
196 for item in l_grand_fathers:
197 if item not in l_fathers:
198 l_fathers.append(item)
201 def sort_products(config, p_infos):
202 """ Sort the p_infos regarding the dependencies between the products
204 :param config Config: The global configuration
205 :param p_infos list: List of (str, Config) => (product_name, product_info)
207 l_prod_sorted = src.deepcopy_list(p_infos)
209 l_fathers = get_recursive_fathers(config,
211 without_native_fixed=True)
212 l_fathers = [father for father in l_fathers if father in p_infos]
215 for p_sorted in l_prod_sorted:
216 if p_sorted in l_fathers:
217 l_fathers.remove(p_sorted)
219 l_prod_sorted.remove(prod)
220 l_prod_sorted.insert(l_prod_sorted.index(p_sorted)+1, prod)
225 def extend_with_fathers(config, p_infos):
226 p_infos_res = src.deepcopy_list(p_infos)
227 for p_name_p_info in p_infos:
228 fathers = get_recursive_fathers(config,
230 without_native_fixed=True)
231 for p_name_p_info_father in fathers:
232 if p_name_p_info_father not in p_infos_res:
233 p_infos_res.append(p_name_p_info_father)
236 def extend_with_children(config, p_infos):
237 p_infos_res = src.deepcopy_list(p_infos)
238 for p_name_p_info in p_infos:
239 children = get_recursive_children(config,
241 without_native_fixed=True)
242 for p_name_p_info_child in children:
243 if p_name_p_info_child not in p_infos_res:
244 p_infos_res.append(p_name_p_info_child)
247 def check_dependencies(config, p_name_p_info):
248 l_depends_not_installed = []
249 fathers = get_recursive_fathers(config, p_name_p_info, without_native_fixed=True)
250 for p_name_father, p_info_father in fathers:
251 if not(src.product.check_installation(p_info_father)):
252 l_depends_not_installed.append(p_name_father)
253 return l_depends_not_installed
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
287 logger.write("\n", 4, False)
288 logger.write("################ ", 4)
289 header = _("Compilation of %s") % src.printcolors.printcLabel(p_name)
290 header += " %s " % ("." * (len_end_line - len(p_name)))
291 logger.write(header, 3)
292 logger.write("\n", 4, False)
295 # Do nothing if the product is not compilable
296 if ("properties" in p_info and "compilation" in p_info.properties and
297 p_info.properties.compilation == "no"):
298 log_step(logger, header, "ignored")
299 logger.write("\n", 3, False)
302 # Do nothing if the product is native
303 if src.product.product_is_native(p_info):
304 log_step(logger, header, "native")
305 logger.write("\n", 3, False)
308 # Clean the build and the install directories
309 # if the corresponding options was called
310 if options.clean_all:
311 log_step(logger, header, "CLEAN BUILD AND INSTALL")
312 sat.clean(config.VARS.application +
313 " --products " + p_name +
314 " --build --install",
317 logger_add_link = logger)
319 # Clean the the install directory
320 # if the corresponding option was called
321 if options.clean_install and not options.clean_all:
322 log_step(logger, header, "CLEAN INSTALL")
323 sat.clean(config.VARS.application +
324 " --products " + p_name +
328 logger_add_link = logger)
330 # Check if it was already successfully installed
331 if src.product.check_installation(p_info):
332 logger.write(_("Already installed\n"))
335 # If the show option was called, do not launch the compilation
336 if options.no_compile:
337 logger.write(_("Not installed\n"))
340 # Check if the dependencies are installed
341 l_depends_not_installed = check_dependencies(config, p_name_info)
342 if len(l_depends_not_installed) > 0:
343 log_step(logger, header, "")
344 logger.write(src.printcolors.printcError(
345 _("ERROR : the following product(s) is(are) mandatory: ")))
346 for prod_name in l_depends_not_installed:
347 logger.write(src.printcolors.printcError(prod_name + " "))
351 # Call the function to compile the product
352 res_prod, len_end_line, error_step = compile_product(sat,
361 # Clean the install directory if there is any
362 logger.write(_("Cleaning the install directory if there is any\n"),
364 sat.clean(config.VARS.application +
365 " --products " + p_name +
369 logger_add_link = logger)
374 logger.write("\r%s%s" % (header, " " * len_end_line), 3)
375 logger.write("\r" + header + src.printcolors.printcError("KO ") + error_step)
376 logger.write("\n==== %(KO)s in compile of %(name)s \n" %
377 { "name" : p_name , "KO" : src.printcolors.printcInfo("ERROR")}, 4)
380 logger.write("\r%s%s" % (header, " " * len_end_line), 3)
381 logger.write("\r" + header + src.printcolors.printcSuccess("OK"))
382 logger.write(_("\nINSTALL directory = %s" %
383 src.printcolors.printcInfo(p_info.install_dir)), 3)
384 logger.write("\n==== %s \n" % src.printcolors.printcInfo("OK"), 4)
385 logger.write("\n==== Compilation of %(name)s %(OK)s \n" %
386 { "name" : p_name , "OK" : src.printcolors.printcInfo("OK")}, 4)
388 logger.write("\n", 3, False)
391 if res_prod != 0 and options.stop_first_fail:
396 def compile_product(sat, p_name_info, config, options, logger, header, len_end):
397 '''Execute the proper configuration command(s)
398 in the product build directory.
400 :param p_name_info tuple: (str, Config) => (product_name, product_info)
401 :param config Config: The global configuration
402 :param logger Logger: The logger instance to use for the display
404 :return: 1 if it fails, else 0.
408 p_name, p_info = p_name_info
410 # Execute "sat configure", "sat make" and "sat install"
414 # Logging and sat command call for configure step
415 len_end_line = len_end
416 log_step(logger, header, "CONFIGURE")
417 res_c = sat.configure(config.VARS.application + " --products " + p_name,
419 logger_add_link = logger)
420 log_res_step(logger, res_c)
424 error_step = "CONFIGURE"
426 # Logging and sat command call for make step
427 # Logging take account of the fact that the product has a compilation
429 if src.product.product_has_script(p_info):
430 # if the product has a compilation script,
431 # it is executed during make step
432 scrit_path_display = src.printcolors.printcLabel(
433 p_info.compil_script)
434 log_step(logger, header, "SCRIPT " + scrit_path_display)
435 len_end_line = len(scrit_path_display)
437 log_step(logger, header, "MAKE")
438 make_arguments = config.VARS.application + " --products " + p_name
439 # Get the make_flags option if there is any
440 if options.makeflags:
441 make_arguments += " --option -j" + options.makeflags
442 res_m = sat.make(make_arguments,
444 logger_add_link = logger)
445 log_res_step(logger, res_m)
451 # Logging and sat command call for make install step
452 log_step(logger, header, "MAKE INSTALL")
453 res_mi = sat.makeinstall(config.VARS.application +
457 logger_add_link = logger)
459 log_res_step(logger, res_mi)
463 error_step = "MAKE INSTALL"
465 # Check that the install directory exists
466 if res==0 and not(os.path.exists(p_info.install_dir)):
468 error_step = "NO INSTALL DIR"
469 msg = _("Error: despite the fact that all the steps ended successfully,"
470 " no install directory was found !")
471 logger.write(src.printcolors.printcError(msg), 4)
472 logger.write("\n", 4)
474 # Add the config file corresponding to the dependencies/versions of the
475 # product that have been successfully compiled
477 logger.write(_("Add the config file in installation directory\n"), 5)
478 add_compile_config_file(p_info, config)
480 return res, len_end_line, error_step
482 def add_compile_config_file(p_info, config):
483 '''Execute the proper configuration command(s)
484 in the product build directory.
486 :param p_info Config: The specific config of the product
487 :param config Config: The global configuration
489 # Create the compile config
490 compile_cfg = src.pyconf.Config()
491 for prod_name in p_info.depend:
492 if prod_name not in compile_cfg:
493 compile_cfg.addMapping(prod_name,
494 src.pyconf.Mapping(compile_cfg),
496 prod_dep_info = src.product.get_product_config(config, prod_name, False)
497 compile_cfg[prod_name] = prod_dep_info.version
498 # Write it in the install directory of the product
499 compile_cfg_path = os.path.join(p_info.install_dir, src.CONFIG_FILENAME)
500 f = open(compile_cfg_path, 'w')
501 compile_cfg.__save__(f)
505 '''method that is called when salomeTools is called with --help option.
507 :return: The text to display for the compile command description.
510 return _("The compile command constructs the products of the application"
511 "\n\nexample:\nsat compile SALOME-master --products KERNEL,GUI,"
512 "MEDCOUPLING --clean_all")
514 def run(args, runner, logger):
515 '''method that is called when salomeTools is called with compile parameter.
519 (options, args) = parser.parse_args(args)
521 # Warn the user if he invoked the clean_all option
522 # without --products option
523 if (options.clean_all and
524 options.products is None and
525 not runner.options.batch):
526 rep = input(_("You used --clean_all without specifying a product"
527 " are you sure you want to continue? [Yes/No] "))
528 if rep.upper() != _("YES").upper():
531 # check that the command has been called with an application
532 src.check_config_has_application( runner.cfg )
534 # Print some informations
535 logger.write(_('Executing the compile commands in the build '
536 'directories of the products of '
537 'the application %s\n') %
538 src.printcolors.printcLabel(runner.cfg.VARS.application), 1)
541 (_("SOURCE directory"),
542 os.path.join(runner.cfg.APPLICATION.workdir, 'SOURCES')),
543 (_("BUILD directory"),
544 os.path.join(runner.cfg.APPLICATION.workdir, 'BUILD'))
546 src.print_info(logger, info)
548 # Get the list of products to treat
549 products_infos = get_products_list(options, runner.cfg, logger)
552 # Extend the list with all recursive dependencies of the given products
553 products_infos = extend_with_fathers(runner.cfg, products_infos)
556 # Extend the list with all products that use the given products
557 products_infos = extend_with_children(runner.cfg, products_infos)
559 # Sort the list regarding the dependencies of the products
560 products_infos = sort_products(runner.cfg, products_infos)
563 # Call the function that will loop over all the products and execute
564 # the right command(s)
565 res = compile_all_products(runner, runner.cfg, options, products_infos, logger)
567 # Print the final state
568 nb_products = len(products_infos)
574 logger.write(_("\nCompilation: %(status)s (%(valid_result)d/%(nb_products)d)\n") % \
575 { 'status': src.printcolors.printc(final_status),
576 'valid_result': nb_products - res,
577 'nb_products': nb_products }, 1)