NCBI C++ ToolKit
msvc_prj_utils.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef PROJECT_TREE_BUILDER__MSVC_PRJ_UTILS__HPP
2 #define PROJECT_TREE_BUILDER__MSVC_PRJ_UTILS__HPP
3 
4 /* $Id: msvc_prj_utils.hpp 80916 2018-01-23 16:23:07Z gouriano $
5  * ===========================================================================
6  *
7  * PUBLIC DOMAIN NOTICE
8  * National Center for Biotechnology Information
9  *
10  * This software/database is a "United States Government Work" under the
11  * terms of the United States Copyright Act. It was written as part of
12  * the author's official duties as a United States Government employee and
13  * thus cannot be copyrighted. This software/database is freely available
14  * to the public for use. The National Library of Medicine and the U.S.
15  * Government have not placed any restriction on its use or reproduction.
16  *
17  * Although all reasonable efforts have been taken to ensure the accuracy
18  * and reliability of the software and data, the NLM and the U.S.
19  * Government do not and cannot warrant the performance or results that
20  * may be obtained by using this software or data. The NLM and the U.S.
21  * Government disclaim all warranties, express or implied, including
22  * warranties of performance, merchantability or fitness for any particular
23  * purpose.
24  *
25  * Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Author: Viatcheslav Gorelenkov
30  *
31  */
32 
33 #include "ptb_registry.hpp"
34 #include <corelib/ncbienv.hpp>
35 #if NCBI_COMPILER_MSVC
37 #endif //NCBI_COMPILER_MSVC
38 #include <set>
39 
41 
42 /////////////////////////////////////////////////////////////////////////////
43 ///
44 /// CProjKey --
45 ///
46 /// Project key abstraction.
47 ///
48 /// Project key (type + project_id).
49 
50 class CProjKey
51 {
52 public:
53  typedef enum {
61  eLast
63 
64  CProjKey(void);
65  CProjKey(TProjType type, const string& project_id);
66  CProjKey(TProjType type, const string& project_id, const string& id_suffix);
67  CProjKey(const CProjKey& key);
69  ~CProjKey(void);
70 
71  bool operator< (const CProjKey& key) const;
72  bool operator== (const CProjKey& key) const;
73  bool operator!= (const CProjKey& key) const;
74 
75  TProjType Type(void) const;
76  const string& Id (void) const;
77  string FullId (void) const;
78 
79 private:
81  string m_Id;
82  string m_Suffix;
83 
84 };
85 
86 #if NCBI_COMPILER_MSVC
88 
89 /// Creates CVisualStudioProject class instance from file.
90 ///
91 /// @param file_path
92 /// Path to file load from.
93 /// @return
94 /// Created on heap CVisualStudioProject instance or NULL
95 /// if failed.
96 CVisualStudioProject * LoadFromXmlFile(const string& file_path);
97 
98 
99 /// Save CVisualStudioProject class instance to file.
100 ///
101 /// @param file_path
102 /// Path to file project will be saved to.
103 /// @param project
104 /// Project to save.
105 void SaveToXmlFile (const string& file_path,
106  const CSerialObject& project);
107 
108 /// Save CVisualStudioProject class instance to file only if no such file
109 /// or contents of this file will be different from already present file.
110 ///
111 /// @param file_path
112 /// Path to file project will be saved to.
113 /// @param project
114 /// Project to save.
115 void SaveIfNewer (const string& file_path,
116  const CSerialObject& project);
117 
118 /// Save object, ignoring certain tags
119 void SaveIfNewer (const string& file_path,
120  const CSerialObject& project,
121  const string& ignore);
122 #endif //NCBI_COMPILER_MSVC
123 bool PromoteIfDifferent(const string& present_path,
124  const string& candidate_path,
125  const string& ignore);
126 
127 /// Consider promotion candidate to present
128 bool PromoteIfDifferent(const string& present_path,
129  const string& candidate_path);
130 
131 
132 /// Generate pseudo-GUID.
133 string GenerateSlnGUID(void);
134 string IdentifySlnGUID(const string& source_dir, const CProjKey& proj);
135 
136 /// Get extension for source file without extension.
137 ///
138 /// @param file_path
139 /// Source file full path withour extension.
140 /// @return
141 /// Extension of source file (".cpp" or ".c")
142 /// if such file exist. Empty string string if there is no
143 /// such file.
144 string SourceFileExt(const string& file_path);
145 
146 
147 /////////////////////////////////////////////////////////////////////////////
148 ///
149 /// SConfigInfo --
150 ///
151 /// Abstraction of configuration informaion.
152 ///
153 /// Configuration name, debug/release flag, runtime library
154 ///
155 
157 {
158  SConfigInfo(void);
159  SConfigInfo(const string& name,
160  bool debug,
161  const string& runtime_library);
162  void DefineRtType();
163  void SetRuntimeLibrary(const string& lib);
164  string GetConfigFullName(void) const;
165  bool operator== (const SConfigInfo& cfg) const;
166 
167  string m_Name;
169  bool m_Debug;
171  bool m_Unicode;
172  enum {
179  rtUnknown = 6
181 };
182 
183 // Helper to load configs from ini files
185  const list<string>& config_names,
186  list<SConfigInfo>* configs);
187 
188 
189 /////////////////////////////////////////////////////////////////////////////
190 ///
191 /// SCustomBuildInfo --
192 ///
193 /// Abstraction of custom build source file.
194 ///
195 /// Information for custom buil source file
196 /// (not *.c, *.cpp, *.midl, *.rc, etc.)
197 /// MSVC does not know how to buil this file and
198 /// we provide information how to do it.
199 ///
200 
202 {
203  string m_SourceFile; // absolute path!
206  string m_Outputs;
208 
209  bool IsEmpty(void) const
210  {
211  return m_SourceFile.empty() || m_CommandLine.empty();
212  }
213  void Clear(void)
214  {
215  m_SourceFile.erase();
216  m_CommandLine.erase();
217  m_Description.erase();
218  m_Outputs.erase();
219  m_AdditionalDependencies.erase();
220  }
222  }
224  m_SourceFile = other.m_SourceFile;
227  m_Outputs = other.m_Outputs;
229  }
230 };
231 
233 {
234  string m_Input;
235  string m_Output;
236  string m_Shell;
237  string m_Script;
238 };
239 
240 /////////////////////////////////////////////////////////////////////////////
241 ///
242 /// CMsvc7RegSettings --
243 ///
244 /// Abstraction of [msvc7] section in app registry.
245 ///
246 /// Settings for generation of msvc 7.10 projects
247 ///
248 
250 {
251 public:
253  eMsvc710 = 0,
257  eMsvc1100, // vs2012
258  eMsvc1200, // vs2013
259  eMsvc1400, // vs2015
260  eMsvc1500, // vs2017
262  eMsvcNone
263  };
268  eXCode
269  };
270 
271  CMsvc7RegSettings(void);
272 
273  string m_Version;
274  list<SConfigInfo> m_ConfigInfo;
279  string m_DllInfo;
280 
281  static void IdentifyPlatform(void);
283  {
284  return sm_MsvcVersion;
285  }
286  static const string& GetMsvcVersionName(void)
287  {
288  return sm_MsvcVersionName;
289  }
291  {
292  return sm_MsvcPlatform;
293  }
294  static const string& GetMsvcPlatformName(void)
295  {
296  return sm_MsvcPlatformName;
297  }
298  static const string& GetRequestedArchs(void)
299  {
300  return sm_RequestedArchs;
301  }
302  static string GetMsvcRegSection(void);
303  static string GetMsvcSection(void);
304 
305  static string GetProjectFileFormatVersion(void);
306  static string GetSolutionFileFormatVersion(void);
307 
308  static string GetConfigNameKeyword(void);
309  static string GetVcprojExt(void);
310 
311  static string GetTopBuilddir(void);
312 private:
315  static string sm_MsvcVersionName;
316  static string sm_MsvcPlatformName;
317  static string sm_RequestedArchs;
318 
321 };
322 
323 
324 /// Is abs_dir a parent of abs_parent_dir.
325 bool IsSubdir(const string& abs_parent_dir, const string& abs_dir);
326 
327 
328 /// Erase if predicate is true
329 template <class C, class P>
330 void EraseIf(C& cont, const P& pred)
331 {
332  for (typename C::iterator p = cont.begin(); p != cont.end(); )
333  {
334  if ( pred(*p) ) {
335  typename C::iterator p_next = p;
336  ++p_next;
337  cont.erase(p);
338  p = p_next;
339  }
340  else
341  ++p;
342  }
343 }
344 
345 
346 /// Get option fron registry from
347 /// [<section>.debug.<ConfigName>] section for debug configuratios
348 /// or [<section>.release.<ConfigName>] for release configurations
349 ///
350 /// if no such option then try
351 /// [<section>.debug]
352 /// or [<section>.release]
353 ///
354 /// if no such option then finally try
355 /// [<section>]
356 ///
357 string GetOpt(const CPtbRegistry& registry,
358  const string& section,
359  const string& opt,
360  const SConfigInfo& config);
361 
362 /// return <config>|Win32 as needed by MSVC compiler
363 string ConfigName(const string& config);
364 
365 
366 
367 //-----------------------------------------------------------------------------
368 
369 // Base interface class for all insertors
371 {
372 public:
374  {
375  }
376 
377  virtual void AddSourceFile (const string& rel_file_path,
378  const string& pch_default) = 0;
379  virtual void AddHeaderFile (const string& rel_file_path) = 0;
380  virtual void AddInlineFile (const string& rel_file_path) = 0;
381 
382  virtual void Finalize (void) = 0;
383 };
384 
385 
386 #if NCBI_COMPILER_MSVC
387 // Insert .cpp and .c files to filter and set PCH usage if necessary
388 class CSrcToFilterInserterWithPch
389 {
390 public:
391  CSrcToFilterInserterWithPch(const string& project_id,
392  const list<SConfigInfo>& configs,
393  const string& project_dir);
394 
395  ~CSrcToFilterInserterWithPch(void);
396 
397  void operator() (CRef<CFilter>& filter,
398  const string& rel_source_file,
399  const string& pch_default);
400 
401 private:
402  string m_ProjectId;
403  const list<SConfigInfo>& m_Configs;
404  const list<SConfigInfo>& m_AllConfigs;
405  string m_ProjectDir;
406 
407  typedef set<string> TPchHeaders;
408  TPchHeaders m_PchHeaders;
409 
410  enum EUsePch {
411  eNotUse = 0,
412  eCreate = 1,
413  eUse = 3
414  };
415  typedef pair<EUsePch, string> TPch;
416 
417  TPch DefinePchUsage(const string& project_dir,
418  const string& rel_source_file,
419  const string& pch_default);
420 
421  void InsertFile (CRef<CFilter>& filter,
422  const string& rel_source_file,
423  const string& pch_default,
424  const string& enable_cfg = kEmptyStr);
425 
426  // Prohibited to:
427  CSrcToFilterInserterWithPch(void);
428  CSrcToFilterInserterWithPch(const CSrcToFilterInserterWithPch&);
429  CSrcToFilterInserterWithPch& operator=(const CSrcToFilterInserterWithPch&);
430 };
431 
432 class CBasicProjectsFilesInserter : public IFilesToProjectInserter
433 {
434 public:
435  CBasicProjectsFilesInserter(CVisualStudioProject* vcproj,
436  const string& project_id,
437  const list<SConfigInfo>& configs,
438  const string& project_dir);
439 
440  virtual ~CBasicProjectsFilesInserter(void);
441 
442  // IFilesToProjectInserter implementation
443  virtual void AddSourceFile (const string& rel_file_path,
444  const string& pch_default);
445  virtual void AddHeaderFile (const string& rel_file_path);
446  virtual void AddInlineFile (const string& rel_file_path);
447 
448  virtual void Finalize (void);
449 
450  struct SFiltersItem
451  {
452  SFiltersItem(void);
453  SFiltersItem(const string& project_dir);
454 
455  CRef<CFilter> m_SourceFiles;
456  CRef<CFilter> m_HeaderFiles;
457  CRef<CFilter> m_HeaderFilesPrivate;
458  CRef<CFilter> m_HeaderFilesImpl;
459  CRef<CFilter> m_InlineFiles;
460 
461  string m_ProjectDir;
462 
463  void Initilize(void);
464 
465  void AddSourceFile (CSrcToFilterInserterWithPch& inserter_w_pch,
466  const string& rel_file_path,
467  const string& pch_default);
468 
469  void AddHeaderFile (const string& rel_file_path);
470 
471  void AddInlineFile (const string& rel_file_path);
472 
473  };
474 
475 private:
476  CVisualStudioProject* m_Vcproj;
477 
478  CSrcToFilterInserterWithPch m_SrcInserter;
479  SFiltersItem m_Filters;
480 
481 
482  // Prohibited to:
483  CBasicProjectsFilesInserter(void);
484  CBasicProjectsFilesInserter(const CBasicProjectsFilesInserter&);
485  CBasicProjectsFilesInserter& operator=(const CBasicProjectsFilesInserter&);
486 };
487 
488 class CDllProjectFilesInserter : public IFilesToProjectInserter
489 {
490 public:
491  CDllProjectFilesInserter(CVisualStudioProject* vcproj,
492  const CProjKey dll_project_key,
493  const list<SConfigInfo>& configs,
494  const string& project_dir);
495 
496  virtual ~CDllProjectFilesInserter(void);
497 
498  // IFilesToProjectInserter implementation
499  virtual void AddSourceFile (const string& rel_file_path,
500  const string& pch_default);
501  virtual void AddHeaderFile (const string& rel_file_path);
502  virtual void AddInlineFile (const string& rel_file_path);
503 
504  virtual void Finalize (void);
505 
506 private:
507  CVisualStudioProject* m_Vcproj;
508  CProjKey m_DllProjectKey;
509  CSrcToFilterInserterWithPch m_SrcInserter;
510  string m_ProjectDir;
511 
512  typedef CBasicProjectsFilesInserter::SFiltersItem TFiltersItem;
513  TFiltersItem m_PrivateFilters;
514  CRef<CFilter> m_HostedLibrariesRootFilter;
515 
516  typedef map<CProjKey, TFiltersItem> THostedLibs;
517  THostedLibs m_HostedLibs;
518 
519  // Prohibited to:
520  CDllProjectFilesInserter(void);
521  CDllProjectFilesInserter(const CDllProjectFilesInserter&);
522  CDllProjectFilesInserter& operator=(const CDllProjectFilesInserter&);
523 };
524 
525 
526 /// Common function shared by
527 /// CMsvcMasterProjectGenerator and CMsvcProjectGenerator
528 void AddCustomBuildFileToFilter(CRef<CFilter>& filter,
529  const list<SConfigInfo> configs,
530  const string& project_dir,
531  const SCustomBuildInfo& build_info);
532 
533 #endif //NCBI_COMPILER_MSVC
534 
535 /// Checks if 2 dirs has the same root
536 bool SameRootDirs(const string& dir1, const string& dir2);
537 
538 /// Project naming schema
539 string CreateProjectName(const CProjKey& project_id);
540 CProjKey CreateProjKey(const string& project_name);
541 
542 
543 /// Utility class for distinguish between static and dll builds
545 {
546 public:
547  CBuildType(bool dll_flag);
548 
549  enum EBuildType {
551  eDll
552  };
553 
554  EBuildType GetType (void) const;
555  string GetTypeStr(void) const;
556 
557 private:
559 
560  //prohibited to:
561  CBuildType(void);
564 };
565 
566 
567 /// Distribution if source files by lib projects
568 /// Uses in dll project to separate source files to groups by libs
570 {
571 public:
572  CDllSrcFilesDistr(void);
573 
574 
575  // Register .cpp .c files during DLL creation
576  void RegisterSource (const string& src_file_path,
577  const CProjKey& dll_project_id,
578  const CProjKey& lib_project_id);
579  // Register .hpp .h files during DLL creation
580  void RegisterHeader (const string& hrd_file_path,
581  const CProjKey& dll_project_id,
582  const CProjKey& lib_project_id);
583  // Register .inl files during DLL creation
584  void RegisterInline (const string& inl_file_path,
585  const CProjKey& dll_project_id,
586  const CProjKey& lib_project_id);
587  void RegisterExtraFile(const string& inl_file_path,
588  const CProjKey& dll_project_id,
589  const CProjKey& lib_project_id);
590 
591  // Retrive original lib_id for .cpp .c file
592  CProjKey GetSourceLib(const string& src_file_path,
593  const CProjKey& dll_project_id) const;
594  // Retrive original lib_id for .cpp .c file
595  CProjKey GetHeaderLib(const string& hdr_file_path,
596  const CProjKey& dll_project_id) const;
597  // Retrive original lib_id for .inl file
598  CProjKey GetInlineLib(const string& inl_file_path,
599  const CProjKey& dll_project_id) const;
600  CProjKey GetExtraFileLib(const string& ex_file_path,
601  const CProjKey& dll_project_id) const;
602  CProjKey GetFileLib(const string& file_path,
603  const CProjKey& dll_project_id) const;
604 private:
605 
606  typedef pair<string, CProjKey> TDllSrcKey;
612 
613  //prohibited to
616 };
617 
618 
619 class CProjItem;
622 IsProducedByDatatool(const string& src_path_abs, const CProjItem& project);
623 
625 
626 #endif //PROJECT_TREE_BUILDER__MSVC_PRJ_UTILS__HPP
Utility class for distinguish between static and dll builds.
CBuildType(const CBuildType &)
string GetTypeStr(void) const
CBuildType & operator=(const CBuildType &)
EBuildType GetType(void) const
EBuildType m_Type
CBuildType(void)
Distribution if source files by lib projects Uses in dll project to separate source files to groups b...
void RegisterExtraFile(const string &inl_file_path, const CProjKey &dll_project_id, const CProjKey &lib_project_id)
pair< string, CProjKey > TDllSrcKey
CDllSrcFilesDistr(const CDllSrcFilesDistr &)
CProjKey GetHeaderLib(const string &hdr_file_path, const CProjKey &dll_project_id) const
void RegisterSource(const string &src_file_path, const CProjKey &dll_project_id, const CProjKey &lib_project_id)
CProjKey GetFileLib(const string &file_path, const CProjKey &dll_project_id) const
CProjKey GetSourceLib(const string &src_file_path, const CProjKey &dll_project_id) const
CProjKey GetExtraFileLib(const string &ex_file_path, const CProjKey &dll_project_id) const
void RegisterInline(const string &inl_file_path, const CProjKey &dll_project_id, const CProjKey &lib_project_id)
CProjKey GetInlineLib(const string &inl_file_path, const CProjKey &dll_project_id) const
CDllSrcFilesDistr & operator=(const CDllSrcFilesDistr &)
void RegisterHeader(const string &hrd_file_path, const CProjKey &dll_project_id, const CProjKey &lib_project_id)
map< TDllSrcKey, CProjKey > TDistrMap
CMsvc7RegSettings –.
static string GetVcprojExt(void)
static EMsvcPlatform GetMsvcPlatform(void)
static string sm_MsvcPlatformName
list< SConfigInfo > m_ConfigInfo
CMsvc7RegSettings & operator=(const CMsvc7RegSettings &)
static string GetMsvcSection(void)
CMsvc7RegSettings(const CMsvc7RegSettings &)
static string sm_RequestedArchs
static string sm_MsvcVersionName
static const string & GetMsvcVersionName(void)
static string GetProjectFileFormatVersion(void)
static const string & GetMsvcPlatformName(void)
static EMsvcVersion sm_MsvcVersion
static void IdentifyPlatform(void)
static string GetTopBuilddir(void)
static string GetSolutionFileFormatVersion(void)
static string GetConfigNameKeyword(void)
static const string & GetRequestedArchs(void)
static EMsvcPlatform sm_MsvcPlatform
static EMsvcVersion GetMsvcVersion(void)
static string GetMsvcRegSection(void)
CNcbiRegistry –.
Definition: ncbireg.hpp:913
CProjItem –.
Definition: proj_item.hpp:54
CProjKey –.
const string & Id(void) const
Definition: proj_item.cpp:112
bool operator<(const CProjKey &key) const
Definition: proj_item.cpp:83
CProjKey & operator=(const CProjKey &key)
Definition: proj_item.cpp:67
string FullId(void) const
Definition: proj_item.cpp:116
bool operator==(const CProjKey &key) const
Definition: proj_item.cpp:94
CProjKey(void)
Definition: proj_item.cpp:41
~CProjKey(void)
Definition: proj_item.cpp:78
string m_Suffix
TProjType Type(void) const
Definition: proj_item.cpp:106
TProjType m_Type
bool operator!=(const CProjKey &key) const
Definition: proj_item.cpp:100
CRef –.
Definition: ncbiobj.hpp:618
Base class for all serializable objects.
Definition: serialbase.hpp:150
CVisualStudioProject –.
virtual void AddInlineFile(const string &rel_file_path)=0
virtual void AddHeaderFile(const string &rel_file_path)=0
virtual void Finalize(void)=0
virtual void AddSourceFile(const string &rel_file_path, const string &pch_default)=0
virtual ~IFilesToProjectInserter(void)
Definition: map.hpp:338
static CMemoryRegistry registry
Definition: cn3d_tools.cpp:81
#define C(s)
Definition: common.h:231
#define P(a, b)
Definition: sqlwparams.h:19
void debug()
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define USING_SCOPE(ns)
Use the specified namespace.
Definition: ncbistl.hpp:78
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
#define kEmptyStr
Definition: ncbistr.hpp:123
string GenerateSlnGUID(void)
Generate pseudo-GUID.
string ConfigName(const string &config)
return <config>|Win32 as needed by MSVC compiler
string SourceFileExt(const string &file_path)
Get extension for source file without extension.
string GetOpt(const CPtbRegistry &registry, const string &section, const string &opt, const SConfigInfo &config)
Get option fron registry from [<section>.debug.
void EraseIf(C &cont, const P &pred)
Erase if predicate is true.
const CDataToolGeneratedSrc * IsProducedByDatatool(const string &src_path_abs, const CProjItem &project)
bool SameRootDirs(const string &dir1, const string &dir2)
Checks if 2 dirs has the same root.
void LoadConfigInfoByNames(const CNcbiRegistry &registry, const list< string > &config_names, list< SConfigInfo > *configs)
bool PromoteIfDifferent(const string &present_path, const string &candidate_path, const string &ignore)
CProjKey CreateProjKey(const string &project_name)
string IdentifySlnGUID(const string &source_dir, const CProjKey &proj)
string CreateProjectName(const CProjKey &project_id)
Project naming schema.
bool IsSubdir(const string &abs_parent_dir, const string &abs_dir)
Is abs_dir a parent of abs_parent_dir.
const struct ncbi::grid::netcache::search::fields::KEY key
Defines unified interface to application:
SConfigInfo –.
enum SConfigInfo::@955 m_rtType
bool operator==(const SConfigInfo &cfg) const
string m_RuntimeLibrary
string GetConfigFullName(void) const
void SetRuntimeLibrary(const string &lib)
SCustomBuildInfo –.
SCustomBuildInfo(const SCustomBuildInfo &other)
string m_AdditionalDependencies
bool IsEmpty(void) const
Definition: type.c:6
Modified on Thu May 23 12:31:26 2024 by modify_doxy.py rev. 669887