00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "torc/bitstream/build/Spartan6BuildHelper.hpp"
00020 #include "torc/bitstream/DeviceInfo.hpp"
00021 #include "torc/common/DirectoryTree.hpp"
00022 #include <boost/filesystem.hpp>
00023 #include <boost/regex.hpp>
00024 #include <iostream>
00025 #include <sstream>
00026 #include <fstream>
00027
00028 namespace torc {
00029 namespace bitstream {
00030
00031 void Spartan6BuildHelper::buildFamilyDeviceInfo(const std::string& inFamilyName,
00032 const std::string& inTemplateName, const std::string& inOutputName,
00033 const torc::common::DeviceVector& inDeviceNames, Bitstream& inBitstream) {
00034
00035
00036 boost::filesystem::path outputPath
00037 = torc::common::DirectoryTree::getWorkingPath() / "torc" / "bitstream" / inOutputName;
00038
00039
00040
00041
00042
00043
00044 std::ofstream outputStream(outputPath.string().c_str(), std::ios::out | std::ios::binary);
00045 if(!outputStream.is_open()) {
00046 std::cerr << "Output static device information file could not be opened: "
00047 << outputPath.string() << std::endl;
00048 return;
00049 }
00050
00051
00052 boost::filesystem::path templatePath = torc::common::DirectoryTree::getWorkingPath()
00053 / "torc" / "bitstream" / "build" / inTemplateName;
00054 std::ifstream templateStream(templatePath.string().c_str(),
00055 std::ios::in | std::ios::binary | std::ios::ate);
00056 if(!templateStream.is_open()) {
00057 std::cerr << "Input static device information template could not be opened: "
00058 << outputPath.string() << std::endl;
00059 return;
00060 }
00061 std::string templateString;
00062
00063 std::ifstream::pos_type size = templateStream.tellg();
00064 templateString.resize(size);
00065
00066 templateStream.seekg(0, std::ios::beg);
00067 templateStream.read(const_cast<char*>(templateString.data()), size);
00068
00069 templateStream.close();
00070
00071
00072 std::stringstream allDeviceInfo;
00073 torc::common::DeviceVector::const_iterator dp = inDeviceNames.begin();
00074 torc::common::DeviceVector::const_iterator de = inDeviceNames.end();
00075 while(dp < de) {
00076 const std::string& device = *dp++;
00077 if(device.empty()) break;
00078 inBitstream.initializeDeviceInfo(device);
00079 inBitstream.writeDeviceInfo(allDeviceInfo, device);
00080 if(dp < de) allDeviceInfo << std::endl;
00081 }
00082
00083
00084 typedef std::map<std::string, std::string> SubstitutionMap;
00085 SubstitutionMap substitutions;
00086 substitutions["%%ARCHITECTURE%%"] = inFamilyName;
00087 substitutions["%%DEVICES%%"] = allDeviceInfo.str();
00088 substitutions["%%GENERATED%%"] = __FILE__;
00089
00090
00091 SubstitutionMap::iterator p = substitutions.begin();
00092 SubstitutionMap::iterator e = substitutions.end();
00093 while(p != e) {
00094
00095 boost::regex re(p->first);
00096 std::stringstream stringStream;
00097 std::ostream_iterator<char, char> stringIterator(stringStream);
00098
00099 boost::regex_replace(stringIterator, templateString.begin(), templateString.end(), re,
00100 p->second, boost::match_default);
00101
00102 templateString = stringStream.str();
00103 p++;
00104 }
00105
00106
00107 outputStream << templateString;
00108 outputStream.close();
00109
00110 }
00111
00112 }
00113 }