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