Skip to content

Commit

Permalink
Added "targetName" and "targetPath" build settings and some fixes.
Browse files Browse the repository at this point in the history
Other changes:
 - Made the comparison for "excludedSourceFiles" path format agnostic
 - Fixed the output directory for the VisualD generator
 - Removed the "binaryPath" global setting (which was undocumented up to now)

See also #26 and #29.
  • Loading branch information
s-ludwig committed Mar 7, 2013
1 parent bda80f1 commit b156eb2
Show file tree
Hide file tree
Showing 12 changed files with 102 additions and 103 deletions.
39 changes: 35 additions & 4 deletions source/dub/compilers/compiler.d
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,16 @@ interface Compiler {
void prepareBuildSettings(ref BuildSettings settings, BuildSetting supported_fields = BuildSetting.all);

/// Adds the appropriate flag to set a target path
void setTarget(ref BuildSettings settings, Path binary_path);
void setTarget(ref BuildSettings settings, in BuildPlatform platform);
}


/// BuildPlatform specific settings, like needed libraries or additional
/// include paths.
struct BuildSettings {
TargetType targetType;
string targetPath;
string targetName;
string[] dflags;
string[] lflags;
string[] libs;
Expand All @@ -81,7 +83,7 @@ struct BuildSettings {
void addLFlags(in string[] value...) { add(lflags, value); }
void addLibs(in string[] value...) { add(libs, value); }
void addSourceFiles(in string[] value...) { add(sourceFiles, value); }
void removeSourceFiles(in string[] value...) { remove(sourceFiles, value); }
void removeSourceFiles(in string[] value...) { removePaths(sourceFiles, value); }
void addCopyFiles(in string[] value...) { add(copyFiles, value); }
void addVersions(in string[] value...) { add(versions, value); }
void addImportPaths(in string[] value...) { add(importPaths, value); }
Expand Down Expand Up @@ -110,9 +112,15 @@ struct BuildSettings {
}
}

private void remove(ref string[] arr, in string[] vals)
private void removePaths(ref string[] arr, in string[] vals)
{
arr = arr.filter!(s => !vals.canFind(s))().array();
bool matches(string s){
foreach( p; vals )
if( Path(s) == Path(p) )
return true;
return false;
}
arr = arr.filter!(s => !matches(s))().array();
}
}

Expand Down Expand Up @@ -150,6 +158,29 @@ enum TargetType {
staticLibrary
}

string getTargetFileName(in BuildSettings settings, in BuildPlatform platform)
{
assert(settings.targetName.length > 0, "No target name set.");
final switch(settings.targetType){
case TargetType.autodetect: assert(false);
case TargetType.sourceLibrary: return null;
case TargetType.executable:
if( platform.platform.canFind("windows") )
return settings.targetName ~ ".exe";
else return settings.targetName;
case TargetType.library:
case TargetType.staticLibrary:
if( platform.platform.canFind("windows") )
return settings.targetName ~ ".lib";
else return "lib" ~ settings.targetName ~ ".a";
case TargetType.dynamicLibrary:
if( platform.platform.canFind("windows") )
return settings.targetName ~ ".dll";
else return "lib" ~ settings.targetName ~ ".so";
}
}



