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