1 /**
2  * Copyright: Copyright (c) 2015 Jacob Carlborg. All rights reserved.
3  * Authors: Jacob Carlborg
4  * Version: Initial created: Jan 31, 2015
5  * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0)
6  */
7 module clang.Compiler;
8 
9 import std.path;
10 import std.typetuple : staticMap;
11 
12 import clang.c.Index;
13 
14 struct Compiler
15 {
16     private
17     {
18         version (Windows)
19             enum root = `C:\`;
20 
21         else
22             enum root = "/";
23 
24         string virtualPath_;
25 
26         static template toInternalHeader (string file)
27         {
28             enum toInternalHeader = InternalHeader(file, import(file));
29         }
30 
31         static struct InternalHeader
32         {
33             string filename;
34             string content;
35         }
36 
37         enum internalHeaders = [
38             staticMap!(
39                 toInternalHeader,
40                 "__stddef_max_align_t.h",
41                 "float.h",
42                 "limits.h",
43                 "stdarg.h",
44                 "stdbool.h",
45                 "stddef.h"
46             )
47         ];
48     }
49 
50     string[] extraIncludePaths ()
51     {
52         return [virtualPath];
53     }
54 
55     string[] extraIncludeFlags ()
56     {
57         import std.algorithm;
58         import std.array;
59         return extraIncludePaths.map!(x => "-I" ~ x).array;
60     }
61 
62     CXUnsavedFile[] extraHeaders ()
63     {
64         import std.algorithm : map;
65         import std.array;
66         import std..string : toStringz;
67 
68         return internalHeaders.map!((e) {
69             auto path = buildPath(virtualPath, e.filename);
70             return CXUnsavedFile(path.toStringz, e.content.ptr, cast(uint)e.content.length);
71         }).array();
72     }
73 
74 private:
75 
76     string virtualPath ()
77     {
78         import std.random;
79         import std.conv;
80 
81         if (virtualPath_.length)
82             return virtualPath_;
83 
84         return virtualPath_ = buildPath(root, uniform(1, 10_000_000).to!string);
85     }
86 }
87 
88 string[] internalIncludeFlags()
89 {
90     import std.algorithm;
91     import std.array;
92 
93     return Compiler.init.extraIncludePaths.map!(path => "-I" ~ path).array();
94 }
95 
96 CXUnsavedFile[] internalHeaders()
97 {
98     return Compiler.init.extraHeaders();
99 }