2008-06-16 Abderrahim Kitouni <a.kitouni@gmail.com>
[anjuta-git-plugin.git] / plugins / build / build_project.c
blob20a106cb86d7c216e89456a5d7f91f3c44c0e1de
1 /*
2 build_project.c
3 Copyright (C) 2000 Kh. Naba Kumar Singh
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program 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
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
23 #include <sys/stat.h>
24 #include <unistd.h>
25 #include <sys/types.h>
26 #include <sys/wait.h>
27 #include <signal.h>
28 #include <string.h>
29 #include <time.h>
30 #include <errno.h>
32 #include <gnome.h>
33 #include "anjuta.h"
34 #include "build_file.h"
35 #include "launcher.h"
36 #include "build_project.h"
37 #include "clean_project.h"
39 /* Private */
40 static void install_as_root (GtkWidget* button, gpointer data);
41 static void install_as_user (GtkWidget* button, gpointer data);
43 void
44 build_project ()
46 gchar* src_dir;
48 if(app->project_dbase->is_saved == FALSE
49 && app->project_dbase->project_is_open==TRUE)
51 gboolean ret;
52 ret = project_dbase_save_project(app->project_dbase);
53 if (ret == FALSE)
54 return;
57 src_dir = project_dbase_get_module_dir (app->project_dbase, MODULE_SOURCE);
58 if (src_dir)
60 gchar *prj_name, *cmd;
62 /* perform a clean, if required before the build */
63 if (app->project_dbase->clean_before_build == TRUE)
65 cmd = command_editor_get_command (app->command_editor,
66 COMMAND_CLEAN_BUILD_MODULE);
67 app->project_dbase->clean_before_build = FALSE;
69 else
71 cmd = command_editor_get_command (app->command_editor,
72 COMMAND_BUILD_MODULE);
74 if (cmd == NULL)
76 anjuta_warning (_("Unable to build module. Check Settings->Commands."));
77 g_free (src_dir);
78 return;
80 chdir (src_dir);
81 anjuta_set_execution_dir(src_dir);
82 g_free (src_dir);
84 if(anjuta_preferences_get_int(ANJUTA_PREFERENCES (app->preferences),
85 BUILD_OPTION_AUTOSAVE))
87 anjuta_save_all_files();
90 if (build_execute_command (cmd) == FALSE)
92 g_free (cmd);
93 return;
95 anjuta_update_app_status (TRUE, _("Build Project"));
96 an_message_manager_clear (app->messages, MESSAGE_BUILD);
97 an_message_manager_append (app->messages, _("Building source directory of the Project: "),
98 MESSAGE_BUILD);
99 prj_name = project_dbase_get_proj_name (app->project_dbase);
100 an_message_manager_append (app->messages, prj_name, MESSAGE_BUILD);
101 an_message_manager_append (app->messages, " ...\n", MESSAGE_BUILD);
102 an_message_manager_append (app->messages, cmd, MESSAGE_BUILD);
103 an_message_manager_append (app->messages, "\n", MESSAGE_BUILD);
104 an_message_manager_show (app->messages, MESSAGE_BUILD);
105 g_free (cmd);
106 g_free (prj_name);
108 else
110 /* Single file build */
111 build_file ();
115 void
116 build_all_project ()
118 gchar *cmd, *prj_name;
120 if(app->project_dbase->is_saved == FALSE
121 && app->project_dbase->project_is_open==TRUE)
123 gboolean ret;
124 ret = project_dbase_save_project(app->project_dbase);
125 if (ret == FALSE)
126 return;
129 if (app->project_dbase->project_is_open)
131 /* perform a clean, if required before the build */
132 if (app->project_dbase->clean_before_build == TRUE)
134 cmd = command_editor_get_command (app->command_editor,
135 COMMAND_CLEAN_BUILD_PROJECT);
136 app->project_dbase->clean_before_build = FALSE;
138 else
140 cmd = command_editor_get_command (app->command_editor,
141 COMMAND_BUILD_PROJECT);
143 if (cmd == NULL)
145 anjuta_warning (_("Unable to build Project. Check Settings->Commands."));
146 return;
148 chdir (app->project_dbase->top_proj_dir);
149 anjuta_set_execution_dir(app->project_dbase->top_proj_dir);
151 if(anjuta_preferences_get_int (ANJUTA_PREFERENCES (app->preferences),
152 BUILD_OPTION_AUTOSAVE))
153 anjuta_save_all_files();
155 if (build_execute_command (cmd) == FALSE)
157 g_free (cmd);
158 return;
160 anjuta_update_app_status (TRUE, _("Build Project"));
161 an_message_manager_clear (app->messages, MESSAGE_BUILD);
162 an_message_manager_append (app->messages,
163 _("Building the whole Project: "),
164 MESSAGE_BUILD);
165 prj_name = project_dbase_get_proj_name (app->project_dbase);
166 an_message_manager_append (app->messages, prj_name, MESSAGE_BUILD);
167 an_message_manager_append (app->messages, " ...\n", MESSAGE_BUILD);
168 an_message_manager_append (app->messages, cmd, MESSAGE_BUILD);
169 an_message_manager_append (app->messages, "\n", MESSAGE_BUILD);
170 an_message_manager_show (app->messages, MESSAGE_BUILD);
171 g_free (cmd);
172 g_free (prj_name);
176 void
177 build_dist_project ()
179 gchar *cmd, *prj_name;
181 if(app->project_dbase->is_saved == FALSE
182 && app->project_dbase->project_is_open==TRUE)
184 gboolean ret;
185 ret = project_dbase_save_project(app->project_dbase);
186 if (ret == FALSE)
187 return;
190 if (app->project_dbase->project_is_open)
192 cmd = command_editor_get_command (app->command_editor, COMMAND_BUILD_TARBALL);
193 if (cmd == NULL)
195 anjuta_warning (_
196 ("Unable to build tarball. Check Settings->Commands."));
197 return;
199 chdir (app->project_dbase->top_proj_dir);
200 anjuta_set_execution_dir(app->project_dbase->top_proj_dir);
202 if(anjuta_preferences_get_int (ANJUTA_PREFERENCES (app->preferences),
203 BUILD_OPTION_AUTOSAVE))
204 anjuta_save_all_files();
206 if (build_execute_command (cmd) == FALSE)
208 g_free (cmd);
209 return;
211 anjuta_update_app_status (TRUE, _("Build Distribution"));
212 an_message_manager_clear (app->messages, MESSAGE_BUILD);
213 an_message_manager_append (app->messages,
215 ("Building the distribution package of the Project: "),
216 MESSAGE_BUILD);
217 prj_name = project_dbase_get_proj_name (app->project_dbase);
218 an_message_manager_append (app->messages, prj_name, MESSAGE_BUILD);
219 an_message_manager_append (app->messages, " ...\n", MESSAGE_BUILD);
220 an_message_manager_append (app->messages, cmd, MESSAGE_BUILD);
221 an_message_manager_append (app->messages, "\n", MESSAGE_BUILD);
222 an_message_manager_show (app->messages, MESSAGE_BUILD);
223 g_free (cmd);
224 g_free (prj_name);
228 void
229 build_install_project ()
231 if(app->project_dbase->is_saved == FALSE
232 && app->project_dbase->project_is_open==TRUE)
234 gboolean ret;
235 ret = project_dbase_save_project(app->project_dbase);
236 if (ret == FALSE)
237 return;
240 if (app->project_dbase->project_is_open) {
241 GtkWidget *dialog;
243 dialog = gtk_message_dialog_new (GTK_WINDOW (app),
244 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
245 GTK_MESSAGE_QUESTION,
246 GTK_BUTTONS_YES_NO,
247 _("Do you prefer installing as root ?"));
249 if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_YES)
250 install_as_root (NULL, NULL);
251 else
252 install_as_user (NULL, NULL);
253 gtk_widget_destroy(dialog);
257 void
258 build_autogen_project ()
260 gchar *cmd, *prj_name;
262 if(app->project_dbase->is_saved == FALSE
263 && app->project_dbase->project_is_open==TRUE)
265 gboolean ret;
266 ret = project_dbase_save_project(app->project_dbase);
267 if (ret == FALSE)
268 return;
271 if (app->project_dbase->project_is_open)
273 chdir (app->project_dbase->top_proj_dir);
274 anjuta_set_execution_dir(app->project_dbase->top_proj_dir);
275 if (file_is_executable ("./autogen.sh"))
277 cmd = g_strdup ("./autogen.sh");
279 else
281 cmd = command_editor_get_command (app->command_editor, COMMAND_BUILD_AUTOGEN);
282 if (cmd == NULL)
284 anjuta_warning (_
285 ("Unable to auto generate. Check Settings->Commands."));
286 return;
290 if (anjuta_preferences_get_int (ANJUTA_PREFERENCES (app->preferences),
291 BUILD_OPTION_AUTOSAVE))
292 anjuta_save_all_files();
294 if (build_execute_command (cmd) == FALSE)
296 g_free (cmd);
297 return;
299 anjuta_update_app_status (TRUE, _("Auto generate Project"));
300 an_message_manager_clear (app->messages, MESSAGE_BUILD);
301 an_message_manager_append (app->messages,
302 _("Auto generating the Project: "),
303 MESSAGE_BUILD);
304 prj_name = project_dbase_get_proj_name (app->project_dbase);
305 an_message_manager_append (app->messages, prj_name, MESSAGE_BUILD);
306 an_message_manager_append (app->messages, " ...\n", MESSAGE_BUILD);
307 an_message_manager_append (app->messages, cmd, MESSAGE_BUILD);
308 an_message_manager_append (app->messages, "\n", MESSAGE_BUILD);
309 an_message_manager_show (app->messages, MESSAGE_BUILD);
310 g_free (cmd);
311 g_free (prj_name);
315 static void
316 install_as_root (GtkWidget* button, gpointer data)
318 gchar *cmd, *build_cmd, *prj_name;
319 build_cmd = command_editor_get_command (app->command_editor,
320 COMMAND_BUILD_INSTALL);
321 if (build_cmd == NULL)
323 anjuta_warning (_
324 ("Unable to install Project. Check Settings->Commands."));
325 return;
327 cmd = g_strconcat ("su --command='", build_cmd, "'", NULL);
328 g_free(build_cmd);
330 chdir (app->project_dbase->top_proj_dir);
331 anjuta_set_execution_dir(app->project_dbase->top_proj_dir);
333 if(anjuta_preferences_get_int (ANJUTA_PREFERENCES (app->preferences),
334 BUILD_OPTION_AUTOSAVE))
336 anjuta_save_all_files();
339 anjuta_update_app_status (TRUE, _("Install Project"));
340 an_message_manager_clear (app->messages, MESSAGE_BUILD);
341 an_message_manager_append (app->messages, _("Installing the Project: "),
342 MESSAGE_BUILD);
343 prj_name = project_dbase_get_proj_name (app->project_dbase);
344 an_message_manager_append (app->messages, prj_name, MESSAGE_BUILD);
345 an_message_manager_append (app->messages, " ...\n", MESSAGE_BUILD);
346 an_message_manager_append (app->messages, cmd, MESSAGE_BUILD);
347 an_message_manager_append (app->messages, "\n", MESSAGE_BUILD);
348 an_message_manager_show (app->messages, MESSAGE_BUILD);
349 g_free (prj_name);
350 build_execute_command (cmd);
351 g_free (cmd);
354 static void
355 install_as_user (GtkWidget* button, gpointer data)
357 gchar *cmd, *prj_name;
358 cmd = command_editor_get_command (app->command_editor, COMMAND_BUILD_INSTALL);
359 if (cmd == NULL)
361 anjuta_warning (_
362 ("Unable to install Project. Check Settings->Commands."));
363 return;
365 chdir (app->project_dbase->top_proj_dir);
366 anjuta_set_execution_dir(app->project_dbase->top_proj_dir);
368 if(anjuta_preferences_get_int (ANJUTA_PREFERENCES (app->preferences),
369 BUILD_OPTION_AUTOSAVE))
371 anjuta_save_all_files();
374 anjuta_update_app_status (TRUE, _("Install Project"));
375 an_message_manager_clear (app->messages, MESSAGE_BUILD);
376 an_message_manager_append (app->messages, _("Installing the Project: "),
377 MESSAGE_BUILD);
378 prj_name = project_dbase_get_proj_name (app->project_dbase);
379 an_message_manager_append (app->messages, prj_name, MESSAGE_BUILD);
380 an_message_manager_append (app->messages, " ...\n", MESSAGE_BUILD);
381 an_message_manager_append (app->messages, cmd, MESSAGE_BUILD);
382 an_message_manager_append (app->messages, "\n", MESSAGE_BUILD);
383 an_message_manager_show (app->messages, MESSAGE_BUILD);
384 g_free (prj_name);
385 build_execute_command (cmd);
386 g_free (cmd);