forked from xamarin/xamarin-macios
-
Notifications
You must be signed in to change notification settings - Fork 1
/
CompilerFlags.cs
334 lines (287 loc) · 9.41 KB
/
CompilerFlags.cs
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Xamarin.Bundler;
namespace Xamarin.Utils
{
public class CompilerFlags
{
public Application Application { get { return Target.App; } }
public Target Target;
public HashSet<string> Frameworks; // if a file, "-F /path/to/X --framework X" and added to Inputs, otherwise "--framework X".
public HashSet<string> WeakFrameworks;
public HashSet<string> LinkWithLibraries; // X, added to Inputs
public HashSet<string> ForceLoadLibraries; // -force_load X, added to Inputs
public HashSet<string> OtherFlags; // X
public List<string> InitialOtherFlags; // same as OtherFlags, only that they're the first argument(s) to clang (because order matters!). This is a list to preserve order (fifo).
public HashSet<string> Defines; // -DX
public HashSet<string> UnresolvedSymbols; // -u X
public HashSet<string> SourceFiles; // X, added to Inputs
// Here we store a list of all the file-system based inputs
// to the compiler. This is used when determining if the
// compiler needs to be called in the first place (dependency
// tracking).
public List<string> Inputs;
public CompilerFlags (Target target)
{
if (target == null)
throw new ArgumentNullException (nameof (target));
this.Target = target;
}
public HashSet<string> AllLibraries {
get {
var rv = new HashSet<string> ();
if (LinkWithLibraries != null)
rv.UnionWith (LinkWithLibraries);
if (ForceLoadLibraries != null)
rv.UnionWith (ForceLoadLibraries);
return rv;
}
}
public void ReferenceSymbols (IEnumerable<Symbol> symbols)
{
if (UnresolvedSymbols == null)
UnresolvedSymbols = new HashSet<string> ();
foreach (var symbol in symbols)
UnresolvedSymbols.Add (symbol.Prefix + symbol.Name);
}
public void AddDefine (string define)
{
if (Defines == null)
Defines = new HashSet<string> ();
Defines.Add (define);
}
public void AddLinkWith (string library, bool force_load = false)
{
if (LinkWithLibraries == null)
LinkWithLibraries = new HashSet<string> ();
if (ForceLoadLibraries == null)
ForceLoadLibraries = new HashSet<string> ();
if (force_load) {
ForceLoadLibraries.Add (library);
} else {
LinkWithLibraries.Add (library);
}
}
public void AddLinkWith (IEnumerable<string> libraries, bool force_load = false)
{
if (libraries == null)
return;
foreach (var lib in libraries)
AddLinkWith (lib, force_load);
}
public void AddSourceFile (string file)
{
if (SourceFiles == null)
SourceFiles = new HashSet<string> ();
SourceFiles.Add (file);
}
public void AddStandardCppLibrary ()
{
if (Driver.XcodeVersion.Major < 10)
return;
// Xcode 10 doesn't ship with libstdc++, so use libc++ instead.
AddOtherFlag ("-stdlib=libc++");
}
public void AddOtherInitialFlag (string flag)
{
if (InitialOtherFlags == null)
InitialOtherFlags = new List<string> ();
InitialOtherFlags.Add (flag);
}
public void AddOtherFlag (string flag)
{
if (OtherFlags == null)
OtherFlags = new HashSet<string> ();
OtherFlags.Add (flag);
}
public void AddOtherFlags (IEnumerable<string> flags)
{
if (flags == null)
return;
if (OtherFlags == null)
OtherFlags = new HashSet<string> ();
OtherFlags.UnionWith (flags);
}
public void LinkWithMono ()
{
var mode = Target.App.LibMonoLinkMode;
switch (mode) {
case AssemblyBuildTarget.DynamicLibrary:
case AssemblyBuildTarget.StaticObject:
AddLinkWith (Application.GetLibMono (mode));
break;
case AssemblyBuildTarget.Framework:
AddFramework (Application.GetLibMono (mode));
break;
default:
throw ErrorHelper.CreateError (100, "Invalid assembly build target: '{0}'. Please file a bug report with a test case (https://github.com/xamarin/xamarin-macios/issues/new).", mode);
}
AddOtherFlag ("-lz");
AddOtherFlag ("-liconv");
}
public void LinkWithXamarin ()
{
var mode = Target.App.LibXamarinLinkMode;
switch (mode) {
case AssemblyBuildTarget.DynamicLibrary:
case AssemblyBuildTarget.StaticObject:
AddLinkWith (Application.GetLibXamarin (mode));
break;
case AssemblyBuildTarget.Framework:
AddFramework (Application.GetLibXamarin (mode));
break;
default:
throw ErrorHelper.CreateError (100, "Invalid assembly build target: '{0}'. Please file a bug report with a test case (https://github.com/xamarin/xamarin-macios/issues/new).", mode);
}
AddFramework ("Foundation");
AddOtherFlag ("-lz");
if (Application.Platform != ApplePlatform.WatchOS && Application.Platform != ApplePlatform.TVOS)
AddFramework ("CFNetwork"); // required by xamarin_start_wwan
}
public void AddFramework (string framework)
{
if (Frameworks == null)
Frameworks = new HashSet<string> ();
Frameworks.Add (framework);
}
public void AddFrameworks (IEnumerable<string> frameworks, IEnumerable<string> weak_frameworks)
{
if (frameworks != null) {
if (Frameworks == null)
Frameworks = new HashSet<string> ();
Frameworks.UnionWith (frameworks);
}
if (weak_frameworks != null) {
if (WeakFrameworks == null)
WeakFrameworks = new HashSet<string> ();
WeakFrameworks.UnionWith (weak_frameworks);
}
}
public void Prepare ()
{
// Check for system frameworks that are only available in newer iOS versions,
// (newer than the deployment target), in which case those need to be weakly linked.
if (Frameworks != null) {
if (WeakFrameworks == null)
WeakFrameworks = new HashSet<string> ();
foreach (var fwk in Frameworks) {
if (!fwk.EndsWith (".framework", StringComparison.OrdinalIgnoreCase) && !fwk.EndsWith (".xcframework", StringComparison.OrdinalIgnoreCase)) {
var add_to = WeakFrameworks;
var framework = Driver.GetFrameworks (Application).Find (fwk);
if (framework != null) {
if (framework.Version > Application.SdkVersion)
continue;
add_to = Application.DeploymentTarget >= framework.Version ? Frameworks : WeakFrameworks;
}
add_to.Add (fwk);
} else {
// believe what we got about user frameworks.
}
}
// Make sure frameworks aren't duplicated, favoring any weak frameworks.
Frameworks.ExceptWith (WeakFrameworks);
}
// force_load libraries take precedence, so remove the libraries
// we need to force load from the list of libraries we just load.
if (LinkWithLibraries != null)
LinkWithLibraries.ExceptWith (ForceLoadLibraries);
}
void AddInput (string file)
{
if (Inputs == null)
return;
Inputs.Add (file);
}
public void WriteArguments (StringBuilder args)
{
Prepare ();
if (InitialOtherFlags != null) {
var idx = 0;
foreach (var flag in InitialOtherFlags) {
args.Insert (idx, flag);
idx += flag.Length;
args.Insert (idx, " ");
idx++;
}
}
ProcessFrameworksForArguments (args);
if (LinkWithLibraries != null) {
foreach (var lib in LinkWithLibraries) {
args.Append (' ').Append (StringUtils.Quote (lib));
AddInput (lib);
}
}
if (ForceLoadLibraries != null) {
foreach (var lib in ForceLoadLibraries) {
args.Append (" -force_load ").Append (StringUtils.Quote (lib));
AddInput (lib);
}
}
if (OtherFlags != null) {
foreach (var flag in OtherFlags)
args.Append (' ').Append (flag);
}
if (Defines != null) {
foreach (var define in Defines)
args.Append (" -D").Append (define);
}
if (UnresolvedSymbols != null) {
foreach (var symbol in UnresolvedSymbols)
args.Append (" -u ").Append (StringUtils.Quote (symbol));
}
if (SourceFiles != null) {
foreach (var src in SourceFiles) {
args.Append (' ').Append (StringUtils.Quote (src));
AddInput (src);
}
}
}
void ProcessFrameworksForArguments (StringBuilder args)
{
bool any_user_framework = false;
if (Frameworks != null) {
foreach (var fw in Frameworks)
ProcessFrameworkForArguments (args, fw, false, ref any_user_framework);
}
if (WeakFrameworks != null) {
foreach (var fw in WeakFrameworks)
ProcessFrameworkForArguments (args, fw, true, ref any_user_framework);
}
if (any_user_framework) {
args.Append (" -Xlinker -rpath -Xlinker @executable_path/Frameworks");
if (Application.IsExtension)
args.Append (" -Xlinker -rpath -Xlinker @executable_path/../../Frameworks");
}
if (Application.HasAnyDynamicLibraries) {
args.Append (" -Xlinker -rpath -Xlinker @executable_path");
if (Application.IsExtension)
args.Append (" -Xlinker -rpath -Xlinker @executable_path/../..");
}
}
void ProcessFrameworkForArguments (StringBuilder args, string fw, bool is_weak, ref bool any_user_framework)
{
var name = Path.GetFileNameWithoutExtension (fw);
if (fw.EndsWith (".framework", StringComparison.OrdinalIgnoreCase)) {
// user framework, we need to pass -F to the linker so that the linker finds the user framework.
any_user_framework = true;
AddInput (Path.Combine (fw, name));
args.Append (" -F ").Append (StringUtils.Quote (Path.GetDirectoryName (fw)));
}
args.Append (is_weak ? " -weak_framework " : " -framework ").Append (name);
}
public override string ToString ()
{
var args = new StringBuilder ();
WriteArguments (args);
return args.ToString ();
}
public void PopulateInputs ()
{
var args = new StringBuilder ();
Inputs = new List<string> ();
WriteArguments (args);
}
}
}