mirror of
https://github.com/ldc-developers/ldc.git
synced 2025-05-08 11:56:12 +03:00
Streamline output filenames and -singleObj behavior some more with DMD
This commit is contained in:
parent
c748eb9ed0
commit
6c03a3d725
4 changed files with 57 additions and 50 deletions
|
@ -1297,18 +1297,10 @@ extern (C++) int mars_mainBody(ref Strings files, ref Strings libmodules)
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (FileName.equals(ext, "exe"))
|
if (FileName.equals(ext, "exe"))
|
||||||
{
|
|
||||||
version (IN_LLVM)
|
|
||||||
{
|
|
||||||
global.params.exefile = files[i];
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
assert(0); // should have already been handled
|
assert(0); // should have already been handled
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
/* Examine extension to see if it is a valid
|
/* Examine extension to see if it is a valid
|
||||||
* D source file extension
|
* D source file extension
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -56,13 +56,8 @@ static void CreateDirectoryOnDisk(llvm::StringRef fileName) {
|
||||||
//////////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
static std::string getOutputName(bool const sharedLib) {
|
static std::string getOutputName(bool const sharedLib) {
|
||||||
if (!sharedLib && global.params.exefile) {
|
if (global.params.exefile)
|
||||||
return global.params.exefile;
|
return global.params.exefile;
|
||||||
}
|
|
||||||
|
|
||||||
if (sharedLib && global.params.objname) {
|
|
||||||
return global.params.objname;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Output name is inferred.
|
// Output name is inferred.
|
||||||
std::string result;
|
std::string result;
|
||||||
|
@ -71,8 +66,7 @@ static std::string getOutputName(bool const sharedLib) {
|
||||||
if (Module::rootModule) {
|
if (Module::rootModule) {
|
||||||
result = Module::rootModule->toChars();
|
result = Module::rootModule->toChars();
|
||||||
} else if (global.params.objfiles->dim) {
|
} else if (global.params.objfiles->dim) {
|
||||||
result = FileName::removeExt(
|
result = FileName::removeExt((*global.params.objfiles)[0]);
|
||||||
static_cast<const char *>(global.params.objfiles->data[0]));
|
|
||||||
} else {
|
} else {
|
||||||
result = "a.out";
|
result = "a.out";
|
||||||
}
|
}
|
||||||
|
@ -80,9 +74,8 @@ static std::string getOutputName(bool const sharedLib) {
|
||||||
const char *extension = nullptr;
|
const char *extension = nullptr;
|
||||||
if (sharedLib) {
|
if (sharedLib) {
|
||||||
extension = global.dll_ext;
|
extension = global.dll_ext;
|
||||||
if (global.params.targetTriple->getOS() != llvm::Triple::Win32) {
|
if (!global.params.mscoff)
|
||||||
result = "lib" + result;
|
result = "lib" + result;
|
||||||
}
|
|
||||||
} else if (global.params.targetTriple->isOSWindows()) {
|
} else if (global.params.targetTriple->isOSWindows()) {
|
||||||
extension = "exe";
|
extension = "exe";
|
||||||
}
|
}
|
||||||
|
@ -92,11 +85,11 @@ static std::string getOutputName(bool const sharedLib) {
|
||||||
// after execution. Make sure the name does not collide with other files
|
// after execution. Make sure the name does not collide with other files
|
||||||
// from other processes by creating a unique filename.
|
// from other processes by creating a unique filename.
|
||||||
llvm::SmallString<128> tempFilename;
|
llvm::SmallString<128> tempFilename;
|
||||||
auto EC = llvm::sys::fs::createTemporaryFile(
|
auto EC = llvm::sys::fs::createTemporaryFile(FileName::name(result.c_str()),
|
||||||
result, extension ? extension : "", tempFilename);
|
extension ? extension : "",
|
||||||
if (!EC) {
|
tempFilename);
|
||||||
|
if (!EC)
|
||||||
result = tempFilename.str();
|
result = tempFilename.str();
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
if (extension) {
|
if (extension) {
|
||||||
result += ".";
|
result += ".";
|
||||||
|
@ -721,28 +714,23 @@ int createStaticLibrary() {
|
||||||
|
|
||||||
// output filename
|
// output filename
|
||||||
std::string libName;
|
std::string libName;
|
||||||
if (global.params.objname) { // explicit
|
if (global.params.libname) { // explicit
|
||||||
libName = global.params.objname;
|
libName = global.params.libname;
|
||||||
} else { // inferred
|
} else { // inferred
|
||||||
// try root module name
|
// try root module name
|
||||||
if (Module::rootModule) {
|
if (Module::rootModule) {
|
||||||
libName = Module::rootModule->toChars();
|
libName = Module::rootModule->toChars();
|
||||||
} else if (global.params.objfiles->dim) {
|
} else if (global.params.objfiles->dim) {
|
||||||
libName = FileName::removeExt(
|
libName = FileName::removeExt((*global.params.objfiles)[0]);
|
||||||
static_cast<const char *>(global.params.objfiles->data[0]));
|
|
||||||
} else {
|
} else {
|
||||||
libName = "a.out";
|
libName = "a.out";
|
||||||
}
|
}
|
||||||
|
libName.push_back('.');
|
||||||
|
libName.append(global.lib_ext);
|
||||||
}
|
}
|
||||||
// KN: The following lines were added to fix a test case failure
|
if (global.params.objdir && !FileName::absolute(libName.c_str()))
|
||||||
// (runnable/test13774.sh).
|
libName = FileName::combine(global.params.objdir, libName.c_str());
|
||||||
// Root cause is that dmd handles it in this why.
|
|
||||||
// As a side effect this change broke compiling with dub.
|
|
||||||
// if (!FileName::absolute(libName.c_str()))
|
|
||||||
// libName = FileName::combine(global.params.objdir, libName.c_str());
|
|
||||||
if (llvm::sys::path::extension(libName).empty()) {
|
|
||||||
libName.append(std::string(".") + global.lib_ext);
|
|
||||||
}
|
|
||||||
if (isTargetWindows) {
|
if (isTargetWindows) {
|
||||||
args.push_back("/OUT:" + libName);
|
args.push_back("/OUT:" + libName);
|
||||||
} else {
|
} else {
|
||||||
|
@ -750,10 +738,8 @@ int createStaticLibrary() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// object files
|
// object files
|
||||||
for (unsigned i = 0; i < global.params.objfiles->dim; i++) {
|
for (unsigned i = 0; i < global.params.objfiles->dim; i++)
|
||||||
const char *p = static_cast<const char *>(global.params.objfiles->data[i]);
|
args.push_back((*global.params.objfiles)[i]);
|
||||||
args.push_back(p);
|
|
||||||
}
|
|
||||||
|
|
||||||
// create path to the library
|
// create path to the library
|
||||||
CreateDirectoryOnDisk(libName);
|
CreateDirectoryOnDisk(libName);
|
||||||
|
|
|
@ -645,16 +645,34 @@ static void parseCommandLine(int argc, char **argv, Strings &sourceFiles,
|
||||||
mRelocModel = llvm::Reloc::PIC_;
|
mRelocModel = llvm::Reloc::PIC_;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (global.params.link && !global.params.dll) {
|
if (global.params.link) {
|
||||||
global.params.exefile = global.params.objname;
|
global.params.exefile = global.params.objname;
|
||||||
if (sourceFiles.dim > 1) {
|
global.params.oneobj = true;
|
||||||
global.params.objname = nullptr;
|
if (global.params.objname) {
|
||||||
|
/* Use this to name the one object file with the same
|
||||||
|
* name as the exe file.
|
||||||
|
*/
|
||||||
|
global.params.objname =
|
||||||
|
FileName::forceExt(global.params.objname, global.obj_ext);
|
||||||
|
/* If output directory is given, use that path rather than
|
||||||
|
* the exe file path.
|
||||||
|
*/
|
||||||
|
if (global.params.objdir) {
|
||||||
|
const char *name = FileName::name(global.params.objname);
|
||||||
|
global.params.objname = FileName::combine(global.params.objdir, name);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else if (global.params.run) {
|
} else if (global.params.run) {
|
||||||
error(Loc(), "flags conflict with -run");
|
error(Loc(), "flags conflict with -run");
|
||||||
} else if (global.params.objname && sourceFiles.dim > 1) {
|
fatal();
|
||||||
if (!(global.params.lib || global.params.dll) && !global.params.oneobj) {
|
} else if (global.params.lib) {
|
||||||
error(Loc(), "multiple source files, but only one .obj name");
|
global.params.libname = global.params.objname;
|
||||||
|
global.params.objname = nullptr;
|
||||||
|
} else {
|
||||||
|
if (global.params.objname && sourceFiles.dim > 1) {
|
||||||
|
global.params.oneobj = true;
|
||||||
|
// error("multiple source files, but only one .obj name");
|
||||||
|
// fatal();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -358,7 +358,8 @@ void writeObjectFile(llvm::Module *m, std::string &filename) {
|
||||||
codegenModule(*gTargetMachine, *m, out,
|
codegenModule(*gTargetMachine, *m, out,
|
||||||
llvm::TargetMachine::CGFT_ObjectFile);
|
llvm::TargetMachine::CGFT_ObjectFile);
|
||||||
} else {
|
} else {
|
||||||
error(Loc(), "cannot write object file: %s", ERRORINFO_STRING(errinfo));
|
error(Loc(), "cannot write object file '%s': %s", filename.c_str(),
|
||||||
|
ERRORINFO_STRING(errinfo));
|
||||||
fatal();
|
fatal();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -399,6 +400,16 @@ void writeModule(llvm::Module *m, std::string filename) {
|
||||||
// eventually do our own path stuff, dmd's is a bit strange.
|
// eventually do our own path stuff, dmd's is a bit strange.
|
||||||
using LLPath = llvm::SmallString<128>;
|
using LLPath = llvm::SmallString<128>;
|
||||||
|
|
||||||
|
// make sure the output directory exists
|
||||||
|
const auto directory = llvm::sys::path::parent_path(filename);
|
||||||
|
if (!directory.empty()) {
|
||||||
|
if (auto ec = llvm::sys::fs::create_directories(directory)) {
|
||||||
|
error(Loc(), "failed to create output directory: %s\n%s",
|
||||||
|
directory.data(), ec.message().c_str());
|
||||||
|
fatal();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// write LLVM bitcode
|
// write LLVM bitcode
|
||||||
if (global.params.output_bc) {
|
if (global.params.output_bc) {
|
||||||
LLPath bcpath(filename);
|
LLPath bcpath(filename);
|
||||||
|
@ -418,11 +429,11 @@ void writeModule(llvm::Module *m, std::string filename) {
|
||||||
if (global.params.output_ll) {
|
if (global.params.output_ll) {
|
||||||
LLPath llpath(filename);
|
LLPath llpath(filename);
|
||||||
llvm::sys::path::replace_extension(llpath, global.ll_ext);
|
llvm::sys::path::replace_extension(llpath, global.ll_ext);
|
||||||
Logger::println("Writing LLVM asm to: %s\n", llpath.c_str());
|
Logger::println("Writing LLVM IR to: %s\n", llpath.c_str());
|
||||||
LLErrorInfo errinfo;
|
LLErrorInfo errinfo;
|
||||||
llvm::raw_fd_ostream aos(llpath.c_str(), errinfo, llvm::sys::fs::F_None);
|
llvm::raw_fd_ostream aos(llpath.c_str(), errinfo, llvm::sys::fs::F_None);
|
||||||
if (aos.has_error()) {
|
if (aos.has_error()) {
|
||||||
error(Loc(), "cannot write LLVM asm file '%s': %s", llpath.c_str(),
|
error(Loc(), "cannot write LLVM IR file '%s': %s", llpath.c_str(),
|
||||||
ERRORINFO_STRING(errinfo));
|
ERRORINFO_STRING(errinfo));
|
||||||
fatal();
|
fatal();
|
||||||
}
|
}
|
||||||
|
@ -438,7 +449,7 @@ void writeModule(llvm::Module *m, std::string filename) {
|
||||||
llvm::sys::fs::createUniqueFile("ldc-%%%%%%%.s", spath);
|
llvm::sys::fs::createUniqueFile("ldc-%%%%%%%.s", spath);
|
||||||
}
|
}
|
||||||
|
|
||||||
Logger::println("Writing native asm to: %s\n", spath.c_str());
|
Logger::println("Writing asm to: %s\n", spath.c_str());
|
||||||
LLErrorInfo errinfo;
|
LLErrorInfo errinfo;
|
||||||
{
|
{
|
||||||
llvm::raw_fd_ostream out(spath.c_str(), errinfo, llvm::sys::fs::F_None);
|
llvm::raw_fd_ostream out(spath.c_str(), errinfo, llvm::sys::fs::F_None);
|
||||||
|
@ -451,7 +462,7 @@ void writeModule(llvm::Module *m, std::string filename) {
|
||||||
codegenModule(*gTargetMachine, *m, out,
|
codegenModule(*gTargetMachine, *m, out,
|
||||||
llvm::TargetMachine::CGFT_AssemblyFile);
|
llvm::TargetMachine::CGFT_AssemblyFile);
|
||||||
} else {
|
} else {
|
||||||
error(Loc(), "cannot write native asm: %s", ERRORINFO_STRING(errinfo));
|
error(Loc(), "cannot write asm: %s", ERRORINFO_STRING(errinfo));
|
||||||
fatal();
|
fatal();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue