update for 0.3.3 release
[vala-lang.git] / gobject / valaccodecompiler.vala
blob06f4a7380a141e54f35158614102282b0eb1960b
1 /* valaccodecompiler.vala
3 * Copyright (C) 2007-2008 Jürg Billeter
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, or (at your option) any later version.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 * Author:
20 * Jürg Billeter <j@bitron.ch>
23 using GLib;
25 /**
26 * Interface to the C compiler.
28 public class Vala.CCodeCompiler : Object {
29 public CCodeCompiler () {
32 static bool package_exists(string package_name) {
33 string pc = "pkg-config --exists " + package_name;
34 int exit_status;
36 try {
37 Process.spawn_command_line_sync (pc, null, null, out exit_status);
38 return (0 == exit_status);
39 } catch (SpawnError e) {
40 Report.error (null, e.message);
41 return false;
45 /**
46 * Compile generated C code to object code and optionally link object
47 * files.
49 * @param context a code context
51 [NoArrayLength]
52 public void compile (CodeContext context, string? cc_command, string[] cc_options) {
53 string pc = "pkg-config --cflags";
54 if (!context.compile_only) {
55 pc += " --libs";
57 pc += " gobject-2.0";
58 if (context.thread) {
59 pc += " gthread-2.0";
61 foreach (string pkg in context.get_packages ()) {
62 if (package_exists (pkg))
63 pc += " " + pkg;
65 string pkgflags;
66 int exit_status;
67 try {
68 Process.spawn_command_line_sync (pc, out pkgflags, null, out exit_status);
69 if (exit_status != 0) {
70 Report.error (null, "pkg-config exited with status %d".printf (exit_status));
71 return;
73 } catch (SpawnError e) {
74 Report.error (null, e.message);
75 return;
78 // TODO compile the C code files in parallel
80 if (cc_command == null) {
81 cc_command = "cc";
83 string cmdline = cc_command;
84 if (context.debug) {
85 cmdline += " -g";
87 if (context.compile_only) {
88 cmdline += " -c";
89 } else if (context.output != null) {
90 string output = context.output;
91 if (context.directory != null && context.directory != "") {
92 output = "%s/%s".printf (context.directory, context.output);
94 cmdline += " -o " + Shell.quote (output);
96 cmdline += " " + pkgflags;
97 foreach (string cc_option in cc_options) {
98 cmdline += " " + cc_option;
101 /* make sure include files can be found if -d is used */
102 if (context.directory != null && context.directory != "") {
103 cmdline += " -I" + Shell.quote (context.directory);
106 /* we're only interested in non-pkg source files */
107 var source_files = context.get_source_files ();
108 foreach (SourceFile file in source_files) {
109 if (!file.external_package) {
110 cmdline += " " + Shell.quote (file.get_csource_filename ());
113 var c_source_files = context.get_c_source_files ();
114 foreach (string file in c_source_files) {
115 cmdline += " " + Shell.quote (file);
118 try {
119 Process.spawn_command_line_sync (cmdline, null, null, out exit_status);
120 if (exit_status != 0) {
121 Report.error (null, "cc exited with status %d".printf (exit_status));
123 } catch (SpawnError e) {
124 Report.error (null, e.message);
127 /* remove generated C source and header files */
128 foreach (SourceFile file in source_files) {
129 if (!file.external_package) {
130 if (!context.save_csources)
131 FileUtils.unlink (file.get_csource_filename ());
132 if (!context.save_cheaders)
133 FileUtils.unlink (file.get_cheader_filename ());