meson.build.o7QLX02
[roobuilder] / src / codegen / valaccodecompiler.vala
1 /* valaccodecompiler.vala
2  *
3  * Copyright (C) 2007-2009  Jürg Billeter
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, or (at your option) any later version.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
18  *
19  * Author:
20  *      Jürg Billeter <j@bitron.ch>
21  */
22
23 using GLib;
24
25 /**
26  * Interface to the C compiler.
27  */
28 public class Vala.CCodeCompiler {
29         public CCodeCompiler () {
30         }
31
32         /**
33          * Compile generated C code to object code and optionally link object
34          * files.
35          *
36          * @param context a code context
37          */
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";
42                 }
43                 foreach (string pkg in context.get_packages ()) {
44                         if (context.pkg_config_exists (pkg)) {
45                                 pc += " " + pkg;
46                         }
47                 }
48                 string? pkgflags;
49                 if (pc.length > 0) {
50                         pkgflags = context.pkg_config_compile_flags (pc);
51                         if (pkgflags == null) {
52                                 return;
53                         }
54                 } else {
55                         pkgflags = "";
56                 }
57
58                 // TODO compile the C code files in parallel
59
60                 if (cc_command == null) {
61                         cc_command = "cc";
62                 }
63                 string cmdline = cc_command;
64                 if (context.debug) {
65                         cmdline += " -g";
66                 }
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);
73                         }
74                         cmdline += " -o " + Shell.quote (output);
75                 }
76
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 ());
82                         }
83                 }
84                 var c_source_files = context.get_c_source_files ();
85                 foreach (string file in c_source_files) {
86                         cmdline += " " + Shell.quote (file);
87                 }
88
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);
94                 }
95
96                 if (context.verbose_mode) {
97                         //stdout.printf ("%s\n", cmdline);
98                 }
99
100                 int exit_status = 0;
101                 try {
102                         Process.spawn_command_line_sync (cmdline, null, null, out exit_status);
103                 } catch (Error e) {
104                         Report.error (null, e.message);
105                 }
106                 if (exit_status != 0) {
107                                 Report.error (null, "cc exited with status %d", exit_status);
108                         }
109                 
110
111                 /* remove generated C source and header files */
112                 if (!context.save_csources) {
113                         foreach (SourceFile file in source_files) {
114                                 if (file.file_type == SourceFileType.SOURCE) {
115                                         FileUtils.unlink (file.get_csource_filename ());
116                                 }
117                         }
118                 }
119         }
120 }