-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcompiler.vala
120 lines (109 loc) · 3.39 KB
/
compiler.vala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
* compiler.vala
*
* Copyright 2014 Jott <das.jott at gmail com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*
*/
public class Compiler
{
private string m_sCompiler = null;
private string m_sWorkingDir = null;
public Compiler(string sCompiler, string sWorkDir)
{
m_sCompiler = sCompiler;
m_sWorkingDir = sWorkDir;
}
public bool compile(string[]? pkgs=null, string[]? flags=null)
{
// never change these command lines!
string sPkgConfig;
bool ok = cmd( "pkg-config --cflags glib-2.0 gobject-2.0%s".printf(serialize(pkgs)), out sPkgConfig);
if (ok) {
ok = cmd( "%s%s -w -fPIC -c %s -I%s *.c".printf(m_sCompiler, serialize(flags), sPkgConfig, JniHeaderPath) );
}
return ok;
}
public bool link(string sLibName, string[]? pkgs=null, string[]? libs=null)
{
// never change these command lines!
string sPkgConfig;
bool ok = cmd( "pkg-config --libs --static glib-2.0 gobject-2.0%s".printf(serialize(pkgs)), out sPkgConfig);
if (ok) {
ok = cmd( "%s -w -shared -o lib%s.so *.o %s%s".printf(m_sCompiler, sLibName, sPkgConfig, serialize(libs," -l")) );
}
return ok;
}
public bool make(string sLibName, string[] pkgs)
{
string sPkgConfig;
bool ok = cmd( "pkg-config --cflags --libs --static glib-2.0 gobject-2.0%s".printf(serialize(pkgs)), out sPkgConfig );
if (ok) {
ok = cmd( "%s -fPIC -shared -o lib%s.so %s -I%s *.c".printf(m_sCompiler, sLibName, sPkgConfig, JniHeaderPath) );
} return ok;
}
public bool clean()
{
//return cmd( "rm -f *.o lib%s.so".printf(LibName) );
return false;
}
// serializes array
private string serialize(string[]? pkgs, string? sDiv=null)
{
string res = "";
if (pkgs != null) {
if (sDiv == null) { sDiv = " "; }
foreach (string pkg in pkgs) {
res += sDiv;
res += pkg;
}
}
return res;
}
// gets the path to jni.h
private string JniHeaderPath
{
get {
if (m_jni_header_path == "") {
string sFind;
// TODO: Make this OS independent
if ( cmd("find /usr/lib -name \"jni.h\"", out sFind) ) {
string[] asPaths = sFind.split("\n");
foreach (string sPath in asPaths) {
if (sPath.has_suffix("/jni.h")) {
m_jni_header_path = Path.get_dirname(sPath);
break;
}
}
}
if (m_jni_header_path == "") {
stderr.printf("error: could not find jni.h\n");
}
}
return m_jni_header_path;
}
}
private string m_jni_header_path = "";
private bool cmd(string sCmd, out string sStdOut=null)
{
var shell = new Sys();
bool ok = shell.spawn_cmd(m_sWorkingDir, sCmd);
sStdOut = shell.StdOut;
return ok;
}
}