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 import src.debug as DBG
25 # Compatibility python 2/3 for input function
26 # input stays input for python 3 and input = raw_input for python 2
33 # Define all possible option for the compile command : sat compile <options>
34 parser = src.options.Options()
35 parser.add_option('p', 'products', 'list2', 'products',
36 _('Optional: products to compile. This option accepts a comma separated list.'))
37 parser.add_option('', 'with_fathers', 'boolean', 'fathers',
38 _("Optional: build all necessary products to the given product (KERNEL is "
39 "build before building GUI)."), False)
40 parser.add_option('', 'with_children', 'boolean', 'children',
41 _("Optional: build all products using the given product (all SMESH plugins"
42 " are build after SMESH)."), False)
43 parser.add_option('', 'clean_all', 'boolean', 'clean_all',
44 _("Optional: clean BUILD dir and INSTALL dir before building product."),
46 parser.add_option('', 'clean_install', 'boolean', 'clean_install',
47 _("Optional: clean INSTALL dir before building product."), False)
48 parser.add_option('', 'make_flags', 'string', 'makeflags',
49 _("Optional: add extra options to the 'make' command."))
50 parser.add_option('', 'show', 'boolean', 'no_compile',
51 _("Optional: DO NOT COMPILE just show if products are installed or not."),
53 parser.add_option('', 'stop_first_fail', 'boolean', 'stop_first_fail', _(
54 "Optional: Stops the command at first product compilation"
56 parser.add_option('', 'check', 'boolean', 'check', _(
57 "Optional: execute the unit tests after compilation"), False)
59 parser.add_option('', 'clean_build_after', 'boolean', 'clean_build_after',
60 _('Optional: remove the build directory after successful compilation'), False)
63 # from sat product infos, represent the product dependencies in a simple python graph
64 # keys are nodes, the list of dependencies are values
65 def get_dependencies_graph(p_infos):
67 for (p_name,p_info) in p_infos:
68 graph[p_name]=p_info.depend
71 # this recursive function calculates all the dependencies of node start
72 def depth_search_graph(graph, start, visited=[]):
73 visited= visited+ [start]
74 for node in graph[start]: # for all nodes in start dependencies
75 if node not in visited:
76 visited=depth_search_graph(graph, node, visited)
79 # find a path from start node to end (a group of nodes)
80 def find_path_graph(graph, start, end, path=[]):
84 if not graph.has_key(start):
86 for node in graph[start]:
88 newpath = find_path_graph(graph, node, end, path)
89 if newpath: return newpath
92 # Topological sorting algo
93 # return in sorted_nodes the list of sorted nodes
94 def depth_first_topo_graph(graph, start, visited=[], sorted_nodes=[]):
95 visited = visited + [start]
96 for node in graph[start]:
97 if node not in visited:
98 visited,sorted_nodes=depth_first_topo_graph(graph, node, visited,sorted_nodes)
100 assert node in sorted_nodes, 'Error : cycle detection for node %s and %s !' % (start,node)
102 sorted_nodes = sorted_nodes + [start]
103 return visited,sorted_nodes
106 # check for p_name that all dependencies are installed
107 def check_dependencies(config, p_name_p_info, all_products_dict):
108 l_depends_not_installed = []
109 for prod in p_name_p_info[1]["depend_all"]:
110 # for each dependency, check the install
111 prod_name, prod_info=all_products_dict[prod]
112 if not(src.product.check_installation(config, prod_info)):
113 l_depends_not_installed.append(prod_name)
114 return l_depends_not_installed # non installed deps
116 def log_step(logger, header, step):
117 logger.write("\r%s%s" % (header, " " * 30), 3)
118 logger.write("\r%s%s" % (header, step), 3)
121 def log_res_step(logger, res):
123 logger.write("%s \n" % src.printcolors.printcSuccess("OK"), 4)
126 logger.write("%s \n" % src.printcolors.printcError("KO"), 4)
129 def compile_all_products(sat, config, options, products_infos, all_products_dict, logger):
130 '''Execute the proper configuration commands
131 in each product build directory.
133 :param config Config: The global configuration
134 :param products_info list: List of
135 (str, Config) => (product_name, product_info)
136 :param all_products_dict: Dict of all products
137 :param logger Logger: The logger instance to use for the display and logging
138 :return: the number of failing commands.
142 for p_name_info in products_infos:
144 p_name, p_info = p_name_info
148 header = _("Compilation of %s") % src.printcolors.printcLabel(p_name)
149 header += " %s " % ("." * (len_end_line - len(p_name)))
150 logger.write(header, 3)
153 # Do nothing if the product is not compilable
154 if not src.product.product_compiles(p_info):
155 log_step(logger, header, "ignored")
156 logger.write("\n", 3, False)
159 # Do nothing if the product is native
160 if src.product.product_is_native(p_info):
161 log_step(logger, header, "native")
162 logger.write("\n", 3, False)
165 # Do nothing if the product is fixed (already compiled by third party)
166 if src.product.product_is_fixed(p_info):
167 log_step(logger, header, "native")
168 logger.write("\n", 3, False)
171 # Clean the build and the install directories
172 # if the corresponding options was called
173 if options.clean_all:
174 log_step(logger, header, "CLEAN BUILD AND INSTALL ")
175 sat.clean(config.VARS.application +
176 " --products " + p_name +
177 " --build --install",
180 logger_add_link = logger)
183 # Clean the the install directory
184 # if the corresponding option was called
185 if options.clean_install and not options.clean_all:
186 log_step(logger, header, "CLEAN INSTALL ")
187 sat.clean(config.VARS.application +
188 " --products " + p_name +
192 logger_add_link = logger)
194 # Recompute the product information to get the right install_dir
195 # (it could change if there is a clean of the install directory)
196 p_info = src.product.get_product_config(config, p_name)
198 # Check if sources was already successfully installed
199 check_source = src.product.check_source(p_info)
200 is_pip= (src.appli_test_property(config,"pip", "yes") and src.product.product_test_property(p_info,"pip", "yes"))
201 # don't check sources with option --show
202 # or for products managed by pip (there sources are in wheels stored in LOCAL.ARCHIVE
203 if not (options.no_compile or is_pip):
205 logger.write(_("Sources of product not found (try 'sat -h prepare') \n"))
206 res += 1 # one more error
209 if src.product.product_is_salome(p_info):
210 # For salome modules, we check if the sources of configuration modules are present
211 # configuration modules have the property "configure_dependency"
213 # get the list of all modules in application
214 all_products_infos = src.product.get_products_infos(config.APPLICATION.products,
217 # for configuration modules, check if sources are present
218 for prod in all_products_dict:
219 product_name, product_info = all_products_dict[prod]
220 if ("properties" in product_info and
221 "configure_dependency" in product_info.properties and
222 product_info.properties.configure_dependency == "yes"):
223 check_source = check_source and src.product.check_source(product_info)
225 logger.write(_("\nERROR : SOURCES of %s not found! It is required for"
226 " the configuration\n" % product_name))
227 logger.write(_(" Get it with the command : sat prepare %s -p %s \n" %
228 (config.APPLICATION.name, product_name)))
230 # if at least one configuration module is not present, we stop compilation
234 # Check if it was already successfully installed
235 if src.product.check_installation(config, p_info):
236 logger.write(_("Already installed"))
237 logger.write(_(" in %s" % p_info.install_dir), 4)
238 logger.write(_("\n"))
241 # If the show option was called, do not launch the compilation
242 if options.no_compile:
243 logger.write(_("Not installed in %s\n" % p_info.install_dir))
246 # Check if the dependencies are installed
247 l_depends_not_installed = check_dependencies(config, p_name_info, all_products_dict)
248 if len(l_depends_not_installed) > 0:
249 log_step(logger, header, "")
250 logger.write(src.printcolors.printcError(
251 _("ERROR : the following mandatory product(s) is(are) not installed: ")))
252 for prod_name in l_depends_not_installed:
253 logger.write(src.printcolors.printcError(prod_name + " "))
257 # Call the function to compile the product
258 res_prod, len_end_line, error_step = compile_product(
259 sat, p_name_info, config, options, logger, header, len_end_line)
264 if error_step != "CHECK":
265 # Clean the install directory if there is any
267 "Cleaning the install directory if there is any\n"),
269 sat.clean(config.VARS.application +
270 " --products " + p_name +
274 logger_add_link = logger)
276 # Clean the build directory if the compilation and tests succeed
277 if options.clean_build_after:
278 log_step(logger, header, "CLEAN BUILD")
279 sat.clean(config.VARS.application +
280 " --products " + p_name +
284 logger_add_link = logger)
288 logger.write("\r%s%s" % (header, " " * len_end_line), 3)
289 logger.write("\r" + header + src.printcolors.printcError("KO ") + error_step)
290 logger.write("\n==== %(KO)s in compile of %(name)s \n" %
291 { "name" : p_name , "KO" : src.printcolors.printcInfo("ERROR")}, 4)
292 if error_step == "CHECK":
293 logger.write(_("\nINSTALL directory = %s" %
294 src.printcolors.printcInfo(p_info.install_dir)), 3)
297 logger.write("\r%s%s" % (header, " " * len_end_line), 3)
298 logger.write("\r" + header + src.printcolors.printcSuccess("OK"))
299 logger.write(_("\nINSTALL directory = %s" %
300 src.printcolors.printcInfo(p_info.install_dir)), 3)
301 logger.write("\n==== %s \n" % src.printcolors.printcInfo("OK"), 4)
302 logger.write("\n==== Compilation of %(name)s %(OK)s \n" %
303 { "name" : p_name , "OK" : src.printcolors.printcInfo("OK")}, 4)
305 logger.write("\n", 3, False)
308 if res_prod != 0 and options.stop_first_fail:
313 def compile_product(sat, p_name_info, config, options, logger, header, len_end):
314 '''Execute the proper configuration command(s)
315 in the product build directory.
317 :param p_name_info tuple: (str, Config) => (product_name, product_info)
318 :param config Config: The global configuration
319 :param logger Logger: The logger instance to use for the display
321 :param header Str: the header to display when logging
322 :param len_end Int: the lenght of the the end of line (used in display)
323 :return: 1 if it fails, else 0.
327 p_name, p_info = p_name_info
329 # Get the build procedure from the product configuration.
331 # build_sources : autotools -> build_configure, configure, make, make install
332 # build_sources : cmake -> cmake, make, make install
333 # build_sources : script -> script executions
337 # check if pip should be used : the application and product have pip property
338 if (src.appli_test_property(config,"pip", "yes") and
339 src.product.product_test_property(p_info,"pip", "yes")):
340 res, len_end_line, error_step = compile_product_pip(sat,
348 if (src.product.product_is_autotools(p_info) or
349 src.product.product_is_cmake(p_info)):
350 res, len_end_line, error_step = compile_product_cmake_autotools(sat,
357 if src.product.product_has_script(p_info):
358 res, len_end_line, error_step = compile_product_script(sat,
366 # Check that the install directory exists
367 if res==0 and not(os.path.exists(p_info.install_dir)):
369 error_step = "NO INSTALL DIR"
370 msg = _("Error: despite the fact that all the steps ended successfully,"
371 " no install directory was found !")
372 logger.write(src.printcolors.printcError(msg), 4)
373 logger.write("\n", 4)
374 return res, len_end, error_step
376 # Add the config file corresponding to the dependencies/versions of the
377 # product that have been successfully compiled
379 logger.write(_("Add the config file in installation directory\n"), 5)
380 src.product.add_compile_config_file(p_info, config)
383 # Do the unit tests (call the check command)
384 log_step(logger, header, "CHECK")
385 res_check = sat.check(
386 config.VARS.application + " --products " + p_name,
388 logger_add_link = logger)
394 return res, len_end_line, error_step
397 def compile_product_pip(sat,
404 '''Execute the proper build procedure for pip products
405 :param p_name_info tuple: (str, Config) => (product_name, product_info)
406 :param config Config: The global configuration
407 :param logger Logger: The logger instance to use for the display
409 :param header Str: the header to display when logging
410 :param len_end Int: the lenght of the the end of line (used in display)
411 :return: 1 if it fails, else 0.
415 p_name, p_info = p_name_info
418 pip_install_in_python=False
419 pip_wheels_dir=os.path.join(config.LOCAL.archive_dir,"wheels")
420 pip_install_cmd=config.INTERNAL.command.pip_install # parametrized in src/internal
422 # b) get the build environment (useful to get the installed python & pip3)
423 build_environ = src.environment.SalomeEnviron(config,
424 src.environment.Environ(dict(os.environ)),
426 environ_info = src.product.get_product_dependencies(config,
428 build_environ.silent = (config.USER.output_verbose_level < 5)
429 build_environ.set_full_environ(logger, environ_info)
431 # c- download : check/get pip wheel in pip_wheels_dir
432 pip_download_cmd=config.INTERNAL.command.pip_download +\
433 " --destination-directory %s --no-deps %s==%s " %\
434 (pip_wheels_dir, p_info.name, p_info.version)
435 logger.write("\n"+pip_download_cmd+"\n", 4, False)
436 res_pip_dwl = (subprocess.call(pip_download_cmd,
438 cwd=config.LOCAL.workdir,
439 env=build_environ.environ.environ,
440 stdout=logger.logTxtFile,
441 stderr=subprocess.STDOUT) == 0)
442 # error is not managed at the stage. error will be handled by pip install
443 # here we just print a message
445 logger.write("Error in pip download\n", 4, False)
448 # d- install (in python or in separate product directory)
449 if src.appli_test_property(config,"pip_install_dir", "python"):
450 # pip will install product in python directory"
451 pip_install_cmd+=" --find-links=%s --build %s %s==%s" %\
452 (pip_wheels_dir, p_info.build_dir, p_info.name, p_info.version)
453 pip_install_in_python=True
456 # pip will install product in product install_dir
457 pip_install_dir=os.path.join(p_info.install_dir, "lib", "python${PYTHON_VERSION:0:3}", "site-packages")
458 pip_install_cmd+=" --find-links=%s --build %s --target %s %s==%s" %\
459 (pip_wheels_dir, p_info.build_dir, pip_install_dir, p_info.name, p_info.version)
460 log_step(logger, header, "PIP")
461 logger.write("\n"+pip_install_cmd+"\n", 4)
462 len_end_line = len_end + 3
465 res_pip = (subprocess.call(pip_install_cmd,
467 cwd=config.LOCAL.workdir,
468 env=build_environ.environ.environ,
469 stdout=logger.logTxtFile,
470 stderr=subprocess.STDOUT) == 0)
474 #log_res_step(logger, res)
478 return res, len_end_line, error_step
482 def compile_product_cmake_autotools(sat,
489 '''Execute the proper build procedure for autotools or cmake
490 in the product build directory.
492 :param p_name_info tuple: (str, Config) => (product_name, product_info)
493 :param config Config: The global configuration
494 :param logger Logger: The logger instance to use for the display
496 :param header Str: the header to display when logging
497 :param len_end Int: the lenght of the the end of line (used in display)
498 :return: 1 if it fails, else 0.
501 p_name, p_info = p_name_info
503 # Execute "sat configure", "sat make" and "sat install"
507 # Logging and sat command call for configure step
508 len_end_line = len_end
509 log_step(logger, header, "CONFIGURE")
510 res_c = sat.configure(config.VARS.application + " --products " + p_name,
512 logger_add_link = logger)
513 log_res_step(logger, res_c)
517 error_step = "CONFIGURE"
519 # Logging and sat command call for make step
520 # Logging take account of the fact that the product has a compilation
522 if src.product.product_has_script(p_info):
523 # if the product has a compilation script,
524 # it is executed during make step
525 scrit_path_display = src.printcolors.printcLabel(
526 p_info.compil_script)
527 log_step(logger, header, "SCRIPT " + scrit_path_display)
528 len_end_line = len(scrit_path_display)
530 log_step(logger, header, "MAKE")
531 make_arguments = config.VARS.application + " --products " + p_name
532 # Get the make_flags option if there is any
533 if options.makeflags:
534 make_arguments += " --option -j" + options.makeflags
535 res_m = sat.make(make_arguments,
537 logger_add_link = logger)
538 log_res_step(logger, res_m)
544 # Logging and sat command call for make install step
545 log_step(logger, header, "MAKE INSTALL")
546 res_mi = sat.makeinstall(config.VARS.application +
550 logger_add_link = logger)
552 log_res_step(logger, res_mi)
556 error_step = "MAKE INSTALL"
558 return res, len_end_line, error_step
560 def compile_product_script(sat,
567 '''Execute the script build procedure in the product build directory.
569 :param p_name_info tuple: (str, Config) => (product_name, product_info)
570 :param config Config: The global configuration
571 :param logger Logger: The logger instance to use for the display
573 :param header Str: the header to display when logging
574 :param len_end Int: the lenght of the the end of line (used in display)
575 :return: 1 if it fails, else 0.
578 p_name, p_info = p_name_info
580 # Execute "sat configure", "sat make" and "sat install"
583 # Logging and sat command call for the script step
584 scrit_path_display = src.printcolors.printcLabel(p_info.compil_script)
585 log_step(logger, header, "SCRIPT " + scrit_path_display)
586 len_end_line = len_end + len(scrit_path_display)
587 res = sat.script(config.VARS.application + " --products " + p_name,
589 logger_add_link = logger)
590 log_res_step(logger, res)
592 return res, len_end_line, error_step
596 '''method that is called when salomeTools is called with --help option.
598 :return: The text to display for the compile command description.
601 return _("The compile command constructs the products of the application"
602 "\n\nexample:\nsat compile SALOME-master --products KERNEL,GUI,"
603 "MEDCOUPLING --clean_all")
605 def run(args, runner, logger):
606 '''method that is called when salomeTools is called with compile parameter.
608 # DBG.write("compile runner.cfg", runner.cfg, True)
610 (options, args) = parser.parse_args(args)
612 # Warn the user if he invoked the clean_all option
613 # without --products option
614 if (options.clean_all and
615 options.products is None and
616 not runner.options.batch):
617 rep = input(_("You used --clean_all without specifying a product"
618 " are you sure you want to continue? [Yes/No] "))
619 if rep.upper() != _("YES").upper():
622 # check that the command has been called with an application
623 src.check_config_has_application( runner.cfg )
625 # Print some informations
626 logger.write(_('Executing the compile commands in the build '
627 'directories of the products of '
628 'the application %s\n') %
629 src.printcolors.printcLabel(runner.cfg.VARS.application), 1)
632 (_("SOURCE directory"),
633 os.path.join(runner.cfg.APPLICATION.workdir, 'SOURCES')),
634 (_("BUILD directory"),
635 os.path.join(runner.cfg.APPLICATION.workdir, 'BUILD'))
637 src.print_info(logger, info)
639 # Get the list of all application products, and create its dependency graph
640 all_products_infos = src.product.get_products_infos(runner.cfg.APPLICATION.products,
642 all_products_graph=get_dependencies_graph(all_products_infos)
643 logger.write("Dependency graph of all application products : %s\n" % all_products_graph, 6)
645 # Get the list of products we have to compile
646 products_infos = src.product.get_products_list(options, runner.cfg, logger)
647 products_list = [pi[0] for pi in products_infos]
649 logger.write("Product we have to compile (as specified by user) : %s\n" % products_list, 5)
651 # Extend the list with all recursive dependencies of the given products
653 for p_name in products_list:
654 visited=depth_search_graph(all_products_graph, p_name, visited)
655 products_list = visited
657 logger.write("Product list to compile with fathers : %s\n" % products_list, 5)
659 # Extend the list with all products that depends upon the given products
661 for n in all_products_graph:
662 # for all products (that are not in products_list):
663 # if we we find a path from the product to the product list,
664 # then we product is a child and we add it to the children list
665 if (n not in children) and (n not in products_list):
666 if find_path_graph(all_products_graph, n, products_list):
667 children = children + [n]
668 # complete products_list (the products we have to compile) with the list of children
669 products_list = products_list + children
670 logger.write("Product list to compile with children : %s\n" % products_list, 5)
672 # Sort the list of all products (topological sort).
673 # the products listed first do not depend upon products listed after
676 for n in all_products_graph:
677 if n not in visited_nodes:
678 visited_nodes,sorted_nodes=depth_first_topo_graph(all_products_graph, n, visited_nodes,sorted_nodes)
679 logger.write("Complete depndency graph topological search (sorting): %s\n" % sorted_nodes, 6)
681 # use the sorted list of all products to sort the list of products we have to compile
682 sorted_product_list=[]
683 for n in sorted_nodes:
684 if n in products_list:
685 sorted_product_list.append(n)
686 logger.write("Sorted list of products to compile : %s\n" % sorted_product_list, 5)
689 # from the sorted list of products to compile, build a sorted list of products infos
690 # a- create a dict to facilitate products_infos sorting
692 for (pname,pinfo) in all_products_infos:
693 all_products_dict[pname]=(pname,pinfo)
694 # b- build a sorted list of products infos in products_infos
696 for product in sorted_product_list:
697 products_infos.append(all_products_dict[product])
699 # for all products to compile, store in "depend_all" field the complete dependencies (recursive)
700 # (will be used by check_dependencies funvtion)
701 for pi in products_infos:
703 dep_prod=depth_search_graph(all_products_graph,pi[0], dep_prod)
704 pi[1]["depend_all"]=dep_prod[1:]
707 # Call the function that will loop over all the products and execute
708 # the right command(s)
709 res = compile_all_products(runner, runner.cfg, options, products_infos, all_products_dict, logger)
711 # Print the final state
712 nb_products = len(products_infos)
718 logger.write(_("\nCompilation: %(status)s (%(valid_result)d/%(nb_products)d)\n") % \
719 { 'status': src.printcolors.printc(final_status),
720 'valid_result': nb_products - res,
721 'nb_products': nb_products }, 1)