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 "compilation" in p_info.properties and
303 p_info.properties.compilation == "no"):
304 log_step(logger, header, "ignored")
305 logger.write("\n", 3, False)
308 # Do nothing if the product is native
309 if src.product.product_is_native(p_info):
310 log_step(logger, header, "native")
311 logger.write("\n", 3, False)
314 # Clean the build and the install directories
315 # if the corresponding options was called
316 if options.clean_all:
317 log_step(logger, header, "CLEAN BUILD AND INSTALL ")
318 sat.clean(config.VARS.application +
319 " --products " + p_name +
320 " --build --install",
323 logger_add_link = logger)
325 # Clean the the install directory
326 # if the corresponding option was called
327 if options.clean_install and not options.clean_all:
328 log_step(logger, header, "CLEAN INSTALL ")
329 sat.clean(config.VARS.application +
330 " --products " + p_name +
334 logger_add_link = logger)
336 # Recompute the product information to get the right install_dir
337 # (it could change if there is a clean of the install directory)
338 p_info = src.product.get_product_config(config, p_name)
340 # Check if sources was already successfully installed
341 check_source = src.product.check_source(p_info)
343 logger.write(_("Sources of product not found (try 'sat -h prepare')\n"))
347 # Check if it was already successfully installed
348 if src.product.check_installation(p_info):
349 logger.write(_("Already installed\n"))
352 # If the show option was called, do not launch the compilation
353 if options.no_compile:
354 logger.write(_("Not installed\n"))
357 # Check if the dependencies are installed
358 l_depends_not_installed = check_dependencies(config, p_name_info)
359 if len(l_depends_not_installed) > 0:
360 log_step(logger, header, "")
361 logger.write(src.printcolors.printcError(
362 _("ERROR : the following product(s) is(are) mandatory: ")))
363 for prod_name in l_depends_not_installed:
364 logger.write(src.printcolors.printcError(prod_name + " "))
368 # Call the function to compile the product
369 res_prod, len_end_line, error_step = compile_product(
370 sat, p_name_info, config, options, logger, header, len_end_line)
375 if error_step != "CHECK":
376 # Clean the install directory if there is any
378 "Cleaning the install directory if there is any\n"),
380 sat.clean(config.VARS.application +
381 " --products " + p_name +
385 logger_add_link = logger)
387 # Clean the build directory if the compilation and tests succeed
388 if options.clean_build_after:
389 log_step(logger, header, "CLEAN BUILD")
390 sat.clean(config.VARS.application +
391 " --products " + p_name +
395 logger_add_link = logger)
399 logger.write("\r%s%s" % (header, " " * len_end_line), 3)
400 logger.write("\r" + header + src.printcolors.printcError("KO ") + error_step)
401 logger.write("\n==== %(KO)s in compile of %(name)s \n" %
402 { "name" : p_name , "KO" : src.printcolors.printcInfo("ERROR")}, 4)
403 if error_step == "CHECK":
404 logger.write(_("\nINSTALL directory = %s" %
405 src.printcolors.printcInfo(p_info.install_dir)), 3)
408 logger.write("\r%s%s" % (header, " " * len_end_line), 3)
409 logger.write("\r" + header + src.printcolors.printcSuccess("OK"))
410 logger.write(_("\nINSTALL directory = %s" %
411 src.printcolors.printcInfo(p_info.install_dir)), 3)
412 logger.write("\n==== %s \n" % src.printcolors.printcInfo("OK"), 4)
413 logger.write("\n==== Compilation of %(name)s %(OK)s \n" %
414 { "name" : p_name , "OK" : src.printcolors.printcInfo("OK")}, 4)
416 logger.write("\n", 3, False)
419 if res_prod != 0 and options.stop_first_fail:
424 def compile_product(sat, p_name_info, config, options, logger, header, len_end):
425 '''Execute the proper configuration command(s)
426 in the product build directory.
428 :param p_name_info tuple: (str, Config) => (product_name, product_info)
429 :param config Config: The global configuration
430 :param logger Logger: The logger instance to use for the display
432 :param header Str: the header to display when logging
433 :param len_end Int: the lenght of the the end of line (used in display)
434 :return: 1 if it fails, else 0.
438 p_name, p_info = p_name_info
440 # Get the build procedure from the product configuration.
442 # build_sources : autotools -> build_configure, configure, make, make install
443 # build_sources : cmake -> cmake, make, make install
444 # build_sources : script -> script executions
446 if (src.product.product_is_autotools(p_info) or
447 src.product.product_is_cmake(p_info)):
448 res, len_end_line, error_step = compile_product_cmake_autotools(sat,
455 if src.product.product_has_script(p_info):
456 res, len_end_line, error_step = compile_product_script(sat,
464 # Check that the install directory exists
465 if res==0 and not(os.path.exists(p_info.install_dir)):
467 error_step = "NO INSTALL DIR"
468 msg = _("Error: despite the fact that all the steps ended successfully,"
469 " no install directory was found !")
470 logger.write(src.printcolors.printcError(msg), 4)
471 logger.write("\n", 4)
472 return res, len_end, error_step
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)
481 # Do the unit tests (call the check command)
482 log_step(logger, header, "CHECK")
483 res_check = sat.check(
484 config.VARS.application + " --products " + p_name,
486 logger_add_link = logger)
492 return res, len_end_line, error_step
494 def compile_product_cmake_autotools(sat,
501 '''Execute the proper build procedure for autotools or cmake
502 in the product build directory.
504 :param p_name_info tuple: (str, Config) => (product_name, product_info)
505 :param config Config: The global configuration
506 :param logger Logger: The logger instance to use for the display
508 :param header Str: the header to display when logging
509 :param len_end Int: the lenght of the the end of line (used in display)
510 :return: 1 if it fails, else 0.
513 p_name, p_info = p_name_info
515 # Execute "sat configure", "sat make" and "sat install"
519 # Logging and sat command call for configure step
520 len_end_line = len_end
521 log_step(logger, header, "CONFIGURE")
522 res_c = sat.configure(config.VARS.application + " --products " + p_name,
524 logger_add_link = logger)
525 log_res_step(logger, res_c)
529 error_step = "CONFIGURE"
531 # Logging and sat command call for make step
532 # Logging take account of the fact that the product has a compilation
534 if src.product.product_has_script(p_info):
535 # if the product has a compilation script,
536 # it is executed during make step
537 scrit_path_display = src.printcolors.printcLabel(
538 p_info.compil_script)
539 log_step(logger, header, "SCRIPT " + scrit_path_display)
540 len_end_line = len(scrit_path_display)
542 log_step(logger, header, "MAKE")
543 make_arguments = config.VARS.application + " --products " + p_name
544 # Get the make_flags option if there is any
545 if options.makeflags:
546 make_arguments += " --option -j" + options.makeflags
547 res_m = sat.make(make_arguments,
549 logger_add_link = logger)
550 log_res_step(logger, res_m)
556 # Logging and sat command call for make install step
557 log_step(logger, header, "MAKE INSTALL")
558 res_mi = sat.makeinstall(config.VARS.application +
562 logger_add_link = logger)
564 log_res_step(logger, res_mi)
568 error_step = "MAKE INSTALL"
570 return res, len_end_line, error_step
572 def compile_product_script(sat,
579 '''Execute the script build procedure in the product build directory.
581 :param p_name_info tuple: (str, Config) => (product_name, product_info)
582 :param config Config: The global configuration
583 :param logger Logger: The logger instance to use for the display
585 :param header Str: the header to display when logging
586 :param len_end Int: the lenght of the the end of line (used in display)
587 :return: 1 if it fails, else 0.
590 p_name, p_info = p_name_info
592 # Execute "sat configure", "sat make" and "sat install"
595 # Logging and sat command call for the script step
596 scrit_path_display = src.printcolors.printcLabel(p_info.compil_script)
597 log_step(logger, header, "SCRIPT " + scrit_path_display)
598 len_end_line = len_end + len(scrit_path_display)
599 res = sat.script(config.VARS.application + " --products " + p_name,
601 logger_add_link = logger)
602 log_res_step(logger, res)
604 return res, len_end_line, error_step
606 def add_compile_config_file(p_info, config):
607 '''Execute the proper configuration command(s)
608 in the product build directory.
610 :param p_info Config: The specific config of the product
611 :param config Config: The global configuration
613 # Create the compile config
614 compile_cfg = src.pyconf.Config()
615 for prod_name in p_info.depend:
616 if prod_name not in compile_cfg:
617 compile_cfg.addMapping(prod_name,
618 src.pyconf.Mapping(compile_cfg),
620 prod_dep_info = src.product.get_product_config(config, prod_name, False)
621 compile_cfg[prod_name] = prod_dep_info.version
622 # Write it in the install directory of the product
623 compile_cfg_path = os.path.join(p_info.install_dir, src.CONFIG_FILENAME)
624 f = open(compile_cfg_path, 'w')
625 compile_cfg.__save__(f)
629 '''method that is called when salomeTools is called with --help option.
631 :return: The text to display for the compile command description.
634 return _("The compile command constructs the products of the application"
635 "\n\nexample:\nsat compile SALOME-master --products KERNEL,GUI,"
636 "MEDCOUPLING --clean_all")
638 def run(args, runner, logger):
639 '''method that is called when salomeTools is called with compile parameter.
641 # DBG.write("compile runner.cfg", runner.cfg, True)
643 (options, args) = parser.parse_args(args)
645 # Warn the user if he invoked the clean_all option
646 # without --products option
647 if (options.clean_all and
648 options.products is None and
649 not runner.options.batch):
650 rep = input(_("You used --clean_all without specifying a product"
651 " are you sure you want to continue? [Yes/No] "))
652 if rep.upper() != _("YES").upper():
655 # check that the command has been called with an application
656 src.check_config_has_application( runner.cfg )
658 # Print some informations
659 logger.write(_('Executing the compile commands in the build '
660 'directories of the products of '
661 'the application %s\n') %
662 src.printcolors.printcLabel(runner.cfg.VARS.application), 1)
665 (_("SOURCE directory"),
666 os.path.join(runner.cfg.APPLICATION.workdir, 'SOURCES')),
667 (_("BUILD directory"),
668 os.path.join(runner.cfg.APPLICATION.workdir, 'BUILD'))
670 src.print_info(logger, info)
672 # Get the list of products to treat
673 products_infos = get_products_list(options, runner.cfg, logger)
676 # Extend the list with all recursive dependencies of the given products
677 products_infos = extend_with_fathers(runner.cfg, products_infos)
680 # Extend the list with all products that use the given products
681 products_infos = extend_with_children(runner.cfg, products_infos)
683 # Sort the list regarding the dependencies of the products
684 products_infos = sort_products(runner.cfg, products_infos)
687 # Call the function that will loop over all the products and execute
688 # the right command(s)
689 res = compile_all_products(runner, runner.cfg, options, products_infos, logger)
691 # Print the final state
692 nb_products = len(products_infos)
698 logger.write(_("\nCompilation: %(status)s (%(valid_result)d/%(nb_products)d)\n") % \
699 { 'status': src.printcolors.printc(final_status),
700 'valid_result': nb_products - res,
701 'nb_products': nb_products }, 1)