e74e44f4f4d14dbb4334cb5305f542238d4c78f1
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.
27 Environment::GetVariable ( const string
& name
)
29 char* value
= getenv ( name
.c_str () );
30 if ( value
!= NULL
&& strlen ( value
) > 0 )
31 return ssprintf ( "%s",
38 Environment::GetEnvironmentVariablePathOrDefault ( const string
& name
,
39 const string
& defaultValue
)
41 const string
& environmentVariableValue
= Environment::GetVariable ( name
);
42 if ( environmentVariableValue
.length () > 0 )
43 return NormalizeFilename ( environmentVariableValue
);
49 Environment::GetIntermediatePath ()
51 return GetEnvironmentVariablePathOrDefault ( "ROS_INTERMEDIATE",
56 Environment::GetOutputPath ()
58 return GetEnvironmentVariablePathOrDefault ( "ROS_OUTPUT",
63 Environment::GetInstallPath ()
65 return GetEnvironmentVariablePathOrDefault ( "ROS_INSTALL",
70 Project::Project ( const string
& filename
)
81 for ( i
= 0; i
< modules
.size (); i
++ )
83 for ( i
= 0; i
< linkerFlags
.size (); i
++ )
84 delete linkerFlags
[i
];
85 for ( i
= 0; i
< cdfiles
.size (); i
++ )
87 for ( i
= 0; i
< installfiles
.size (); i
++ )
88 delete installfiles
[i
];
93 Project::LookupProperty ( const string
& name
) const
95 for ( size_t i
= 0; i
< non_if_data
.properties
.size (); i
++ )
97 const Property
* property
= non_if_data
.properties
[i
];
98 if ( property
->name
== name
)
105 Project::ResolveNextProperty ( string
& s
) const
107 size_t i
= s
.find ( "${" );
108 if ( i
== string::npos
)
110 if ( i
!= string::npos
)
113 if ( s
[i
+ 1] == '{' )
117 size_t j
= s
.find ( endCharacter
);
118 if ( j
!= string::npos
)
120 int propertyNameLength
= j
- i
- 2;
121 string propertyName
= s
.substr ( i
+ 2, propertyNameLength
);
122 const Property
* property
= LookupProperty ( propertyName
);
123 if ( property
!= NULL
)
124 return s
.replace ( i
, propertyNameLength
+ 3, property
->value
);
131 Project::ResolveProperties ( const string
& s
) const
138 s2
= ResolveNextProperty ( s3
);
139 } while ( s2
!= s3
);
144 Project::SetConfigurationOption ( char* s
,
146 string
* alternativeName
)
148 const Property
* property
= LookupProperty ( name
);
149 if ( property
!= NULL
&& property
->value
.length () > 0 )
153 property
->name
.c_str (),
154 property
->value
.c_str () );
156 else if ( property
!= NULL
)
160 property
->name
.c_str () );
162 else if ( alternativeName
!= NULL
)
166 alternativeName
->c_str () );
171 Project::SetConfigurationOption ( char* s
,
174 SetConfigurationOption ( s
, name
, NULL
);
178 Project::WriteConfigurationFile ()
183 buf
= (char*) malloc ( 10*1024 );
185 throw OutOfMemoryException ();
188 s
= s
+ sprintf ( s
, "/* Automatically generated. " );
189 s
= s
+ sprintf ( s
, "Edit config.xml to change configuration */\n" );
190 s
= s
+ sprintf ( s
, "#ifndef __INCLUDE_CONFIG_H\n" );
191 s
= s
+ sprintf ( s
, "#define __INCLUDE_CONFIG_H\n" );
193 SetConfigurationOption ( s
, "ARCH" );
194 SetConfigurationOption ( s
, "OPTIMIZED" );
195 SetConfigurationOption ( s
, "MP", new string ( "UP" ) );
196 SetConfigurationOption ( s
, "ACPI" );
197 SetConfigurationOption ( s
, "_3GB" );
199 s
= s
+ sprintf ( s
, "#endif /* __INCLUDE_CONFIG_H */\n" );
201 FileSupportCode::WriteIfChanged ( buf
, "include" + sSep
+ "roscfg.h" );
207 Project::ExecuteInvocations ()
209 for ( size_t i
= 0; i
< modules
.size (); i
++ )
210 modules
[i
]->InvokeModule ();
217 head
= XMLLoadFile ( xmlfile
, path
, xmlbuildfiles
);
219 for ( size_t i
= 0; i
< head
->subElements
.size (); i
++ )
221 if ( head
->subElements
[i
]->name
== "project" )
223 node
= head
->subElements
[i
];
225 this->ProcessXML ( path
);
231 node
= head
->subElements
[0];
233 throw InvalidBuildFileException (
235 "Document contains no 'project' tag." );
239 Project::ProcessXML ( const string
& path
)
241 const XMLAttribute
*att
;
242 if ( node
->name
!= "project" )
243 throw Exception ( "internal tool error: Project::ProcessXML() called with non-<project> node" );
245 att
= node
->GetAttribute ( "name", false );
251 att
= node
->GetAttribute ( "makefile", true );
253 makefile
= att
->value
;
256 for ( i
= 0; i
< node
->subElements
.size (); i
++ )
257 ProcessXMLSubElement ( *node
->subElements
[i
], path
);
258 for ( i
= 0; i
< modules
.size (); i
++ )
259 modules
[i
]->ProcessXML ();
260 for ( i
= 0; i
< linkerFlags
.size (); i
++ )
261 linkerFlags
[i
]->ProcessXML ();
262 non_if_data
.ProcessXML ();
263 for ( i
= 0; i
< cdfiles
.size (); i
++ )
264 cdfiles
[i
]->ProcessXML ();
265 for ( i
= 0; i
< installfiles
.size (); i
++ )
266 installfiles
[i
]->ProcessXML ();
270 Project::ProcessXMLSubElement ( const XMLElement
& e
,
274 bool subs_invalid
= false;
275 string
subpath(path
);
276 if ( e
.name
== "module" )
279 throw InvalidBuildFileException (
281 "<module> is not a valid sub-element of <if>" );
282 Module
* module
= new Module ( *this, e
, path
);
283 if ( LocateModule ( module
->name
) )
284 throw InvalidBuildFileException (
286 "module name conflict: '%s' (originally defined at %s)",
287 module
->name
.c_str(),
288 module
->node
.location
.c_str() );
289 modules
.push_back ( module
);
290 return; // defer processing until later
292 else if ( e
.name
== "cdfile" )
294 CDFile
* cdfile
= new CDFile ( *this, e
, path
);
295 cdfiles
.push_back ( cdfile
);
298 else if ( e
.name
== "installfile" )
300 InstallFile
* installfile
= new InstallFile ( *this, e
, path
);
301 installfiles
.push_back ( installfile
);
304 else if ( e
.name
== "directory" )
306 const XMLAttribute
* att
= e
.GetAttribute ( "name", true );
308 subpath
= GetSubPath ( e
.location
, path
, att
->value
);
310 else if ( e
.name
== "include" )
312 Include
* include
= new Include ( *this, &e
);
314 pIf
->data
.includes
.push_back ( include
);
316 non_if_data
.includes
.push_back ( include
);
319 else if ( e
.name
== "define" )
321 Define
* define
= new Define ( *this, e
);
323 pIf
->data
.defines
.push_back ( define
);
325 non_if_data
.defines
.push_back ( define
);
328 else if ( e
.name
== "compilerflag" )
330 CompilerFlag
* pCompilerFlag
= new CompilerFlag ( *this, e
);
332 pIf
->data
.compilerFlags
.push_back ( pCompilerFlag
);
334 non_if_data
.compilerFlags
.push_back ( pCompilerFlag
);
337 else if ( e
.name
== "linkerflag" )
339 linkerFlags
.push_back ( new LinkerFlag ( *this, e
) );
342 else if ( e
.name
== "if" )
345 pIf
= new If ( e
, *this, NULL
);
347 pOldIf
->data
.ifs
.push_back ( pIf
);
349 non_if_data
.ifs
.push_back ( pIf
);
350 subs_invalid
= false;
352 else if ( e
.name
== "ifnot" )
355 pIf
= new If ( e
, *this, NULL
, true );
357 pOldIf
->data
.ifs
.push_back ( pIf
);
359 non_if_data
.ifs
.push_back ( pIf
);
360 subs_invalid
= false;
362 else if ( e
.name
== "property" )
364 Property
* property
= new Property ( e
, *this, NULL
);
366 pIf
->data
.properties
.push_back ( property
);
368 non_if_data
.properties
.push_back ( property
);
370 if ( subs_invalid
&& e
.subElements
.size() )
371 throw InvalidBuildFileException (
373 "<%s> cannot have sub-elements",
375 for ( size_t i
= 0; i
< e
.subElements
.size (); i
++ )
376 ProcessXMLSubElement ( *e
.subElements
[i
], subpath
, pIf
);
380 Project::LocateModule ( const string
& name
)
382 for ( size_t i
= 0; i
< modules
.size (); i
++ )
384 if (modules
[i
]->name
== name
)
392 Project::LocateModule ( const string
& name
) const
394 for ( size_t i
= 0; i
< modules
.size (); i
++ )
396 if ( modules
[i
]->name
== name
)
404 Project::GetProjectFilename () const