1 /*
2 Boost Software License - Version 1.0 - August 17th, 2003
3 Permission is hereby granted, free of charge, to any person or organization
4 obtaining a copy of the software and accompanying documentation covered by
5 this license ( the "Software" ) to use, reproduce, display, distribute,
6 execute, and transmit the Software, and to prepare derivative works of the
7 Software, and to permit third-parties to whom the Software is furnished to
8 do so, all subject to the following:
9 The copyright notices in the Software and this entire statement, including
10 the above license grant, this restriction and the following disclaimer,
11 must be included in all copies of the Software, in whole or in part, and
12 all derivative works of the Software, unless such copies or derivative
13 works are solely in the form of machine-executable object code generated by
14 a source language processor.
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
18 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
19 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
20 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 DEALINGS IN THE SOFTWARE.
22 */
23 module derelict.nvrtc;
24 
25 /**
26     Translation of nvrtc.h
27 */
28 
29 import derelict.util.loader;
30 
31 private
32 {
33     import derelict.util.system;
34 
35     static if(Derelict_OS_Windows)
36         enum libNames = "nvrtc64_80.dll,nvrtc-builtins64_80.dll";
37     else static if (Derelict_OS_Linux)
38     {
39         version(X86_64)
40             enum libNames = "libnvrtc.so.8.0,libnvrtc.so,libnvrtc-builtins.so.8.0,libnvrtc-builtins.so";
41         else
42             static assert(0, "Need to implement NVRTC libNames for this arch.");
43     }
44     else
45         static assert(0, "Need to implement NVRTC libNames for this operating system.");
46 
47     enum functionTypes = [
48         ["nvrtcGetErrorString", "const char *", "nvrtcResult"],
49         ["nvrtcVersion", "nvrtcResult", "int *", "int *"],
50         ["nvrtcCreateProgram", "nvrtcResult", "nvrtcProgram *", "const char *", "const char *", "int", "const char **",
51             "const char **"],
52         ["nvrtcDestroyProgram", "nvrtcResult", "nvrtcProgram *"],
53         ["nvrtcCompileProgram", "nvrtcResult", "nvrtcProgram, int", "const char **"],
54         ["nvrtcGetPTXSize", "nvrtcResult", "nvrtcProgram", "size_t *"],
55         ["nvrtcGetPTX", "nvrtcResult", "nvrtcProgram", "char *"],
56         ["nvrtcGetProgramLogSize", "nvrtcResult", "nvrtcProgram", "size_t *"],
57         ["nvrtcGetProgramLog", "nvrtcResult", "nvrtcProgram", "char *"],
58         ["nvrtcAddNameExpression", "nvrtcResult", "nvrtcProgram", "const char **"],
59         ["nvrtcGetLoweredName", "nvrtcResult", "nvrtcProgram", "const char *", "const char **"]
60     ];
61 
62     string generateFunctionAliases()
63     {
64         import std.algorithm : joiner;
65         import std.conv : to;
66 
67         string ret;
68 
69         foreach(ft; functionTypes)
70         {
71             ret ~= "alias da_" ~ ft[0] ~ " = " ~ ft[1] ~ " function(" ~ ft[2 .. $].joiner(",").to!string ~ ");";
72         }
73 
74         return ret;
75     }
76 
77     string generateFunctionPointers()
78     {
79         string ret;
80 
81         foreach(ft; functionTypes)
82         {
83             ret ~= "da_" ~ ft[0] ~ " " ~ ft[0] ~ ";";
84         }
85 
86         return ret;
87     }
88 
89     string generateFunctionBinds()
90     {
91         string ret;
92 
93         foreach(ft; functionTypes)
94         {
95             ret ~= "bindFunc(cast(void**)&" ~ ft[0] ~ ", \"" ~ ft[0] ~ "\");";
96         }
97 
98         return ret;
99     }
100 }
101 
102 alias nvrtcResult = int;
103 enum : nvrtcResult
104 {
105     NVRTC_SUCCESS = 0,
106     NVRTC_ERROR_OUT_OF_MEMORY = 1,
107     NVRTC_ERROR_PROGRAM_CREATION_FAILURE = 2,
108     NVRTC_ERROR_INVALID_INPUT = 3,
109     NVRTC_ERROR_INVALID_PROGRAM = 4,
110     NVRTC_ERROR_INVALID_OPTION = 5,
111     NVRTC_ERROR_COMPILATION = 6,
112     NVRTC_ERROR_BUILTIN_OPERATION_FAILURE = 7,
113     NVRTC_ERROR_NO_NAME_EXPRESSIONS_AFTER_COMPILATION = 8,
114     NVRTC_ERROR_NO_LOWERED_NAMES_BEFORE_COMPILATION = 9,
115     NVRTC_ERROR_NAME_EXPRESSION_NOT_VALID = 10,
116     NVRTC_ERROR_INTERNAL_ERROR = 11
117 }
118 
119 struct _nvrtcProgram;
120 alias nvrtcProgram = _nvrtcProgram *;
121 
122 extern(System) @nogc nothrow
123 {
124     mixin(generateFunctionAliases());
125 }
126 
127 __gshared
128 {
129     mixin(generateFunctionPointers());
130 }
131 
132 class DerelictNVRTCLoader : SharedLibLoader
133 {
134     public
135     {
136         this()
137         {
138             super(libNames);
139         }
140     }
141 
142     protected
143     {
144         override void loadSymbols()
145         {
146             mixin(generateFunctionBinds());
147         }
148     }
149 }
150 
151 __gshared DerelictNVRTCLoader DerelictNVRTC;
152 
153 shared static this()
154 {
155     DerelictNVRTC = new DerelictNVRTCLoader();
156 }
157 
158 unittest
159 {
160     DerelictNVRTC.load();
161 
162     int major, minor;
163     nvrtcVersion(&major, &minor);
164 
165     import std.stdio : writeln;
166     writeln("NVRTC version ", major, ".", minor);
167 }