diff options
| author | Bent Bisballe Nyeng <deva@aasimon.org> | 2021-06-15 19:14:45 +0200 | 
|---|---|---|
| committer | Bent Bisballe Nyeng <deva@aasimon.org> | 2021-06-15 19:14:45 +0200 | 
| commit | 33addfbf9cc21cd69b3d6476eb0c062bb2c6fcfb (patch) | |
| tree | eb7c6e6ff474c8b437071aaa59c4c8bc10f88d09 | |
| parent | f4eb3c80e63b5323dbb19b1103ba1db2dd1c4c41 (diff) | |
Re-link if link flags change.
| -rw-r--r-- | task_ld.cc | 66 | ||||
| -rw-r--r-- | task_ld.h | 3 | 
2 files changed, 67 insertions, 2 deletions
@@ -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; +} @@ -30,8 +30,11 @@ public:  	std::string target() const override;  private: +	std::string flagsString() const; +  	std::vector<std::filesystem::path> objectFiles;  	std::filesystem::path targetFile; +	std::filesystem::path flagsFile;  	const BuildConfiguration& config;  	const Settings& settings;  | 
