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