torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Spartan6BuildHelper.cpp
Go to the documentation of this file.
1 // Torc - Copyright 2011-2013 University of Southern California. All Rights Reserved.
2 // $HeadURL$
3 // $Id$
4 
5 // This program is free software: you can redistribute it and/or modify it under the terms of the
6 // GNU General Public License as published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
11 // the GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License along with this program. If
14 // not, see <http://www.gnu.org/licenses/>.
15 
16 /// \file
17 /// \brief Source for the Spartan6BuildHelper class.
18 
22 #include <boost/filesystem.hpp>
23 #include <boost/regex.hpp>
24 #include <iostream>
25 #include <sstream>
26 #include <fstream>
27 
28 namespace torc {
29 namespace bitstream {
30 
32  const std::string& inTemplateName, const std::string& inOutputName,
33  const torc::common::DeviceVector& inDeviceNames, Bitstream& inBitstream) {
34 
35  // preflight and open the output file
36  boost::filesystem::path outputPath
37  = torc::common::DirectoryTree::getWorkingPath() / "torc" / "bitstream" / inOutputName;
38 /// \todo Reinstate check for existing static device information file.
39 // if(boost::filesystem::exists(outputPath)) {
40 // std::cerr << "Existing static device information file will not be replaced: "
41 // << outputPath.string() << std::endl;
42 // return;
43 // }
44  std::ofstream outputStream(outputPath.string().c_str(), std::ios::out | std::ios::binary);
45  if(!outputStream.is_open()) {
46  std::cerr << "Output static device information file could not be opened: "
47  << outputPath.string() << std::endl;
48  return;
49  }
50 
51  // read the device info template
53  / "torc" / "bitstream" / "build" / inTemplateName;
54  std::ifstream templateStream(templatePath.string().c_str(),
55  std::ios::in | std::ios::binary | std::ios::ate);
56  if(!templateStream.is_open()) {
57  std::cerr << "Input static device information template could not be opened: "
58  << outputPath.string() << std::endl;
59  return;
60  }
61  std::string templateString;
62  // resize the string according to the file length
63  std::ifstream::pos_type size = templateStream.tellg();
64  templateString.resize(size);
65  // rewind to the beginning and read the entire file directly into the string
66  templateStream.seekg(0, std::ios::beg);
67  templateStream.read(const_cast<char*>(templateString.data()), size);
68  // close close file
69  templateStream.close();
70 
71  // iterate over all devices
72  std::stringstream allDeviceInfo;
73  torc::common::DeviceVector::const_iterator dp = inDeviceNames.begin();
74  torc::common::DeviceVector::const_iterator de = inDeviceNames.end();
75  while(dp < de) {
76  const std::string& device = *dp++;
77  if(device.empty()) break;
78  inBitstream.initializeDeviceInfo(device);
79  inBitstream.writeDeviceInfo(allDeviceInfo, device);
80  if(dp < de) allDeviceInfo << std::endl;
81  }
82 
83  // define the substitutions
84  typedef std::map<std::string, std::string> SubstitutionMap;
85  SubstitutionMap substitutions;
86  substitutions["%%ARCHITECTURE%%"] = inFamilyName;
87  substitutions["%%DEVICES%%"] = allDeviceInfo.str();
88  substitutions["%%GENERATED%%"] = __FILE__;
89 
90  // replace all substitution strings in the template
91  SubstitutionMap::iterator p = substitutions.begin();
92  SubstitutionMap::iterator e = substitutions.end();
93  while(p != e) {
94  // create the regular expression, and prepare to replace
95  boost::regex re(p->first);
96  std::stringstream stringStream;
97  std::ostream_iterator<char, char> stringIterator(stringStream);
98  // replace all occurrences of the current substitution pattern
99  boost::regex_replace(stringIterator, templateString.begin(), templateString.end(), re,
100  p->second, boost::match_default);
101  // copy the results back into place
102  templateString = stringStream.str();
103  p++;
104  }
105 
106  // write the output file
107  outputStream << templateString;
108  outputStream.close();
109 
110  }
111 
112 } // namespace bitstream
113 } // namespace torc
Header for the DirectoryTree class.
std::string string
Xilinx bitstream base class.
Header for the Spartan6BuildHelper class.
virtual void initializeDeviceInfo(const std::string &inDeviceName)
Initialize the maps between frame indexes and frame addresses. This is generally only useful for int...
boost::filesystem::path path
static const boost::filesystem::path & getWorkingPath(void)
Returns the absolute path to the working directory.
static void buildFamilyDeviceInfo(const std::string &inFamilyName, const std::string &inTemplateName, const std::string &inOutputName, const torc::common::DeviceVector &inDeviceNames, Bitstream &inBitstream)
Build the static device information for the specified family.
std::vector< std::string > DeviceVector
Vector of device names.
Definition: Devices.hpp:119
virtual void writeDeviceInfo(std::ostream &inStream, const std::string &inDeviceName)
Output static device information to a stream.
Definition: Bitstream.cpp:74
Header for the DeviceInfo class.