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
22 import src.debug as DBG
24 # Compatibility python 2/3 for input function
25 # input stays input for python 3 and input = raw_input for python 2
31 # Define all possible option for the compile command : sat compile <options>
32 parser = src.options.Options()
33 parser.add_option('p', 'products', 'list2', 'products',
34 _('Optional: products to compile. This option can be'
35 ' passed several time to compile several products.'))
36 parser.add_option('', 'with_fathers', 'boolean', 'fathers',
37 _("Optional: build all necessary products to the given product (KERNEL is "
38 "build before building GUI)."), False)
39 parser.add_option('', 'with_children', 'boolean', 'children',
40 _("Optional: build all products using the given product (all SMESH plugins"
41 " are build after SMESH)."), False)
42 parser.add_option('', 'clean_all', 'boolean', 'clean_all',
43 _("Optional: clean BUILD dir and INSTALL dir before building product."),
45 parser.add_option('', 'clean_install', 'boolean', 'clean_install',
46 _("Optional: clean INSTALL dir before building product."), False)
47 parser.add_option('', 'make_flags', 'string', 'makeflags',
48 _("Optional: add extra options to the 'make' command."))
49 parser.add_option('', 'show', 'boolean', 'no_compile',
50 _("Optional: DO NOT COMPILE just show if products are installed or not."),
52 parser.add_option('', 'stop_first_fail', 'boolean', 'stop_first_fail', _(
53 "Optional: Stops the command at first product compilation"
55 parser.add_option('', 'check', 'boolean', 'check', _(
56 "Optional: execute the unit tests after compilation"), False)
58 parser.add_option('', 'clean_build_after', 'boolean', 'clean_build_after',
59 _('Optional: remove the build directory after successful compilation'), False)
61 def get_products_list(options, cfg, logger):
62 '''method that gives the product list with their informations from
63 configuration regarding the passed options.
65 :param options Options: The Options instance that stores the commands
67 :param cfg Config: The global configuration
68 :param logger Logger: The logger instance to use for the display and
70 :return: The list of (product name, product_informations).
73 # Get the products to be prepared, regarding the options
74 if options.products is None:
75 # No options, get all products sources
76 products = cfg.APPLICATION.products
78 # if option --products, check that all products of the command line
79 # are present in the application.
80 products = options.products
82 if p not in cfg.APPLICATION.products:
83 raise src.SatException(_("Product %(product)s "
84 "not defined in application %(application)s") %
85 { 'product': p, 'application': cfg.VARS.application} )
87 # Construct the list of tuple containing
88 # the products name and their definition
89 products_infos = src.product.get_products_infos(products, cfg)
91 products_infos = [pi for pi in products_infos if not(
92 src.product.product_is_fixed(pi[1]))]
96 def get_children(config, p_name_p_info):
98 p_name, __ = p_name_p_info
99 # Get all products of the application
100 products = config.APPLICATION.products
101 products_infos = src.product.get_products_infos(products, config)
102 for p_name_potential_child, p_info_potential_child in products_infos:
103 if ("depend" in p_info_potential_child and
104 p_name in p_info_potential_child.depend):
105 l_res.append(p_name_potential_child)
108 def get_recursive_children(config, p_name_p_info, without_native_fixed=False):
109 """ Get the recursive list of the product that depend on
110 the product defined by prod_info
112 :param config Config: The global configuration
113 :param prod_info Config: The specific config of the product
114 :param without_native_fixed boolean: If true, do not include the fixed
115 or native products in the result
116 :return: The list of product_informations.
119 p_name, __ = p_name_p_info
120 # Initialization of the resulting list
123 # Get the direct children (not recursive)
124 l_direct_children = get_children(config, p_name_p_info)
125 # Minimal case : no child
126 if l_direct_children == []:
128 # Add the children and call the function to get the children of the
130 for child_name in l_direct_children:
131 l_children_name = [pn_pi[0] for pn_pi in l_children]
132 if child_name not in l_children_name:
133 if child_name not in config.APPLICATION.products:
134 msg = _("The product %(child_name)s that is in %(product_nam"
135 "e)s children is not present in application "
136 "%(appli_name)s" % {"child_name" : child_name,
137 "product_name" : p_name.name,
138 "appli_name" : config.VARS.application})
139 raise src.SatException(msg)
140 prod_info_child = src.product.get_product_config(config,
142 pname_pinfo_child = (prod_info_child.name, prod_info_child)
143 # Do not append the child if it is native or fixed and
144 # the corresponding parameter is called
145 if without_native_fixed:
146 if not(src.product.product_is_native(prod_info_child) or
147 src.product.product_is_fixed(prod_info_child)):
148 l_children.append(pname_pinfo_child)
150 l_children.append(pname_pinfo_child)
151 # Get the children of the children
152 l_grand_children = get_recursive_children(config,
154 without_native_fixed = without_native_fixed)
155 l_children += l_grand_children
158 def get_recursive_fathers(config, p_name_p_info, without_native_fixed=False):
159 """ Get the recursive list of the dependencies of the product defined by
162 :param config Config: The global configuration
163 :param prod_info Config: The specific config of the product
164 :param without_native_fixed boolean: If true, do not include the fixed
165 or native products in the result
166 :return: The list of product_informations.
169 p_name, p_info = p_name_p_info
170 # Initialization of the resulting list
172 # Minimal case : no dependencies
173 if "depend" not in p_info or p_info.depend == []:
175 # Add the dependencies and call the function to get the dependencies of the
177 for father_name in p_info.depend:
178 l_fathers_name = [pn_pi[0] for pn_pi in l_fathers]
179 if father_name not in l_fathers_name:
180 if father_name not in config.APPLICATION.products:
181 msg = _("The product %(father_name)s that is in %(product_nam"
182 "e)s dependencies is not present in application "
183 "%(appli_name)s" % {"father_name" : father_name,
184 "product_name" : p_name,
185 "appli_name" : config.VARS.application})
186 raise src.SatException(msg)
187 prod_info_father = src.product.get_product_config(config,
189 pname_pinfo_father = (prod_info_father.name, prod_info_father)
190 # Do not append the father if it is native or fixed and
191 # the corresponding parameter is called
192 if without_native_fixed:
193 if not(src.product.product_is_native(prod_info_father) or
194 src.product.product_is_fixed(prod_info_father)):
195 l_fathers.append(pname_pinfo_father)
197 l_fathers.append(pname_pinfo_father)
198 # Get the dependencies of the dependency
199 l_grand_fathers = get_recursive_fathers(config,
201 without_native_fixed = without_native_fixed)
202 for item in l_grand_fathers:
203 if item not in l_fathers:
204 l_fathers.append(item)
207 def sort_products(config, p_infos):
208 """ Sort the p_infos regarding the dependencies between the products
210 :param config Config: The global configuration
211 :param p_infos list: List of (str, Config) => (product_name, product_info)
213 l_prod_sorted = src.deepcopy_list(p_infos)
215 l_fathers = get_recursive_fathers(config,
217 without_native_fixed=True)
218 l_fathers = [father for father in l_fathers if father in p_infos]
221 for p_sorted in l_prod_sorted:
222 if p_sorted in l_fathers:
223 l_fathers.remove(p_sorted)
225 l_prod_sorted.remove(prod)
226 l_prod_sorted.insert(l_prod_sorted.index(p_sorted)+1, prod)
231 def extend_with_fathers(config, p_infos):
232 p_infos_res = src.deepcopy_list(p_infos)
233 for p_name_p_info in p_infos:
234 fathers = get_recursive_fathers(config,
236 without_native_fixed=True)
237 for p_name_p_info_father in fathers:
238 if p_name_p_info_father not in p_infos_res:
239 p_infos_res.append(p_name_p_info_father)
242 def extend_with_children(config, p_infos):
243 p_infos_res = src.deepcopy_list(p_infos)
244 for p_name_p_info in p_infos:
245 children = get_recursive_children(config,
247 without_native_fixed=True)
248 for p_name_p_info_child in children:
249 if p_name_p_info_child not in p_infos_res:
250 p_infos_res.append(p_name_p_info_child)
253 def check_dependencies(config, p_name_p_info):
254 l_depends_not_installed = []
255 fathers = get_recursive_fathers(config, p_name_p_info, without_native_fixed=True)
256 for p_name_father, p_info_father in fathers:
257 if not(src.product.check_installation(p_info_father)):
258 l_depends_not_installed.append(p_name_father)
259 return l_depends_not_installed
261 def log_step(logger, header, step):
262 logger.write("\r%s%s" % (header, " " * 30), 3)
263 logger.write("\r%s%s" % (header, step), 3)
264 logger.write("\n==== %s \n" % src.printcolors.printcInfo(step), 4)
267 def log_res_step(logger, res):
269 logger.write("%s \n" % src.printcolors.printcSuccess("OK"), 4)
272 logger.write("%s \n" % src.printcolors.printcError("KO"), 4)
275 def compile_all_products(sat, config, options, products_infos, logger):
276 '''Execute the proper configuration commands
277 in each product build directory.
279 :param config Config: The global configuration
280 :param products_info list: List of
281 (str, Config) => (product_name, product_info)
282 :param logger Logger: The logger instance to use for the display and logging
283 :return: the number of failing commands.
287 for p_name_info in products_infos:
289 p_name, p_info = p_name_info
293 logger.write("\n", 4, False)
294 logger.write("################ ", 4)
295 header = _("Compilation of %s") % src.printcolors.printcLabel(p_name)
296 header += " %s " % ("." * (len_end_line - len(p_name)))
297 logger.write(header, 3)
298 logger.write("\n", 4, False)
301 # Do nothing if the product is not compilable
302 if ("properties" in p_info and
303 "compilation" in p_info.properties and
304 p_info.properties.compilation == "no"):
306 log_step(logger, header, "ignored")
307 logger.write("\n", 3, False)
310 # Do nothing if the product is native
311 if src.product.product_is_native(p_info):
312 log_step(logger, header, "native")
313 logger.write("\n", 3, False)
316 # Clean the build and the install directories
317 # if the corresponding options was called
318 if options.clean_all:
319 log_step(logger, header, "CLEAN BUILD AND INSTALL ")
320 sat.clean(config.VARS.application +
321 " --products " + p_name +
322 " --build --install",
325 logger_add_link = logger)
327 # Clean the the install directory
328 # if the corresponding option was called
329 if options.clean_install and not options.clean_all:
330 log_step(logger, header, "CLEAN INSTALL ")
331 sat.clean(config.VARS.application +
332 " --products " + p_name +
336 logger_add_link = logger)
338 # Recompute the product information to get the right install_dir
339 # (it could change if there is a clean of the install directory)
340 p_info = src.product.get_product_config(config, p_name)
342 # Check if sources was already successfully installed
343 check_source = src.product.check_source(p_info)
345 logger.write(_("Sources of product not found (try 'sat -h prepare') \n"))
349 if src.product.product_is_salome(p_info):
350 # For salome modules, we check if the sources of configuration modules are present
351 # configuration modules have the property "configure_dependency"
353 # get the list of all modules in application
354 all_products_infos = src.product.get_products_infos(config.APPLICATION.products,
357 # for configuration modules, check if sources are present
358 for product_name, product_info in all_products_infos:
359 if ("properties" in product_info and
360 "configure_dependency" in product_info.properties and
361 product_info.properties.configure_dependency == "yes"):
362 check_source = check_source and src.product.check_source(product_info)
364 logger.write(_("\nERROR : SOURCES of %s not found! It is required for"
365 " the configuration\n" % product_name))
366 logger.write(_(" Get it with the command : sat prepare %s -p %s \n" %
367 (config.APPLICATION.name, product_name)))
369 # if at least one configuration module is not present, we stop compilation
373 # Check if it was already successfully installed
374 if src.product.check_installation(p_info):
375 logger.write(_("Already installed\n"))
378 # If the show option was called, do not launch the compilation
379 if options.no_compile:
380 logger.write(_("Not installed\n"))
383 # Check if the dependencies are installed
384 l_depends_not_installed = check_dependencies(config, p_name_info)
385 if len(l_depends_not_installed) > 0:
386 log_step(logger, header, "")
387 logger.write(src.printcolors.printcError(
388 _("ERROR : the following product(s) is(are) mandatory: ")))
389 for prod_name in l_depends_not_installed:
390 logger.write(src.printcolors.printcError(prod_name + " "))
394 # Call the function to compile the product
395 res_prod, len_end_line, error_step = compile_product(
396 sat, p_name_info, config, options, logger, header, len_end_line)
401 if error_step != "CHECK":
402 # Clean the install directory if there is any
404 "Cleaning the install directory if there is any\n"),
406 sat.clean(config.VARS.application +
407 " --products " + p_name +
411 logger_add_link = logger)
413 # Clean the build directory if the compilation and tests succeed
414 if options.clean_build_after:
415 log_step(logger, header, "CLEAN BUILD")
416 sat.clean(config.VARS.application +
417 " --products " + p_name +
421 logger_add_link = logger)
425 logger.write("\r%s%s" % (header, " " * len_end_line), 3)
426 logger.write("\r" + header + src.printcolors.printcError("KO ") + error_step)
427 logger.write("\n==== %(KO)s in compile of %(name)s \n" %
428 { "name" : p_name , "KO" : src.printcolors.printcInfo("ERROR")}, 4)
429 if error_step == "CHECK":
430 logger.write(_("\nINSTALL directory = %s" %
431 src.printcolors.printcInfo(p_info.install_dir)), 3)
434 logger.write("\r%s%s" % (header, " " * len_end_line), 3)
435 logger.write("\r" + header + src.printcolors.printcSuccess("OK"))
436 logger.write(_("\nINSTALL directory = %s" %
437 src.printcolors.printcInfo(p_info.install_dir)), 3)
438 logger.write("\n==== %s \n" % src.printcolors.printcInfo("OK"), 4)
439 logger.write("\n==== Compilation of %(name)s %(OK)s \n" %
440 { "name" : p_name , "OK" : src.printcolors.printcInfo("OK")}, 4)
442 logger.write("\n", 3, False)
445 if res_prod != 0 and options.stop_first_fail:
450 def compile_product(sat, p_name_info, config, options, logger, header, len_end):
451 '''Execute the proper configuration command(s)
452 in the product build directory.
454 :param p_name_info tuple: (str, Config) => (product_name, product_info)
455 :param config Config: The global configuration
456 :param logger Logger: The logger instance to use for the display
458 :param header Str: the header to display when logging
459 :param len_end Int: the lenght of the the end of line (used in display)
460 :return: 1 if it fails, else 0.
464 p_name, p_info = p_name_info
466 # Get the build procedure from the product configuration.
468 # build_sources : autotools -> build_configure, configure, make, make install
469 # build_sources : cmake -> cmake, make, make install
470 # build_sources : script -> script executions
472 if (src.product.product_is_autotools(p_info) or
473 src.product.product_is_cmake(p_info)):
474 res, len_end_line, error_step = compile_product_cmake_autotools(sat,
481 if src.product.product_has_script(p_info):
482 res, len_end_line, error_step = compile_product_script(sat,
490 # Check that the install directory exists
491 if res==0 and not(os.path.exists(p_info.install_dir)):
493 error_step = "NO INSTALL DIR"
494 msg = _("Error: despite the fact that all the steps ended successfully,"
495 " no install directory was found !")
496 logger.write(src.printcolors.printcError(msg), 4)
497 logger.write("\n", 4)
498 return res, len_end, error_step
500 # Add the config file corresponding to the dependencies/versions of the
501 # product that have been successfully compiled
503 logger.write(_("Add the config file in installation directory\n"), 5)
504 add_compile_config_file(p_info, config)
507 # Do the unit tests (call the check command)
508 log_step(logger, header, "CHECK")
509 res_check = sat.check(
510 config.VARS.application + " --products " + p_name,
512 logger_add_link = logger)
518 return res, len_end_line, error_step
520 def compile_product_cmake_autotools(sat,
527 '''Execute the proper build procedure for autotools or cmake
528 in the product build directory.
530 :param p_name_info tuple: (str, Config) => (product_name, product_info)
531 :param config Config: The global configuration
532 :param logger Logger: The logger instance to use for the display
534 :param header Str: the header to display when logging
535 :param len_end Int: the lenght of the the end of line (used in display)
536 :return: 1 if it fails, else 0.
539 p_name, p_info = p_name_info
541 # Execute "sat configure", "sat make" and "sat install"
545 # Logging and sat command call for configure step
546 len_end_line = len_end
547 log_step(logger, header, "CONFIGURE")
548 res_c = sat.configure(config.VARS.application + " --products " + p_name,
550 logger_add_link = logger)
551 log_res_step(logger, res_c)
555 error_step = "CONFIGURE"
557 # Logging and sat command call for make step
558 # Logging take account of the fact that the product has a compilation
560 if src.product.product_has_script(p_info):
561 # if the product has a compilation script,
562 # it is executed during make step
563 scrit_path_display = src.printcolors.printcLabel(
564 p_info.compil_script)
565 log_step(logger, header, "SCRIPT " + scrit_path_display)
566 len_end_line = len(scrit_path_display)
568 log_step(logger, header, "MAKE")
569 make_arguments = config.VARS.application + " --products " + p_name
570 # Get the make_flags option if there is any
571 if options.makeflags:
572 make_arguments += " --option -j" + options.makeflags
573 res_m = sat.make(make_arguments,
575 logger_add_link = logger)
576 log_res_step(logger, res_m)
582 # Logging and sat command call for make install step
583 log_step(logger, header, "MAKE INSTALL")
584 res_mi = sat.makeinstall(config.VARS.application +
588 logger_add_link = logger)
590 log_res_step(logger, res_mi)
594 error_step = "MAKE INSTALL"
596 return res, len_end_line, error_step
598 def compile_product_script(sat,
605 '''Execute the script build procedure in the product build directory.
607 :param p_name_info tuple: (str, Config) => (product_name, product_info)
608 :param config Config: The global configuration
609 :param logger Logger: The logger instance to use for the display
611 :param header Str: the header to display when logging
612 :param len_end Int: the lenght of the the end of line (used in display)
613 :return: 1 if it fails, else 0.
616 p_name, p_info = p_name_info
618 # Execute "sat configure", "sat make" and "sat install"
621 # Logging and sat command call for the script step
622 scrit_path_display = src.printcolors.printcLabel(p_info.compil_script)
623 log_step(logger, header, "SCRIPT " + scrit_path_display)
624 len_end_line = len_end + len(scrit_path_display)
625 res = sat.script(config.VARS.application + " --products " + p_name,
627 logger_add_link = logger)
628 log_res_step(logger, res)
630 return res, len_end_line, error_step
632 def add_compile_config_file(p_info, config):
633 '''Execute the proper configuration command(s)
634 in the product build directory.
636 :param p_info Config: The specific config of the product
637 :param config Config: The global configuration
639 # Create the compile config
640 compile_cfg = src.pyconf.Config()
641 for prod_name in p_info.depend:
642 if prod_name not in compile_cfg:
643 compile_cfg.addMapping(prod_name,
644 src.pyconf.Mapping(compile_cfg),
646 prod_dep_info = src.product.get_product_config(config, prod_name, False)
647 compile_cfg[prod_name] = prod_dep_info.version
648 # Write it in the install directory of the product
649 compile_cfg_path = os.path.join(p_info.install_dir, src.CONFIG_FILENAME)
650 f = open(compile_cfg_path, 'w')
651 compile_cfg.__save__(f)
655 '''method that is called when salomeTools is called with --help option.
657 :return: The text to display for the compile command description.
660 return _("The compile command constructs the products of the application"
661 "\n\nexample:\nsat compile SALOME-master --products KERNEL,GUI,"
662 "MEDCOUPLING --clean_all")
664 def run(args, runner, logger):
665 '''method that is called when salomeTools is called with compile parameter.
667 # DBG.write("compile runner.cfg", runner.cfg, True)
669 (options, args) = parser.parse_args(args)
671 # Warn the user if he invoked the clean_all option
672 # without --products option
673 if (options.clean_all and
674 options.products is None and
675 not runner.options.batch):
676 rep = input(_("You used --clean_all without specifying a product"
677 " are you sure you want to continue? [Yes/No] "))
678 if rep.upper() != _("YES").upper():
681 # check that the command has been called with an application
682 src.check_config_has_application( runner.cfg )
684 # Print some informations
685 logger.write(_('Executing the compile commands in the build '
686 'directories of the products of '
687 'the application %s\n') %
688 src.printcolors.printcLabel(runner.cfg.VARS.application), 1)
691 (_("SOURCE directory"),
692 os.path.join(runner.cfg.APPLICATION.workdir, 'SOURCES')),
693 (_("BUILD directory"),
694 os.path.join(runner.cfg.APPLICATION.workdir, 'BUILD'))
696 src.print_info(logger, info)
698 # Get the list of products to treat
699 products_infos = get_products_list(options, runner.cfg, logger)
702 # Extend the list with all recursive dependencies of the given products
703 products_infos = extend_with_fathers(runner.cfg, products_infos)
706 # Extend the list with all products that use the given products
707 products_infos = extend_with_children(runner.cfg, products_infos)
709 # Sort the list regarding the dependencies of the products
710 products_infos = sort_products(runner.cfg, products_infos)
713 # Call the function that will loop over all the products and execute
714 # the right command(s)
715 res = compile_all_products(runner, runner.cfg, options, products_infos, logger)
717 # Print the final state
718 nb_products = len(products_infos)
724 logger.write(_("\nCompilation: %(status)s (%(valid_result)d/%(nb_products)d)\n") % \
725 { 'status': src.printcolors.printc(final_status),
726 'valid_result': nb_products - res,
727 'nb_products': nb_products }, 1)