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"
54 parser.add_option('', 'check', 'boolean', 'check', _(
55 "Optional: execute the unit tests after compilation"), False)
57 def get_products_list(options, cfg, logger):
58 '''method that gives the product list with their informations from
59 configuration regarding the passed options.
61 :param options Options: The Options instance that stores the commands
63 :param cfg Config: The global configuration
64 :param logger Logger: The logger instance to use for the display and
66 :return: The list of (product name, product_informations).
69 # Get the products to be prepared, regarding the options
70 if options.products is None:
71 # No options, get all products sources
72 products = cfg.APPLICATION.products
74 # if option --products, check that all products of the command line
75 # are present in the application.
76 products = options.products
78 if p not in cfg.APPLICATION.products:
79 raise src.SatException(_("Product %(product)s "
80 "not defined in application %(application)s") %
81 { 'product': p, 'application': cfg.VARS.application} )
83 # Construct the list of tuple containing
84 # the products name and their definition
85 products_infos = src.product.get_products_infos(products, cfg)
87 products_infos = [pi for pi in products_infos if not(
88 src.product.product_is_fixed(pi[1]))]
92 def get_children(config, p_name_p_info):
94 p_name, __ = p_name_p_info
95 # Get all products of the application
96 products = config.APPLICATION.products
97 products_infos = src.product.get_products_infos(products, config)
98 for p_name_potential_child, p_info_potential_child in products_infos:
99 if ("depend" in p_info_potential_child and
100 p_name in p_info_potential_child.depend):
101 l_res.append(p_name_potential_child)
104 def get_recursive_children(config, p_name_p_info, without_native_fixed=False):
105 """ Get the recursive list of the product that depend on
106 the product defined by prod_info
108 :param config Config: The global configuration
109 :param prod_info Config: The specific config of the product
110 :param without_native_fixed boolean: If true, do not include the fixed
111 or native products in the result
112 :return: The list of product_informations.
115 p_name, __ = p_name_p_info
116 # Initialization of the resulting list
119 # Get the direct children (not recursive)
120 l_direct_children = get_children(config, p_name_p_info)
121 # Minimal case : no child
122 if l_direct_children == []:
124 # Add the children and call the function to get the children of the
126 for child_name in l_direct_children:
127 l_children_name = [pn_pi[0] for pn_pi in l_children]
128 if child_name not in l_children_name:
129 if child_name not in config.APPLICATION.products:
130 msg = _("The product %(child_name)s that is in %(product_nam"
131 "e)s children is not present in application "
132 "%(appli_name)s" % {"child_name" : child_name,
133 "product_name" : p_name.name,
134 "appli_name" : config.VARS.application})
135 raise src.SatException(msg)
136 prod_info_child = src.product.get_product_config(config,
138 pname_pinfo_child = (prod_info_child.name, prod_info_child)
139 # Do not append the child if it is native or fixed and
140 # the corresponding parameter is called
141 if without_native_fixed:
142 if not(src.product.product_is_native(prod_info_child) or
143 src.product.product_is_fixed(prod_info_child)):
144 l_children.append(pname_pinfo_child)
146 l_children.append(pname_pinfo_child)
147 # Get the children of the children
148 l_grand_children = get_recursive_children(config,
150 without_native_fixed = without_native_fixed)
151 l_children += l_grand_children
154 def get_recursive_fathers(config, p_name_p_info, without_native_fixed=False):
155 """ Get the recursive list of the dependencies of the product defined by
158 :param config Config: The global configuration
159 :param prod_info Config: The specific config of the product
160 :param without_native_fixed boolean: If true, do not include the fixed
161 or native products in the result
162 :return: The list of product_informations.
165 p_name, p_info = p_name_p_info
166 # Initialization of the resulting list
168 # Minimal case : no dependencies
169 if "depend" not in p_info or p_info.depend == []:
171 # Add the dependencies and call the function to get the dependencies of the
173 for father_name in p_info.depend:
174 l_fathers_name = [pn_pi[0] for pn_pi in l_fathers]
175 if father_name not in l_fathers_name:
176 if father_name not in config.APPLICATION.products:
177 msg = _("The product %(father_name)s that is in %(product_nam"
178 "e)s dependencies is not present in application "
179 "%(appli_name)s" % {"father_name" : father_name,
180 "product_name" : p_name,
181 "appli_name" : config.VARS.application})
182 raise src.SatException(msg)
183 prod_info_father = src.product.get_product_config(config,
185 pname_pinfo_father = (prod_info_father.name, prod_info_father)
186 # Do not append the father if it is native or fixed and
187 # the corresponding parameter is called
188 if without_native_fixed:
189 if not(src.product.product_is_native(prod_info_father) or
190 src.product.product_is_fixed(prod_info_father)):
191 l_fathers.append(pname_pinfo_father)
193 l_fathers.append(pname_pinfo_father)
194 # Get the dependencies of the dependency
195 l_grand_fathers = get_recursive_fathers(config,
197 without_native_fixed = without_native_fixed)
198 for item in l_grand_fathers:
199 if item not in l_fathers:
200 l_fathers.append(item)
203 def sort_products(config, p_infos):
204 """ Sort the p_infos regarding the dependencies between the products
206 :param config Config: The global configuration
207 :param p_infos list: List of (str, Config) => (product_name, product_info)
209 l_prod_sorted = src.deepcopy_list(p_infos)
211 l_fathers = get_recursive_fathers(config,
213 without_native_fixed=True)
214 l_fathers = [father for father in l_fathers if father in p_infos]
217 for p_sorted in l_prod_sorted:
218 if p_sorted in l_fathers:
219 l_fathers.remove(p_sorted)
221 l_prod_sorted.remove(prod)
222 l_prod_sorted.insert(l_prod_sorted.index(p_sorted)+1, prod)
227 def extend_with_fathers(config, p_infos):
228 p_infos_res = src.deepcopy_list(p_infos)
229 for p_name_p_info in p_infos:
230 fathers = get_recursive_fathers(config,
232 without_native_fixed=True)
233 for p_name_p_info_father in fathers:
234 if p_name_p_info_father not in p_infos_res:
235 p_infos_res.append(p_name_p_info_father)
238 def extend_with_children(config, p_infos):
239 p_infos_res = src.deepcopy_list(p_infos)
240 for p_name_p_info in p_infos:
241 children = get_recursive_children(config,
243 without_native_fixed=True)
244 for p_name_p_info_child in children:
245 if p_name_p_info_child not in p_infos_res:
246 p_infos_res.append(p_name_p_info_child)
249 def check_dependencies(config, p_name_p_info):
250 l_depends_not_installed = []
251 fathers = get_recursive_fathers(config, p_name_p_info, without_native_fixed=True)
252 for p_name_father, p_info_father in fathers:
253 if not(src.product.check_installation(p_info_father)):
254 l_depends_not_installed.append(p_name_father)
255 return l_depends_not_installed
257 def log_step(logger, header, step):
258 logger.write("\r%s%s" % (header, " " * 30), 3)
259 logger.write("\r%s%s" % (header, step), 3)
260 logger.write("\n==== %s \n" % src.printcolors.printcInfo(step), 4)
263 def log_res_step(logger, res):
265 logger.write("%s \n" % src.printcolors.printcSuccess("OK"), 4)
268 logger.write("%s \n" % src.printcolors.printcError("KO"), 4)
271 def compile_all_products(sat, config, options, products_infos, logger):
272 '''Execute the proper configuration commands
273 in each product build directory.
275 :param config Config: The global configuration
276 :param products_info list: List of
277 (str, Config) => (product_name, product_info)
278 :param logger Logger: The logger instance to use for the display and logging
279 :return: the number of failing commands.
283 for p_name_info in products_infos:
285 p_name, p_info = p_name_info
289 logger.write("\n", 4, False)
290 logger.write("################ ", 4)
291 header = _("Compilation of %s") % src.printcolors.printcLabel(p_name)
292 header += " %s " % ("." * (len_end_line - len(p_name)))
293 logger.write(header, 3)
294 logger.write("\n", 4, False)
297 # Do nothing if the product is not compilable
298 if ("properties" in p_info and "compilation" in p_info.properties and
299 p_info.properties.compilation == "no"):
300 log_step(logger, header, "ignored")
301 logger.write("\n", 3, False)
304 # Do nothing if the product is native
305 if src.product.product_is_native(p_info):
306 log_step(logger, header, "native")
307 logger.write("\n", 3, False)
310 # Clean the build and the install directories
311 # if the corresponding options was called
312 if options.clean_all:
313 log_step(logger, header, "CLEAN BUILD AND INSTALL")
314 sat.clean(config.VARS.application +
315 " --products " + p_name +
316 " --build --install",
319 logger_add_link = logger)
321 # Clean the the install directory
322 # if the corresponding option was called
323 if options.clean_install and not options.clean_all:
324 log_step(logger, header, "CLEAN INSTALL")
325 sat.clean(config.VARS.application +
326 " --products " + p_name +
330 logger_add_link = logger)
332 # Recompute the product information to get the right install_dir
333 # (it could change if there is a clean of the install directory)
334 p_info = src.product.get_product_config(config, p_name)
336 # Check if it was already successfully installed
337 if src.product.check_installation(p_info):
338 logger.write(_("Already installed\n"))
341 # If the show option was called, do not launch the compilation
342 if options.no_compile:
343 logger.write(_("Not installed\n"))
346 # Check if the dependencies are installed
347 l_depends_not_installed = check_dependencies(config, p_name_info)
348 if len(l_depends_not_installed) > 0:
349 log_step(logger, header, "")
350 logger.write(src.printcolors.printcError(
351 _("ERROR : the following product(s) is(are) mandatory: ")))
352 for prod_name in l_depends_not_installed:
353 logger.write(src.printcolors.printcError(prod_name + " "))
357 # Call the function to compile the product
358 res_prod, len_end_line, error_step = compile_product(sat,
369 if error_step != "CHECK":
370 # Clean the install directory if there is any
372 "Cleaning the install directory if there is any\n"),
374 sat.clean(config.VARS.application +
375 " --products " + p_name +
379 logger_add_link = logger)
383 logger.write("\r%s%s" % (header, " " * len_end_line), 3)
384 logger.write("\r" + header + src.printcolors.printcError("KO ") + error_step)
385 logger.write("\n==== %(KO)s in compile of %(name)s \n" %
386 { "name" : p_name , "KO" : src.printcolors.printcInfo("ERROR")}, 4)
387 if error_step == "CHECK":
388 logger.write(_("\nINSTALL directory = %s" %
389 src.printcolors.printcInfo(p_info.install_dir)), 3)
392 logger.write("\r%s%s" % (header, " " * len_end_line), 3)
393 logger.write("\r" + header + src.printcolors.printcSuccess("OK"))
394 logger.write(_("\nINSTALL directory = %s" %
395 src.printcolors.printcInfo(p_info.install_dir)), 3)
396 logger.write("\n==== %s \n" % src.printcolors.printcInfo("OK"), 4)
397 logger.write("\n==== Compilation of %(name)s %(OK)s \n" %
398 { "name" : p_name , "OK" : src.printcolors.printcInfo("OK")}, 4)
400 logger.write("\n", 3, False)
403 if res_prod != 0 and options.stop_first_fail:
408 def compile_product(sat, p_name_info, config, options, logger, header, len_end):
409 '''Execute the proper configuration command(s)
410 in the product build directory.
412 :param p_name_info tuple: (str, Config) => (product_name, product_info)
413 :param config Config: The global configuration
414 :param logger Logger: The logger instance to use for the display
416 :param header Str: the header to display when logging
417 :param len_end Int: the lenght of the the end of line (used in display)
418 :return: 1 if it fails, else 0.
422 p_name, p_info = p_name_info
424 # Get the build procedure from the product configuration.
426 # build_sources : autotools -> build_configure, configure, make, make install
427 # build_sources : cmake -> cmake, make, make install
428 # build_sources : script -> script executions
430 if (src.product.product_is_autotools(p_info) or
431 src.product.product_is_cmake(p_info)):
432 res, len_end_line, error_step = compile_product_cmake_autotools(sat,
439 if src.product.product_has_script(p_info):
440 res, len_end_line, error_step = compile_product_script(sat,
448 # Check that the install directory exists
449 if res==0 and not(os.path.exists(p_info.install_dir)):
451 error_step = "NO INSTALL DIR"
452 msg = _("Error: despite the fact that all the steps ended successfully,"
453 " no install directory was found !")
454 logger.write(src.printcolors.printcError(msg), 4)
455 logger.write("\n", 4)
456 return res, len_end_line, error_step
458 # Add the config file corresponding to the dependencies/versions of the
459 # product that have been successfully compiled
461 logger.write(_("Add the config file in installation directory\n"), 5)
462 add_compile_config_file(p_info, config)
465 # Do the unit tests (call the check command)
466 log_step(logger, header, "CHECK")
467 res_check = sat.check(
468 config.VARS.application + " --products " + p_name,
470 logger_add_link = logger)
476 return res, len_end_line, error_step
478 def compile_product_cmake_autotools(sat,
485 '''Execute the proper build procedure for autotools or cmake
486 in the product build directory.
488 :param p_name_info tuple: (str, Config) => (product_name, product_info)
489 :param config Config: The global configuration
490 :param logger Logger: The logger instance to use for the display
492 :param header Str: the header to display when logging
493 :param len_end Int: the lenght of the the end of line (used in display)
494 :return: 1 if it fails, else 0.
497 p_name, p_info = p_name_info
499 # Execute "sat configure", "sat make" and "sat install"
503 # Logging and sat command call for configure step
504 len_end_line = len_end
505 log_step(logger, header, "CONFIGURE")
506 res_c = sat.configure(config.VARS.application + " --products " + p_name,
508 logger_add_link = logger)
509 log_res_step(logger, res_c)
513 error_step = "CONFIGURE"
515 # Logging and sat command call for make step
516 # Logging take account of the fact that the product has a compilation
518 if src.product.product_has_script(p_info):
519 # if the product has a compilation script,
520 # it is executed during make step
521 scrit_path_display = src.printcolors.printcLabel(
522 p_info.compil_script)
523 log_step(logger, header, "SCRIPT " + scrit_path_display)
524 len_end_line = len(scrit_path_display)
526 log_step(logger, header, "MAKE")
527 make_arguments = config.VARS.application + " --products " + p_name
528 # Get the make_flags option if there is any
529 if options.makeflags:
530 make_arguments += " --option -j" + options.makeflags
531 res_m = sat.make(make_arguments,
533 logger_add_link = logger)
534 log_res_step(logger, res_m)
540 # Logging and sat command call for make install step
541 log_step(logger, header, "MAKE INSTALL")
542 res_mi = sat.makeinstall(config.VARS.application +
546 logger_add_link = logger)
548 log_res_step(logger, res_mi)
552 error_step = "MAKE INSTALL"
554 return res, len_end_line, error_step
556 def compile_product_script(sat,
563 '''Execute the script build procedure in the product build directory.
565 :param p_name_info tuple: (str, Config) => (product_name, product_info)
566 :param config Config: The global configuration
567 :param logger Logger: The logger instance to use for the display
569 :param header Str: the header to display when logging
570 :param len_end Int: the lenght of the the end of line (used in display)
571 :return: 1 if it fails, else 0.
574 p_name, p_info = p_name_info
576 # Execute "sat configure", "sat make" and "sat install"
579 # Logging and sat command call for the script step
580 scrit_path_display = src.printcolors.printcLabel(p_info.compil_script)
581 log_step(logger, header, "SCRIPT " + scrit_path_display)
582 len_end_line = len_end + len(scrit_path_display)
583 res = sat.script(config.VARS.application + " --products " + p_name,
585 logger_add_link = logger)
586 log_res_step(logger, res)
588 return res, len_end_line, error_step
590 def add_compile_config_file(p_info, config):
591 '''Execute the proper configuration command(s)
592 in the product build directory.
594 :param p_info Config: The specific config of the product
595 :param config Config: The global configuration
597 # Create the compile config
598 compile_cfg = src.pyconf.Config()
599 for prod_name in p_info.depend:
600 if prod_name not in compile_cfg:
601 compile_cfg.addMapping(prod_name,
602 src.pyconf.Mapping(compile_cfg),
604 prod_dep_info = src.product.get_product_config(config, prod_name, False)
605 compile_cfg[prod_name] = prod_dep_info.version
606 # Write it in the install directory of the product
607 compile_cfg_path = os.path.join(p_info.install_dir, src.CONFIG_FILENAME)
608 f = open(compile_cfg_path, 'w')
609 compile_cfg.__save__(f)
613 '''method that is called when salomeTools is called with --help option.
615 :return: The text to display for the compile command description.
618 return _("The compile command constructs the products of the application"
619 "\n\nexample:\nsat compile SALOME-master --products KERNEL,GUI,"
620 "MEDCOUPLING --clean_all")
622 def run(args, runner, logger):
623 '''method that is called when salomeTools is called with compile parameter.
627 (options, args) = parser.parse_args(args)
629 # Warn the user if he invoked the clean_all option
630 # without --products option
631 if (options.clean_all and
632 options.products is None and
633 not runner.options.batch):
634 rep = input(_("You used --clean_all without specifying a product"
635 " are you sure you want to continue? [Yes/No] "))
636 if rep.upper() != _("YES").upper():
639 # check that the command has been called with an application
640 src.check_config_has_application( runner.cfg )
642 # Print some informations
643 logger.write(_('Executing the compile commands in the build '
644 'directories of the products of '
645 'the application %s\n') %
646 src.printcolors.printcLabel(runner.cfg.VARS.application), 1)
649 (_("SOURCE directory"),
650 os.path.join(runner.cfg.APPLICATION.workdir, 'SOURCES')),
651 (_("BUILD directory"),
652 os.path.join(runner.cfg.APPLICATION.workdir, 'BUILD'))
654 src.print_info(logger, info)
656 # Get the list of products to treat
657 products_infos = get_products_list(options, runner.cfg, logger)
660 # Extend the list with all recursive dependencies of the given products
661 products_infos = extend_with_fathers(runner.cfg, products_infos)
664 # Extend the list with all products that use the given products
665 products_infos = extend_with_children(runner.cfg, products_infos)
667 # Sort the list regarding the dependencies of the products
668 products_infos = sort_products(runner.cfg, products_infos)
671 # Call the function that will loop over all the products and execute
672 # the right command(s)
673 res = compile_all_products(runner, runner.cfg, options, products_infos, logger)
675 # Print the final state
676 nb_products = len(products_infos)
682 logger.write(_("\nCompilation: %(status)s (%(valid_result)d/%(nb_products)d)\n") % \
683 { 'status': src.printcolors.printc(final_status),
684 'valid_result': nb_products - res,
685 'nb_products': nb_products }, 1)