- Remove svn:needs-lock, svn:eol-type, and svn:eol-tyle properties.
[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 {
103 vector<const IfableData*> ifs_list;
104 ifs_list.push_back ( &module.project.non_if_data );
105 ifs_list.push_back ( &module.non_if_data );
106
107 module_data * current_data;
108 ModuleMap::iterator mod_it = module_map.find ( module.name );
109 if (mod_it != module_map.end ())
110 {
111 current_data = mod_it->second;
112 }
113 else
114 {
115 current_data = new module_data();
116 if (current_data)
117 {
118 module_map.insert (std::make_pair<string, module_data*>(module.name, current_data));
119 }
120 }
121 while ( ifs_list.size() )
122 {
123 const IfableData& data = *ifs_list.back();
124 ifs_list.pop_back();
125 const vector<Library*>& libs = data.libraries;
126 for ( size_t j = 0; j < libs.size(); j++ )
127 {
128 ModuleMap::iterator it = module_map.find ( libs[j]->name );
129
130 if ( it != module_map.end ())
131 {
132 module_data * data = it->second;
133 data->references.push_back ( module.name );
134 }
135 else
136 {
137 module_data * data = new module_data();
138 if ( data )
139 {
140 data->references.push_back ( module.name );
141 }
142 module_map.insert ( std::make_pair<string, module_data*>( libs[j]->name, data ) );
143 }
144 current_data->libraries.push_back ( libs[j]->name );
145 }
146 }
147 }
148 }
149
150 fprintf ( m_DepMapFile, "<?xml version=\"1.0\" encoding=\"iso-8859-1\" ?>\r\n" );
151 fprintf ( m_DepMapFile, "<?xml-stylesheet type=\"text/xsl\" href=\"depmap.xsl\"?>\r\n" );
152 fprintf ( m_DepMapFile, "<components>\r\n" );
153
154 for( std::map<std::string, Module*>::const_iterator p = ProjectNode.modules.begin(); p != ProjectNode.modules.end(); ++ p )
155 {
156 Module& module = *p->second;
157
158 ModuleMap::iterator it = module_map.find ( module.name );
159 if ( it != module_map.end () )
160 {
161 module_data * data = it->second;
162
163
164
165 fprintf ( m_DepMapFile, "\t<component>\r\n" );
166 fprintf ( m_DepMapFile, "\t\t<name>%s</name>\r\n", module.name.c_str () );
167 fprintf ( m_DepMapFile, "\t\t<base>%s</base>\r\n", module.output->relative_path.c_str () );
168 fprintf ( m_DepMapFile, "\t\t<ref_count>%u</ref_count>\r\n", (unsigned int)data->references.size () );
169 fprintf ( m_DepMapFile, "\t\t<lib_count>%u</lib_count>\r\n", (unsigned int)data->libraries.size () );
170 #if 0
171 if ( data->references.size () )
172 {
173 fprintf ( m_DepMapFile, "\t<references>\r\n" );
174 for ( size_t j = 0; j < data->references.size (); j++ )
175 {
176 fprintf ( m_DepMapFile, "\t\t<reference name =\"%s\" />\r\n", data->references[j].c_str () );
177 }
178 fprintf ( m_DepMapFile, "\t</references>\r\n" );
179 }
180
181 if ( data->libraries.size () )
182 {
183 fprintf ( m_DepMapFile, "\t<libraries>\r\n" );
184 for ( size_t j = 0; j < data->libraries.size (); j++ )
185 {
186 fprintf ( m_DepMapFile, "\t\t<library name =\"%s\" />\r\n", data->libraries[j].c_str () );
187 }
188 fprintf ( m_DepMapFile, "\t</libraries>\r\n" );
189 }
190 #endif
191 fprintf ( m_DepMapFile, "\t</component>\r\n" );
192 }
193 }
194
195 fprintf ( m_DepMapFile, "</components>" );
196 }
197
198
199 DepMapConfiguration::DepMapConfiguration ( const std::string &name )
200 {
201 /* nothing to do here */
202 }
203
204