msvc_prj_files_collector.cpp
上传用户:yhdzpy8989
上传日期:2007-06-13
资源大小:13604k
文件大小:13k
源码类别:

生物技术

开发平台:

C/C++

  1. /*
  2.  * ===========================================================================
  3.  * PRODUCTION $Log: msvc_prj_files_collector.cpp,v $
  4.  * PRODUCTION Revision 1000.1  2004/06/01 18:30:58  gouriano
  5.  * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.6
  6.  * PRODUCTION
  7.  * ===========================================================================
  8.  */
  9. /* $Id: msvc_prj_files_collector.cpp,v 1000.1 2004/06/01 18:30:58 gouriano Exp $
  10.  * ===========================================================================
  11.  *
  12.  *                            PUBLIC DOMAIN NOTICE
  13.  *               National Center for Biotechnology Information
  14.  *
  15.  *  This software/database is a "United States Government Work" under the
  16.  *  terms of the United States Copyright Act.  It was written as part of
  17.  *  the author's official duties as a United States Government employee and
  18.  *  thus cannot be copyrighted.  This software/database is freely available
  19.  *  to the public for use. The National Library of Medicine and the U.S.
  20.  *  Government have not placed any restriction on its use or reproduction.
  21.  *
  22.  *  Although all reasonable efforts have been taken to ensure the accuracy
  23.  *  and reliability of the software and data, the NLM and the U.S.
  24.  *  Government do not and cannot warrant the performance or results that
  25.  *  may be obtained by using this software or data. The NLM and the U.S.
  26.  *  Government disclaim all warranties, express or implied, including
  27.  *  warranties of performance, merchantability or fitness for any particular
  28.  *  purpose.
  29.  *
  30.  *  Please cite the author in any work or product based on this material.
  31.  *
  32.  * ===========================================================================
  33.  *
  34.  * Author:  Viatcheslav Gorelenkov
  35.  *
  36.  */
  37. #include <ncbi_pch.hpp>
  38. #include <app/project_tree_builder/stl_msvc_usage.hpp>
  39. #include <app/project_tree_builder/msvc_prj_files_collector.hpp>
  40. #include <app/project_tree_builder/msvc_prj_utils.hpp>
  41. BEGIN_NCBI_SCOPE
  42. static void s_CollectRelPathes(const string&        path_from,
  43.                                const list<string>&  abs_dirs,
  44.                                const list<string>&  file_exts,
  45.                                list<string>*        rel_pathes);
  46. //-----------------------------------------------------------------------------
  47. CMsvcPrjFilesCollector::CMsvcPrjFilesCollector
  48.                                 (const CMsvcPrjProjectContext& project_context,
  49.                                  const CProjItem&              project)
  50. {
  51.     CollectSources  (project, project_context, &m_SourceFiles);
  52.     CollectHeaders  (project, project_context, &m_HeaderFiles);
  53.     CollectInlines  (project, project_context, &m_InlineFiles);
  54.     CollectResources(project, project_context, &m_ResourceFiles);
  55. }
  56. CMsvcPrjFilesCollector::~CMsvcPrjFilesCollector(void)
  57. {
  58. }
  59. const list<string>& CMsvcPrjFilesCollector::SourceFiles(void) const
  60. {
  61.     return m_SourceFiles;
  62. }
  63. const list<string>& CMsvcPrjFilesCollector::HeaderFiles(void) const
  64. {
  65.     return m_HeaderFiles;
  66. }
  67. const list<string>& CMsvcPrjFilesCollector::InlineFiles(void) const
  68. {
  69.     return m_InlineFiles;
  70. }
  71. // source files helpers -------------------------------------------------------
  72. const list<string>& CMsvcPrjFilesCollector::ResourceFiles(void) const
  73. {
  74.     return m_ResourceFiles;
  75. }
  76. struct PSourcesExclude
  77. {
  78.     PSourcesExclude(const list<string>& excluded_sources)
  79.     {
  80.         copy(excluded_sources.begin(), excluded_sources.end(), 
  81.              inserter(m_ExcludedSources, m_ExcludedSources.end()) );
  82.     }
  83.     bool operator() (const string& src) const
  84.     {
  85.         string src_base;
  86.         CDirEntry::SplitPath(src, NULL, &src_base);
  87.         return m_ExcludedSources.find(src_base) != m_ExcludedSources.end();
  88.     }
  89. private:
  90.     set<string> m_ExcludedSources;
  91. };
  92. static bool s_IsProducedByDatatool(const string&    src_path_abs,
  93.                                    const CProjItem& project)
  94. {
  95.     if ( project.m_DatatoolSources.empty() )
  96.         return false;
  97.     string src_base;
  98.     CDirEntry::SplitPath(src_path_abs, NULL, &src_base);
  99.     // guess name.asn file name from name__ or name___
  100.     string asn_base;
  101.     if ( NStr::EndsWith(src_base, "___") ) {
  102.         asn_base = src_base.substr(0, src_base.length() -3);
  103.     } else if ( NStr::EndsWith(src_base, "__") ) {
  104.         asn_base = src_base.substr(0, src_base.length() -2);
  105.     } else {
  106.         return false;
  107.     }
  108.     string asn_name = asn_base + ".asn";
  109.     //try to find this name in datatool generated sources container
  110.     ITERATE(list<CDataToolGeneratedSrc>, p, project.m_DatatoolSources) {
  111.         const CDataToolGeneratedSrc& asn = *p;
  112.         if (asn.m_SourceFile == asn_name)
  113.             return true;
  114.     }
  115.     return false;
  116. }
  117. static bool s_IsInsideDatatoolSourceDir(const string& src_path_abs)
  118. {
  119.     string dir_name;
  120.     CDirEntry::SplitPath(src_path_abs, &dir_name);
  121.     //This files must be inside datatool src dir
  122.     CDir dir(dir_name);
  123.     if ( dir.GetEntries("*.module").empty() ) 
  124.         return false;
  125.     if ( dir.GetEntries("*.asn").empty() &&
  126.          dir.GetEntries("*.dtd").empty()  ) 
  127.         return false;
  128.     return true;
  129. }
  130. void 
  131. CMsvcPrjFilesCollector::CollectSources (const CProjItem&              project,
  132.                                         const CMsvcPrjProjectContext& context,
  133.                                         list<string>*                 rel_pathes)
  134. {
  135.     rel_pathes->clear();
  136.     list<string> sources;
  137.     ITERATE(list<string>, p, project.m_Sources) {
  138.         const string& src_rel = *p;
  139.         string src_path = 
  140.             CDirEntry::ConcatPath(project.m_SourcesBaseDir, src_rel);
  141.         src_path = CDirEntry::NormalizePath(src_path);
  142.         sources.push_back(src_path);
  143.     }
  144.     list<string> included_sources;
  145.     context.GetMsvcProjectMakefile().GetAdditionalSourceFiles //TODO
  146.                                             (SConfigInfo(),&included_sources);
  147.     ITERATE(list<string>, p, included_sources) {
  148.         sources.push_back(CDirEntry::NormalizePath
  149.                                         (CDirEntry::ConcatPath
  150.                                               (project.m_SourcesBaseDir, *p)));
  151.     }
  152.     list<string> excluded_sources;
  153.     context.GetMsvcProjectMakefile().GetExcludedSourceFiles //TODO
  154.                                             (SConfigInfo(), &excluded_sources);
  155.     PSourcesExclude pred(excluded_sources);
  156.     EraseIf(sources, pred);
  157.     ITERATE(list<string>, p, sources) {
  158.         const string& abs_path = *p; // whithout ext.
  159.         string ext = SourceFileExt(abs_path);
  160.         if ( !ext.empty() ) {
  161.             // add ext to file
  162.             string source_file_abs_path = abs_path + ext;
  163.             rel_pathes->push_back(
  164.                 CDirEntry::CreateRelativePath(context.ProjectDir(), 
  165.                                               source_file_abs_path));
  166.         } 
  167.         else if ( s_IsProducedByDatatool(abs_path, project) ||
  168.                   s_IsInsideDatatoolSourceDir(abs_path) ) {
  169.             // .cpp file extension
  170.             rel_pathes->push_back(
  171.                 CDirEntry::CreateRelativePath(context.ProjectDir(), 
  172.                                               abs_path + ".cpp"));
  173.         } else {
  174.             LOG_POST(Warning <<"Can not resolve/find source file : " + abs_path);
  175.         }
  176.     }
  177. }
  178. // header files helpers -------------------------------------------------------
  179. void 
  180. CMsvcPrjFilesCollector::CollectHeaders(const CProjItem&              project,
  181.                                        const CMsvcPrjProjectContext& context,
  182.                                        list<string>*                 rel_pathes)
  183. {
  184.     rel_pathes->clear();
  185.     // .h and .hpp files may be in include or source dirs:
  186.     list<string> abs_dirs(context.IncludeDirsAbs());
  187.     copy(context.SourcesDirsAbs().begin(), 
  188.          context.SourcesDirsAbs().end(), 
  189.          back_inserter(abs_dirs));
  190.     //collect *.h and *.hpp files
  191.     list<string> exts;
  192.     exts.push_back(".h");
  193.     exts.push_back(".hpp");
  194.     s_CollectRelPathes(context.ProjectDir(), abs_dirs, exts, rel_pathes);
  195. }
  196. // inline files helpers -------------------------------------------------------
  197. void 
  198. CMsvcPrjFilesCollector::CollectInlines(const CProjItem&              project,
  199.                                        const CMsvcPrjProjectContext& context,
  200.                                        list<string>*                 rel_pathes)
  201. {
  202.     rel_pathes->clear();
  203.     // .inl files may be in include or source dirs:
  204.     list<string> abs_dirs(context.IncludeDirsAbs());
  205.     copy(context.SourcesDirsAbs().begin(), 
  206.          context.SourcesDirsAbs().end(), 
  207.          back_inserter(abs_dirs));
  208.     //collect *.inl files
  209.     list<string> exts(1, ".inl");
  210.     s_CollectRelPathes(context.ProjectDir(), abs_dirs, exts, rel_pathes);
  211. }
  212. // resource files helpers -------------------------------------------------------
  213. void 
  214. CMsvcPrjFilesCollector::CollectResources
  215.     (const CProjItem&              project,
  216.      const CMsvcPrjProjectContext& context,
  217.      list<string>*                 rel_pathes)
  218. {
  219.     rel_pathes->clear();
  220.     // resources from msvc makefile - first priority
  221.     list<string> included_sources;
  222.     context.GetMsvcProjectMakefile().GetResourceFiles
  223.                                             (SConfigInfo(),&included_sources);
  224.     list<string> sources;
  225.     ITERATE(list<string>, p, included_sources) {
  226.         sources.push_back(CDirEntry::NormalizePath
  227.                                         (CDirEntry::ConcatPath
  228.                                               (project.m_SourcesBaseDir, *p)));
  229.     }
  230.     ITERATE(list<string>, p, sources) {
  231.         const string& abs_path = *p; // whith ext.
  232.         rel_pathes->push_back(
  233.             CDirEntry::CreateRelativePath(context.ProjectDir(), 
  234.                                           abs_path));
  235.     }
  236.     if ( !rel_pathes->empty() )
  237.         return;
  238.     // if there are no makefile resources - 'll use defaults
  239.     string default_rc;
  240.     if (project.m_ProjType == CProjKey::eApp) {
  241.         default_rc = GetApp().GetSite().GetAppDefaultResource();
  242.     }
  243.     if ( !default_rc.empty() ) {
  244.         string abs_path = GetApp().GetProjectTreeInfo().m_Compilers;
  245.         abs_path = 
  246.             CDirEntry::ConcatPath(abs_path, 
  247.                                   GetApp().GetRegSettings().m_CompilersSubdir);
  248.         abs_path = CDirEntry::ConcatPath(abs_path, default_rc);
  249.         abs_path = CDirEntry::NormalizePath(abs_path);
  250.         rel_pathes->push_back(
  251.             CDirEntry::CreateRelativePath(context.ProjectDir(), 
  252.                                           abs_path));
  253.     }
  254. }
  255. //-----------------------------------------------------------------------------
  256. // Collect all files from specified dirs having specified exts
  257. static void s_CollectRelPathes(const string&        path_from,
  258.                                const list<string>&  abs_dirs,
  259.                                const list<string>&  file_exts,
  260.                                list<string>*        rel_pathes)
  261. {
  262.     rel_pathes->clear();
  263.     list<string> pathes;
  264.     ITERATE(list<string>, p, file_exts) {
  265.         const string& ext = *p;
  266.         ITERATE(list<string>, n, abs_dirs) {
  267.             CDir dir(*n);
  268.             if ( !dir.Exists() )
  269.                 continue;
  270.             CDir::TEntries contents = dir.GetEntries("*" + ext);
  271.             ITERATE(CDir::TEntries, i, contents) {
  272.                 if ( (*i)->IsFile() ) {
  273.                     string path  = (*i)->GetPath();
  274.                     if ( NStr::EndsWith(path, ext, NStr::eNocase) )
  275.                         pathes.push_back(path);
  276.                 }
  277.             }
  278.         }
  279.     }
  280.     ITERATE(list<string>, p, pathes)
  281.         rel_pathes->push_back(CDirEntry::CreateRelativePath(path_from, *p));
  282. }
  283. END_NCBI_SCOPE
  284. /*
  285.  * ===========================================================================
  286.  * $Log: msvc_prj_files_collector.cpp,v $
  287.  * Revision 1000.1  2004/06/01 18:30:58  gouriano
  288.  * PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.6
  289.  *
  290.  * Revision 1.6  2004/05/21 21:41:41  gorelenk
  291.  * Added PCH ncbi_pch.hpp
  292.  *
  293.  * Revision 1.5  2004/05/19 14:25:53  gorelenk
  294.  * Changed implementation of CMsvcPrjFilesCollector::CollectResources -
  295.  * default resource will be added omly if there are no excplicit resource
  296.  * in msvc makefile.
  297.  *
  298.  * Revision 1.4  2004/05/17 14:38:39  gorelenk
  299.  * Changed CMsvcPrjFilesCollector::CollectResources - implemented addition of
  300.  * default resource to 'app' projects.
  301.  *
  302.  * Revision 1.3  2004/05/10 19:54:34  gorelenk
  303.  * Changed s_CollectRelPathes .
  304.  *
  305.  * Revision 1.2  2004/03/05 20:32:48  gorelenk
  306.  * Added implementation of CMsvcPrjFilesCollector member-functions.
  307.  *
  308.  * Revision 1.1  2004/03/05 18:04:55  gorelenk
  309.  * Initial revision.
  310.  *
  311.  * ===========================================================================
  312.  */