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)
263 # there was an error, we clean install dir, unless :
264 # - the error step is "check", or
265 # - the product is managed by pip and installed in python dir
266 do_not_clean_install=False
267 if (error_step == "CHECK") or (is_pip and \
268 src.appli_test_property(config,"pip_install_dir", "python")) :
269 do_not_clean_install=True
270 if not do_not_clean_install:
271 # Clean the install directory if there is any
273 "Cleaning the install directory if there is any\n"),
275 sat.clean(config.VARS.application +
276 " --products " + p_name +
280 logger_add_link = logger)
282 # Clean the build directory if the compilation and tests succeed
283 if options.clean_build_after:
284 log_step(logger, header, "CLEAN BUILD")
285 sat.clean(config.VARS.application +
286 " --products " + p_name +
290 logger_add_link = logger)
294 logger.write("\r%s%s" % (header, " " * len_end_line), 3)
295 logger.write("\r" + header + src.printcolors.printcError("KO ") + error_step)
296 logger.write("\n==== %(KO)s in compile of %(name)s \n" %
297 { "name" : p_name , "KO" : src.printcolors.printcInfo("ERROR")}, 4)
298 if error_step == "CHECK":
299 logger.write(_("\nINSTALL directory = %s" %
300 src.printcolors.printcInfo(p_info.install_dir)), 3)
303 logger.write("\r%s%s" % (header, " " * len_end_line), 3)
304 logger.write("\r" + header + src.printcolors.printcSuccess("OK"))
305 logger.write(_("\nINSTALL directory = %s" %
306 src.printcolors.printcInfo(p_info.install_dir)), 3)
307 logger.write("\n==== %s \n" % src.printcolors.printcInfo("OK"), 4)
308 logger.write("\n==== Compilation of %(name)s %(OK)s \n" %
309 { "name" : p_name , "OK" : src.printcolors.printcInfo("OK")}, 4)
311 logger.write("\n", 3, False)
314 if res_prod != 0 and options.stop_first_fail:
319 def compile_product(sat, p_name_info, config, options, logger, header, len_end):
320 '''Execute the proper configuration command(s)
321 in the product build directory.
323 :param p_name_info tuple: (str, Config) => (product_name, product_info)
324 :param config Config: The global configuration
325 :param logger Logger: The logger instance to use for the display
327 :param header Str: the header to display when logging
328 :param len_end Int: the lenght of the the end of line (used in display)
329 :return: 1 if it fails, else 0.
333 p_name, p_info = p_name_info
335 # Get the build procedure from the product configuration.
337 # build_sources : autotools -> build_configure, configure, make, make install
338 # build_sources : cmake -> cmake, make, make install
339 # build_sources : script -> script executions
343 # check if pip should be used : the application and product have pip property
344 if (src.appli_test_property(config,"pip", "yes") and
345 src.product.product_test_property(p_info,"pip", "yes")):
346 res, len_end_line, error_step = compile_product_pip(sat,
354 if (src.product.product_is_autotools(p_info) or
355 src.product.product_is_cmake(p_info)):
356 res, len_end_line, error_step = compile_product_cmake_autotools(sat,
363 if src.product.product_has_script(p_info):
364 res, len_end_line, error_step = compile_product_script(sat,
372 # Check that the install directory exists
373 if res==0 and not(os.path.exists(p_info.install_dir)):
375 error_step = "NO INSTALL DIR"
376 msg = _("Error: despite the fact that all the steps ended successfully,"
377 " no install directory was found !")
378 logger.write(src.printcolors.printcError(msg), 4)
379 logger.write("\n", 4)
380 return res, len_end, error_step
382 # Add the config file corresponding to the dependencies/versions of the
383 # product that have been successfully compiled
385 logger.write(_("Add the config file in installation directory\n"), 5)
386 src.product.add_compile_config_file(p_info, config)
389 # Do the unit tests (call the check command)
390 log_step(logger, header, "CHECK")
391 res_check = sat.check(
392 config.VARS.application + " --products " + p_name,
394 logger_add_link = logger)
400 return res, len_end_line, error_step
403 def compile_product_pip(sat,
410 '''Execute the proper build procedure for pip products
411 :param p_name_info tuple: (str, Config) => (product_name, product_info)
412 :param config Config: The global configuration
413 :param logger Logger: The logger instance to use for the display
415 :param header Str: the header to display when logging
416 :param len_end Int: the lenght of the the end of line (used in display)
417 :return: 1 if it fails, else 0.
421 p_name, p_info = p_name_info
424 pip_install_in_python=False
425 pip_wheels_dir=os.path.join(config.LOCAL.archive_dir,"wheels")
426 pip_install_cmd=config.INTERNAL.command.pip_install # parametrized in src/internal
428 # b) get the build environment (useful to get the installed python & pip3)
429 build_environ = src.environment.SalomeEnviron(config,
430 src.environment.Environ(dict(os.environ)),
432 environ_info = src.product.get_product_dependencies(config,
434 build_environ.silent = (config.USER.output_verbose_level < 5)
435 build_environ.set_full_environ(logger, environ_info)
437 # c- download : check/get pip wheel in pip_wheels_dir
438 pip_download_cmd=config.INTERNAL.command.pip_download +\
439 " --destination-directory %s --no-deps %s==%s " %\
440 (pip_wheels_dir, p_info.name, p_info.version)
441 logger.write("\n"+pip_download_cmd+"\n", 4, False)
442 res_pip_dwl = (subprocess.call(pip_download_cmd,
444 cwd=config.LOCAL.workdir,
445 env=build_environ.environ.environ,
446 stdout=logger.logTxtFile,
447 stderr=subprocess.STDOUT) == 0)
448 # error is not managed at the stage. error will be handled by pip install
449 # here we just print a message
451 logger.write("Error in pip download\n", 4, False)
454 # d- install (in python or in separate product directory)
455 if src.appli_test_property(config,"pip_install_dir", "python"):
456 # pip will install product in python directory"
457 pip_install_cmd+=" --find-links=%s --build %s %s==%s" %\
458 (pip_wheels_dir, p_info.build_dir, p_info.name, p_info.version)
459 pip_install_in_python=True
462 # pip will install product in product install_dir
463 pip_install_dir=os.path.join(p_info.install_dir, "lib", "python${PYTHON_VERSION:0:3}", "site-packages")
464 pip_install_cmd+=" --find-links=%s --build %s --target %s %s==%s" %\
465 (pip_wheels_dir, p_info.build_dir, pip_install_dir, p_info.name, p_info.version)
466 log_step(logger, header, "PIP")
467 logger.write("\n"+pip_install_cmd+"\n", 4)
468 len_end_line = len_end + 3
471 res_pip = (subprocess.call(pip_install_cmd,
473 cwd=config.LOCAL.workdir,
474 env=build_environ.environ.environ,
475 stdout=logger.logTxtFile,
476 stderr=subprocess.STDOUT) == 0)
480 #log_res_step(logger, res)
483 logger.write("\nError in pip command, please consult details with sat log command's internal traces\n", 5)
485 return res, len_end_line, error_step
489 def compile_product_cmake_autotools(sat,
496 '''Execute the proper build procedure for autotools or cmake
497 in the product build directory.
499 :param p_name_info tuple: (str, Config) => (product_name, product_info)
500 :param config Config: The global configuration
501 :param logger Logger: The logger instance to use for the display
503 :param header Str: the header to display when logging
504 :param len_end Int: the lenght of the the end of line (used in display)
505 :return: 1 if it fails, else 0.
508 p_name, p_info = p_name_info
510 # Execute "sat configure", "sat make" and "sat install"
514 # Logging and sat command call for configure step
515 len_end_line = len_end
516 log_step(logger, header, "CONFIGURE")
517 res_c = sat.configure(config.VARS.application + " --products " + p_name,
519 logger_add_link = logger)
520 log_res_step(logger, res_c)
524 error_step = "CONFIGURE"
526 # Logging and sat command call for make step
527 # Logging take account of the fact that the product has a compilation
529 if src.product.product_has_script(p_info):
530 # if the product has a compilation script,
531 # it is executed during make step
532 scrit_path_display = src.printcolors.printcLabel(
533 p_info.compil_script)
534 log_step(logger, header, "SCRIPT " + scrit_path_display)
535 len_end_line = len(scrit_path_display)
537 log_step(logger, header, "MAKE")
538 make_arguments = config.VARS.application + " --products " + p_name
539 # Get the make_flags option if there is any
540 if options.makeflags:
541 make_arguments += " --option -j" + options.makeflags
542 res_m = sat.make(make_arguments,
544 logger_add_link = logger)
545 log_res_step(logger, res_m)
551 # Logging and sat command call for make install step
552 log_step(logger, header, "MAKE INSTALL")
553 res_mi = sat.makeinstall(config.VARS.application +
557 logger_add_link = logger)
559 log_res_step(logger, res_mi)
563 error_step = "MAKE INSTALL"
565 return res, len_end_line, error_step
567 def compile_product_script(sat,
574 '''Execute the script build procedure in the product build directory.
576 :param p_name_info tuple: (str, Config) => (product_name, product_info)
577 :param config Config: The global configuration
578 :param logger Logger: The logger instance to use for the display
580 :param header Str: the header to display when logging
581 :param len_end Int: the lenght of the the end of line (used in display)
582 :return: 1 if it fails, else 0.
585 p_name, p_info = p_name_info
587 # Execute "sat configure", "sat make" and "sat install"
590 # Logging and sat command call for the script step
591 scrit_path_display = src.printcolors.printcLabel(p_info.compil_script)
592 log_step(logger, header, "SCRIPT " + scrit_path_display)
593 len_end_line = len_end + len(scrit_path_display)
594 res = sat.script(config.VARS.application + " --products " + p_name,
596 logger_add_link = logger)
597 log_res_step(logger, res)
599 return res, len_end_line, error_step
603 '''method that is called when salomeTools is called with --help option.
605 :return: The text to display for the compile command description.
608 return _("The compile command constructs the products of the application"
609 "\n\nexample:\nsat compile SALOME-master --products KERNEL,GUI,"
610 "MEDCOUPLING --clean_all")
612 def run(args, runner, logger):
613 '''method that is called when salomeTools is called with compile parameter.
615 # DBG.write("compile runner.cfg", runner.cfg, True)
617 (options, args) = parser.parse_args(args)
619 # Warn the user if he invoked the clean_all option
620 # without --products option
621 if (options.clean_all and
622 options.products is None and
623 not runner.options.batch):
624 rep = input(_("You used --clean_all without specifying a product"
625 " are you sure you want to continue? [Yes/No] "))
626 if rep.upper() != _("YES").upper():
629 # check that the command has been called with an application
630 src.check_config_has_application( runner.cfg )
632 # Print some informations
633 logger.write(_('Executing the compile commands in the build '
634 'directories of the products of '
635 'the application %s\n') %
636 src.printcolors.printcLabel(runner.cfg.VARS.application), 1)
639 (_("SOURCE directory"),
640 os.path.join(runner.cfg.APPLICATION.workdir, 'SOURCES')),
641 (_("BUILD directory"),
642 os.path.join(runner.cfg.APPLICATION.workdir, 'BUILD'))
644 src.print_info(logger, info)
646 # Get the list of all application products, and create its dependency graph
647 all_products_infos = src.product.get_products_infos(runner.cfg.APPLICATION.products,
649 all_products_graph=get_dependencies_graph(all_products_infos)
650 logger.write("Dependency graph of all application products : %s\n" % all_products_graph, 6)
652 # Get the list of products we have to compile
653 products_infos = src.product.get_products_list(options, runner.cfg, logger)
654 products_list = [pi[0] for pi in products_infos]
656 logger.write("Product we have to compile (as specified by user) : %s\n" % products_list, 5)
658 # Extend the list with all recursive dependencies of the given products
660 for p_name in products_list:
661 visited=depth_search_graph(all_products_graph, p_name, visited)
662 products_list = visited
664 logger.write("Product list to compile with fathers : %s\n" % products_list, 5)
666 # Extend the list with all products that depends upon the given products
668 for n in all_products_graph:
669 # for all products (that are not in products_list):
670 # if we we find a path from the product to the product list,
671 # then we product is a child and we add it to the children list
672 if (n not in children) and (n not in products_list):
673 if find_path_graph(all_products_graph, n, products_list):
674 children = children + [n]
675 # complete products_list (the products we have to compile) with the list of children
676 products_list = products_list + children
677 logger.write("Product list to compile with children : %s\n" % products_list, 5)
679 # Sort the list of all products (topological sort).
680 # the products listed first do not depend upon products listed after
683 for n in all_products_graph:
684 if n not in visited_nodes:
685 visited_nodes,sorted_nodes=depth_first_topo_graph(all_products_graph, n, visited_nodes,sorted_nodes)
686 logger.write("Complete depndency graph topological search (sorting): %s\n" % sorted_nodes, 6)
688 # use the sorted list of all products to sort the list of products we have to compile
689 sorted_product_list=[]
690 for n in sorted_nodes:
691 if n in products_list:
692 sorted_product_list.append(n)
693 logger.write("Sorted list of products to compile : %s\n" % sorted_product_list, 5)
696 # from the sorted list of products to compile, build a sorted list of products infos
697 # a- create a dict to facilitate products_infos sorting
699 for (pname,pinfo) in all_products_infos:
700 all_products_dict[pname]=(pname,pinfo)
701 # b- build a sorted list of products infos in products_infos
703 for product in sorted_product_list:
704 products_infos.append(all_products_dict[product])
706 # for all products to compile, store in "depend_all" field the complete dependencies (recursive)
707 # (will be used by check_dependencies funvtion)
708 for pi in products_infos:
710 dep_prod=depth_search_graph(all_products_graph,pi[0], dep_prod)
711 pi[1]["depend_all"]=dep_prod[1:]
714 # Call the function that will loop over all the products and execute
715 # the right command(s)
716 res = compile_all_products(runner, runner.cfg, options, products_infos, all_products_dict, logger)
718 # Print the final state
719 nb_products = len(products_infos)
725 logger.write(_("\nCompilation: %(status)s (%(valid_result)d/%(nb_products)d)\n") % \
726 { 'status': src.printcolors.printc(final_status),
727 'valid_result': nb_products - res,
728 'nb_products': nb_products }, 1)