private {
Compiler[] s_compilers;
Expand Down
5 changes: 3 additions & 2 deletions source/dub/compilers/dmd.d
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class DmdCompiler : Compiler {
assert(fields & BuildSetting.copyFiles);
}

void setTarget(ref BuildSettings settings, Path binary_path)
void setTarget(ref BuildSettings settings, in BuildPlatform platform)
{
final switch(settings.targetType){
case TargetType.autodetect: assert(false, "Invalid target type: autodetect");
Expand All @@ -105,6 +105,7 @@ class DmdCompiler : Compiler {
break;
}

settings.addDFlags("-of"~binary_path.toNativeString());
auto tpath = Path(settings.targetPath) ~ getTargetFileName(settings, platform);
settings.addDFlags("-of"~tpath.toNativeString());
}
}
5 changes: 3 additions & 2 deletions source/dub/compilers/gdc.d
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class GdcCompiler : Compiler {
assert(fields & BuildSetting.copyFiles);
}

void setTarget(ref BuildSettings settings, Path binary_path)
void setTarget(ref BuildSettings settings, in BuildPlatform platform)
{
final switch(settings.targetType){
case TargetType.autodetect: assert(false, "Invalid target type: autodetect");
Expand All @@ -147,6 +147,7 @@ class GdcCompiler : Compiler {
break;
}

settings.addDFlags("-o", binary_path.toNativeString());
auto tpath = Path(settings.targetPath) ~ getTargetFileName(settings, platform);
settings.addDFlags("-o", tpath.toNativeString());
}
}
5 changes: 3 additions & 2 deletions source/dub/compilers/ldc.d
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class LdcCompiler : Compiler {
assert(fields & BuildSetting.copyFiles);
}

void setTarget(ref BuildSettings settings, Path binary_path)
void setTarget(ref BuildSettings settings, in BuildPlatform platform)
{
final switch(settings.targetType){
case TargetType.autodetect: assert(false, "Invalid target type: autodetect");
Expand All @@ -103,6 +103,7 @@ class LdcCompiler : Compiler {
break;
}

settings.addDFlags("-of"~binary_path.toNativeString());
auto tpath = Path(settings.targetPath) ~ getTargetFileName(settings, platform);
settings.addDFlags("-of"~tpath.toNativeString());
}
}
2 changes: 0 additions & 2 deletions source/dub/dub.d
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,6 @@ class Dub {

@property inout(PackageManager) packageManager() inout { return m_packageManager; }

@property Path binaryPath() const { return m_project.binaryPath; }

void loadPackageFromCwd()
{
loadPackage(m_cwd);
Expand Down
44 changes: 12 additions & 32 deletions source/dub/generators/build.d
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,8 @@ class BuildGenerator : ProjectGenerator {

void generateProject(GeneratorSettings settings)
{
//Added check for existance of [AppNameInPackagejson].d
//If exists, use that as the starting file.
auto outfile = getBinName(m_project);
auto mainsrc = getMainSourceFile(m_project);
auto cwd = Path(getcwd());

logDebug("Application output name is '%s'", outfile);

auto buildsettings = settings.buildSettings;
m_project.addBuildSettings(buildsettings, settings.platform, settings.config);
buildsettings.addDFlags(["-w"/*, "-property"*/]);
Expand All @@ -72,11 +66,9 @@ class BuildGenerator : ProjectGenerator {
// setup for command line
settings.compiler.prepareBuildSettings(buildsettings, BuildSetting.commandLine);

Path run_exe_file;
Path exe_file_path;
if( generate_binary ){
if( !settings.run ){
settings.compiler.setTarget(buildsettings, m_project.binaryPath~outfile);
} else {
if( settings.run ){
import std.random;
auto rnd = to!string(uniform(uint.min, uint.max)) ~ "-";
auto tmp = environment.get("TEMP");
Expand All @@ -85,10 +77,13 @@ class BuildGenerator : ProjectGenerator {
version(Posix) tmp = "/tmp";
else tmp = ".";
}
run_exe_file = Path(tmp~"/.rdmd/source/"~rnd~outfile);
settings.compiler.setTarget(buildsettings, run_exe_file);
buildsettings.targetPath = Path(tmp~"/.rdmd/source/").toNativeString();
buildsettings.targetName = rnd ~ buildsettings.targetName;
}
exe_file_path = Path(buildsettings.targetPath) ~ getTargetFileName(buildsettings, settings.platform);
settings.compiler.setTarget(buildsettings, settings.platform);
}
logDebug("Application output name is '%s'", exe_file_path.toNativeString());

string[] flags = buildsettings.dflags;

Expand Down Expand Up @@ -127,37 +122,22 @@ class BuildGenerator : ProjectGenerator {
logInfo("Copying files...");
foreach( f; buildsettings.copyFiles ){
auto src = Path(f);
auto dst = (run_exe_file.empty ? m_project.binaryPath : run_exe_file.parentPath) ~ Path(f).head;
auto dst = exe_file_path.parentPath ~ Path(f).head;
logDebug(" %s to %s", src.toNativeString(), dst.toNativeString());
try copyFile(src, dst, true);
catch logWarn("Failed to copy to %s", dst.toNativeString());
}
}

if( settings.run ){
auto prg_pid = spawnProcess(run_exe_file.toNativeString(), settings.runArgs);
logDebug("Running %s...", exe_file_path.toNativeString());
auto prg_pid = spawnProcess(exe_file_path.toNativeString(), settings.runArgs);
result = prg_pid.wait();
remove(run_exe_file.toNativeString());
remove(exe_file_path.toNativeString());
foreach( f; buildsettings.copyFiles )
remove((run_exe_file.parentPath ~ Path(f).head).toNativeString());
remove((exe_file_path.parentPath ~ Path(f).head).toNativeString());
enforce(result == 0, "Program exited with code "~to!string(result));
}
}
}
}

private string getBinName(in Project prj)
{
// take the project name as the base or fall back to "app"
string ret = prj.name;
if( ret.length == 0 ) ret ="app";
version(Windows) { ret ~= ".exe"; }
return ret;
}

private Path getMainSourceFile(in Project prj)
{
auto p = Path("source") ~ (prj.name ~ ".d");
return existsFile(p) ? p : Path("source/app.d");
}

12 changes: 6 additions & 6 deletions source/dub/generators/generator.d
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,23 @@ struct GeneratorSettings {
*/
ProjectGenerator createProjectGenerator(string generator_type, Project app, PackageManager mgr)
{
enforce(app !is null, "app==null, Need an application to work on!");
enforce(mgr !is null, "mgr==null, Need a package manager to work on!");
assert(app !is null && mgr !is null, "Project and package manager needed to create a generator.");

generator_type = generator_type.toLower();
switch(generator_type) {
default:
throw new Exception("Unknown project generator: "~generator_type);
case "build":
logTrace("Generating build generator.");
logTrace("Creating build generator.");
return new BuildGenerator(app, mgr);
case "rdmd":
logTrace("Generating rdmd generator.");
logTrace("Creating rdmd generator.");
return new RdmdGenerator(app, mgr);
case "mono-d":
logTrace("Generating MonoD generator.");
logTrace("Creating MonoD generator.");
return new MonoDGenerator(app, mgr);
case "visuald":
logTrace("Generating VisualD generator.");
logTrace("Creating VisualD generator.");
return new VisualDGenerator(app, mgr);
}
}
Expand Down
4 changes: 2 additions & 2 deletions source/dub/generators/monod.d
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,11 @@ class MonoDGenerator : ProjectGenerator {
config.configName, config.platformName);

sln.put(" <DebugSymbols>True</DebugSymbols>\n");
auto outpath = pack.binaryPath.relativeTo(pack.path).toNativeString();
auto outpath = Path(buildsettings.targetPath).toNativeString();
sln.formattedWrite(" <OutputPath>%s</OutputPath>\n", outpath.length ? outpath : ".");
sln.put(" <Externalconsole>True</Externalconsole>\n");
sln.put(" <Target>Executable</Target>\n");
sln.formattedWrite(" <OutputName>%s</OutputName>\n", pack.name);
sln.formattedWrite(" <OutputName>%s</OutputName>\n", buildsettings.targetName);
sln.put(" <UnittestMode>False</UnittestMode>\n");
sln.formattedWrite(" <ObjectsDirectory>%s</ObjectsDirectory>\n", (Path("obj/")~config.configName).toNativeString());
sln.put(" <DebugLevel>0</DebugLevel>\n");
Expand Down
39 changes: 12 additions & 27 deletions source/dub/generators/rdmd.d
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,8 @@ class RdmdGenerator : ProjectGenerator {
{
//Added check for existance of [AppNameInPackagejson].d
//If exists, use that as the starting file.
auto outfile = getBinName(m_project);
auto mainsrc = getMainSourceFile(m_project);

logDebug("Application output name is '%s'", outfile);

auto buildsettings = settings.buildSettings;
m_project.addBuildSettings(buildsettings, settings.platform, settings.config);
// do not pass all source files to RDMD, only the main source file
Expand All @@ -69,9 +66,7 @@ class RdmdGenerator : ProjectGenerator {
// or with "/" instead of "\"
Path run_exe_file;
if( generate_binary ){
if( !settings.run ){
settings.compiler.setTarget(buildsettings, m_project.binaryPath~outfile);
} else {
if( settings.run ){
import std.random;
auto rnd = to!string(uniform(uint.min, uint.max)) ~ "-";
auto tmp = environment.get("TEMP");
Expand All @@ -80,11 +75,15 @@ class RdmdGenerator : ProjectGenerator {
version(Posix) tmp = "/tmp";
else tmp = ".";
}
run_exe_file = Path(tmp~"/.rdmd/source/"~rnd~outfile);
settings.compiler.setTarget(buildsettings, run_exe_file);
buildsettings.targetPath = (Path(tmp)~"/.rdmd/source/").toNativeString();
buildsettings.targetName = rnd ~ buildsettings.targetName;
run_exe_file = Path(buildsettings.targetPath) ~ getTargetFileName(buildsettings, settings.platform);
}
settings.compiler.setTarget(buildsettings, settings.platform);
}

logDebug("Application output name is '%s'", getTargetFileName(buildsettings, settings.platform));

string[] flags = ["--build-only", "--compiler="~settings.compilerBinary];
flags ~= buildsettings.dflags;
flags ~= (mainsrc).toNativeString();
Expand Down Expand Up @@ -124,7 +123,7 @@ class RdmdGenerator : ProjectGenerator {
logInfo("Copying files...");
foreach( f; buildsettings.copyFiles ){
auto src = Path(f);
auto dst = (run_exe_file.empty ? m_project.binaryPath : run_exe_file.parentPath) ~ Path(f).head;
auto dst = (run_exe_file.empty ? Path(buildsettings.targetPath) : run_exe_file.parentPath) ~ Path(f).head;
logDebug(" %s to %s", src.toNativeString(), dst.toNativeString());
try copyFile(src, dst, true);
catch logWarn("Failed to copy to %s", dst.toNativeString());
Expand All @@ -143,24 +142,10 @@ class RdmdGenerator : ProjectGenerator {
}
}

private string getBinName(in Project prj)
{
// take the project name as the base or fall back to "app"
string ret = prj.name;
if( ret.length == 0 ) ret ="app";
version(Windows) { ret ~= ".exe"; }
return ret;
}

private Path getMainSourceFile(in Project prj)
{
foreach(p; ["source", "src", "."]){
foreach(f; [prj.name, "app"]){
auto fp = Path(p) ~ (f ~ ".d");
if( existsFile(fp) )
return fp;
}
}
return Path("app.d");
foreach( f; ["source/app.d", "src/app.d", "source/"~prj.name~".d", "src/"~prj.name~".d"])
if( exists(f) )
return Path(f);
return Path("source/app.d");
}

8 changes: 4 additions & 4 deletions source/dub/generators/visuald.d
Original file line number Diff line number Diff line change
Expand Up @@ -281,13 +281,13 @@ EndGlobal");
<optimize>%s</optimize>", type == Config.Release? "1":"0");

// Lib or exe?
bool createLib = pack != m_app.mainPackage();
string libIdentifier = createLib? "1" : "0";
bool is_lib = buildsettings.targetType == TargetType.executable;
string debugSuffix = type == Config.Debug? "_d" : "";
string extension = createLib? "lib" : "exe";
auto bin_path = Path(buildsettings.targetPath);
bin_path.endsWithSlash = true;
ret.formattedWrite("
<lib>%s</lib>
<exefile>bin\\$(ProjectName)%s.%s</exefile>", libIdentifier, debugSuffix, extension);
<exefile>%s%s%s.%s</exefile>", is_lib ? "1" : "0", bin_path.toNativeString(), buildsettings.targetName, debugSuffix, is_lib ? "lib" : "exe");

// include paths and string imports
string imports = join(getSettings!"importPaths"(), " ");
Expand Down
Loading

0 comments on commit b156eb2

Please sign in to comment.