Salome HOME
unification des launcher python et exe, pour passage au mode exe du lanceur salome
[tools/sat.git] / commands / compile.py
1 #!/usr/bin/env python
2 #-*- coding:utf-8 -*-
3 #  Copyright (C) 2010-2012  CEA/DEN
4 #
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.
9 #
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.
14 #
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
18
19 import os
20 import re
21 import subprocess
22 import src
23 import src.debug as DBG
24
25 # Compatibility python 2/3 for input function
26 # input stays input for python 3 and input = raw_input for python 2
27 try: 
28     input = raw_input
29 except NameError: 
30     pass
31
32
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."),
45     False)
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."),
52     False)
53 parser.add_option('', 'stop_first_fail', 'boolean', 'stop_first_fail', _(
54                   "Optional: Stops the command at first product compilation"
55                   " fail."), False)
56 parser.add_option('', 'check', 'boolean', 'check', _(
57                   "Optional: execute the unit tests after compilation"), False)
58
59 parser.add_option('', 'clean_build_after', 'boolean', 'clean_build_after', 
60                   _('Optional: remove the build directory after successful compilation'), False)
61
62
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):
66     graph={}
67     for (p_name,p_info) in p_infos:
68         graph[p_name]=p_info.depend
69     return graph
70
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)
77     return visited
78
79 # find a path from start node to end (a group of nodes)
80 def find_path_graph(graph, start, end, path=[]):
81     path = path + [start]
82     if start in end:
83         return path
84     if start not in graph:
85         return None
86     for node in graph[start]:
87         if node not in path:
88             newpath = find_path_graph(graph, node, end, path)
89             if newpath: return newpath
90     return None
91
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     if start not in graph:
97         raise src.SatException('Error in product dependencies : %s product is referenced in products dependencies but is not present in the application !' % start)
98     for node in graph[start]:
99         if node not in visited:
100             visited,sorted_nodes=depth_first_topo_graph(graph, node, visited,sorted_nodes)
101         else:
102             if node not in sorted_nodes:
103                 raise src.SatException('Error in product dependencies : cycle detection for node %s and %s !' % (start,node))
104     
105     sorted_nodes = sorted_nodes + [start]
106     return visited,sorted_nodes
107
108
109 # check for p_name that all dependencies are installed
110 def check_dependencies(config, p_name_p_info, all_products_dict):
111     l_depends_not_installed = []
112     for prod in p_name_p_info[1]["depend_all"]:
113         # for each dependency, check the install
114         prod_name, prod_info=all_products_dict[prod]
115         if not(src.product.check_installation(config, prod_info)):
116             l_depends_not_installed.append(prod_name)
117     return l_depends_not_installed   # non installed deps
118
119 def log_step(logger, header, step):
120     logger.write("\r%s%s" % (header, " " * 30), 3)
121     logger.write("\r%s%s" % (header, step), 3)
122     logger.flush()
123
124 def log_res_step(logger, res):
125     if res == 0:
126         logger.write("%s \n" % src.printcolors.printcSuccess("OK"), 4)
127         logger.flush()
128     else:
129         logger.write("%s \n" % src.printcolors.printcError("KO"), 4)
130         logger.flush()
131
132 def compile_all_products(sat, config, options, products_infos, all_products_dict, logger):
133     '''Execute the proper configuration commands 
134        in each product build directory.
135
136     :param config Config: The global configuration
137     :param products_info list: List of 
138                                  (str, Config) => (product_name, product_info)
139     :param all_products_dict: Dict of all products 
140     :param logger Logger: The logger instance to use for the display and logging
141     :return: the number of failing commands.
142     :rtype: int
143     '''
144     res = 0
145     for p_name_info in products_infos:
146         
147         p_name, p_info = p_name_info
148         
149         # Logging
150         len_end_line = 30
151         header = _("Compilation of %s") % src.printcolors.printcLabel(p_name)
152         header += " %s " % ("." * (len_end_line - len(p_name)))
153         logger.write(header, 3)
154         logger.flush()
155
156         # Do nothing if the product is not compilable
157         if not src.product.product_compiles(p_info):
158             log_step(logger, header, "ignored")
159             logger.write("\n", 3, False)
160             continue
161
162         # Do nothing if the product is native
163         if src.product.product_is_native(p_info):
164             log_step(logger, header, "native")
165             logger.write("\n", 3, False)
166             continue
167
168         # Do nothing if the product is fixed (already compiled by third party)
169         if src.product.product_is_fixed(p_info):
170             log_step(logger, header, "native")
171             logger.write("\n", 3, False)
172             continue
173
174         # Clean the build and the install directories 
175         # if the corresponding options was called
176         if options.clean_all:
177             log_step(logger, header, "CLEAN BUILD AND INSTALL ")
178             sat.clean(config.VARS.application + 
179                       " --products " + p_name + 
180                       " --build --install",
181                       batch=True,
182                       verbose=0,
183                       logger_add_link = logger)
184
185
186         # Clean the the install directory 
187         # if the corresponding option was called
188         if options.clean_install and not options.clean_all:
189             log_step(logger, header, "CLEAN INSTALL ")
190             sat.clean(config.VARS.application + 
191                       " --products " + p_name + 
192                       " --install",
193                       batch=True,
194                       verbose=0,
195                       logger_add_link = logger)
196         
197         # Recompute the product information to get the right install_dir
198         # (it could change if there is a clean of the install directory)
199         p_info = src.product.get_product_config(config, p_name)
200         
201         # Check if sources was already successfully installed
202         check_source = src.product.check_source(p_info)
203         is_pip= (src.appli_test_property(config,"pip", "yes") and src.product.product_test_property(p_info,"pip", "yes"))
204         # don't check sources with option --show 
205         # or for products managed by pip (there sources are in wheels stored in LOCAL.ARCHIVE
206         if not (options.no_compile or is_pip): 
207             if not check_source:
208                 logger.write(_("Sources of product not found (try 'sat -h prepare') \n"))
209                 res += 1 # one more error
210                 continue
211         
212         if src.product.product_is_salome(p_info):
213             # For salome modules, we check if the sources of configuration modules are present
214             # configuration modules have the property "configure_dependency"
215
216             # get the list of all modules in application 
217             all_products_infos = src.product.get_products_infos(config.APPLICATION.products,
218                                                                 config)
219             check_source = True
220             # for configuration modules, check if sources are present
221             for prod in all_products_dict:
222                 product_name, product_info = all_products_dict[prod]
223                 if ("properties" in product_info and
224                     "configure_dependency" in product_info.properties and
225                     product_info.properties.configure_dependency == "yes"):
226                     check_source = check_source and src.product.check_source(product_info)
227                     if not check_source:
228                         logger.write(_("\nERROR : SOURCES of %s not found! It is required for" 
229                                        " the configuration\n" % product_name))
230                         logger.write(_("        Get it with the command : sat prepare %s -p %s \n" % 
231                                       (config.APPLICATION.name, product_name)))
232             if not check_source:
233                 # if at least one configuration module is not present, we stop compilation
234                 res += 1
235                 continue
236         
237         # Check if it was already successfully installed
238         if src.product.check_installation(config, p_info):
239             logger.write(_("Already installed"))
240             logger.write(_(" in %s" % p_info.install_dir), 4)
241             logger.write(_("\n"))
242             continue
243         
244         # If the show option was called, do not launch the compilation
245         if options.no_compile:
246             logger.write(_("Not installed in %s\n" % p_info.install_dir))
247             continue
248         
249         # Check if the dependencies are installed
250         l_depends_not_installed = check_dependencies(config, p_name_info, all_products_dict)
251         if len(l_depends_not_installed) > 0:
252             log_step(logger, header, "")
253             logger.write(src.printcolors.printcError(
254                     _("ERROR : the following mandatory product(s) is(are) not installed: ")))
255             for prod_name in l_depends_not_installed:
256                 logger.write(src.printcolors.printcError(prod_name + " "))
257             logger.write("\n")
258             continue
259         
260         # Call the function to compile the product
261         res_prod, len_end_line, error_step = compile_product(
262              sat, p_name_info, config, options, logger, header, len_end_line)
263         
264         if res_prod != 0:
265             res += 1
266             # there was an error, we clean install dir, unless :
267             #  - the error step is "check", or
268             #  - the product is managed by pip and installed in python dir
269             do_not_clean_install=False
270             if (error_step == "CHECK") or (is_pip and \
271                 src.appli_test_property(config,"pip_install_dir", "python")) :
272                 do_not_clean_install=True
273             if not do_not_clean_install:
274                 # Clean the install directory if there is any
275                 logger.write(_(
276                             "Cleaning the install directory if there is any\n"),
277                              5)
278                 sat.clean(config.VARS.application + 
279                           " --products " + p_name + 
280                           " --install",
281                           batch=True,
282                           verbose=0,
283                           logger_add_link = logger)
284         else:
285             # Clean the build directory if the compilation and tests succeed
286             if options.clean_build_after:
287                 log_step(logger, header, "CLEAN BUILD")
288                 sat.clean(config.VARS.application + 
289                           " --products " + p_name + 
290                           " --build",
291                           batch=True,
292                           verbose=0,
293                           logger_add_link = logger)
294
295         # Log the result
296         if res_prod > 0:
297             logger.write("\r%s%s" % (header, " " * len_end_line), 3)
298             logger.write("\r" + header + src.printcolors.printcError("KO ") + error_step)
299             logger.write("\n==== %(KO)s in compile of %(name)s \n" %
300                 { "name" : p_name , "KO" : src.printcolors.printcInfo("ERROR")}, 4)
301             if error_step == "CHECK":
302                 logger.write(_("\nINSTALL directory = %s" % 
303                            src.printcolors.printcInfo(p_info.install_dir)), 3)
304             logger.flush()
305         else:
306             logger.write("\r%s%s" % (header, " " * len_end_line), 3)
307             logger.write("\r" + header + src.printcolors.printcSuccess("OK"))
308             logger.write(_("\nINSTALL directory = %s" % 
309                            src.printcolors.printcInfo(p_info.install_dir)), 3)
310             logger.write("\n==== %s \n" % src.printcolors.printcInfo("OK"), 4)
311             logger.write("\n==== Compilation of %(name)s %(OK)s \n" %
312                 { "name" : p_name , "OK" : src.printcolors.printcInfo("OK")}, 4)
313             logger.flush()
314         logger.write("\n", 3, False)
315         
316         
317         if res_prod != 0 and options.stop_first_fail:
318             break
319         
320     return res
321
322 def compile_product(sat, p_name_info, config, options, logger, header, len_end):
323     '''Execute the proper configuration command(s) 
324        in the product build directory.
325     
326     :param p_name_info tuple: (str, Config) => (product_name, product_info)
327     :param config Config: The global configuration
328     :param logger Logger: The logger instance to use for the display 
329                           and logging
330     :param header Str: the header to display when logging
331     :param len_end Int: the lenght of the the end of line (used in display)
332     :return: 1 if it fails, else 0.
333     :rtype: int
334     '''
335     
336     p_name, p_info = p_name_info
337           
338     # Get the build procedure from the product configuration.
339     # It can be :
340     # build_sources : autotools -> build_configure, configure, make, make install
341     # build_sources : cmake     -> cmake, make, make install
342     # build_sources : script    -> script executions
343     res = 0
344
345     
346     # check if pip should be used : the application and product have pip property
347     if (src.appli_test_property(config,"pip", "yes") and 
348        src.product.product_test_property(p_info,"pip", "yes")):
349             res, len_end_line, error_step = compile_product_pip(sat,
350                                                                 p_name_info,
351                                                                 config,
352                                                                 options,
353                                                                 logger,
354                                                                 header,
355                                                                 len_end)
356     else:
357         if (src.product.product_is_autotools(p_info) or 
358                                               src.product.product_is_cmake(p_info)):
359             res, len_end_line, error_step = compile_product_cmake_autotools(sat,
360                                                                       p_name_info,
361                                                                       config,
362                                                                       options,
363                                                                       logger,
364                                                                       header,
365                                                                       len_end)
366         if src.product.product_has_script(p_info):
367             res, len_end_line, error_step = compile_product_script(sat,
368                                                                    p_name_info,
369                                                                    config,
370                                                                    options,
371                                                                    logger,
372                                                                    header,
373                                                                    len_end)
374
375     # Check that the install directory exists
376     if res==0 and not(os.path.exists(p_info.install_dir)):
377         res = 1
378         error_step = "NO INSTALL DIR"
379         msg = _("Error: despite the fact that all the steps ended successfully,"
380                 " no install directory was found !")
381         logger.write(src.printcolors.printcError(msg), 4)
382         logger.write("\n", 4)
383         return res, len_end, error_step
384     
385     # Add the config file corresponding to the dependencies/versions of the 
386     # product that have been successfully compiled
387     if res==0:       
388         logger.write(_("Add the config file in installation directory\n"), 5)
389         src.product.add_compile_config_file(p_info, config)
390         
391         if options.check:
392             # Do the unit tests (call the check command)
393             log_step(logger, header, "CHECK")
394             res_check = sat.check(
395                               config.VARS.application + " --products " + p_name,
396                               verbose = 0,
397                               logger_add_link = logger)
398             if res_check != 0:
399                 error_step = "CHECK"
400                 
401             res += res_check
402     
403     return res, len_end_line, error_step
404
405
406 def compile_product_pip(sat,
407                         p_name_info,
408                         config,
409                         options,
410                         logger,
411                         header,
412                         len_end):
413     '''Execute the proper build procedure for pip products
414     :param p_name_info tuple: (str, Config) => (product_name, product_info)
415     :param config Config: The global configuration
416     :param logger Logger: The logger instance to use for the display 
417                           and logging
418     :param header Str: the header to display when logging
419     :param len_end Int: the lenght of the the end of line (used in display)
420     :return: 1 if it fails, else 0.
421     :rtype: int
422     '''
423     # a) initialisation
424     p_name, p_info = p_name_info
425     res = 0
426     error_step = ""
427     pip_install_in_python=False
428     pip_wheels_dir=os.path.join(config.LOCAL.archive_dir,"wheels")
429     pip_install_cmd=config.INTERNAL.command.pip_install # parametrized in src/internal
430
431     # b) get the build environment (useful to get the installed python & pip3)
432     build_environ = src.environment.SalomeEnviron(config,
433                              src.environment.Environ(dict(os.environ)),
434                              True)
435     environ_info = src.product.get_product_dependencies(config,
436                                                         p_info)
437     build_environ.silent = (config.USER.output_verbose_level < 5)
438     build_environ.set_full_environ(logger, environ_info)
439
440     # c- download : check/get pip wheel in pip_wheels_dir
441     pip_download_cmd=config.INTERNAL.command.pip_download +\
442                      " --destination-directory %s --no-deps %s==%s " %\
443                      (pip_wheels_dir, p_info.name, p_info.version)
444     logger.write("\n"+pip_download_cmd+"\n", 4, False) 
445     res_pip_dwl = (subprocess.call(pip_download_cmd, 
446                                    shell=True, 
447                                    cwd=config.LOCAL.workdir,
448                                    env=build_environ.environ.environ,
449                                    stdout=logger.logTxtFile, 
450                                    stderr=subprocess.STDOUT) == 0)
451     # error is not managed at the stage. error will be handled by pip install
452     # here we just print a message
453     if not res_pip_dwl:
454         logger.write("Error in pip download\n", 4, False)
455
456
457     # d- install (in python or in separate product directory)
458     if src.appli_test_property(config,"pip_install_dir", "python"):
459         # pip will install product in python directory"
460         pip_install_cmd+=" --find-links=%s --build %s %s==%s" %\
461         (pip_wheels_dir, p_info.build_dir, p_info.name, p_info.version)
462         pip_install_in_python=True
463         
464     else: 
465         # pip will install product in product install_dir
466         pip_install_dir=os.path.join(p_info.install_dir, "lib", "python${PYTHON_VERSION:0:3}", "site-packages")
467         pip_install_cmd+=" --find-links=%s --build %s --target %s %s==%s" %\
468         (pip_wheels_dir, p_info.build_dir, pip_install_dir, p_info.name, p_info.version)
469     log_step(logger, header, "PIP")
470     logger.write("\n"+pip_install_cmd+"\n", 4)
471     len_end_line = len_end + 3
472     error_step = ""
473
474     res_pip = (subprocess.call(pip_install_cmd, 
475                                shell=True, 
476                                cwd=config.LOCAL.workdir,
477                                env=build_environ.environ.environ,
478                                stdout=logger.logTxtFile, 
479                                stderr=subprocess.STDOUT) == 0)        
480     if res_pip:
481         res=0
482     else:
483         #log_res_step(logger, res)
484         res=1
485         error_step = "PIP"
486         logger.write("\nError in pip command, please consult details with sat log command's internal traces\n", 3)
487
488     return res, len_end_line, error_step 
489
490
491
492 def compile_product_cmake_autotools(sat,
493                                     p_name_info,
494                                     config,
495                                     options,
496                                     logger,
497                                     header,
498                                     len_end):
499     '''Execute the proper build procedure for autotools or cmake
500        in the product build directory.
501     
502     :param p_name_info tuple: (str, Config) => (product_name, product_info)
503     :param config Config: The global configuration
504     :param logger Logger: The logger instance to use for the display 
505                           and logging
506     :param header Str: the header to display when logging
507     :param len_end Int: the lenght of the the end of line (used in display)
508     :return: 1 if it fails, else 0.
509     :rtype: int
510     '''
511     p_name, p_info = p_name_info
512     
513     # Execute "sat configure", "sat make" and "sat install"
514     res = 0
515     error_step = ""
516     
517     # Logging and sat command call for configure step
518     len_end_line = len_end
519     log_step(logger, header, "CONFIGURE")
520     res_c = sat.configure(config.VARS.application + " --products " + p_name,
521                           verbose = 0,
522                           logger_add_link = logger)
523     log_res_step(logger, res_c)
524     res += res_c
525     
526     if res_c > 0:
527         error_step = "CONFIGURE"
528     else:
529         # Logging and sat command call for make step
530         # Logging take account of the fact that the product has a compilation 
531         # script or not
532         if src.product.product_has_script(p_info):
533             # if the product has a compilation script, 
534             # it is executed during make step
535             scrit_path_display = src.printcolors.printcLabel(
536                                                         p_info.compil_script)
537             log_step(logger, header, "SCRIPT " + scrit_path_display)
538             len_end_line = len(scrit_path_display)
539         else:
540             log_step(logger, header, "MAKE")
541         make_arguments = config.VARS.application + " --products " + p_name
542         # Get the make_flags option if there is any
543         if options.makeflags:
544             make_arguments += " --option -j" + options.makeflags
545         res_m = sat.make(make_arguments,
546                          verbose = 0,
547                          logger_add_link = logger)
548         log_res_step(logger, res_m)
549         res += res_m
550         
551         if res_m > 0:
552             error_step = "MAKE"
553         else: 
554             # Logging and sat command call for make install step
555             log_step(logger, header, "MAKE INSTALL")
556             res_mi = sat.makeinstall(config.VARS.application + 
557                                      " --products " + 
558                                      p_name,
559                                     verbose = 0,
560                                     logger_add_link = logger)
561
562             log_res_step(logger, res_mi)
563             res += res_mi
564             
565             if res_mi > 0:
566                 error_step = "MAKE INSTALL"
567                 
568     return res, len_end_line, error_step 
569
570 def compile_product_script(sat,
571                            p_name_info,
572                            config,
573                            options,
574                            logger,
575                            header,
576                            len_end):
577     '''Execute the script build procedure in the product build directory.
578     
579     :param p_name_info tuple: (str, Config) => (product_name, product_info)
580     :param config Config: The global configuration
581     :param logger Logger: The logger instance to use for the display 
582                           and logging
583     :param header Str: the header to display when logging
584     :param len_end Int: the lenght of the the end of line (used in display)
585     :return: 1 if it fails, else 0.
586     :rtype: int
587     '''
588     p_name, p_info = p_name_info
589     
590     # Execute "sat configure", "sat make" and "sat install"
591     error_step = ""
592     
593     # Logging and sat command call for the script step
594     scrit_path_display = src.printcolors.printcLabel(p_info.compil_script)
595     log_step(logger, header, "SCRIPT " + scrit_path_display)
596     len_end_line = len_end + len(scrit_path_display)
597     res = sat.script(config.VARS.application + " --products " + p_name,
598                      verbose = 0,
599                      logger_add_link = logger)
600     log_res_step(logger, res)
601               
602     return res, len_end_line, error_step 
603
604     
605 def description():
606     '''method that is called when salomeTools is called with --help option.
607     
608     :return: The text to display for the compile command description.
609     :rtype: str
610     '''
611     return _("The compile command constructs the products of the application"
612              "\n\nexample:\nsat compile SALOME-master --products KERNEL,GUI,"
613              "MEDCOUPLING --clean_all")
614   
615 def run(args, runner, logger):
616     '''method that is called when salomeTools is called with compile parameter.
617     '''
618     # Parse the options
619     (options, args) = parser.parse_args(args)
620
621     # Warn the user if he invoked the clean_all option 
622     # without --products option
623     if (options.clean_all and 
624         options.products is None and 
625         not runner.options.batch):
626         rep = input(_("You used --clean_all without specifying a product"
627                           " are you sure you want to continue? [Yes/No] "))
628         if rep.upper() != _("YES").upper():
629             return 0
630         
631     # check that the command has been called with an application
632     src.check_config_has_application( runner.cfg )
633
634     # Print some informations
635     logger.write(_('Executing the compile commands in the build '
636                                 'directories of the products of '
637                                 'the application %s\n') % 
638                 src.printcolors.printcLabel(runner.cfg.VARS.application), 1)
639     
640     info = [
641             (_("SOURCE directory"),
642              os.path.join(runner.cfg.APPLICATION.workdir, 'SOURCES')),
643             (_("BUILD directory"),
644              os.path.join(runner.cfg.APPLICATION.workdir, 'BUILD'))
645             ]
646     src.print_info(logger, info)
647
648     # Get the list of all application products, and create its dependency graph
649     all_products_infos = src.product.get_products_infos(runner.cfg.APPLICATION.products,
650                                                         runner.cfg)
651     all_products_graph=get_dependencies_graph(all_products_infos)
652     #logger.write("Dependency graph of all application products : %s\n" % all_products_graph, 6)
653     DBG.write("Dependency graph of all application products : ", all_products_graph)
654
655     # Get the list of products we have to compile
656     products_infos = src.product.get_products_list(options, runner.cfg, logger)
657     products_list = [pi[0] for pi in products_infos]
658
659     logger.write("Product we have to compile (as specified by user) : %s\n" % products_list, 5)
660     if options.fathers:
661         # Extend the list with all recursive dependencies of the given products
662         visited=[]
663         for p_name in products_list:
664             visited=depth_search_graph(all_products_graph, p_name, visited)
665         products_list = visited
666
667     logger.write("Product list to compile with fathers : %s\n" % products_list, 5)
668     if options.children:
669         # Extend the list with all products that depends upon the given products
670         children=[]
671         for n in all_products_graph:
672             # for all products (that are not in products_list):
673             # if we we find a path from the product to the product list,
674             # then we product is a child and we add it to the children list 
675             if (n not in children) and (n not in products_list):
676                 if find_path_graph(all_products_graph, n, products_list):
677                     children = children + [n]
678         # complete products_list (the products we have to compile) with the list of children
679         products_list = products_list + children
680         logger.write("Product list to compile with children : %s\n" % products_list, 5)
681
682     # Sort the list of all products (topological sort).
683     # the products listed first do not depend upon products listed after
684     visited_nodes=[]
685     sorted_nodes=[]
686     for n in all_products_graph:
687         if n not in visited_nodes:
688             visited_nodes,sorted_nodes=depth_first_topo_graph(all_products_graph, n, visited_nodes,sorted_nodes)
689     logger.write("Complete dependency graph topological search (sorting): %s\n" % sorted_nodes, 6)
690
691 #   use the sorted list of all products to sort the list of products we have to compile
692     sorted_product_list=[]
693     for n in sorted_nodes:
694         if n in products_list:
695             sorted_product_list.append(n)
696     logger.write("Sorted list of products to compile : %s\n" % sorted_product_list, 5)
697
698     
699     # from the sorted list of products to compile, build a sorted list of products infos
700     #  a- create a dict to facilitate products_infos sorting
701     all_products_dict={}
702     for (pname,pinfo) in all_products_infos:
703         all_products_dict[pname]=(pname,pinfo)
704     #  b- build a sorted list of products infos in products_infos
705     products_infos=[]
706     for product in sorted_product_list:
707         products_infos.append(all_products_dict[product])
708
709     # for all products to compile, store in "depend_all" field the complete dependencies (recursive) 
710     # (will be used by check_dependencies funvtion)
711     for pi in products_infos:
712         dep_prod=[]
713         dep_prod=depth_search_graph(all_products_graph,pi[0], dep_prod)
714         pi[1]["depend_all"]=dep_prod[1:]
715         
716
717     # Call the function that will loop over all the products and execute
718     # the right command(s)
719     res = compile_all_products(runner, runner.cfg, options, products_infos, all_products_dict, logger)
720     
721     # Print the final state
722     nb_products = len(products_infos)
723     if res == 0:
724         final_status = "OK"
725     else:
726         final_status = "KO"
727    
728     logger.write(_("\nCompilation: %(status)s (%(valid_result)d/%(nb_products)d)\n") % \
729         { 'status': src.printcolors.printc(final_status), 
730           'valid_result': nb_products - res,
731           'nb_products': nb_products }, 1)    
732     
733     code = res
734     if code != 0:
735         code = 1
736     return code