Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/tools/bcp/add_path.cpp @ 30

Last change on this file since 30 was 29, checked in by landauf, 17 years ago

updated boost from 1_33_1 to 1_34_1

File size: 15.1 KB
Line 
1/*
2 *
3 * Copyright (c) 2003 Dr John Maddock
4 * Use, modification and distribution is subject to the
5 * Boost Software License, Version 1.0. (See accompanying file
6 * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 *
8 * This file implements the following:
9 *    void bcp_implementation::add_path(const fs::path& p)
10 *    void bcp_implementation::add_directory(const fs::path& p)
11 *    void bcp_implementation::add_file(const fs::path& p)
12 *    void bcp_implementation::add_dependent_lib(const std::string& libname)
13 */
14
15#include "bcp_imp.hpp"
16#include "fileview.hpp"
17#include <boost/regex.hpp>
18#include <boost/filesystem/operations.hpp>
19#include <boost/filesystem/exception.hpp>
20#include <iostream>
21
22
23void bcp_implementation::add_path(const fs::path& p)
24{
25   fs::path normalized_path = p;
26   normalized_path.normalize();
27   if(fs::exists(m_boost_path / normalized_path))
28   {
29      if(fs::is_directory(m_boost_path / normalized_path))
30         add_directory(normalized_path);
31      else
32         add_file(normalized_path);
33   }
34   else
35   {
36      std::cerr << "CAUTION: dependent file " << p.string() << " does not exist." << std::endl;
37   }
38}
39
40void bcp_implementation::add_directory(const fs::path& p)
41{
42   //
43   // don't add files created by build system
44   //
45   if((p.leaf() == "bin") || (p.leaf() == "bin-stage"))
46      return; 
47   //
48   // don't add directories not under version control:
49   //
50   if(m_cvs_mode && !fs::exists(m_boost_path / p / "CVS/Entries"))
51      return;
52   //
53   // enermerate files and directories:
54   //
55   fs::directory_iterator i(m_boost_path / p);
56   fs::directory_iterator j;
57   while(i != j)
58   {
59      //
60      // we need to convert *i back into
61      // a relative path, what follows is a hack:
62      //
63      std::string s(i->string());
64      if(m_boost_path.string().size())
65         s.erase(0, m_boost_path.string().size() + 1);
66      if(!m_dependencies.count(fs::path(s))) 
67         m_dependencies[fs::path(s)] = p; // set up dependency tree
68      add_path(fs::path(s));
69      ++i;
70   }
71}
72
73void bcp_implementation::add_file(const fs::path& p)
74{
75   //
76   // if the file does not exist in cvs then don't do anything with it:
77   //
78   if(m_cvs_mode && (m_cvs_paths.find(p) == m_cvs_paths.end()))
79      return;
80   //
81   // if we've already seen the file return:
82   //
83   if(m_copy_paths.find(p) != m_copy_paths.end())
84      return;
85   //
86   // add the file to our list:
87   //
88   m_copy_paths.insert(p);
89   //
90   // if this is a source file, scan for dependencies:
91   //
92   if(is_source_file(p))
93   {
94      add_file_dependencies(p, false);
95   }
96   //
97   // if this is a html file, scan for dependencies:
98   //
99   if(is_html_file(p))
100   {
101      static const boost::regex e(
102         "<(?:img[^>]*src=|link[^>]*href=)(\"[^\"]+\"|\\w+)[^>]*>"
103         );
104
105      fileview view(m_boost_path / p);
106      boost::regex_token_iterator<const char*> i(view.begin(), view.end(), e, 1);
107      boost::regex_token_iterator<const char*> j;
108      while(i != j)
109      {
110         //
111         // extract the dependent name:
112         //
113         std::string s(*i);
114         if(s[0] == '\"')
115         {
116            // remove quotes:
117            assert(s.size() > 2);
118            s.erase(0, 1);
119            s.erase(s.size() - 1);
120         }
121         //
122         // if the name starts with ./ remove it
123         // or we'll get an error:
124         if(s.compare(0, 2, "./") == 0)
125            s.erase(0, 2);
126         if(s.find(':') == std::string::npos)
127         {
128            // only concatonate if it's a relative path
129            // rather than a URL:
130            fs::path dep(p.branch_path() / s);
131            if(!m_dependencies.count(dep)) 
132               m_dependencies[dep] = p; // set up dependency tree
133            add_path(dep);
134         }
135         ++i;
136      }
137   }
138   //
139   // now scan for "special" dependencies:
140   // anything that we can't automatically detect...
141   //
142static const std::pair<fs::path, fs::path>
143   specials[] = {
144      std::pair<fs::path, fs::path>("boost/config.hpp", "boost/config"),
145      std::pair<fs::path, fs::path>("tools/build/allyourbase.jam", "Jamrules"),
146      std::pair<fs::path, fs::path>("tools/build/allyourbase.jam", "project-root.jam"),
147      std::pair<fs::path, fs::path>("tools/build/allyourbase.jam", "boost-build.jam"),
148      std::pair<fs::path, fs::path>("tools/build/v1/allyourbase.jam", "Jamrules"),
149      std::pair<fs::path, fs::path>("tools/build/v1/allyourbase.jam", "project-root.jam"),
150      std::pair<fs::path, fs::path>("tools/build/v1/allyourbase.jam", "boost-build.jam"),
151      std::pair<fs::path, fs::path>("tools/build/v2/boost-build.jam", "Jamrules"),
152      std::pair<fs::path, fs::path>("tools/build/v2/boost-build.jam", "project-root.jam"),
153      std::pair<fs::path, fs::path>("tools/build/v2/boost-build.jam", "boost-build.jam"),
154      std::pair<fs::path, fs::path>("tools/build/v2/boost-build.jam", "Jamfile.v2"),
155      std::pair<fs::path, fs::path>("boost/preprocessor/iterate.hpp", "boost/preprocessor/iteration"),
156      std::pair<fs::path, fs::path>("boost/preprocessor/slot/slot.hpp", "boost/preprocessor/slot/detail"),
157      std::pair<fs::path, fs::path>("boost/function.hpp", "boost/function/detail"),
158      std::pair<fs::path, fs::path>("boost/regex/config.hpp", "boost/regex/user.hpp"),
159      std::pair<fs::path, fs::path>("boost/signals/signal_template.hpp", "boost/function"),
160      std::pair<fs::path, fs::path>("boost/mpl/list.hpp", "boost/mpl/list"),
161      std::pair<fs::path, fs::path>("boost/mpl/list_c.hpp", "boost/mpl/list"),
162      std::pair<fs::path, fs::path>("boost/mpl/vector.hpp", "boost/mpl/vector"),
163      std::pair<fs::path, fs::path>("boost/mpl/deque.hpp", "boost/mpl/vector"),
164      std::pair<fs::path, fs::path>("boost/mpl/vector_c.hpp", "boost/mpl/vector"),
165      std::pair<fs::path, fs::path>("boost/mpl/map.hpp", "boost/mpl/map"),
166      std::pair<fs::path, fs::path>("boost/mpl/set.hpp", "boost/mpl/set"),
167      std::pair<fs::path, fs::path>("boost/mpl/set_c.hpp", "boost/mpl/set"),
168      std::pair<fs::path, fs::path>("boost/mpl/aux_/include_preprocessed.hpp", "boost/mpl/aux_/preprocessed"),
169      std::pair<fs::path, fs::path>("boost/mpl/vector/aux_/include_preprocessed.hpp", "boost/mpl/vector/aux_/preprocessed"),
170      std::pair<fs::path, fs::path>("boost/mpl/set/aux_/include_preprocessed.hpp", "boost/mpl/set/aux_/preprocessed"),
171      std::pair<fs::path, fs::path>("boost/mpl/map/aux_/include_preprocessed.hpp", "boost/mpl/map/aux_/preprocessed"),
172      std::pair<fs::path, fs::path>("boost/mpl/list/aux_/include_preprocessed.hpp", "boost/mpl/list/aux_/preprocessed"),
173      std::pair<fs::path, fs::path>("libs/graph/src/python/visitor.hpp", "libs/graph/src/python"),
174   };
175
176   for(unsigned int n = 0; n < (sizeof(specials)/sizeof(specials[0])); ++n)
177   {
178      if(0 == compare_paths(specials[n].first, p))
179      {
180         if(!m_dependencies.count(specials[n].second)) 
181            m_dependencies[specials[n].second] = p; // set up dependency tree
182         add_path(specials[n].second);
183      }
184   }
185
186}
187
188void bcp_implementation::add_dependent_lib(const std::string& libname, const fs::path& p)
189{
190   //
191   // if the boost library libname has source associated with it
192   // then add the source to our list:
193   //
194   if(fs::exists(m_boost_path / "libs" / libname / "src"))
195   {
196      if(!m_dependencies.count(fs::path("libs") / libname / "src")) 
197         m_dependencies[fs::path("libs") / libname / "src"] = p; // set up dependency tree
198      add_path(fs::path("libs") / libname / "src");
199      if(fs::exists(m_boost_path / "libs" / libname / "build"))
200      {
201         if(!m_dependencies.count(fs::path("libs") / libname / "build")) 
202            m_dependencies[fs::path("libs") / libname / "build"] = p; // set up dependency tree
203         add_path(fs::path("libs") / libname / "build");
204      }
205   }
206}
207
208void bcp_implementation::add_file_dependencies(const fs::path& p, bool scanfile)
209{
210   static const boost::regex e(
211      "^[[:blank:]]*(?://@bcp[[:blank:]]+([^\\n]*)\n)?#[[:blank:]]*include[[:blank:]]*[\"<]([^\">]+)[\">]"
212      );
213
214   if(!m_dependencies.count(p)) 
215      m_dependencies[p] = p; // set terminal dependency
216
217   fileview view;
218   if(scanfile)
219      view.open(p);
220   else
221      view.open(m_boost_path / p);
222   if(m_license_mode && !scanfile)
223      scan_license(p, view);
224   const int subs[] = { 1, 2 };
225   boost::regex_token_iterator<const char*> i(view.begin(), view.end(), e, subs);
226   boost::regex_token_iterator<const char*> j;
227   while(i != j)
228   {
229      //
230      // *i contains the name of the include file,
231      // check first for a file of that name in the
232      // same directory as the file we are scanning,
233      // and if that fails, then check the boost-root:
234      //
235      fs::path include_file;
236      try{
237         std::string discart_message = *i;
238         ++i;
239         if(discart_message.size())
240         {
241            // The include is optional and should be discarded:
242            std::cout << "Optional functionality won't be copied: " << discart_message << std::endl;
243            std::cout << "Add the file " << *i << " to the list of dependencies to extract to copy this functionality." << std::endl;
244            ++i;
245            continue;
246         }
247         include_file = i->str();
248      }
249      catch(const fs::filesystem_error& e)
250      {
251         std::cerr << "Can't parse filename " << *i << " included by file " << p.string() << std::endl;
252         ++i;
253         continue;
254      }
255      fs::path test_file(m_boost_path / p.branch_path() / include_file);
256      if(fs::exists(test_file) && !fs::is_directory(test_file) && (p.branch_path().string() != "boost"))
257      {
258         if(!m_dependencies.count(p.branch_path() / include_file)) 
259            m_dependencies[p.branch_path() / include_file] = p;
260         add_path(p.branch_path() / include_file);
261      }
262      else if(fs::exists(m_boost_path / include_file))
263      {
264         if(!m_dependencies.count(include_file)) 
265            m_dependencies[include_file] = p;
266         add_path(include_file);
267      }
268      ++i;
269   }
270   //
271   // Now we need to scan for Boost.Preprocessor includes that
272   // are included via preprocessor iteration:
273   //
274   boost::regex ppfiles("^[[:blank:]]*#[[:blank:]]*define[[:blank:]]+(?:BOOST_PP_FILENAME|BOOST_PP_ITERATION_PARAMS|BOOST_PP_INDIRECT_SELF)[^\\n]+?[\"<]([^\">]+)[\">]");
275   i = boost::regex_token_iterator<const char*>(view.begin(), view.end(), ppfiles, 1);
276   while(i != j)
277   {
278      //
279      // *i contains the name of the include file,
280      // check first for a file of that name in the
281      // same directory as the file we are scanning,
282      // and if that fails, then check the boost-root:
283      //
284      fs::path include_file;
285      try{
286         include_file = i->str();
287      }
288      catch(const fs::filesystem_error& e)
289      {
290         std::cerr << "Can't parse filename " << *i << " included by file " << p.string() << std::endl;
291         ++i;
292         continue;
293      }
294      fs::path test_file(m_boost_path / p.branch_path() / include_file);
295      if(fs::exists(test_file) && !fs::is_directory(test_file) && (p.branch_path().string() != "boost"))
296      {
297         if(!m_dependencies.count(p.branch_path() / include_file)) 
298            m_dependencies[p.branch_path() / include_file] = p;
299         add_path(p.branch_path() / include_file);
300      }
301      else if(fs::exists(m_boost_path / include_file))
302      {
303         if(!m_dependencies.count(include_file)) 
304            m_dependencies[include_file] = p;
305         add_path(include_file);
306      }
307      else
308      {
309         std::cerr << "CAUTION: Boost.Preprocessor iterated file " << include_file.string() << " does not exist." << std::endl;
310      }
311      ++i;
312   }
313
314   //
315   // Scan for any #include MACRO includes that we don't recognise.
316   //
317   // Begin by declaring all of the macros that get #included that
318   // we know about and are correctly handled as special cases:
319   //
320   static const std::string known_macros[] = {
321      "BOOST_USER_CONFIG",
322      "BOOST_COMPILER_CONFIG",
323      "BOOST_STDLIB_CONFIG",
324      "BOOST_PLATFORM_CONFIG",
325      "BOOST_PP_FILENAME_1",
326      "BOOST_PP_ITERATION_PARAMS_1",
327      "BOOST_PP_FILENAME_2",
328      "BOOST_PP_ITERATION_PARAMS_2",
329      "BOOST_PP_FILENAME_3",
330      "BOOST_PP_ITERATION_PARAMS_3",
331      "BOOST_PP_FILENAME_4",
332      "BOOST_PP_ITERATION_PARAMS_4",
333      "BOOST_PP_FILENAME_5",
334      "BOOST_PP_ITERATION_PARAMS_5",
335      "BOOST_PP_INDIRECT_SELF",
336      "BOOST_PP_INCLUDE_SELF()",
337      "BOOST_PP_ITERATE",
338      "BOOST_PP_LOCAL_ITERATE",
339      "BOOST_PP_ITERATE()",
340      "BOOST_PP_LOCAL_ITERATE()",
341      "BOOST_PP_ASSIGN_SLOT(1)",
342      "BOOST_PP_ASSIGN_SLOT(2)",
343      "BOOST_PP_ASSIGN_SLOT(3)",
344      "BOOST_PP_ASSIGN_SLOT(4)",
345      "BOOST_PP_ASSIGN_SLOT(5)",
346      "BOOST_ABI_PREFIX",
347      "BOOST_ABI_SUFFIX",
348      "BOOST_PP_STRINGIZE(boost/mpl/aux_/preprocessed/AUX_PREPROCESSED_HEADER)",
349      "BOOST_PP_STRINGIZE(boost/mpl/list/AUX778076_HEADER)",
350      "BOOST_PP_STRINGIZE(boost/mpl/list/AUX778076_LIST_C_HEADER)",
351      "BOOST_PP_STRINGIZE(boost/mpl/list/AUX778076_LIST_HEADER)",
352      "BOOST_PP_STRINGIZE(boost/mpl/map/aux_/preprocessed/AUX778076_HEADER)",
353      "BOOST_PP_STRINGIZE(boost/mpl/map/AUX778076_MAP_HEADER)",
354      "BOOST_PP_STRINGIZE(boost/mpl/set/aux_/preprocessed/AUX778076_HEADER)",
355      "BOOST_PP_STRINGIZE(boost/mpl/set/AUX778076_SET_HEADER)",
356      "BOOST_PP_STRINGIZE(boost/mpl/set/AUX778076_SET_C_HEADER)",
357      "BOOST_PP_STRINGIZE(boost/mpl/vector/AUX778076_VECTOR_HEADER)",
358      "BOOST_PP_STRINGIZE(boost/mpl/vector/aux_/preprocessed/AUX778076_HEADER)",
359      "BOOST_PP_STRINGIZE(boost/mpl/vector/AUX778076_DEQUE_HEADER)",
360      "BOOST_PP_STRINGIZE(boost/mpl/vector/AUX778076_VECTOR_C_HEADER)",
361      "BOOST_REGEX_USER_CONFIG",
362      "BGL_PYTHON_EVENTS_HEADER",
363   };
364
365   boost::regex indirect_includes("^[[:blank:]]*#[[:blank:]]*include[[:blank:]]+([^\"<][^\n]*?)[[:blank:]]*$");
366   i = boost::regex_token_iterator<const char*>(view.begin(), view.end(), indirect_includes, 1);
367   while(i != j)
368   {
369      const std::string* known_macros_end = known_macros + sizeof(known_macros)/sizeof(known_macros[0]);
370      if(known_macros_end == std::find(known_macros, known_macros_end, i->str()))
371      {
372         std::cerr << "CAUTION: don't know how to trace depenencies through macro: " << *i << " in file: " << p.string() << std::endl;
373      }
374      ++i;
375   }
376   //
377   // if the file contains a cpp_main / unit_test_main / test_main
378   // it is dependent upon Boost.test even if it doesn't
379   // include any of the Boost.test headers directly.
380   //
381   static const boost::regex m("^\\s*int\\s+(?:cpp_main|test_main|unit_test_main)");
382   boost::cmatch what;
383   if(boost::regex_search(view.begin(), view.end(), what, m))
384   {
385      add_dependent_lib("test", p);
386   }
387   //
388   // grab the name of the library to which the header belongs,
389   // and if that library has source then add the source to our
390   // list:
391   //
392   // this regex catches boost/libname.hpp or boost/libname/whatever:
393   //
394   static const boost::regex lib1("boost/([^\\./]+)(?!detail).*");
395   boost::smatch swhat;
396   if(boost::regex_match(p.string(), swhat, lib1))
397   {
398      add_dependent_lib(swhat.str(1), p);
399   }
400   //
401   // and this one catches boost/x/y/whatever (for example numeric/ublas):
402   //
403   static const boost::regex lib2("boost/([^/]+/[^/]+)/(?!detail).*");
404   if(boost::regex_match(p.string(), swhat, lib2))
405   {
406      add_dependent_lib(swhat.str(1), p);
407   }
408}
Note: See TracBrowser for help on using the repository browser.