add support for <if> and <property>
[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 string
63 MingwBackend::GenerateProjectCFLAGS ()
64 {
65 size_t i;
66 string clags;
67 for ( i = 0; i < ProjectNode.includes.size (); i++ )
68 {
69 Include& include = *ProjectNode.includes[i];
70 if (clags.length () > 0)
71 clags += " ";
72 clags += "-I" + include.directory;
73 }
74
75 for ( i = 0; i < ProjectNode.defines.size (); i++ )
76 {
77 Define& define = *ProjectNode.defines[i];
78 if ( clags.length () > 0 )
79 clags += " ";
80 clags += "-D" + define.name;
81 if ( define.value.size() > 0 )
82 {
83 clags += "=";
84 clags += define.value;
85 }
86 }
87 return clags;
88 }
89
90 void
91 MingwBackend::GenerateGlobalVariables ()
92 {
93 size_t i;
94
95 fprintf ( fMakefile, "host_gcc = gcc\n" );
96 fprintf ( fMakefile, "host_ar = ar\n" );
97 fprintf ( fMakefile, "host_ld = ld\n" );
98 fprintf ( fMakefile, "rm = del /f /q\n" );
99 fprintf ( fMakefile, "gcc = gcc\n" );
100 fprintf ( fMakefile, "ld = ld\n" );
101 fprintf ( fMakefile, "ar = ar\n" );
102 fprintf ( fMakefile, "dlltool = dlltool\n" );
103 fprintf ( fMakefile, "PROJECT_CFLAGS = %s\n", GenerateProjectCFLAGS ().c_str () );
104 for ( i = 0; i < ProjectNode.properties.size(); i++ )
105 {
106 Property& prop = *ProjectNode.properties[i];
107 fprintf ( fMakefile, "%s := %s\n",
108 prop.name.c_str(),
109 prop.value.c_str() );
110 }
111 fprintf ( fMakefile, "\n" );
112 }
113
114 void
115 MingwBackend::GenerateAllTarget ()
116 {
117 fprintf ( fMakefile, "all:" );
118 for ( size_t i = 0; i < ProjectNode.modules.size (); i++ )
119 {
120 Module& module = *ProjectNode.modules[i];
121 fprintf ( fMakefile,
122 " %s",
123 FixupTargetFilename ( module.GetPath () ).c_str () );
124 }
125 fprintf ( fMakefile, "\n\t\n\n" );
126 }
127
128 void
129 MingwBackend::ProcessModule ( Module& module )
130 {
131 MingwModuleHandler* h = MingwModuleHandler::LookupHandler (
132 module.node.location,
133 module.type );
134 h->Process ( module );
135 }
136
137 string
138 FixupTargetFilename ( const string& targetFilename )
139 {
140 return string("$(ROS_INTERMEDIATE)") + targetFilename;
141 }