2 * Copyright (C) 2005 Casper S. Hornstrup
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.
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.
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.
22 #include "backend/backend.h"
28 Environment::GetVariable ( const string
& name
)
30 char* value
= getenv ( name
.c_str () );
31 if ( value
!= NULL
&& strlen ( value
) > 0 )
32 return ssprintf ( "%s",
39 Environment::GetEnvironmentVariablePathOrDefault ( const string
& name
,
40 const string
& defaultValue
)
42 const string
& environmentVariableValue
= Environment::GetVariable ( name
);
43 if ( environmentVariableValue
.length () > 0 )
44 return NormalizeFilename ( environmentVariableValue
);
50 Environment::GetIntermediatePath ()
52 string defaultIntermediate
=
53 string( "obj-" ) + GetEnvironmentVariablePathOrDefault ( "ROS_CDOUTPUT", "i386" );
54 return GetEnvironmentVariablePathOrDefault ( "ROS_INTERMEDIATE",
55 defaultIntermediate
);
59 Environment::GetOutputPath ()
61 string defaultOutput
=
62 string( "output-" ) + GetEnvironmentVariablePathOrDefault ( "ROS_CDOUTPUT", "i386" );
63 return GetEnvironmentVariablePathOrDefault ( "ROS_OUTPUT",
68 Environment::GetInstallPath ()
70 string defaultInstall
=
71 string( "reactos." ) + GetEnvironmentVariablePathOrDefault ( "ROS_CDOUTPUT", "" );
72 return GetEnvironmentVariablePathOrDefault ( "ROS_INSTALL",
77 Environment::GetCdOutputPath ()
79 return GetEnvironmentVariablePathOrDefault ( "ROS_CDOUTPUT",
83 ParseContext::ParseContext ()
85 compilationUnit (NULL
)
90 FileLocation::FileLocation ( Directory
* directory
,
91 std::string filename
)
92 : directory (directory
),
98 Project::Project ( const Configuration
& configuration
,
99 const string
& filename
)
100 : xmlfile (filename
),
103 configuration (configuration
)
113 #ifdef NOT_NEEDED_SINCE_THESE_ARE_CLEANED_BY_IFABLE_DATA
114 for ( i
= 0; i
< modules
.size (); i
++ )
117 for ( i
= 0; i
< linkerFlags
.size (); i
++ )
118 delete linkerFlags
[i
];
119 for ( i
= 0; i
< cdfiles
.size (); i
++ )
121 for ( i
= 0; i
< installfiles
.size (); i
++ )
122 delete installfiles
[i
];
127 Project::LookupProperty ( const string
& name
) const
129 for ( size_t i
= 0; i
< non_if_data
.properties
.size (); i
++ )
131 const Property
* property
= non_if_data
.properties
[i
];
132 if ( property
->name
== name
)
139 Project::ResolveNextProperty ( string
& s
) const
141 size_t i
= s
.find ( "${" );
142 if ( i
== string::npos
)
144 if ( i
!= string::npos
)
147 if ( s
[i
+ 1] == '{' )
151 size_t j
= s
.find ( endCharacter
);
152 if ( j
!= string::npos
)
154 int propertyNameLength
= j
- i
- 2;
155 string propertyName
= s
.substr ( i
+ 2, propertyNameLength
);
156 const Property
* property
= LookupProperty ( propertyName
);
157 if ( property
!= NULL
)
158 return s
.replace ( i
, propertyNameLength
+ 3, property
->value
);
165 Project::ResolveProperties ( const string
& s
) const
172 s2
= ResolveNextProperty ( s3
);
173 } while ( s2
!= s3
);
178 Project::SetConfigurationOption ( char* s
,
180 string
* alternativeName
)
182 const Property
* property
= LookupProperty ( name
);
183 if ( property
!= NULL
&& property
->value
.length () > 0 )
187 property
->name
.c_str (),
188 property
->value
.c_str () );
190 else if ( property
!= NULL
)
194 property
->name
.c_str () );
196 else if ( alternativeName
!= NULL
)
200 alternativeName
->c_str () );
205 Project::SetConfigurationOption ( char* s
,
208 SetConfigurationOption ( s
, name
, NULL
);
212 Project::WriteConfigurationFile ()
217 buf
= (char*) malloc ( 10*1024 );
219 throw OutOfMemoryException ();
222 s
= s
+ sprintf ( s
, "/* Automatically generated. " );
223 s
= s
+ sprintf ( s
, "Edit config.xml to change configuration */\n" );
224 s
= s
+ sprintf ( s
, "#ifndef __INCLUDE_CONFIG_H\n" );
225 s
= s
+ sprintf ( s
, "#define __INCLUDE_CONFIG_H\n" );
227 SetConfigurationOption ( s
, "ARCH" );
228 SetConfigurationOption ( s
, "OPTIMIZED" );
229 SetConfigurationOption ( s
, "MP", new string ( "UP" ) );
230 SetConfigurationOption ( s
, "ACPI" );
231 SetConfigurationOption ( s
, "_3GB" );
233 s
= s
+ sprintf ( s
, "#endif /* __INCLUDE_CONFIG_H */\n" );
235 FileSupportCode::WriteIfChanged ( buf
, "include" + sSep
+ "roscfg.h" );
241 Project::ExecuteInvocations ()
243 for ( size_t i
= 0; i
< modules
.size (); i
++ )
244 modules
[i
]->InvokeModule ();
251 head
= XMLLoadFile ( xmlfile
, path
, xmlbuildfiles
);
253 for ( size_t i
= 0; i
< head
->subElements
.size (); i
++ )
255 if ( head
->subElements
[i
]->name
== "project" )
257 node
= head
->subElements
[i
];
265 node
= head
->subElements
[0];
267 throw XMLInvalidBuildFileException (
269 "Document contains no 'project' tag." );
273 Project::ProcessXML ( const string
& path
)
275 const XMLAttribute
*att
;
276 if ( node
->name
!= "project" )
277 throw Exception ( "internal tool error: Project::ProcessXML() called with non-<project> node" );
279 att
= node
->GetAttribute ( "name", false );
285 att
= node
->GetAttribute ( "makefile", true );
287 makefile
= att
->value
;
290 for ( i
= 0; i
< node
->subElements
.size (); i
++ )
292 ParseContext parseContext
;
293 ProcessXMLSubElement ( *node
->subElements
[i
], path
, parseContext
);
296 non_if_data
.ProcessXML ();
298 non_if_data
.ExtractModules( modules
);
300 for ( i
= 0; i
< non_if_data
.ifs
.size (); i
++ )
302 const Property
*property
=
303 LookupProperty( non_if_data
.ifs
[i
]->property
);
305 if( !property
) continue;
308 (non_if_data
.ifs
[i
]->negated
&&
309 (property
->value
!= non_if_data
.ifs
[i
]->value
)) ||
310 (property
->value
== non_if_data
.ifs
[i
]->value
);
312 non_if_data
.ifs
[i
]->data
.ExtractModules( modules
);
315 If
* if_data
= non_if_data
.ifs
[i
];
316 non_if_data
.ifs
.erase ( non_if_data
.ifs
.begin () + i
);
321 for ( i
= 0; i
< linkerFlags
.size (); i
++ )
322 linkerFlags
[i
]->ProcessXML ();
323 for ( i
= 0; i
< modules
.size (); i
++ )
324 modules
[i
]->ProcessXML ();
325 for ( i
= 0; i
< cdfiles
.size (); i
++ )
326 cdfiles
[i
]->ProcessXML ();
327 for ( i
= 0; i
< installfiles
.size (); i
++ )
328 installfiles
[i
]->ProcessXML ();
332 Project::ProcessXMLSubElement ( const XMLElement
& e
,
334 ParseContext
& parseContext
)
336 bool subs_invalid
= false;
337 If
* pOldIf
= parseContext
.ifData
;
339 string
subpath(path
);
340 if ( e
.name
== "module" )
342 Module
* module
= new Module ( *this, e
, path
);
343 if ( LocateModule ( module
->name
) )
344 throw XMLInvalidBuildFileException (
346 "module name conflict: '%s' (originally defined at %s)",
347 module
->name
.c_str(),
348 module
->node
.location
.c_str() );
349 if ( parseContext
.ifData
)
350 parseContext
.ifData
->data
.modules
.push_back( module
);
352 non_if_data
.modules
.push_back ( module
);
353 return; // defer processing until later
355 else if ( e
.name
== "cdfile" )
357 CDFile
* cdfile
= new CDFile ( *this, e
, path
);
358 cdfiles
.push_back ( cdfile
);
361 else if ( e
.name
== "installfile" )
363 InstallFile
* installfile
= new InstallFile ( *this, e
, path
);
364 installfiles
.push_back ( installfile
);
367 else if ( e
.name
== "directory" )
369 const XMLAttribute
* att
= e
.GetAttribute ( "name", true );
371 subpath
= GetSubPath ( e
.location
, path
, att
->value
);
373 else if ( e
.name
== "include" )
375 Include
* include
= new Include ( *this, &e
);
376 if ( parseContext
.ifData
)
377 parseContext
.ifData
->data
.includes
.push_back ( include
);
379 non_if_data
.includes
.push_back ( include
);
382 else if ( e
.name
== "define" )
384 Define
* define
= new Define ( *this, e
);
385 if ( parseContext
.ifData
)
386 parseContext
.ifData
->data
.defines
.push_back ( define
);
388 non_if_data
.defines
.push_back ( define
);
391 else if ( e
.name
== "compilerflag" )
393 CompilerFlag
* pCompilerFlag
= new CompilerFlag ( *this, e
);
394 if ( parseContext
.ifData
)
395 parseContext
.ifData
->data
.compilerFlags
.push_back ( pCompilerFlag
);
397 non_if_data
.compilerFlags
.push_back ( pCompilerFlag
);
400 else if ( e
.name
== "linkerflag" )
402 linkerFlags
.push_back ( new LinkerFlag ( *this, e
) );
405 else if ( e
.name
== "if" )
407 parseContext
.ifData
= new If ( e
, *this, NULL
);
409 pOldIf
->data
.ifs
.push_back ( parseContext
.ifData
);
411 non_if_data
.ifs
.push_back ( parseContext
.ifData
);
412 subs_invalid
= false;
414 else if ( e
.name
== "ifnot" )
416 parseContext
.ifData
= new If ( e
, *this, NULL
, true );
418 pOldIf
->data
.ifs
.push_back ( parseContext
.ifData
);
420 non_if_data
.ifs
.push_back ( parseContext
.ifData
);
421 subs_invalid
= false;
423 else if ( e
.name
== "property" )
425 Property
* property
= new Property ( e
, *this, NULL
);
426 if ( parseContext
.ifData
)
427 parseContext
.ifData
->data
.properties
.push_back ( property
);
429 non_if_data
.properties
.push_back ( property
);
431 if ( subs_invalid
&& e
.subElements
.size() )
433 throw XMLInvalidBuildFileException (
435 "<%s> cannot have sub-elements",
438 for ( size_t i
= 0; i
< e
.subElements
.size (); i
++ )
439 ProcessXMLSubElement ( *e
.subElements
[i
], subpath
, parseContext
);
441 parseContext
.ifData
= pOldIf
;
445 Project::LocateModule ( const string
& name
)
447 for ( size_t i
= 0; i
< modules
.size (); i
++ )
449 if (modules
[i
]->name
== name
)
457 Project::LocateModule ( const string
& name
) const
459 for ( size_t i
= 0; i
< modules
.size (); i
++ )
461 if ( modules
[i
]->name
== name
)
469 Project::GetProjectFilename () const