Salome HOME
#8577 extension du domaine des properties
[tools/sat.git] / commands / configure.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
21 import src
22
23 # Define all possible option for configure command :  sat configure <options>
24 parser = src.options.Options()
25 parser.add_option('p', 'products', 'list2', 'products',
26     _('Optional: products to configure. This option can be'
27     ' passed several time to configure several products.'))
28 parser.add_option('o', 'option', 'string', 'option',
29     _('Optional: Option to add to the configure or cmake command.'), "")
30
31
32 def log_step(logger, header, step):
33     logger.write("\r%s%s" % (header, " " * 20), 3)
34     logger.write("\r%s%s" % (header, step), 3)
35     logger.write("\n==== %s \n" % src.printcolors.printcInfo(step), 4)
36     logger.flush()
37
38 def log_res_step(logger, res):
39     if res == 0:
40         logger.write("%s \n" % src.printcolors.printcSuccess("OK"), 4)
41         logger.flush()
42     else:
43         logger.write("%s \n" % src.printcolors.printcError("KO"), 4)
44         logger.flush()
45
46 def configure_all_products(config, products_infos, conf_option, logger):
47     '''Execute the proper configuration commands 
48        in each product build directory.
49
50     :param config Config: The global configuration
51     :param products_info list: List of 
52                                  (str, Config) => (product_name, product_info)
53     :param conf_option str: The options to add to the command
54     :param logger Logger: The logger instance to use for the display and logging
55     :return: the number of failing commands.
56     :rtype: int
57     '''
58     res = 0
59     for p_name_info in products_infos:
60         res_prod = configure_product(p_name_info, conf_option, config, logger)
61         if res_prod != 0:
62             res += 1 
63     return res
64
65 def configure_product(p_name_info, conf_option, config, logger):
66     '''Execute the proper configuration command(s) 
67        in the product build directory.
68     
69     :param p_name_info tuple: (str, Config) => (product_name, product_info)
70     :param conf_option str: The options to add to the command
71     :param config Config: The global configuration
72     :param logger Logger: The logger instance to use for the display 
73                           and logging
74     :return: 1 if it fails, else 0.
75     :rtype: int
76     '''
77     
78     p_name, p_info = p_name_info
79     
80     # Logging
81     logger.write("\n", 4, False)
82     logger.write("################ ", 4)
83     header = _("Configuration of %s") % src.printcolors.printcLabel(p_name)
84     header += " %s " % ("." * (20 - len(p_name)))
85     logger.write(header, 3)
86     logger.write("\n", 4, False)
87     logger.flush()
88
89     # Do nothing if he product is not compilable
90     if ("properties" in p_info and "compilation" in p_info.properties and 
91                                         p_info.properties.compilation == "no"):
92         log_step(logger, header, "ignored")
93         logger.write("\n", 3, False)
94         return 0
95
96     # Instantiate the class that manages all the construction commands
97     # like cmake, make, make install, make test, environment management, etc...
98     builder = src.compilation.Builder(config, logger, p_info)
99     
100     # Prepare the environment
101     log_step(logger, header, "PREPARE ENV")
102     res_prepare = builder.prepare()
103     log_res_step(logger, res_prepare)
104     
105     # Execute buildconfigure, configure if the product is autotools
106     # Execute cmake if the product is cmake
107     res = 0
108     if src.product.product_is_autotools(p_info):
109         log_step(logger, header, "BUILDCONFIGURE")
110         res_bc = builder.build_configure()
111         log_res_step(logger, res_bc)
112         res += res_bc
113         log_step(logger, header, "CONFIGURE")
114         res_c = builder.configure(conf_option)
115         log_res_step(logger, res_c)
116         res += res_c
117     if src.product.product_is_cmake(p_info):
118         log_step(logger, header, "CMAKE")
119         res_cm = builder.cmake(conf_option)
120         log_res_step(logger, res_cm)
121         res += res_cm
122     
123     # Log the result
124     if res > 0:
125         logger.write("\r%s%s" % (header, " " * 20), 3)
126         logger.write("\r" + header + src.printcolors.printcError("KO"))
127         logger.write("==== %(KO)s in configuration of %(name)s \n" %
128             { "name" : p_name , "KO" : src.printcolors.printcInfo("ERROR")}, 4)
129         logger.flush()
130     else:
131         logger.write("\r%s%s" % (header, " " * 20), 3)
132         logger.write("\r" + header + src.printcolors.printcSuccess("OK"))
133         logger.write("==== %s \n" % src.printcolors.printcInfo("OK"), 4)
134         logger.write("==== Configuration of %(name)s %(OK)s \n" %
135             { "name" : p_name , "OK" : src.printcolors.printcInfo("OK")}, 4)
136         logger.flush()
137     logger.write("\n", 3, False)
138
139     return res
140
141 def description():
142     '''method that is called when salomeTools is called with --help option.
143     
144     :return: The text to display for the configure command description.
145     :rtype: str
146     '''
147     return _("The configure command executes in the build directory"
148              " the configure commands corresponding\nto the compilation mode"
149              " of the application products.\nThe possible compilation modes"
150              " are \"cmake\", \"autotools\", or a script.\n\nHere are the "
151              "commands to be run :\nautotools: build_configure and configure\n"
152              "cmake: cmake\nscript: do nothing\n\nexample:\nsat configure "
153              "SALOME-master --products KERNEL,GUI,PARAVIS")
154   
155 def run(args, runner, logger):
156     '''method that is called when salomeTools is called with make parameter.
157     '''
158     
159     # Parse the options
160     (options, args) = parser.parse_args(args)
161
162     # check that the command has been called with an application
163     src.check_config_has_application( runner.cfg )
164
165     # Get the list of products to treat
166     products_infos = src.product.get_products_list(options, runner.cfg, logger)
167     
168     # Print some informations
169     logger.write(_('Configuring the sources of the application %s\n') % 
170                 src.printcolors.printcLabel(runner.cfg.VARS.application), 1)
171     
172     info = [(_("BUILD directory"),
173              os.path.join(runner.cfg.APPLICATION.workdir, 'BUILD'))]
174     src.print_info(logger, info)
175     
176     # Call the function that will loop over all the products and execute
177     # the right command(s)
178     if options.option is None:
179         options.option = ""
180     res = configure_all_products(runner.cfg, products_infos, options.option, logger)
181     
182     # Print the final state
183     nb_products = len(products_infos)
184     if res == 0:
185         final_status = "OK"
186     else:
187         final_status = "KO"
188    
189     logger.write(_("\nConfiguration: %(status)s (%(valid_result)d/%(nb_products)d)\n") % \
190         { 'status': src.printcolors.printc(final_status), 
191           'valid_result': nb_products - res,
192           'nb_products': nb_products }, 1)    
193     
194     return res