73fc90620f19f15e80e61c06a496cc4bfd872788
[reactos.git] / reactos / tools / rbuild / backend / mingw / mingw.cpp
1
2 #include "../../pch.h"
3
4 #include "mingw.h"
5
6 using std::string;
7 using std::vector;
8
9 static class MingwFactory : public Backend::Factory
10 {
11 public:
12 MingwFactory() : Factory ( "mingw" ) {}
13 Backend* operator() ( Project& project )
14 {
15 return new MingwBackend ( project );
16 }
17 } factory;
18
19
20 MingwBackend::MingwBackend ( Project& project )
21 : Backend ( project )
22 {
23 }
24
25 void
26 MingwBackend::Process ()
27 {
28 CreateMakefile ();
29 GenerateHeader ();
30 GenerateGlobalVariables ();
31 GenerateAllTarget ();
32 for ( size_t i = 0; i < ProjectNode.modules.size (); i++ )
33 {
34 Module& module = *ProjectNode.modules[i];
35 ProcessModule ( module );
36 }
37 CloseMakefile ();
38 }
39
40 void
41 MingwBackend::CreateMakefile ()
42 {
43 fMakefile = fopen ( ProjectNode.makefile.c_str (), "w" );
44 if ( !fMakefile )
45 throw AccessDeniedException ( ProjectNode.makefile );
46 MingwModuleHandler::SetMakefile ( fMakefile );
47 }
48
49 void
50 MingwBackend::CloseMakefile ()
51 {
52 if (fMakefile)
53 fclose ( fMakefile );
54 }
55
56 void
57 MingwBackend::GenerateHeader ()
58 {
59 fprintf ( fMakefile, "# THIS FILE IS AUTOMATICALLY GENERATED, EDIT 'ReactOS.xml' INSTEAD\n\n" );
60 }
61
62 void
63 MingwBackend::GenerateGlobalCFlagsAndProperties (
64 const char* op,
65 const vector<Property*>& properties,
66 const vector<Include*>& includes,
67 const vector<Define*>& defines,
68 const vector<If*>& ifs )
69 {
70 size_t i;
71
72 for ( i = 0; i < properties.size(); i++ )
73 {
74 Property& prop = *properties[i];
75 fprintf ( fMakefile, "%s := %s\n",
76 prop.name.c_str(),
77 prop.value.c_str() );
78 }
79
80 if ( includes.size() || defines.size() )
81 {
82 fprintf (
83 fMakefile,
84 "PROJECT_CFLAGS %s",
85 op );
86 for ( i = 0; i < includes.size(); i++ )
87 {
88 fprintf (
89 fMakefile,
90 " -I%s",
91 includes[i]->directory.c_str() );
92 }
93 for ( i = 0; i < defines.size(); i++ )
94 {
95 Define& d = *defines[i];
96 fprintf (
97 fMakefile,
98 " -D%s",
99 d.name.c_str() );
100 if ( d.value.size() )
101 fprintf (
102 fMakefile,
103 "=%s",
104 d.value.c_str() );
105 }
106 fprintf ( fMakefile, "\n" );
107 }
108
109 for ( i = 0; i < ifs.size(); i++ )
110 {
111 If& rIf = *ifs[i];
112 if ( rIf.defines.size() || rIf.includes.size() || rIf.ifs.size() )
113 {
114 fprintf (
115 fMakefile,
116 "ifeq (\"$(%s)\",\"%s\")\n",
117 rIf.property.c_str(),
118 rIf.value.c_str() );
119 GenerateGlobalCFlagsAndProperties (
120 "+=",
121 rIf.properties,
122 rIf.includes,
123 rIf.defines,
124 rIf.ifs );
125 fprintf (
126 fMakefile,
127 "endif\n\n" );
128 }
129 }
130 }
131
132 void
133 MingwBackend::GenerateGlobalVariables ()
134 {
135 fprintf ( fMakefile, "host_gcc = gcc\n" );
136 fprintf ( fMakefile, "host_ar = ar\n" );
137 fprintf ( fMakefile, "host_ld = ld\n" );
138 fprintf ( fMakefile, "rm = del /f /q\n" );
139 fprintf ( fMakefile, "gcc = gcc\n" );
140 fprintf ( fMakefile, "ld = ld\n" );
141 fprintf ( fMakefile, "ar = ar\n" );
142 fprintf ( fMakefile, "dlltool = dlltool\n\n" );
143 GenerateGlobalCFlagsAndProperties (
144 "=",
145 ProjectNode.properties,
146 ProjectNode.includes,
147 ProjectNode.defines,
148 ProjectNode.ifs );
149 fprintf ( fMakefile, "\n" );
150 }
151
152 void
153 MingwBackend::GenerateAllTarget ()
154 {
155 fprintf ( fMakefile, "all:" );
156 for ( size_t i = 0; i < ProjectNode.modules.size (); i++ )
157 {
158 Module& module = *ProjectNode.modules[i];
159 fprintf ( fMakefile,
160 " %s",
161 FixupTargetFilename ( module.GetPath () ).c_str () );
162 }
163 fprintf ( fMakefile, "\n\t\n\n" );
164 }
165
166 void
167 MingwBackend::ProcessModule ( Module& module )
168 {
169 MingwModuleHandler* h = MingwModuleHandler::LookupHandler (
170 module.node.location,
171 module.type );
172 h->Process ( module );
173 }
174
175 string
176 FixupTargetFilename ( const string& targetFilename )
177 {
178 return string("$(ROS_INTERMEDIATE)") + targetFilename;
179 }