9
9
#include < cstdio>
10
10
#include < functional>
11
11
12
- class LibReference {
13
- public:
14
- virtual ~LibReference () {};
15
- virtual bool valid () const = 0;
16
- virtual bool loadModule (const char * path) = 0;
17
- virtual void unLoadModule () = 0;
18
- virtual void * raw_ptr (const char * functionName) = 0;
19
- static LibReference* create ();
20
- };
21
-
22
- class CLangProject ;
23
12
24
- class CLangBuilder {
25
- friend class CLangProject ;
26
- public:
27
-
28
- virtual ~CLangBuilder () {};
29
-
30
- // / returns the module extension (dll for windows)
31
- virtual const char * getModuleExtension () const = 0;
32
-
33
- // / compile the project
34
- virtual void compile (CLangProject& project) = 0;
13
+ class cppModule ;
14
+ class cppBuilder ;
35
15
36
- // / converts relative path to file to the full path
37
- virtual std::string pathInHeap (const std::string& relative) = 0;
38
-
39
- // / returns LLVM-CL download path for the current OS
40
- virtual std::string downloadPath () = 0;
41
-
42
- // / returns true if the LLVM-CL exists in system and available to be used
43
- virtual bool valid () = 0;
44
-
45
- static CLangBuilder* create ();
46
- };
47
-
48
- class CLangProject {
16
+ class cppProject {
49
17
protected:
50
18
std::vector<std::string> files;
51
19
std::string modulePath;
52
20
std::string log;
53
21
std::string _options;
54
22
std::string _includes;
55
- LibReference * ref;
56
- CLangBuilder * builder;
23
+ cppModule * ref;
24
+ cppBuilder * builder;
57
25
void _add (const std::string& opt);
58
26
void _remove (const std::string& opt);
59
27
public:
60
- CLangProject ();
61
- virtual ~CLangProject ();
28
+ cppProject ();
29
+ virtual ~cppProject ();
62
30
63
31
// / Compile the project (if need).
64
32
void recompileIfNeed ();
@@ -67,25 +35,25 @@ class CLangProject {
67
35
bool valid ();
68
36
69
37
// / Add the cpp file into the project, provide the full path
70
- CLangProject & addFile (const std::string& path);
38
+ cppProject & addFile (const std::string& path);
71
39
72
40
// / Add the include file into the project
73
- CLangProject & addIncludeFolder (const std::string& path);
41
+ cppProject & addIncludeFolder (const std::string& path);
74
42
75
43
// / Add the text of cpp file to be compiled into the project
76
- CLangProject & addSource (const char * cpp_text);
44
+ cppProject & addSource (const char * cpp_text);
77
45
78
46
// / Compile (in future) with the speed optimization
79
- CLangProject & speedOptimization ();
47
+ cppProject & speedOptimization ();
80
48
81
49
// / Compile (in future) with the size optimization
82
- CLangProject & sizeOptimization ();
50
+ cppProject & sizeOptimization ();
83
51
84
52
// / Compile debug version (this is just option setting, not compilation itself)
85
- CLangProject & debug ();
53
+ cppProject & debug ();
86
54
87
55
// / Compile release version (this is just option setting, not compilation itself)
88
- CLangProject & release ();
56
+ cppProject & release ();
89
57
90
58
// / Returns "" if the LLVM-CL installed correctly, othervice it returns the path to download, you may display the message to offer the download.
91
59
std::string checkIfCompilerInstalled ();
@@ -111,7 +79,7 @@ class CLangProject {
111
79
};
112
80
113
81
template <class F >
114
- std::function<F> CLangProject ::bind (const char * functionName) {
82
+ std::function<F> cppProject ::bind (const char * functionName) {
115
83
std::function<F> f1 = nullptr ;
116
84
if (!ref) {
117
85
recompileIfNeed ();
@@ -122,6 +90,40 @@ std::function<F> CLangProject::bind(const char* functionName) {
122
90
return f1;
123
91
}
124
92
93
+ class cppModule {
94
+ public:
95
+ virtual ~cppModule () {};
96
+ virtual bool valid () const = 0;
97
+ virtual bool loadModule (const char * path) = 0;
98
+ virtual void unLoadModule () = 0;
99
+ virtual void * raw_ptr (const char * functionName) = 0;
100
+ static cppModule* create ();
101
+ };
102
+
103
+ class cppBuilder {
104
+ friend class cppProject ;
105
+ public:
106
+
107
+ virtual ~cppBuilder () {};
108
+
109
+ // / returns the module extension (dll for windows)
110
+ virtual const char * getModuleExtension () const = 0;
111
+
112
+ // / compile the project
113
+ virtual void compile (cppProject& project) = 0;
114
+
115
+ // / converts relative path to file to the full path
116
+ virtual std::string pathInHeap (const std::string& relative) = 0;
117
+
118
+ // / returns LLVM-CL download path for the current OS
119
+ virtual std::string downloadPath () = 0;
120
+
121
+ // / returns true if the LLVM-CL exists in system and available to be used
122
+ virtual bool valid () = 0;
123
+
124
+ static cppBuilder* create ();
125
+ };
126
+
125
127
126
128
// / implementation
127
129
@@ -262,65 +264,65 @@ inline static std::string _replace(const std::string& str, const std::string& su
262
264
return tmp;
263
265
}
264
266
265
- inline void CLangProject ::_add (const std::string& opt) {
267
+ inline void cppProject ::_add (const std::string& opt) {
266
268
_remove (opt);
267
269
_options += " " + opt;
268
270
_options = _replace (_options, " " , " " );
269
271
}
270
272
271
- inline void CLangProject ::_remove (const std::string& opt) {
273
+ inline void cppProject ::_remove (const std::string& opt) {
272
274
_options = _replace (_options, opt, " " );
273
275
_options = _replace (_options, " " , " " );
274
276
}
275
277
276
- inline CLangProject::CLangProject () {
277
- builder = CLangBuilder ::create ();
278
+ inline cppProject::cppProject () {
279
+ builder = cppBuilder ::create ();
278
280
ref = nullptr ;
279
281
_options = " /std:c++latest /LD" ;
280
282
}
281
283
282
- inline CLangProject ::~CLangProject () {
284
+ inline cppProject ::~cppProject () {
283
285
if (builder)delete (builder);
284
286
builder = nullptr ;
285
287
if (ref)delete (ref);
286
288
ref = nullptr ;
287
289
}
288
290
289
- inline bool CLangProject ::valid () {
291
+ inline bool cppProject ::valid () {
290
292
return ref != nullptr ;
291
293
}
292
294
293
- inline std::string& CLangProject ::compileLog () {
295
+ inline std::string& cppProject ::compileLog () {
294
296
return log;
295
297
}
296
298
297
- inline std::string& CLangProject ::module () {
299
+ inline std::string& cppProject ::module () {
298
300
return modulePath;
299
301
}
300
302
301
- inline std::vector<std::string>& CLangProject ::filesList () {
303
+ inline std::vector<std::string>& cppProject ::filesList () {
302
304
return files;
303
305
}
304
306
305
- inline std::string& CLangProject ::options () {
307
+ inline std::string& cppProject ::options () {
306
308
return _options;
307
309
}
308
310
309
- inline std::string& CLangProject ::includes () {
311
+ inline std::string& cppProject ::includes () {
310
312
return _includes;
311
313
}
312
314
313
- inline CLangProject& CLangProject ::addFile (const std::string& path) {
315
+ inline cppProject& cppProject ::addFile (const std::string& path) {
314
316
filesList ().push_back (path);
315
317
return *this ;
316
318
}
317
319
318
- inline CLangProject& CLangProject ::addIncludeFolder (const std::string& path) {
320
+ inline cppProject& cppProject ::addIncludeFolder (const std::string& path) {
319
321
includes () += " /I " + path;
320
322
return *this ;
321
323
}
322
324
323
- inline CLangProject& CLangProject ::addSource (const char * cpp_text) {
325
+ inline cppProject& cppProject ::addSource (const char * cpp_text) {
324
326
std::string fn = " temp_" + md5::hash (cpp_text) + " .cpp" ;
325
327
std::string res = builder->pathInHeap (fn);
326
328
std::ofstream f (res);
@@ -332,30 +334,30 @@ inline CLangProject& CLangProject::addSource(const char* cpp_text) {
332
334
return *this ;
333
335
}
334
336
335
- inline CLangProject& CLangProject ::speedOptimization () {
337
+ inline cppProject& cppProject ::speedOptimization () {
336
338
_add (" /Ot" );
337
339
return *this ;
338
340
}
339
341
340
- inline CLangProject& CLangProject ::sizeOptimization () {
342
+ inline cppProject& cppProject ::sizeOptimization () {
341
343
_add (" /Os" );
342
344
return *this ;
343
345
}
344
346
345
- inline CLangProject& CLangProject ::debug () {
347
+ inline cppProject& cppProject ::debug () {
346
348
_remove (" /LD" );
347
349
_add (" /LDd" );
348
350
_add (" -fuse-ld=lld -Z7" );
349
351
return *this ;
350
352
}
351
353
352
- inline CLangProject& CLangProject ::release () {
354
+ inline cppProject& cppProject ::release () {
353
355
_remove (" /LDd" );
354
356
_add (" /LD" );
355
357
return *this ;
356
358
}
357
359
358
- inline std::string CLangProject ::checkIfCompilerInstalled () {
360
+ inline std::string cppProject ::checkIfCompilerInstalled () {
359
361
if (builder) {
360
362
if (!builder->valid ()) {
361
363
return builder->downloadPath ();
@@ -364,11 +366,11 @@ inline std::string CLangProject::checkIfCompilerInstalled() {
364
366
return " " ;
365
367
}
366
368
367
- inline void CLangProject ::recompileIfNeed () {
369
+ inline void cppProject ::recompileIfNeed () {
368
370
if (builder) {
369
371
builder->compile (*this );
370
372
if (ref)delete (ref);
371
- ref = LibReference ::create ();
373
+ ref = cppModule ::create ();
372
374
ref->loadModule (module ().c_str ());
373
375
}
374
376
}
@@ -398,7 +400,7 @@ struct funcRef {
398
400
FARPROC address;
399
401
};
400
402
401
- class LibReferenceWin : public LibReference {
403
+ class LibReferenceWin : public cppModule {
402
404
HMODULE h;
403
405
std::vector<funcRef> refs;
404
406
public:
@@ -507,7 +509,7 @@ inline std::string exec(const char* cmd) {
507
509
return strResult;
508
510
}
509
511
510
- class CLangBuilderWin : public CLangBuilder {
512
+ class CLangBuilderWin : public cppBuilder {
511
513
std::string clangPath;
512
514
std::string tempPath;
513
515
public:
@@ -532,7 +534,7 @@ class CLangBuilderWin : public CLangBuilder {
532
534
const char * getModuleExtension () const override {
533
535
return " dll" ;
534
536
}
535
- void compile (CLangProject & project) override {
537
+ void compile (cppProject & project) override {
536
538
std::filesystem::path p = _getexepath ();
537
539
p.replace_extension (" lib" );
538
540
std::string libpath = p.string ();
@@ -581,11 +583,11 @@ class CLangBuilderWin : public CLangBuilder {
581
583
}
582
584
};
583
585
584
- inline LibReference* LibReference ::create () {
586
+ inline cppModule* cppModule ::create () {
585
587
return new LibReferenceWin;
586
588
}
587
589
588
- inline CLangBuilder* CLangBuilder ::create () {
590
+ inline cppBuilder* cppBuilder ::create () {
589
591
return new CLangBuilderWin;
590
592
}
591
593
0 commit comments