1 /** 2 Generator for direct compiler builds. 3 4 Copyright: © 2013-2013 rejectedsoftware e.K. 5 License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file. 6 Authors: Sönke Ludwig 7 */ 8 module dub.generators.build; 9 10 import dub.compilers.compiler; 11 import dub.compilers.utils; 12 import dub.generators.generator; 13 import dub.internal.utils; 14 import dub.internal.vibecompat.core.file; 15 import dub.internal.vibecompat.inet.path; 16 import dub.internal.logging; 17 import dub.package_; 18 import dub.packagemanager; 19 import dub.project; 20 21 import std.algorithm; 22 import std.array; 23 import std.conv; 24 import std.exception; 25 import std.file; 26 import std.process; 27 import std.string; 28 import std.encoding : sanitize; 29 30 string getObjSuffix(const scope ref BuildPlatform platform) 31 { 32 return platform.isWindows() ? ".obj" : ".o"; 33 } 34 35 string computeBuildName(string config, in GeneratorSettings settings, const string[][] hashing...) 36 { 37 import std.digest.sha : SHA256, toHexString; 38 39 SHA256 hash; 40 hash.start(); 41 void addHash(in string[] strings...) { foreach (s; strings) { hash.put(cast(ubyte[])s); hash.put(0); } hash.put(0); } 42 foreach(strings; hashing) 43 addHash(strings); 44 const hashstr = hash.finish().toHexString(); 45 46 return format("%s-%s-%s-%s-%s_v%s-%s", config, settings.buildType, 47 settings.platform.platform.join("."), 48 settings.platform.architecture.join("."), 49 settings.platform.compiler, settings.platform.compilerVersion, hashstr); 50 } 51 52 class BuildGenerator : ProjectGenerator { 53 private { 54 PackageManager m_packageMan; 55 NativePath[] m_temporaryFiles; 56 } 57 58 this(Project project) 59 { 60 super(project); 61 m_packageMan = project.packageManager; 62 } 63 64 override void generateTargets(GeneratorSettings settings, in TargetInfo[string] targets) 65 { 66 import std.path : setExtension; 67 scope (exit) cleanupTemporaries(); 68 69 void checkPkgRequirements(const(Package) pkg) 70 { 71 const tr = pkg.recipe.toolchainRequirements; 72 tr.checkPlatform(settings.platform, pkg.name); 73 } 74 75 checkPkgRequirements(m_project.rootPackage); 76 foreach (pkg; m_project.dependencies) 77 checkPkgRequirements(pkg); 78 79 auto root_ti = targets[m_project.rootPackage.name]; 80 const rootTT = root_ti.buildSettings.targetType; 81 82 enforce(!(settings.rdmd && rootTT == TargetType.none), 83 "Building package with target type \"none\" with rdmd is not supported yet."); 84 85 logInfo("Starting", Color.light_green, 86 "Performing \"%s\" build using %s for %-(%s, %).", 87 settings.buildType.color(Color.magenta), settings.platform.compilerBinary, 88 settings.platform.architecture); 89 90 bool any_cached = false; 91 92 NativePath[string] target_paths; 93 94 NativePath[] dynamicLibDepsFilesToCopy; // to the root package output dir 95 const copyDynamicLibDepsLinkerFiles = rootTT == TargetType.dynamicLibrary || rootTT == TargetType.none; 96 const copyDynamicLibDepsRuntimeFiles = copyDynamicLibDepsLinkerFiles || rootTT == TargetType.executable; 97 98 bool[string] visited; 99 void buildTargetRec(string target) 100 { 101 if (target in visited) return; 102 visited[target] = true; 103 104 auto ti = targets[target]; 105 106 foreach (dep; ti.dependencies) 107 buildTargetRec(dep); 108 109 NativePath[] additional_dep_files; 110 auto bs = ti.buildSettings.dup; 111 const tt = bs.targetType; 112 foreach (ldep; ti.linkDependencies) { 113 const ldepPath = target_paths[ldep].toNativeString(); 114 const doLink = tt != TargetType.staticLibrary && !(bs.options & BuildOption.syntaxOnly); 115 116 if (doLink && isLinkerFile(settings.platform, ldepPath)) 117 bs.addSourceFiles(ldepPath); 118 else 119 additional_dep_files ~= target_paths[ldep]; 120 121 if (targets[ldep].buildSettings.targetType == TargetType.dynamicLibrary) { 122 // copy the .{dll,so,dylib} 123 if (copyDynamicLibDepsRuntimeFiles) 124 dynamicLibDepsFilesToCopy ~= NativePath(ldepPath); 125 126 if (settings.platform.isWindows()) { 127 // copy the accompanying .pdb if found 128 if (copyDynamicLibDepsRuntimeFiles) { 129 const pdb = ldepPath.setExtension(".pdb"); 130 if (existsFile(pdb)) 131 dynamicLibDepsFilesToCopy ~= NativePath(pdb); 132 } 133 134 const importLib = ldepPath.setExtension(".lib"); 135 if (existsFile(importLib)) { 136 // link dependee against the import lib 137 if (doLink) 138 bs.addSourceFiles(importLib); 139 // and copy 140 if (copyDynamicLibDepsLinkerFiles) 141 dynamicLibDepsFilesToCopy ~= NativePath(importLib); 142 } 143 144 // copy the .exp file if found 145 const exp = ldepPath.setExtension(".exp"); 146 if (copyDynamicLibDepsLinkerFiles && existsFile(exp)) 147 dynamicLibDepsFilesToCopy ~= NativePath(exp); 148 } 149 } 150 } 151 NativePath tpath; 152 if (tt != TargetType.none) { 153 if (buildTarget(settings, bs, ti.pack, ti.config, ti.packages, additional_dep_files, tpath)) 154 any_cached = true; 155 } 156 target_paths[target] = tpath; 157 } 158 159 // build all targets 160 if (settings.rdmd || rootTT == TargetType.staticLibrary) { 161 // RDMD always builds everything at once and static libraries don't need their 162 // dependencies to be built 163 NativePath tpath; 164 buildTarget(settings, root_ti.buildSettings.dup, m_project.rootPackage, root_ti.config, root_ti.packages, null, tpath); 165 return; 166 } 167 168 buildTargetRec(m_project.rootPackage.name); 169 170 if (dynamicLibDepsFilesToCopy.length) { 171 const rootTargetPath = root_ti.buildSettings.targetPath; 172 173 if (!existsFile(rootTargetPath)) 174 mkdirRecurse(rootTargetPath); 175 176 foreach (src; dynamicLibDepsFilesToCopy) { 177 logDiagnostic("Copying target from %s to %s", src.toNativeString(), rootTargetPath); 178 hardLinkFile(src, NativePath(rootTargetPath) ~ src.head, true); 179 } 180 } 181 182 if (any_cached) { 183 logInfo("Finished", Color.green, 184 "To force a rebuild of up-to-date targets, run again with --force" 185 ); 186 } 187 } 188 189 override void performPostGenerateActions(GeneratorSettings settings, in TargetInfo[string] targets) 190 { 191 // run the generated executable 192 auto buildsettings = targets[m_project.rootPackage.name].buildSettings.dup; 193 if (settings.run && !(buildsettings.options & BuildOption.syntaxOnly)) { 194 NativePath exe_file_path; 195 if (m_tempTargetExecutablePath.empty) 196 exe_file_path = getTargetPath(buildsettings, settings); 197 else 198 exe_file_path = m_tempTargetExecutablePath ~ settings.compiler.getTargetFileName(buildsettings, settings.platform); 199 runTarget(exe_file_path, buildsettings, settings.runArgs, settings); 200 } 201 } 202 203 private bool buildTarget(GeneratorSettings settings, BuildSettings buildsettings, in Package pack, string config, in Package[] packages, in NativePath[] additional_dep_files, out NativePath target_path) 204 { 205 import std.path : absolutePath; 206 207 auto cwd = NativePath(getcwd()); 208 bool generate_binary = !(buildsettings.options & BuildOption.syntaxOnly); 209 210 auto build_id = computeBuildID(config, buildsettings, settings); 211 212 // make all paths relative to shrink the command line 213 string makeRelative(string path) { return shrinkPath(NativePath(path), cwd); } 214 foreach (ref f; buildsettings.sourceFiles) f = makeRelative(f); 215 foreach (ref p; buildsettings.importPaths) p = makeRelative(p); 216 foreach (ref p; buildsettings.stringImportPaths) p = makeRelative(p); 217 218 // perform the actual build 219 bool cached = false; 220 if (settings.rdmd) performRDMDBuild(settings, buildsettings, pack, config, target_path); 221 else if (settings.direct || !generate_binary) performDirectBuild(settings, buildsettings, pack, config, target_path); 222 else cached = performCachedBuild(settings, buildsettings, pack, config, build_id, packages, additional_dep_files, target_path); 223 224 // HACK: cleanup dummy doc files, we shouldn't specialize on buildType 225 // here and the compiler shouldn't need dummy doc output. 226 if (settings.buildType == "ddox") { 227 if ("__dummy.html".exists) 228 removeFile("__dummy.html"); 229 if ("__dummy_docs".exists) 230 rmdirRecurse("__dummy_docs"); 231 } 232 233 // run post-build commands 234 if (!cached && buildsettings.postBuildCommands.length) { 235 logInfo("Post-build", Color.light_green, "Running commands"); 236 runBuildCommands(CommandType.postBuild, buildsettings.postBuildCommands, pack, m_project, settings, buildsettings, 237 [["DUB_BUILD_PATH" : target_path is NativePath.init ? "" : target_path.parentPath.toNativeString.absolutePath]]); 238 } 239 240 return cached; 241 } 242 243 private bool performCachedBuild(GeneratorSettings settings, BuildSettings buildsettings, in Package pack, string config, 244 string build_id, in Package[] packages, in NativePath[] additional_dep_files, out NativePath target_binary_path) 245 { 246 auto cwd = NativePath(getcwd()); 247 248 NativePath target_path; 249 if (settings.tempBuild) { 250 string packageName = pack.basePackage is null ? pack.name : pack.basePackage.name; 251 m_tempTargetExecutablePath = target_path = getTempDir() ~ format(".dub/build/%s-%s/%s/", packageName, pack.version_, build_id); 252 } 253 else target_path = pack.path ~ format(".dub/build/%s/", build_id); 254 255 if (!settings.force && isUpToDate(target_path, buildsettings, settings, pack, packages, additional_dep_files)) { 256 logInfo("Up-to-date", Color.green, "%s %s: target for configuration [%s] is up to date.", 257 pack.name.color(Mode.bold), pack.version_, config.color(Color.blue)); 258 logDiagnostic("Using existing build in %s.", target_path.toNativeString()); 259 target_binary_path = target_path ~ settings.compiler.getTargetFileName(buildsettings, settings.platform); 260 if (!settings.tempBuild) 261 copyTargetFile(target_path, buildsettings, settings); 262 return true; 263 } 264 265 if (!isWritableDir(target_path, true)) { 266 if (!settings.tempBuild) 267 logInfo("Build directory %s is not writable. Falling back to direct build in the system's temp folder.", target_path.relativeTo(cwd).toNativeString()); 268 performDirectBuild(settings, buildsettings, pack, config, target_path); 269 return false; 270 } 271 272 logInfo("Building", Color.light_green, "%s %s: building configuration [%s]", pack.name.color(Mode.bold), pack.version_, config.color(Color.blue)); 273 274 if( buildsettings.preBuildCommands.length ){ 275 logInfo("Pre-build", Color.light_green, "Running commands"); 276 runBuildCommands(CommandType.preBuild, buildsettings.preBuildCommands, pack, m_project, settings, buildsettings); 277 } 278 279 // override target path 280 auto cbuildsettings = buildsettings; 281 cbuildsettings.targetPath = shrinkPath(target_path, cwd); 282 buildWithCompiler(settings, cbuildsettings); 283 target_binary_path = getTargetPath(cbuildsettings, settings); 284 285 if (!settings.tempBuild) 286 copyTargetFile(target_path, buildsettings, settings); 287 288 return false; 289 } 290 291 private void performRDMDBuild(GeneratorSettings settings, ref BuildSettings buildsettings, in Package pack, string config, out NativePath target_path) 292 { 293 auto cwd = NativePath(getcwd()); 294 //Added check for existence of [AppNameInPackagejson].d 295 //If exists, use that as the starting file. 296 NativePath mainsrc; 297 if (buildsettings.mainSourceFile.length) { 298 mainsrc = NativePath(buildsettings.mainSourceFile); 299 if (!mainsrc.absolute) mainsrc = pack.path ~ mainsrc; 300 } else { 301 mainsrc = getMainSourceFile(pack); 302 logWarn(`Package has no "mainSourceFile" defined. Using best guess: %s`, mainsrc.relativeTo(pack.path).toNativeString()); 303 } 304 305 // do not pass all source files to RDMD, only the main source file 306 buildsettings.sourceFiles = buildsettings.sourceFiles.filter!(s => !s.endsWith(".d"))().array(); 307 settings.compiler.prepareBuildSettings(buildsettings, settings.platform, BuildSetting.commandLine); 308 309 auto generate_binary = !buildsettings.dflags.canFind("-o-"); 310 311 // Create start script, which will be used by the calling bash/cmd script. 312 // build "rdmd --force %DFLAGS% -I%~dp0..\source -Jviews -Isource @deps.txt %LIBS% source\app.d" ~ application arguments 313 // or with "/" instead of "\" 314 bool tmp_target = false; 315 if (generate_binary) { 316 if (settings.tempBuild || (settings.run && !isWritableDir(NativePath(buildsettings.targetPath), true))) { 317 import std.random; 318 auto rnd = to!string(uniform(uint.min, uint.max)) ~ "-"; 319 auto tmpdir = getTempDir()~".rdmd/source/"; 320 buildsettings.targetPath = tmpdir.toNativeString(); 321 buildsettings.targetName = rnd ~ buildsettings.targetName; 322 m_temporaryFiles ~= tmpdir; 323 tmp_target = true; 324 } 325 target_path = getTargetPath(buildsettings, settings); 326 settings.compiler.setTarget(buildsettings, settings.platform); 327 } 328 329 logDiagnostic("Application output name is '%s'", settings.compiler.getTargetFileName(buildsettings, settings.platform)); 330 331 string[] flags = ["--build-only", "--compiler="~settings.platform.compilerBinary]; 332 if (settings.force) flags ~= "--force"; 333 flags ~= buildsettings.dflags; 334 flags ~= mainsrc.relativeTo(cwd).toNativeString(); 335 336 if (buildsettings.preBuildCommands.length){ 337 logInfo("Pre-build", Color.light_green, "Running commands"); 338 runCommands(buildsettings.preBuildCommands); 339 } 340 341 logInfo("Building", Color.light_green, "%s %s [%s]", pack.name.color(Mode.bold), pack.version_, config.color(Color.blue)); 342 343 logInfo("Running rdmd..."); 344 logDiagnostic("rdmd %s", join(flags, " ")); 345 auto rdmd_pid = spawnProcess("rdmd" ~ flags); 346 auto result = rdmd_pid.wait(); 347 enforce(result == 0, "Build command failed with exit code "~to!string(result)); 348 349 if (tmp_target) { 350 m_temporaryFiles ~= target_path; 351 foreach (f; buildsettings.copyFiles) 352 m_temporaryFiles ~= NativePath(buildsettings.targetPath).parentPath ~ NativePath(f).head; 353 } 354 } 355 356 private void performDirectBuild(GeneratorSettings settings, ref BuildSettings buildsettings, in Package pack, string config, out NativePath target_path) 357 { 358 auto cwd = NativePath(getcwd()); 359 auto generate_binary = !(buildsettings.options & BuildOption.syntaxOnly); 360 361 // make file paths relative to shrink the command line 362 foreach (ref f; buildsettings.sourceFiles) { 363 auto fp = NativePath(f); 364 if( fp.absolute ) fp = fp.relativeTo(cwd); 365 f = fp.toNativeString(); 366 } 367 368 logInfo("Building", Color.light_green, "%s %s [%s]", pack.name.color(Mode.bold), pack.version_, config.color(Color.blue)); 369 370 // make all target/import paths relative 371 string makeRelative(string path) { 372 auto p = NativePath(path); 373 // storing in a separate temprary to work around #601 374 auto prel = p.absolute ? p.relativeTo(cwd) : p; 375 return prel.toNativeString(); 376 } 377 buildsettings.targetPath = makeRelative(buildsettings.targetPath); 378 foreach (ref p; buildsettings.importPaths) p = makeRelative(p); 379 foreach (ref p; buildsettings.stringImportPaths) p = makeRelative(p); 380 381 bool is_temp_target = false; 382 if (generate_binary) { 383 if (settings.tempBuild || (settings.run && !isWritableDir(NativePath(buildsettings.targetPath), true))) { 384 import std.random; 385 auto rnd = to!string(uniform(uint.min, uint.max)); 386 auto tmppath = getTempDir()~("dub/"~rnd~"/"); 387 buildsettings.targetPath = tmppath.toNativeString(); 388 m_temporaryFiles ~= tmppath; 389 is_temp_target = true; 390 } 391 target_path = getTargetPath(buildsettings, settings); 392 } 393 394 if( buildsettings.preBuildCommands.length ){ 395 logInfo("Pre-build", Color.light_green, "Running commands"); 396 runBuildCommands(CommandType.preBuild, buildsettings.preBuildCommands, pack, m_project, settings, buildsettings); 397 } 398 399 buildWithCompiler(settings, buildsettings); 400 401 if (is_temp_target) { 402 m_temporaryFiles ~= target_path; 403 foreach (f; buildsettings.copyFiles) 404 m_temporaryFiles ~= NativePath(buildsettings.targetPath).parentPath ~ NativePath(f).head; 405 } 406 } 407 408 private string computeBuildID(string config, in BuildSettings buildsettings, GeneratorSettings settings) 409 { 410 const(string[])[] hashing = [ 411 buildsettings.versions, 412 buildsettings.debugVersions, 413 buildsettings.dflags, 414 buildsettings.lflags, 415 buildsettings.stringImportPaths, 416 buildsettings.importPaths, 417 settings.platform.architecture, 418 [ 419 (cast(uint)(buildsettings.options & ~BuildOption.color)).to!string, // exclude color option from id 420 settings.platform.compilerBinary, 421 settings.platform.compiler, 422 settings.platform.compilerVersion, 423 ], 424 ]; 425 426 return computeBuildName(config, settings, hashing); 427 } 428 429 private void copyTargetFile(in NativePath build_path, in BuildSettings buildsettings, in GeneratorSettings settings) 430 { 431 if (!existsFile(NativePath(buildsettings.targetPath))) 432 mkdirRecurse(buildsettings.targetPath); 433 434 string[] filenames = [ 435 settings.compiler.getTargetFileName(buildsettings, settings.platform) 436 ]; 437 438 // Windows: add .pdb (for executables and DLLs) and/or import .lib & .exp (for DLLs) if found 439 if (settings.platform.isWindows()) { 440 void addIfFound(string extension) { 441 import std.path : setExtension; 442 const candidate = filenames[0].setExtension(extension); 443 if (existsFile(build_path ~ candidate)) 444 filenames ~= candidate; 445 } 446 447 const tt = buildsettings.targetType; 448 if (tt == TargetType.executable || tt == TargetType.dynamicLibrary) 449 addIfFound(".pdb"); 450 451 if (tt == TargetType.dynamicLibrary) { 452 addIfFound(".lib"); 453 addIfFound(".exp"); 454 } 455 } 456 457 foreach (filename; filenames) 458 { 459 auto src = build_path ~ filename; 460 logDiagnostic("Copying target from %s to %s", src.toNativeString(), buildsettings.targetPath); 461 hardLinkFile(src, NativePath(buildsettings.targetPath) ~ filename, true); 462 } 463 } 464 465 private bool isUpToDate(NativePath target_path, BuildSettings buildsettings, GeneratorSettings settings, in Package main_pack, in Package[] packages, in NativePath[] additional_dep_files) 466 { 467 import std.datetime; 468 469 auto targetfile = target_path ~ settings.compiler.getTargetFileName(buildsettings, settings.platform); 470 if (!existsFile(targetfile)) { 471 logDiagnostic("Target '%s' doesn't exist, need rebuild.", targetfile.toNativeString()); 472 return false; 473 } 474 auto targettime = getFileInfo(targetfile).timeModified; 475 476 auto allfiles = appender!(string[]); 477 allfiles ~= buildsettings.sourceFiles; 478 allfiles ~= buildsettings.importFiles; 479 allfiles ~= buildsettings.stringImportFiles; 480 allfiles ~= buildsettings.extraDependencyFiles; 481 // TODO: add library files 482 foreach (p; packages) 483 allfiles ~= (p.recipePath != NativePath.init ? p : p.basePackage).recipePath.toNativeString(); 484 foreach (f; additional_dep_files) allfiles ~= f.toNativeString(); 485 bool checkSelectedVersions = !settings.single; 486 if (checkSelectedVersions && main_pack is m_project.rootPackage && m_project.rootPackage.getAllDependencies().length > 0) 487 allfiles ~= (main_pack.path ~ SelectedVersions.defaultFile).toNativeString(); 488 489 foreach (file; allfiles.data) { 490 if (!existsFile(file)) { 491 logDiagnostic("File %s doesn't exist, triggering rebuild.", file); 492 return false; 493 } 494 auto ftime = getFileInfo(file).timeModified; 495 if (ftime > Clock.currTime) 496 logWarn("File '%s' was modified in the future. Please re-save.", file); 497 if (ftime > targettime) { 498 logDiagnostic("File '%s' modified, need rebuild.", file); 499 return false; 500 } 501 } 502 return true; 503 } 504 505 /// Output an unique name to represent the source file. 506 /// Calls with path that resolve to the same file on the filesystem will return the same, 507 /// unless they include different symbolic links (which are not resolved). 508 509 static string pathToObjName(const scope ref BuildPlatform platform, string path) 510 { 511 import std.digest.crc : crc32Of; 512 import std.path : buildNormalizedPath, dirSeparator, relativePath, stripDrive; 513 if (path.endsWith(".d")) path = path[0 .. $-2]; 514 auto ret = buildNormalizedPath(getcwd(), path).replace(dirSeparator, "."); 515 auto idx = ret.lastIndexOf('.'); 516 const objSuffix = getObjSuffix(platform); 517 return idx < 0 ? ret ~ objSuffix : format("%s_%(%02x%)%s", ret[idx+1 .. $], crc32Of(ret[0 .. idx]), objSuffix); 518 } 519 520 /// Compile a single source file (srcFile), and write the object to objName. 521 static string compileUnit(string srcFile, string objName, BuildSettings bs, GeneratorSettings gs) { 522 NativePath tempobj = NativePath(bs.targetPath)~objName; 523 string objPath = tempobj.toNativeString(); 524 bs.libs = null; 525 bs.lflags = null; 526 bs.sourceFiles = [ srcFile ]; 527 bs.targetType = TargetType.object; 528 gs.compiler.prepareBuildSettings(bs, gs.platform, BuildSetting.commandLine); 529 gs.compiler.setTarget(bs, gs.platform, objPath); 530 gs.compiler.invoke(bs, gs.platform, gs.compileCallback); 531 return objPath; 532 } 533 534 private void buildWithCompiler(GeneratorSettings settings, BuildSettings buildsettings) 535 { 536 auto generate_binary = !(buildsettings.options & BuildOption.syntaxOnly); 537 auto is_static_library = buildsettings.targetType == TargetType.staticLibrary || buildsettings.targetType == TargetType.library; 538 539 scope (failure) { 540 logDiagnostic("FAIL %s %s %s" , buildsettings.targetPath, buildsettings.targetName, buildsettings.targetType); 541 auto tpath = getTargetPath(buildsettings, settings); 542 if (generate_binary && existsFile(tpath)) 543 removeFile(tpath); 544 } 545 if (settings.buildMode == BuildMode.singleFile && generate_binary) { 546 import std.parallelism, std.range : walkLength; 547 548 auto lbuildsettings = buildsettings; 549 auto srcs = buildsettings.sourceFiles.filter!(f => !isLinkerFile(settings.platform, f)); 550 auto objs = new string[](srcs.walkLength); 551 552 void compileSource(size_t i, string src) { 553 logInfo("Compiling", Color.light_green, "%s", src); 554 const objPath = pathToObjName(settings.platform, src); 555 objs[i] = compileUnit(src, objPath, buildsettings, settings); 556 } 557 558 if (settings.parallelBuild) { 559 foreach (i, src; srcs.parallel(1)) compileSource(i, src); 560 } else { 561 foreach (i, src; srcs.array) compileSource(i, src); 562 } 563 564 logInfo("Linking", Color.light_green, "%s", buildsettings.targetName.color(Mode.bold)); 565 lbuildsettings.sourceFiles = is_static_library ? [] : lbuildsettings.sourceFiles.filter!(f => isLinkerFile(settings.platform, f)).array; 566 settings.compiler.setTarget(lbuildsettings, settings.platform); 567 settings.compiler.prepareBuildSettings(lbuildsettings, settings.platform, BuildSetting.commandLineSeparate|BuildSetting.sourceFiles); 568 settings.compiler.invokeLinker(lbuildsettings, settings.platform, objs, settings.linkCallback); 569 570 // NOTE: separate compile/link is not yet enabled for GDC. 571 } else if (generate_binary && (settings.buildMode == BuildMode.allAtOnce || settings.compiler.name == "gdc" || is_static_library)) { 572 // don't include symbols of dependencies (will be included by the top level target) 573 if (is_static_library) buildsettings.sourceFiles = buildsettings.sourceFiles.filter!(f => !isLinkerFile(settings.platform, f)).array; 574 575 // setup for command line 576 settings.compiler.setTarget(buildsettings, settings.platform); 577 settings.compiler.prepareBuildSettings(buildsettings, settings.platform, BuildSetting.commandLine); 578 579 // invoke the compiler 580 settings.compiler.invoke(buildsettings, settings.platform, settings.compileCallback); 581 } else { 582 // determine path for the temporary object file 583 string tempobjname = buildsettings.targetName ~ getObjSuffix(settings.platform); 584 NativePath tempobj = NativePath(buildsettings.targetPath) ~ tempobjname; 585 586 // setup linker command line 587 auto lbuildsettings = buildsettings; 588 lbuildsettings.sourceFiles = lbuildsettings.sourceFiles.filter!(f => isLinkerFile(settings.platform, f)).array; 589 if (generate_binary) settings.compiler.setTarget(lbuildsettings, settings.platform); 590 settings.compiler.prepareBuildSettings(lbuildsettings, settings.platform, BuildSetting.commandLineSeparate|BuildSetting.sourceFiles); 591 592 // setup compiler command line 593 buildsettings.libs = null; 594 buildsettings.lflags = null; 595 if (generate_binary) buildsettings.addDFlags("-c", "-of"~tempobj.toNativeString()); 596 buildsettings.sourceFiles = buildsettings.sourceFiles.filter!(f => !isLinkerFile(settings.platform, f)).array; 597 598 settings.compiler.prepareBuildSettings(buildsettings, settings.platform, BuildSetting.commandLine); 599 600 settings.compiler.invoke(buildsettings, settings.platform, settings.compileCallback); 601 602 if (generate_binary) { 603 if (settings.tempBuild) { 604 logInfo("Linking", Color.light_green, "%s => %s", buildsettings.targetName.color(Mode.bold), buildsettings.getTargetPath(settings)); 605 } else { 606 logInfo("Linking", Color.light_green, "%s", buildsettings.targetName.color(Mode.bold)); 607 } 608 settings.compiler.invokeLinker(lbuildsettings, settings.platform, [tempobj.toNativeString()], settings.linkCallback); 609 } 610 } 611 } 612 613 private void runTarget(NativePath exe_file_path, in BuildSettings buildsettings, string[] run_args, GeneratorSettings settings) 614 { 615 if (buildsettings.targetType == TargetType.executable) { 616 auto cwd = NativePath(getcwd()); 617 auto runcwd = cwd; 618 if (buildsettings.workingDirectory.length) { 619 runcwd = NativePath(buildsettings.workingDirectory); 620 if (!runcwd.absolute) runcwd = cwd ~ runcwd; 621 } 622 if (!exe_file_path.absolute) exe_file_path = cwd ~ exe_file_path; 623 runPreRunCommands(m_project.rootPackage, m_project, settings, buildsettings); 624 logInfo("Running", Color.green, "%s %s", exe_file_path.relativeTo(runcwd), run_args.join(" ")); 625 string[string] env; 626 foreach (aa; [buildsettings.environments, buildsettings.runEnvironments]) 627 foreach (k, v; aa) 628 env[k] = v; 629 if (settings.runCallback) { 630 auto res = execute([ exe_file_path.toNativeString() ] ~ run_args, 631 env, Config.none, size_t.max, runcwd.toNativeString()); 632 settings.runCallback(res.status, res.output); 633 settings.targetExitStatus = res.status; 634 runPostRunCommands(m_project.rootPackage, m_project, settings, buildsettings); 635 } else { 636 auto prg_pid = spawnProcess([ exe_file_path.toNativeString() ] ~ run_args, 637 env, Config.none, runcwd.toNativeString()); 638 auto result = prg_pid.wait(); 639 settings.targetExitStatus = result; 640 runPostRunCommands(m_project.rootPackage, m_project, settings, buildsettings); 641 enforce(result == 0, "Program exited with code "~to!string(result)); 642 } 643 } else 644 enforce(false, "Target is a library. Skipping execution."); 645 } 646 647 private void runPreRunCommands(in Package pack, in Project proj, in GeneratorSettings settings, 648 in BuildSettings buildsettings) 649 { 650 if (buildsettings.preRunCommands.length) { 651 logInfo("Pre-run", Color.light_green, "Running commands..."); 652 runBuildCommands(CommandType.preRun, buildsettings.preRunCommands, pack, proj, settings, buildsettings); 653 } 654 } 655 656 private void runPostRunCommands(in Package pack, in Project proj, in GeneratorSettings settings, 657 in BuildSettings buildsettings) 658 { 659 if (buildsettings.postRunCommands.length) { 660 logInfo("Post-run", Color.light_green, "Running commands..."); 661 runBuildCommands(CommandType.postRun, buildsettings.postRunCommands, pack, proj, settings, buildsettings); 662 } 663 } 664 665 private void cleanupTemporaries() 666 { 667 foreach_reverse (f; m_temporaryFiles) { 668 try { 669 if (f.endsWithSlash) rmdir(f.toNativeString()); 670 else remove(f.toNativeString()); 671 } catch (Exception e) { 672 logWarn("Failed to remove temporary file '%s': %s", f.toNativeString(), e.msg); 673 logDiagnostic("Full error: %s", e.toString().sanitize); 674 } 675 } 676 m_temporaryFiles = null; 677 } 678 } 679 680 private NativePath getMainSourceFile(in Package prj) 681 { 682 foreach (f; ["source/app.d", "src/app.d", "source/"~prj.name~".d", "src/"~prj.name~".d"]) 683 if (existsFile(prj.path ~ f)) 684 return prj.path ~ f; 685 return prj.path ~ "source/app.d"; 686 } 687 688 private NativePath getTargetPath(const scope ref BuildSettings bs, const scope ref GeneratorSettings settings) 689 { 690 return NativePath(bs.targetPath) ~ settings.compiler.getTargetFileName(bs, settings.platform); 691 } 692 693 private string shrinkPath(NativePath path, NativePath base) 694 { 695 auto orig = path.toNativeString(); 696 if (!path.absolute) return orig; 697 version (Windows) 698 { 699 // avoid relative paths starting with `..\`: https://github.com/dlang/dub/issues/2143 700 if (!path.startsWith(base)) return orig; 701 } 702 auto rel = path.relativeTo(base).toNativeString(); 703 return rel.length < orig.length ? rel : orig; 704 } 705 706 unittest { 707 assert(shrinkPath(NativePath("/foo/bar/baz"), NativePath("/foo")) == NativePath("bar/baz").toNativeString()); 708 version (Windows) 709 assert(shrinkPath(NativePath("/foo/bar/baz"), NativePath("/foo/baz")) == NativePath("/foo/bar/baz").toNativeString()); 710 else 711 assert(shrinkPath(NativePath("/foo/bar/baz"), NativePath("/foo/baz")) == NativePath("../bar/baz").toNativeString()); 712 assert(shrinkPath(NativePath("/foo/bar/baz"), NativePath("/bar/")) == NativePath("/foo/bar/baz").toNativeString()); 713 assert(shrinkPath(NativePath("/foo/bar/baz"), NativePath("/bar/baz")) == NativePath("/foo/bar/baz").toNativeString()); 714 } 715 716 unittest { // issue #1235 - pass no library files to compiler command line when building a static lib 717 import dub.internal.vibecompat.data.json : parseJsonString; 718 import dub.compilers.gdc : GDCCompiler; 719 import dub.platform : determinePlatform; 720 721 version (Windows) auto libfile = "bar.lib"; 722 else auto libfile = "bar.a"; 723 724 auto desc = parseJsonString(`{"name": "test", "targetType": "library", "sourceFiles": ["foo.d", "`~libfile~`"]}`); 725 auto pack = new Package(desc, NativePath("/tmp/fooproject")); 726 auto pman = new PackageManager(pack.path, NativePath("/tmp/foo/"), NativePath("/tmp/foo/"), false); 727 auto prj = new Project(pman, pack); 728 729 final static class TestCompiler : GDCCompiler { 730 override void invoke(in BuildSettings settings, in BuildPlatform platform, void delegate(int, string) output_callback) { 731 assert(!settings.dflags[].any!(f => f.canFind("bar"))); 732 } 733 override void invokeLinker(in BuildSettings settings, in BuildPlatform platform, string[] objects, void delegate(int, string) output_callback) { 734 assert(false); 735 } 736 } 737 738 GeneratorSettings settings; 739 settings.platform = BuildPlatform(determinePlatform(), ["x86"], "gdc", "test", 2075); 740 settings.compiler = new TestCompiler; 741 settings.config = "library"; 742 settings.buildType = "debug"; 743 settings.tempBuild = true; 744 745 auto gen = new BuildGenerator(prj); 746 gen.generate(settings); 747 }