1 /** 2 Command-line options 3 */ 4 module dpp.runtime.options; 5 6 @safe: 7 8 version(Windows) 9 enum exeExtension = ".exe"; 10 else 11 enum exeExtension = ""; 12 13 14 struct Options { 15 16 enum usage = "Usage: d++ [options] [D compiler options] <filename.dpp> [D compiler args]"; 17 18 string[] dppFileNames; 19 int indentation; 20 bool debugOutput; 21 string[] includePaths; 22 bool keepPreCppFiles; 23 bool keepDlangFiles; 24 bool parseAsCpp; 25 bool preprocessOnly; 26 string dlangCompiler = "dmd"; 27 string[] dlangCompilerArgs; 28 string[] defines; 29 bool earlyExit; 30 bool hardFail; 31 string mscrtlib; 32 bool cppStdLib; 33 bool ignoreMacros; 34 bool detailedUntranslatable; 35 string[] ignoredNamespaces; 36 string[] ignoredCursors; 37 bool ignoreSystemPaths; 38 string[] ignoredPaths; 39 string[string] prebuiltHeaders; 40 bool alwaysScopedEnums; 41 string cppStandard = "c++17"; 42 string[] clangOptions; 43 bool noSystemHeaders; 44 string cppPath; 45 string srcOutputPath; 46 47 this(string[] args) { 48 49 import clang: systemPaths; 50 import std.exception: enforce; 51 import std.path: stripExtension, extension, buildPath, absolutePath; 52 import std.file: tempDir; 53 import std.algorithm: map, filter, canFind, startsWith; 54 import std.array: array; 55 import std.conv: text; 56 57 parseArgs(args); 58 if(earlyExit) return; 59 60 if(preprocessOnly) 61 keepDlangFiles = true; 62 63 dppFileNames = args.filter!(a => a.extension == ".dpp").array; 64 enforce(dppFileNames.length != 0, "No .dpp input file specified\n" ~ usage); 65 66 // Remove the name of this binary and the name of the .dpp input file from args 67 // so that a D compiler can use the remaining entries. 68 dlangCompilerArgs = 69 args[1..$].filter!(a => a.extension != ".dpp").array ~ 70 dFileNames; 71 72 // use msvcrtd.lib if no other libc is specified 73 bool addNODEFAULTLIB = false; 74 version(Windows) { 75 if(mscrtlib) { 76 if(dlangCompiler == "dmd") { 77 dlangCompilerArgs ~= mscrtlib ~ ".lib"; 78 addNODEFAULTLIB = true; 79 } else if (dlangCompiler == "ldc2") { 80 dlangCompilerArgs ~= "--mscrtlib=" ~ mscrtlib; 81 } 82 } 83 } else { 84 assert(mscrtlib == "", "Using --mscrtlib flag on a non-windows platform is not allowed."); 85 } 86 87 // if no -of option is given, default to the name of the .dpp file 88 if(!dlangCompilerArgs.canFind!(a => a.startsWith("-of")) && !dlangCompilerArgs.canFind("-c")) { 89 dlangCompilerArgs ~= ((dlangCompiler == "ldc2") ? "--of=" : "-of=") ~ 90 args. 91 filter!(a => a.extension == ".dpp" || a.extension == ".d") 92 .front 93 .stripExtension 94 ~ exeExtension; 95 } 96 97 version(Windows) { 98 // append the arch flag if not provided manually 99 if(!dlangCompilerArgs.canFind!(a => a == "-m64" || a == "-m32" || a == "-m32mscoff")) { 100 version(X86_64) { 101 dlangCompilerArgs ~= "-m64"; 102 } 103 version(x86) { 104 dlangCompilerArgs ~= (dlangCompiler == "dmd") ? "-m32mscoff" : "-m32"; 105 } 106 } 107 if(addNODEFAULTLIB) { 108 dlangCompilerArgs ~= "-L=\"/NODEFAULTLIB:libcmt\""; 109 } 110 assert(!cppStdLib, "C++ std lib functionality not implemented yet for Windows"); 111 } 112 113 if(cppStdLib) { 114 dlangCompilerArgs ~= "-L-lstdc++"; 115 parseAsCpp = true; 116 } 117 118 if (!noSystemHeaders) 119 includePaths = systemPaths ~ includePaths; 120 } 121 122 string[] dFileNames() @safe pure const { 123 import std.algorithm: map; 124 import std.array: array; 125 return dppFileNames.map!(a => toDFileName(a)).array; 126 } 127 128 string toDFileName(in string dppFileName) @safe pure nothrow const { 129 import std.path: stripExtension, dirName, isAbsolute, buildPath, baseName; 130 131 const outputPath = srcOutputPath == "" 132 ? dppFileName.dirName 133 : srcOutputPath; 134 const fileName = dppFileName.baseName.stripExtension ~ ".d"; 135 136 return buildPath(outputPath, fileName); 137 } 138 139 private void parseArgs(ref string[] args) { 140 import std.getopt: getopt, defaultGetoptPrinter, config; 141 import std.algorithm : map; 142 import std.array : split, join; 143 auto helpInfo = 144 getopt( 145 args, 146 config.passThrough, 147 "print-cursors", "Print debug information", &debugOutput, 148 "include-path", "Include paths", &includePaths, 149 "keep-pre-cpp-files", "Do not delete the temporary pre-preprocessed file", &keepPreCppFiles, 150 "keep-d-files", "Do not delete the temporary D file to be compiled", &keepDlangFiles, 151 "preprocess-only", "Only transform the .dpp file into a .d file, don't compile", &preprocessOnly, 152 "compiler", "D compiler to use", &dlangCompiler, 153 "parse-as-cpp", "Parse header as C++", &parseAsCpp, 154 "define", "C Preprocessor macro", &defines, 155 "hard-fail", "Translate nothing if any part fails", &hardFail, 156 "mscrtlib", 157 "MS C runtime library to link (e.g. use `--mscrtlib=msvcrtd`, if there are missing references)", 158 &mscrtlib, 159 "c++-std-lib", "Link to the C++ standard library", &cppStdLib, 160 "ignore-macros", "Ignore preprocessor macros", &ignoreMacros, 161 "ignore-ns", "Ignore a C++ namespace", &ignoredNamespaces, 162 "ignore-cursor", "Ignore a C++ cursor", &ignoredCursors, 163 "ignore-path", "Ignore a file path, note it globs so you will want to use *", &ignoredPaths, 164 "ignore-system-paths", 165 "Adds system paths to the ignore-paths list (you can add them back individually with --include-path)", 166 &ignoreSystemPaths, 167 "prebuilt-header", 168 "Declare a #include can be safely replaced with import. You should also ignore-path to prevent retranslating the file", 169 &prebuiltHeaders, 170 "detailed-untranslatables", 171 "Show details about untranslatable cursors", 172 &detailedUntranslatable, 173 "scoped-enums", "Don't redeclare enums to mimic C", &alwaysScopedEnums, 174 "c++-standard", "The C++ language standard (e.g. \"c++14\")", &cppStandard, 175 "clang-option", "Pass option to libclang", &clangOptions, 176 "no-sys-headers", "Don't include system headers by default", &noSystemHeaders, 177 "cpp-path", "Path to the C preprocessor executable", &cppPath, 178 "source-output-path", "Path to emit the resulting D files to", &srcOutputPath, 179 ); 180 181 clangOptions = map!(e => e.split(" "))(clangOptions).join(); 182 183 if(helpInfo.helpWanted) { 184 () @trusted { 185 defaultGetoptPrinter(usage, helpInfo.options); 186 }(); 187 earlyExit = true; 188 } 189 190 if(ignoreSystemPaths) { 191 import clang: systemPaths; 192 import std.algorithm: filter, canFind; 193 foreach(sp; systemPaths.filter!(p => !includePaths.canFind(p))) 194 ignoredPaths ~= sp ~ "*"; 195 } 196 } 197 198 void indent() @safe pure nothrow { 199 indentation += 4; 200 } 201 202 Options dup() @safe pure nothrow const { 203 Options ret; 204 foreach(i, ref elt; ret.tupleof) { 205 static if(__traits(compiles, this.tupleof[i].dup)) 206 elt = this.tupleof[i].dup; 207 else static if(is(typeof(this.tupleof[i]) == const(K[V]), K, V)) 208 { 209 try // surprised looping over the AA is not nothrow but meh 210 foreach(k, v; this.tupleof[i]) 211 elt[k] = v; 212 catch(Exception) assert(0); 213 } 214 else 215 elt = this.tupleof[i]; 216 } 217 218 ret.includePaths = includePaths.dup; 219 ret.defines = defines.dup; 220 221 return ret; 222 } 223 224 void log(T...)(auto ref T args) @trusted const { 225 version(unittest) import unit_threaded.io: writeln = writelnUt; 226 else import std.stdio: writeln; 227 228 version(unittest) 229 enum shouldLog = true; 230 else 231 const shouldLog = debugOutput; 232 233 if(shouldLog) 234 writeln(indentationString, args); 235 } 236 237 private auto indentationString() @safe pure nothrow const { 238 import std.array: appender; 239 auto app = appender!(char[]); 240 app.reserve(indentation); 241 foreach(i; 0 .. indentation) app ~= " "; 242 return app.data; 243 } 244 }