summaryrefslogtreecommitdiff
path: root/task_ld.cc
diff options
context:
space:
mode:
authorBent Bisballe Nyeng <deva@aasimon.org>2021-06-15 19:14:45 +0200
committerBent Bisballe Nyeng <deva@aasimon.org>2021-06-15 19:14:45 +0200
commit33addfbf9cc21cd69b3d6476eb0c062bb2c6fcfb (patch)
treeeb7c6e6ff474c8b437071aaa59c4c8bc10f88d09 /task_ld.cc
parentf4eb3c80e63b5323dbb19b1103ba1db2dd1c4c41 (diff)
Re-link if link flags change.
Diffstat (limited to 'task_ld.cc')
-rw-r--r--task_ld.cc66
1 files changed, 64 insertions, 2 deletions
diff --git a/task_ld.cc b/task_ld.cc
index 0cd4c95..de74af2 100644
--- a/task_ld.cc
+++ b/task_ld.cc
@@ -7,6 +7,22 @@
#include "settings.h"
#include "execute.h"
+namespace
+{
+std::string readFile(const std::string &fileName)
+{
+ std::ifstream ifs(fileName.c_str(), std::ios::in | std::ios::binary | std::ios::ate);
+
+ std::ifstream::pos_type fileSize = ifs.tellg();
+ ifs.seekg(0, std::ios::beg);
+
+ std::vector<char> bytes(fileSize);
+ ifs.read(bytes.data(), fileSize);
+
+ return std::string(bytes.data(), fileSize);
+}
+} // namespace ::
+
TaskLD::TaskLD(const BuildConfiguration& config,
const Settings& settings,
const std::string& target,
@@ -21,6 +37,9 @@ TaskLD::TaskLD(const BuildConfiguration& config,
std::filesystem::path objectFile = object;
objectFiles.push_back(objectFile);
}
+
+ flagsFile = settings.builddir / targetFile.stem();
+ flagsFile += ".flags";
}
bool TaskLD::dirty()
@@ -30,6 +49,11 @@ bool TaskLD::dirty()
return true;
}
+ if(!std::filesystem::exists(flagsFile))
+ {
+ return true;
+ }
+
for(const auto& objectFile : objectFiles)
{
if(std::filesystem::last_write_time(targetFile) <=
@@ -39,6 +63,15 @@ bool TaskLD::dirty()
}
}
+ {
+ auto lastFlags = readFile(flagsFile);
+ if(flagsString() != lastFlags)
+ {
+ //std::cout << "The compiler flags changed\n";
+ return true;
+ }
+ }
+
return false;
}
@@ -66,12 +99,28 @@ int TaskLD::run()
args.push_back("-o");
args.push_back(std::string(targetFile));
+ { // Write flags to file.
+ std::ofstream flagsStream(flagsFile);
+ flagsStream << flagsString();
+ }
+
return execute("/usr/bin/g++", args);
}
int TaskLD::clean()
{
- std::filesystem::remove(targetFile);
+ if(std::filesystem::exists(targetFile))
+ {
+ std::cout << "Removing " << std::string(targetFile) << "\n";
+ std::filesystem::remove(targetFile);
+ }
+
+ if(std::filesystem::exists(flagsFile))
+ {
+ std::cout << "Removing " << std::string(flagsFile) << "\n";
+ std::filesystem::remove(flagsFile);
+ }
+
return 0;
}
@@ -82,6 +131,19 @@ std::vector<std::string> TaskLD::depends() const
std::string TaskLD::target() const
{
- std::cout << "Removing " << std::string(targetFile) << "\n";
return std::string(targetFile);
}
+
+std::string TaskLD::flagsString() const
+{
+ std::string flagsStr;
+ for(const auto& flag : config.ldflags)
+ {
+ if(!flagsStr.empty())
+ {
+ flagsStr += " ";
+ }
+ flagsStr += flag;
+ }
+ return flagsStr;
+}