cb5dae13b62ea55884b684df36f2fa64b53a9e14
[reactos.git] / reactos / tools / rbuild / module.cpp
1 // module.cpp
2
3 #include "pch.h"
4 #include <assert.h>
5
6 #include "rbuild.h"
7
8 using std::string;
9 using std::vector;
10
11 string
12 FixSeparator ( const string& s )
13 {
14 string s2(s);
15 char* p = strchr ( &s2[0], CBAD_SEP );
16 while ( p )
17 {
18 *p++ = CSEP;
19 p = strchr ( p, CBAD_SEP );
20 }
21 return s2;
22 }
23
24 Module::Module ( Project* project,
25 const XMLElement& moduleNode,
26 const string& modulePath )
27 : project(project),
28 node(moduleNode)
29 {
30 path = FixSeparator ( modulePath );
31
32 const XMLAttribute* att = moduleNode.GetAttribute ( "name", true );
33 assert(att);
34 name = att->value;
35
36 att = moduleNode.GetAttribute ( "type", true );
37 assert(att);
38 type = GetModuleType ( *att );
39
40 att = moduleNode.GetAttribute ( "extension", false );
41 if (att != NULL)
42 extension = att->value;
43 else
44 extension = GetDefaultModuleExtension ();
45 }
46
47 Module::~Module ()
48 {
49 size_t i;
50 for ( i = 0; i < files.size(); i++ )
51 delete files[i];
52 for ( i = 0; i < libraries.size(); i++ )
53 delete libraries[i];
54 }
55
56 void
57 Module::ProcessXML ( const XMLElement& e,
58 const string& path )
59 {
60 string subpath ( path );
61 if ( e.name == "file" && e.value.size () )
62 {
63 files.push_back ( new File ( FixSeparator ( path + CSEP + e.value ) ) );
64 }
65 else if ( e.name == "library" && e.value.size () )
66 {
67 libraries.push_back ( new Library ( e.value ) );
68 }
69 else if ( e.name == "directory" )
70 {
71 const XMLAttribute* att = e.GetAttribute ( "name", true );
72 assert(att);
73 subpath = FixSeparator ( path + CSEP + att->value );
74 }
75 else if ( e.name == "include" )
76 {
77 Include* include = new Include ( project, this, e );
78 includes.push_back ( include );
79 include->ProcessXML ( e );
80 }
81 else if ( e.name == "define" )
82 {
83 Define* define = new Define ( project, this, e );
84 defines.push_back ( define );
85 define->ProcessXML ( e );
86 }
87 for ( size_t i = 0; i < e.subElements.size (); i++ )
88 ProcessXML ( *e.subElements[i], subpath );
89 }
90
91 ModuleType
92 Module::GetModuleType ( const XMLAttribute& attribute )
93 {
94 if ( attribute.value == "buildtool" )
95 return BuildTool;
96 if ( attribute.value == "staticlibrary" )
97 return StaticLibrary;
98 if ( attribute.value == "kernelmodedll" )
99 return KernelModeDLL;
100 throw InvalidAttributeValueException ( attribute.name,
101 attribute.value );
102 }
103
104 string
105 Module::GetDefaultModuleExtension ()
106 {
107 switch (type)
108 {
109 case BuildTool:
110 return EXEPOSTFIX;
111 case StaticLibrary:
112 return ".a";
113 case KernelModeDLL:
114 return ".dll";
115 }
116 throw InvalidOperationException (__FILE__,
117 __LINE__);
118 }
119
120 string
121 Module::GetPath ()
122 {
123 return FixSeparator (path) + CSEP + name + extension;
124 }
125
126
127 File::File ( const string& _name )
128 : name(_name)
129 {
130 }
131
132
133 Library::Library ( const string& _name )
134 : name(_name)
135 {
136 }