Salome HOME
First version of command compile
[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     # Instantiate the class that manages all the construction commands
122     # like cmake, make, make install, make test, environment management, etc...
123     builder = src.compilation.Builder(config, logger, p_info)
124     
125     # Prepare the environment
126     log_step(logger, header, "PREPARE ENV")
127     res_prepare = builder.prepare()
128     log_res_step(logger, res_prepare)
129     
130     # Execute buildconfigure, configure if the product is autotools
131     # Execute cmake if the product is cmake
132     res = 0
133     if src.product.product_is_autotools(p_info):
134         log_step(logger, header, "BUILDCONFIGURE")
135         res_bc = builder.build_configure()
136         log_res_step(logger, res_bc)
137         res += res_bc
138         log_step(logger, header, "CONFIGURE")
139         res_c = builder.configure(conf_option)
140         log_res_step(logger, res_c)
141         res += res_c
142     if src.product.product_is_cmake(p_info):
143         log_step(logger, header, "CMAKE")
144         res_cm = builder.cmake(conf_option)
145         log_res_step(logger, res_cm)
146         res += res_cm
147     
148     # Log the result
149     if res > 0:
150         logger.write("\r%s%s" % (header, " " * 20), 3)
151         logger.write("\r" + header + src.printcolors.printcError("KO"))
152         logger.write("==== %(KO)s in configuration of %(name)s \n" %
153             { "name" : p_name , "KO" : src.printcolors.printcInfo("ERROR")}, 4)
154         logger.flush()
155     else:
156         logger.write("\r%s%s" % (header, " " * 20), 3)
157         logger.write("\r" + header + src.printcolors.printcSuccess("OK"))
158         logger.write("==== %s \n" % src.printcolors.printcInfo("OK"), 4)
159         logger.write("==== Configuration of %(name)s %(OK)s \n" %
160             { "name" : p_name , "OK" : src.printcolors.printcInfo("OK")}, 4)
161         logger.flush()
162     logger.write("\n", 3, False)
163
164     return res
165
166 def description():
167     '''method that is called when salomeTools is called with --help option.
168     
169     :return: The text to display for the configure command description.
170     :rtype: str
171     '''
172     return _("The configure command executes in the build directory"
173              " the configure commands corresponding to the compilation mode"
174              " of the application products.\nThe possible compilation modes"
175              " are \"cmake\", \"autotools\", or a script.\n\nHere are the "
176              "commands to be run :\nautotools: build_configure and configure\n"
177              "cmake: cmake\nscript: N\A")
178   
179 def run(args, runner, logger):
180     '''method that is called when salomeTools is called with make parameter.
181     '''
182     
183     # Parse the options
184     (options, args) = parser.parse_args(args)
185
186     # check that the command has been called with an application
187     src.check_config_has_application( runner.cfg )
188
189     # Get the list of products to treat
190     products_infos = get_products_list(options, runner.cfg, logger)
191     
192     # Print some informations
193     logger.write(_('Configuring the sources of the application %s\n') % 
194                 src.printcolors.printcLabel(runner.cfg.VARS.application), 1)
195     
196     info = [(_("BUILD directory"),
197              os.path.join(runner.cfg.APPLICATION.workdir, 'BUILD'))]
198     src.print_info(logger, info)
199     
200     # Call the function that will loop over all the products and execute
201     # the right command(s)
202     if options.option is None:
203         options.option = ""
204     res = configure_all_products(runner.cfg, products_infos, options.option, logger)
205     
206     # Print the final state
207     nb_products = len(products_infos)
208     if res == 0:
209         final_status = "OK"
210     else:
211         final_status = "KO"
212    
213     logger.write(_("\nConfiguration: %(status)s (%(valid_result)d/%(nb_products)d)\n") % \
214         { 'status': src.printcolors.printc(final_status), 
215           'valid_result': nb_products - res,
216           'nb_products': nb_products }, 1)    
217     
218     return res