df6b23afb2fc2d8c712a3df07d67e0a5ed5f5151
[reactos.git] / reactos / tools / rbuild / backend / dependencymap / dependencymap.cpp
1 /*
2 * Copyright (C) 2007 Christoph von Wittich
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18 #ifdef _MSC_VER
19 #pragma warning ( disable : 4786 )
20 #endif//_MSC_VER
21
22 #include <iostream>
23 #include <fstream>
24 #include <string>
25 #include <vector>
26 #include <map>
27
28 #include <stdio.h>
29
30 #include "dependencymap.h"
31 #include "../mingw/mingw.h"
32
33 using std::string;
34 using std::vector;
35 using std::map;
36 using std::ifstream;
37
38 #ifdef OUT
39 #undef OUT
40 #endif//OUT
41
42
43 static class DepMapFactory : public Backend::Factory
44 {
45 public:
46
47 DepMapFactory() : Factory("DepMap", "Dependency Map") {}
48 Backend *operator() (Project &project,
49 Configuration& configuration)
50 {
51 return new DepMapBackend(project, configuration);
52 }
53
54 } factory;
55
56
57 DepMapBackend::DepMapBackend(Project &project,
58 Configuration& configuration) : Backend(project, configuration)
59 {
60
61 }
62
63 void DepMapBackend::Process()
64 {
65 string filename_depmap ( "dependencymap.xml" );
66 printf ( "Creating dependecy map: %s\n", filename_depmap.c_str() );
67
68 m_DepMapFile = fopen ( filename_depmap.c_str(), "wb" );
69
70 if ( !m_DepMapFile )
71 {
72 printf ( "Could not create file '%s'.\n", filename_depmap.c_str() );
73 return;
74 }
75
76 _generate_depmap ( m_DepMapFile );
77
78 fclose ( m_DepMapFile );
79 printf ( "Done.\n" );
80 }
81
82 void
83 DepMapBackend::_clean_project_files ( void )
84 {
85 remove ( "dependencymap.xml" );
86 }
87
88
89 void
90 DepMapBackend::_generate_depmap ( FILE* OUT )
91 {
92 /* add dependencies */
93
94 typedef map<string, module_data*> ModuleMap;
95 ModuleMap module_map;
96
97 for( std::map<std::string, Module*>::const_iterator p = ProjectNode.modules.begin(); p != ProjectNode.modules.end(); ++ p )
98 {
99 Module& module = *p->second;
100 if ((module.type != Iso) &&
101 (module.type != LiveIso) &&
102 (module.type != IsoRegTest) &&
103 (module.type != LiveIsoRegTest))
104 {
105 vector<const IfableData*> ifs_list;
106 ifs_list.push_back ( &module.project.non_if_data );
107 ifs_list.push_back ( &module.non_if_data );
108
109 module_data * current_data;
110 ModuleMap::iterator mod_it = module_map.find ( module.name );
111 if (mod_it != module_map.end ())
112 {
113 current_data = mod_it->second;
114 }
115 else
116 {
117 current_data = new module_data();
118 if (current_data)
119 {
120 module_map.insert (std::make_pair<string, module_data*>(module.name, current_data));
121 }
122 }
123 while ( ifs_list.size() )
124 {
125 const IfableData& data = *ifs_list.back();
126 ifs_list.pop_back();
127 const vector<Library*>& libs = data.libraries;
128 for ( size_t j = 0; j < libs.size(); j++ )
129 {
130 ModuleMap::iterator it = module_map.find ( libs[j]->name );
131
132 if ( it != module_map.end ())
133 {
134 module_data * data = it->second;
135 data->references.push_back ( module.name );
136 }
137 else
138 {
139 module_data * data = new module_data();
140 if ( data )
141 {
142 data->references.push_back ( module.name );
143 }
144 module_map.insert ( std::make_pair<string, module_data*>( libs[j]->name, data ) );
145 }
146 current_data->libraries.push_back ( libs[j]->name );
147 }
148 }
149 }
150 }
151
152 fprintf ( m_DepMapFile, "<?xml version=\"1.0\" encoding=\"iso-8859-1\" ?>\r\n" );
153 fprintf ( m_DepMapFile, "<?xml-stylesheet type=\"text/xsl\" href=\"depmap.xsl\"?>\r\n" );
154 fprintf ( m_DepMapFile, "<components>\r\n" );
155
156 for( std::map<std::string, Module*>::const_iterator p = ProjectNode.modules.begin(); p != ProjectNode.modules.end(); ++ p )
157 {
158 Module& module = *p->second;
159
160 ModuleMap::iterator it = module_map.find ( module.name );
161 if ( it != module_map.end () )
162 {
163 module_data * data = it->second;
164
165
166
167 fprintf ( m_DepMapFile, "\t<component>\r\n" );
168 fprintf ( m_DepMapFile, "\t\t<name>%s</name>\r\n", module.name.c_str () );
169 fprintf ( m_DepMapFile, "\t\t<base>%s</base>\r\n", module.output->relative_path.c_str () );
170 fprintf ( m_DepMapFile, "\t\t<ref_count>%u</ref_count>\r\n", (unsigned int)data->references.size () );
171 fprintf ( m_DepMapFile, "\t\t<lib_count>%u</lib_count>\r\n", (unsigned int)data->libraries.size () );
172 #if 0
173 if ( data->references.size () )
174 {
175 fprintf ( m_DepMapFile, "\t<references>\r\n" );
176 for ( size_t j = 0; j < data->references.size (); j++ )
177 {
178 fprintf ( m_DepMapFile, "\t\t<reference name =\"%s\" />\r\n", data->references[j].c_str () );
179 }
180 fprintf ( m_DepMapFile, "\t</references>\r\n" );
181 }
182
183 if ( data->libraries.size () )
184 {
185 fprintf ( m_DepMapFile, "\t<libraries>\r\n" );
186 for ( size_t j = 0; j < data->libraries.size (); j++ )
187 {
188 fprintf ( m_DepMapFile, "\t\t<library name =\"%s\" />\r\n", data->libraries[j].c_str () );
189 }
190 fprintf ( m_DepMapFile, "\t</libraries>\r\n" );
191 }
192 #endif
193 fprintf ( m_DepMapFile, "\t</component>\r\n" );
194 }
195 }
196
197 fprintf ( m_DepMapFile, "</components>" );
198 }
199
200
201 DepMapConfiguration::DepMapConfiguration ( const std::string &name )
202 {
203 /* nothing to do here */
204 }
205
206