vala: Allow read-only properties
[vala-gnome.git] / codegen / valaccodecompiler.vala
blob76549c94c9a3b16c31e44f3e3fb3cb0f64b51ac6
1 /* valaccodecompiler.vala
3 * Copyright (C) 2007-2009 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 {
29 public CCodeCompiler () {
32 /**
33 * Compile generated C code to object code and optionally link object
34 * files.
36 * @param context a code context
38 public void compile (CodeContext context, string? cc_command, string[] cc_options) {
39 string pc = "";
40 if (context.profile == Profile.GOBJECT) {
41 pc += " gobject-2.0";
43 foreach (string pkg in context.get_packages ()) {
44 if (context.pkg_config_exists (pkg)) {
45 pc += " " + pkg;
48 string? pkgflags;
49 if (pc.length > 0) {
50 pkgflags = context.pkg_config_compile_flags (pc);
51 if (pkgflags == null) {
52 return;
54 } else {
55 pkgflags = "";
58 // TODO compile the C code files in parallel
60 if (cc_command == null) {
61 cc_command = "cc";
63 string cmdline = cc_command;
64 if (context.debug) {
65 cmdline += " -g";
67 if (context.compile_only) {
68 cmdline += " -c";
69 } else if (context.output != null) {
70 string output = context.output;
71 if (context.directory != null && context.directory != "" && !Path.is_absolute (context.output)) {
72 output = "%s%c%s".printf (context.directory, Path.DIR_SEPARATOR, context.output);
74 cmdline += " -o " + Shell.quote (output);
77 /* we're only interested in non-pkg source files */
78 var source_files = context.get_source_files ();
79 foreach (SourceFile file in source_files) {
80 if (file.file_type == SourceFileType.SOURCE) {
81 cmdline += " " + Shell.quote (file.get_csource_filename ());
84 var c_source_files = context.get_c_source_files ();
85 foreach (string file in c_source_files) {
86 cmdline += " " + Shell.quote (file);
89 // add libraries after source files to fix linking
90 // with --as-needed and on Windows
91 cmdline += " " + pkgflags.strip ();
92 foreach (string cc_option in cc_options) {
93 cmdline += " " + Shell.quote (cc_option);
96 if (context.verbose_mode) {
97 stdout.printf ("%s\n", cmdline);
100 try {
101 int exit_status;
102 Process.spawn_command_line_sync (cmdline, null, null, out exit_status);
103 if (exit_status != 0) {
104 Report.error (null, "cc exited with status %d".printf (exit_status));
106 } catch (SpawnError e) {
107 Report.error (null, e.message);
110 /* remove generated C source and header files */
111 foreach (SourceFile file in source_files) {
112 if (file.file_type == SourceFileType.SOURCE) {
113 if (!context.save_csources) {
114 FileUtils.unlink (file.get_csource_filename ());