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",
69 ParseContext::ParseContext ()
71 compilationUnit (NULL
)
76 FileLocation::FileLocation ( Directory
* directory
,
77 std::string filename
)
78 : directory (directory
),
84 Project::Project ( const string
& filename
)
95 for ( i
= 0; i
< modules
.size (); i
++ )
97 for ( i
= 0; i
< linkerFlags
.size (); i
++ )
98 delete linkerFlags
[i
];
99 for ( i
= 0; i
< cdfiles
.size (); i
++ )
101 for ( i
= 0; i
< installfiles
.size (); i
++ )
102 delete installfiles
[i
];
107 Project::LookupProperty ( const string
& name
) const
109 for ( size_t i
= 0; i
< non_if_data
.properties
.size (); i
++ )
111 const Property
* property
= non_if_data
.properties
[i
];
112 if ( property
->name
== name
)
119 Project::ResolveNextProperty ( string
& s
) const
121 size_t i
= s
.find ( "${" );
122 if ( i
== string::npos
)
124 if ( i
!= string::npos
)
127 if ( s
[i
+ 1] == '{' )
131 size_t j
= s
.find ( endCharacter
);
132 if ( j
!= string::npos
)
134 int propertyNameLength
= j
- i
- 2;
135 string propertyName
= s
.substr ( i
+ 2, propertyNameLength
);
136 const Property
* property
= LookupProperty ( propertyName
);
137 if ( property
!= NULL
)
138 return s
.replace ( i
, propertyNameLength
+ 3, property
->value
);
145 Project::ResolveProperties ( const string
& s
) const
152 s2
= ResolveNextProperty ( s3
);
153 } while ( s2
!= s3
);
158 Project::SetConfigurationOption ( char* s
,
160 string
* alternativeName
)
162 const Property
* property
= LookupProperty ( name
);
163 if ( property
!= NULL
&& property
->value
.length () > 0 )
167 property
->name
.c_str (),
168 property
->value
.c_str () );
170 else if ( property
!= NULL
)
174 property
->name
.c_str () );
176 else if ( alternativeName
!= NULL
)
180 alternativeName
->c_str () );
185 Project::SetConfigurationOption ( char* s
,
188 SetConfigurationOption ( s
, name
, NULL
);
192 Project::WriteConfigurationFile ()
197 buf
= (char*) malloc ( 10*1024 );
199 throw OutOfMemoryException ();
202 s
= s
+ sprintf ( s
, "/* Automatically generated. " );
203 s
= s
+ sprintf ( s
, "Edit config.xml to change configuration */\n" );
204 s
= s
+ sprintf ( s
, "#ifndef __INCLUDE_CONFIG_H\n" );
205 s
= s
+ sprintf ( s
, "#define __INCLUDE_CONFIG_H\n" );
207 SetConfigurationOption ( s
, "ARCH" );
208 SetConfigurationOption ( s
, "OPTIMIZED" );
209 SetConfigurationOption ( s
, "MP", new string ( "UP" ) );
210 SetConfigurationOption ( s
, "ACPI" );
211 SetConfigurationOption ( s
, "_3GB" );
213 s
= s
+ sprintf ( s
, "#endif /* __INCLUDE_CONFIG_H */\n" );
215 FileSupportCode::WriteIfChanged ( buf
, "include" + sSep
+ "roscfg.h" );
221 Project::ExecuteInvocations ()
223 for ( size_t i
= 0; i
< modules
.size (); i
++ )
224 modules
[i
]->InvokeModule ();
231 head
= XMLLoadFile ( xmlfile
, path
, xmlbuildfiles
);
233 for ( size_t i
= 0; i
< head
->subElements
.size (); i
++ )
235 if ( head
->subElements
[i
]->name
== "project" )
237 node
= head
->subElements
[i
];
239 this->ProcessXML ( path
);
245 node
= head
->subElements
[0];
247 throw InvalidBuildFileException (
249 "Document contains no 'project' tag." );
253 Project::ProcessXML ( const string
& path
)
255 const XMLAttribute
*att
;
256 if ( node
->name
!= "project" )
257 throw Exception ( "internal tool error: Project::ProcessXML() called with non-<project> node" );
259 att
= node
->GetAttribute ( "name", false );
265 att
= node
->GetAttribute ( "makefile", true );
267 makefile
= att
->value
;
270 for ( i
= 0; i
< node
->subElements
.size (); i
++ )
272 ParseContext parseContext
;
273 ProcessXMLSubElement ( *node
->subElements
[i
], path
, parseContext
);
275 for ( i
= 0; i
< modules
.size (); i
++ )
276 modules
[i
]->ProcessXML ();
277 for ( i
= 0; i
< linkerFlags
.size (); i
++ )
278 linkerFlags
[i
]->ProcessXML ();
279 non_if_data
.ProcessXML ();
280 for ( i
= 0; i
< cdfiles
.size (); i
++ )
281 cdfiles
[i
]->ProcessXML ();
282 for ( i
= 0; i
< installfiles
.size (); i
++ )
283 installfiles
[i
]->ProcessXML ();
287 Project::ProcessXMLSubElement ( const XMLElement
& e
,
289 ParseContext
& parseContext
)
291 bool subs_invalid
= false;
292 string
subpath(path
);
293 if ( e
.name
== "module" )
295 if ( parseContext
.ifData
)
296 throw InvalidBuildFileException (
298 "<module> is not a valid sub-element of <if>" );
299 Module
* module
= new Module ( *this, e
, path
);
300 if ( LocateModule ( module
->name
) )
301 throw InvalidBuildFileException (
303 "module name conflict: '%s' (originally defined at %s)",
304 module
->name
.c_str(),
305 module
->node
.location
.c_str() );
306 modules
.push_back ( module
);
307 return; // defer processing until later
309 else if ( e
.name
== "cdfile" )
311 CDFile
* cdfile
= new CDFile ( *this, e
, path
);
312 cdfiles
.push_back ( cdfile
);
315 else if ( e
.name
== "installfile" )
317 InstallFile
* installfile
= new InstallFile ( *this, e
, path
);
318 installfiles
.push_back ( installfile
);
321 else if ( e
.name
== "directory" )
323 const XMLAttribute
* att
= e
.GetAttribute ( "name", true );
325 subpath
= GetSubPath ( e
.location
, path
, att
->value
);
327 else if ( e
.name
== "include" )
329 Include
* include
= new Include ( *this, &e
);
330 if ( parseContext
.ifData
)
331 parseContext
.ifData
->data
.includes
.push_back ( include
);
333 non_if_data
.includes
.push_back ( include
);
336 else if ( e
.name
== "define" )
338 Define
* define
= new Define ( *this, e
);
339 if ( parseContext
.ifData
)
340 parseContext
.ifData
->data
.defines
.push_back ( define
);
342 non_if_data
.defines
.push_back ( define
);
345 else if ( e
.name
== "compilerflag" )
347 CompilerFlag
* pCompilerFlag
= new CompilerFlag ( *this, e
);
348 if ( parseContext
.ifData
)
349 parseContext
.ifData
->data
.compilerFlags
.push_back ( pCompilerFlag
);
351 non_if_data
.compilerFlags
.push_back ( pCompilerFlag
);
354 else if ( e
.name
== "linkerflag" )
356 linkerFlags
.push_back ( new LinkerFlag ( *this, e
) );
359 else if ( e
.name
== "if" )
361 If
* pOldIf
= parseContext
.ifData
;
362 parseContext
.ifData
= new If ( e
, *this, NULL
);
364 pOldIf
->data
.ifs
.push_back ( parseContext
.ifData
);
366 non_if_data
.ifs
.push_back ( parseContext
.ifData
);
367 subs_invalid
= false;
369 else if ( e
.name
== "ifnot" )
371 If
* pOldIf
= parseContext
.ifData
;
372 parseContext
.ifData
= new If ( e
, *this, NULL
, true );
374 pOldIf
->data
.ifs
.push_back ( parseContext
.ifData
);
376 non_if_data
.ifs
.push_back ( parseContext
.ifData
);
377 subs_invalid
= false;
379 else if ( e
.name
== "property" )
381 Property
* property
= new Property ( e
, *this, NULL
);
382 if ( parseContext
.ifData
)
383 parseContext
.ifData
->data
.properties
.push_back ( property
);
385 non_if_data
.properties
.push_back ( property
);
387 if ( subs_invalid
&& e
.subElements
.size() )
388 throw InvalidBuildFileException (
390 "<%s> cannot have sub-elements",
392 for ( size_t i
= 0; i
< e
.subElements
.size (); i
++ )
393 ProcessXMLSubElement ( *e
.subElements
[i
], subpath
, parseContext
);
397 Project::LocateModule ( const string
& name
)
399 for ( size_t i
= 0; i
< modules
.size (); i
++ )
401 if (modules
[i
]->name
== name
)
409 Project::LocateModule ( const string
& name
) const
411 for ( size_t i
= 0; i
< modules
.size (); i
++ )
413 if ( modules
[i
]->name
== name
)
421 Project::GetProjectFilename () const