/
compiler.h
611 lines (458 loc) · 16.2 KB
/
compiler.h
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
// Copyright (C) 2017-2018 Egor Pugin <egor.pugin@gmail.com>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#pragma once
#include "options.h"
#include "options_cl.h"
#include "options_cl_vs.h"
#include "types.h"
#include <sw/builder/program.h>
#include <sw/manager/version.h>
#include <primitives/exceptions.h>
#include <memory>
#define SW_COMMON_COMPILER_API \
SW_DECLARE_PROGRAM_CLONE; \
void prepareCommand1(const TargetBase &t) override
namespace sw
{
namespace builder
{
struct Command;
}
namespace driver
{
struct Command;
}
struct Solution;
struct TargetBase;
struct Target;
struct NativeLinker;
/*enum class VisualStudioVersion
{
Unspecified,
//VS7 = 71,
VS8 = 80,
VS9 = 90,
VS10 = 100,
VS11 = 110,
VS12 = 120,
//VS13 = 130 was skipped
VS14 = 140,
VS15 = 150,
VS16 = 160,
};*/
SW_DRIVER_CPP_API
void detectCompilers(struct Solution &s);
SW_DRIVER_CPP_API
const StringSet &getCppHeaderFileExtensions();
SW_DRIVER_CPP_API
const StringSet &getCppSourceFileExtensions();
SW_DRIVER_CPP_API
bool isCppHeaderFileExtension(const String &);
SW_DRIVER_CPP_API
bool isCppSourceFileExtensions(const String &);
// maybe add Object type?
struct VSInstance : Program
{
path root;
Version version;
// one installation may have more that one versions (tool sets)
VersionSet cl_versions; // cl has 19.xx versions (19.15, 19.16, 19.20 etc.)
VersionSet link_versions; // tools has 14.xx versions (14.15, 14.16, 14.20 etc.)
using Program::Program;
//using Program::operator=;
std::shared_ptr<builder::Command> getCommand() const override { return nullptr; } // return path to devenv?
std::shared_ptr<Program> clone() const override { return std::make_shared<VSInstance>(*this); }
Version &getVersion() override { return version; }
void activate(struct Solution &s) const;
};
// toolchain
struct SW_DRIVER_CPP_API NativeToolchain
{
struct SW_DRIVER_CPP_API SDK
{
// root to sdks
// example: c:\\Program Files (x86)\\Windows Kits
path Root;
// sdk dir in root
// win: 7.0 7.0A, 7.1, 7.1A, 8, 8.1, 10 ...
// osx: 10.12, 10.13, 10.14 ...
// android: 1, 2, 3, ..., 28
path Version; // make string?
// windows10:
// 10.0.10240.0, 10.0.17763.0 ...
path BuildNumber;
path getPath(const path &subdir = {}) const;
String getWindowsTargetPlatformVersion() const;
void setAndroidApiVersion(int v);
};
struct SDK SDK;
// also OS SDK (win sdk, macos sdk, linux headers etc.)
//std::optional<UnresolvedPackage> os sdk
std::optional<UnresolvedPackage> libc;
//std::optional<UnresolvedPackage> libcpp;
std::shared_ptr<NativeLinker> Librarian;
std::shared_ptr<NativeLinker> Linker;
// rc (resource compiler)
// ar, more tools...
// more native compilers (cuda etc.)
::sw::CompilerType CompilerType = CompilerType::UnspecifiedCompiler;
//LinkerType LinkerType; // rename - use type from selected tool
BuildLibrariesAs LibrariesType = LibraryType::Shared;
::sw::ConfigurationType ConfigurationType = ConfigurationType::Release;
// win, vs
bool MT = false;
// toolset
// win sdk
// more settings
// misc
bool CopySharedLibraries = true;
// service
/// set on server to eat everything
//bool AssignAll = false;
// members
//String getConfig() const;
};
// compilers
struct SW_DRIVER_CPP_API CompilerBaseProgram : Program
{
String Prefix;
String Extension;
using Program::Program;
CompilerBaseProgram(const CompilerBaseProgram &);
std::shared_ptr<builder::Command> prepareCommand(const TargetBase &t);
std::shared_ptr<builder::Command> getCommand(const TargetBase &t);
std::shared_ptr<builder::Command> getCommand() const override;
std::shared_ptr<builder::Command> createCommand(const SwContext &swctx);
protected:
std::shared_ptr<driver::Command> cmd;
bool prepared = false;
virtual void prepareCommand1(const TargetBase &t) = 0;
virtual std::shared_ptr<driver::Command> createCommand1(const SwContext &swctx) const;
};
struct SW_DRIVER_CPP_API Compiler : CompilerBaseProgram
{
using CompilerBaseProgram::CompilerBaseProgram;
virtual ~Compiler() = default;
};
struct SW_DRIVER_CPP_API NativeCompiler : Compiler,
NativeCompilerOptions//, OptionsGroup<NativeCompilerOptions>
{
CompilerType Type = CompilerType::UnspecifiedCompiler;
using Compiler::Compiler;
virtual ~NativeCompiler() = default;
virtual path getOutputFile() const = 0;
virtual void setSourceFile(const path &input_file, path &output_file) = 0;
String getObjectExtension(const struct OS &o) const;
protected:
mutable Files dependencies;
};
struct SW_DRIVER_CPP_API VisualStudio
{
//VisualStudioVersion vs_version = VisualStudioVersion::Unspecified;
String toolset;
virtual ~VisualStudio() = default;
};
struct SW_DRIVER_CPP_API VisualStudioCompiler : VisualStudio,
NativeCompiler,
CommandLineOptions<VisualStudioCompilerOptions>
{
using NativeCompiler::NativeCompiler;
virtual ~VisualStudioCompiler() = default;
using NativeCompilerOptions::operator=;
SW_COMMON_COMPILER_API;
void setOutputFile(const path &output_file);
path getOutputFile() const override;
void setSourceFile(const path &input_file, path &output_file) override;
protected:
std::shared_ptr<driver::Command> createCommand1(const SwContext &swctx) const override;
private:
Version gatherVersion() const override { return Program::gatherVersion(file, "/?"); }
};
struct SW_DRIVER_CPP_API VisualStudioASMCompiler : VisualStudio, NativeCompiler,
CommandLineOptions<VisualStudioAssemblerOptions>
{
using NativeCompiler::NativeCompiler;
virtual ~VisualStudioASMCompiler() = default;
using NativeCompilerOptions::operator=;
SW_COMMON_COMPILER_API;
path getOutputFile() const override;
void setSourceFile(const path &input_file, path &output_file) override;
void setOutputFile(const path &output_file);
protected:
std::shared_ptr<driver::Command> createCommand1(const SwContext &swctx) const override;
private:
Version gatherVersion() const override { return Program::gatherVersion(file, "/?"); }
};
struct SW_DRIVER_CPP_API Clang
{
};
struct SW_DRIVER_CPP_API ClangCompiler : Clang, NativeCompiler,
CommandLineOptions<ClangOptions>
{
using NativeCompiler::NativeCompiler;
using NativeCompilerOptions::operator=;
SW_COMMON_COMPILER_API;
void setOutputFile(const path &output_file);
void setSourceFile(const path &input_file, path &output_file) override;
path getOutputFile() const override;
protected:
std::shared_ptr<driver::Command> createCommand1(const SwContext &swctx) const override;
};
struct SW_DRIVER_CPP_API ClangCl : Clang
{
//Version vs_target_version;
};
struct SW_DRIVER_CPP_API ClangClCompiler : ClangCl,
NativeCompiler,
CommandLineOptions<VisualStudioCompilerOptions>,
CommandLineOptions<ClangClOptions>
{
using NativeCompiler::NativeCompiler;
using NativeCompilerOptions::operator=;
SW_COMMON_COMPILER_API;
void setOutputFile(const path &output_file);
void setSourceFile(const path &input_file, path &output_file) override;
path getOutputFile() const override;
protected:
std::shared_ptr<driver::Command> createCommand1(const SwContext &swctx) const override;
};
struct SW_DRIVER_CPP_API GNU
{
};
struct SW_DRIVER_CPP_API GNUASMCompiler : GNU, NativeCompiler,
CommandLineOptions<GNUAssemblerOptions>
{
using NativeCompiler::NativeCompiler;
virtual ~GNUASMCompiler() = default;
using NativeCompilerOptions::operator=;
SW_COMMON_COMPILER_API;
void setSourceFile(const path &input_file, path &output_file) override;
void setOutputFile(const path &output_file);
path getOutputFile() const override;
protected:
std::shared_ptr<driver::Command> createCommand1(const SwContext &swctx) const override;
};
struct SW_DRIVER_CPP_API ClangASMCompiler : GNUASMCompiler
{
using GNUASMCompiler::GNUASMCompiler;
SW_DECLARE_PROGRAM_CLONE;
};
struct SW_DRIVER_CPP_API GNUCompiler : GNU, NativeCompiler,
CommandLineOptions<GNUOptions>
{
using NativeCompiler::NativeCompiler;
using NativeCompilerOptions::operator=;
SW_COMMON_COMPILER_API;
void setOutputFile(const path &output_file);
void setSourceFile(const path &input_file, path &output_file) override;
path getOutputFile() const override;
protected:
std::shared_ptr<driver::Command> createCommand1(const SwContext &swctx) const override;
};
// linkers
struct SW_DRIVER_CPP_API Linker : CompilerBaseProgram
{
using CompilerBaseProgram::CompilerBaseProgram;
virtual ~Linker() = default;
};
struct SW_DRIVER_CPP_API NativeLinker : Linker,
NativeLinkerOptions//, OptionsGroup<NativeLinkerOptions>
{
LinkerType Type = LinkerType::UnspecifiedLinker;
String Prefix;
String Suffix;
using Linker::Linker;
virtual void setObjectFiles(const Files &files) = 0; // actually this is addObjectFiles()
virtual void setInputLibraryDependencies(const FilesOrdered &files) {}
virtual void setOutputFile(const path &out) = 0;
virtual void setImportLibrary(const path &out) = 0;
virtual void setLinkLibraries(const FilesOrdered &in) {}
virtual path getOutputFile() const = 0;
virtual path getImportLibrary() const = 0;
FilesOrdered gatherLinkDirectories() const;
FilesOrdered gatherLinkLibraries(bool system = false) const;
};
struct SW_DRIVER_CPP_API VisualStudioLibraryTool : VisualStudio,
NativeLinker,
CommandLineOptions<VisualStudioLibraryToolOptions>
{
using NativeLinker::NativeLinker;
void setObjectFiles(const Files &files) override;
void setOutputFile(const path &out) override;
void setImportLibrary(const path &out) override;
path getOutputFile() const override;
path getImportLibrary() const override;
protected:
virtual void getAdditionalOptions(driver::Command *c) const = 0;
void prepareCommand1(const TargetBase &t) override;
private:
Version gatherVersion() const override { return Program::gatherVersion(file, "/?"); }
};
struct SW_DRIVER_CPP_API VisualStudioLinker : VisualStudioLibraryTool,
CommandLineOptions<VisualStudioLinkerOptions>
{
using VisualStudioLibraryTool::VisualStudioLibraryTool;
using NativeLinkerOptions::operator=;
SW_DECLARE_PROGRAM_CLONE;
void getAdditionalOptions(driver::Command *c) const override;
void setInputLibraryDependencies(const FilesOrdered &files) override;
protected:
void prepareCommand1(const TargetBase &t) override;
};
struct SW_DRIVER_CPP_API VisualStudioLibrarian : VisualStudioLibraryTool,
CommandLineOptions<VisualStudioLibrarianOptions>
{
using VisualStudioLibraryTool::VisualStudioLibraryTool;
using NativeLinkerOptions::operator=;
SW_DECLARE_PROGRAM_CLONE;
void getAdditionalOptions(driver::Command *c) const override;
};
struct SW_DRIVER_CPP_API GNULibraryTool : GNU,
NativeLinker,
CommandLineOptions<GNULibraryToolOptions>
{
using NativeLinker::NativeLinker;
protected:
virtual void getAdditionalOptions(driver::Command *c) const = 0;
};
struct SW_DRIVER_CPP_API GNULinker : GNULibraryTool,
CommandLineOptions<GNULinkerOptions>
{
bool use_start_end_groups = true;
using GNULibraryTool::GNULibraryTool;
using NativeLinkerOptions::operator=;
void getAdditionalOptions(driver::Command *c) const override;
void setInputLibraryDependencies(const FilesOrdered &files) override;
void setObjectFiles(const Files &files) override;
void setOutputFile(const path &out) override;
void setImportLibrary(const path &out) override;
void setLinkLibraries(const FilesOrdered &in) override;
path getOutputFile() const override;
path getImportLibrary() const override;
SW_COMMON_COMPILER_API;
};
struct SW_DRIVER_CPP_API GNULibrarian : GNULibraryTool,
CommandLineOptions<GNULibrarianOptions>
{
using GNULibraryTool::GNULibraryTool;
using NativeLinkerOptions::operator=;
void getAdditionalOptions(driver::Command *c) const override;
void setObjectFiles(const Files &files) override;
void setOutputFile(const path &out) override;
void setImportLibrary(const path &out) override;
virtual path getOutputFile() const override;
virtual path getImportLibrary() const override;
SW_COMMON_COMPILER_API;
protected:
Version gatherVersion() const override { return Program::gatherVersion(file, "-V"); }
};
// other tools
// win resources
struct SW_DRIVER_CPP_API RcTool :
CompilerBaseProgram,
CommandLineOptions<RcToolOptions>
{
FilesOrdered system_idirs;
using CompilerBaseProgram::CompilerBaseProgram;
SW_COMMON_COMPILER_API;
void setOutputFile(const path &output_file);
void setSourceFile(const path &input_file);
String getObjectExtension(const OS &) const { return ".res"; }
protected:
Version gatherVersion() const override { return Program::gatherVersion(file, "/?"); }
};
// C#
struct SW_DRIVER_CPP_API CSharpCompiler : Compiler
{
using Compiler::Compiler;
virtual void setOutputFile(const path &output_file) = 0;
virtual void addSourceFile(const path &input_file) = 0;
};
// roslyn compiler?
struct SW_DRIVER_CPP_API VisualStudioCSharpCompiler :
CSharpCompiler,
CommandLineOptions<VisualStudioCSharpCompilerOptions>
{
using CSharpCompiler::CSharpCompiler;
SW_COMMON_COMPILER_API;
void setOutputFile(const path &output_file) override;
void addSourceFile(const path &input_file) override;
protected:
Version gatherVersion() const override { return Program::gatherVersion(file, "/?"); }
};
struct SW_DRIVER_CPP_API RustCompiler : Compiler,
CommandLineOptions<RustCompilerOptions>
{
using Compiler::Compiler;
SW_COMMON_COMPILER_API;
void setOutputFile(const path &output_file);
void setSourceFile(const path &input_file);
};
struct SW_DRIVER_CPP_API GoCompiler : Compiler,
CommandLineOptions<GoCompilerOptions>
{
using Compiler::Compiler;
SW_COMMON_COMPILER_API;
void setOutputFile(const path &output_file);
void setSourceFile(const path &input_file);
private:
Version gatherVersion() const override { return Program::gatherVersion(file, "version"); }
};
struct SW_DRIVER_CPP_API FortranCompiler : Compiler,
CommandLineOptions<FortranCompilerOptions>
{
using Compiler::Compiler;
SW_COMMON_COMPILER_API;
void setOutputFile(const path &output_file);
void setSourceFile(const path &input_file);
};
struct SW_DRIVER_CPP_API JavaCompiler : Compiler,
CommandLineOptions<JavaCompilerOptions>
{
using Compiler::Compiler;
SW_COMMON_COMPILER_API;
void setOutputDir(const path &output_dir);
void setSourceFile(const path &input_file);
private:
Version gatherVersion() const override { return Program::gatherVersion(file, "-version", "(\\d+)\\.(\\d+)\\.(\\d+)(_(\\d+))?"); }
};
struct SW_DRIVER_CPP_API KotlinCompiler : Compiler,
CommandLineOptions<KotlinCompilerOptions>
{
using Compiler::Compiler;
SW_COMMON_COMPILER_API;
void setOutputFile(const path &output_file);
void setSourceFile(const path &input_file);
private:
Version gatherVersion() const override { return Program::gatherVersion(file, "-version"); }
};
struct SW_DRIVER_CPP_API DCompiler : Compiler,
CommandLineOptions<DCompilerOptions>
{
using Compiler::Compiler;
SW_COMMON_COMPILER_API;
void setOutputFile(const path &output_file);
void setObjectDir(const path &dir);
void setSourceFile(const path &input_file);
};
// TODO: compiled
// VB, VB.NET, Obj-C (check work), Pascal (+Delphi?), swift, dart, cobol, lisp, ada, haskell, F#, erlang
// TODO: interpreted
// python, js, php, R, ruby, matlab, perl, lua,
// TODO (other):
// Groovy, scala, prolog, apex, julia, clojure, bash
/*
How to add new lang:
- Add compiler
- Add 'void detectXCompilers(struct Solution &s);' function
- Call it from 'detectCompilers()'
- Add compiler options
- Add targets
- Add source file
- Add language
- Activate language (program) in solution
*/
}