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 return GetEnvironmentVariablePathOrDefault ( "ROS_INTERMEDIATE",
57 Environment::GetOutputPath ()
59 return GetEnvironmentVariablePathOrDefault ( "ROS_OUTPUT",
64 Environment::GetInstallPath ()
66 return GetEnvironmentVariablePathOrDefault ( "ROS_INSTALL",
70 ParseContext::ParseContext ()
72 compilationUnit (NULL
)
77 FileLocation::FileLocation ( Directory
* directory
,
78 std::string filename
)
79 : directory (directory
),
85 Project::Project ( const Configuration
& configuration
,
86 const string
& filename
)
90 configuration (configuration
)
100 #ifdef NOT_NEEDED_SINCE_THESE_ARE_CLEANED_BY_IFABLE_DATA
101 for ( i
= 0; i
< modules
.size (); i
++ )
104 for ( i
= 0; i
< linkerFlags
.size (); i
++ )
105 delete linkerFlags
[i
];
106 for ( i
= 0; i
< cdfiles
.size (); i
++ )
108 for ( i
= 0; i
< installfiles
.size (); i
++ )
109 delete installfiles
[i
];
114 Project::LookupProperty ( const string
& name
) const
116 for ( size_t i
= 0; i
< non_if_data
.properties
.size (); i
++ )
118 const Property
* property
= non_if_data
.properties
[i
];
119 if ( property
->name
== name
)
126 Project::ResolveNextProperty ( string
& s
) const
128 size_t i
= s
.find ( "${" );
129 if ( i
== string::npos
)
131 if ( i
!= string::npos
)
134 if ( s
[i
+ 1] == '{' )
138 size_t j
= s
.find ( endCharacter
);
139 if ( j
!= string::npos
)
141 int propertyNameLength
= j
- i
- 2;
142 string propertyName
= s
.substr ( i
+ 2, propertyNameLength
);
143 const Property
* property
= LookupProperty ( propertyName
);
144 if ( property
!= NULL
)
145 return s
.replace ( i
, propertyNameLength
+ 3, property
->value
);
152 Project::ResolveProperties ( const string
& s
) const
159 s2
= ResolveNextProperty ( s3
);
160 } while ( s2
!= s3
);
165 Project::SetConfigurationOption ( char* s
,
167 string
* alternativeName
)
169 const Property
* property
= LookupProperty ( name
);
170 if ( property
!= NULL
&& property
->value
.length () > 0 )
174 property
->name
.c_str (),
175 property
->value
.c_str () );
177 else if ( property
!= NULL
)
181 property
->name
.c_str () );
183 else if ( alternativeName
!= NULL
)
187 alternativeName
->c_str () );
192 Project::SetConfigurationOption ( char* s
,
195 SetConfigurationOption ( s
, name
, NULL
);
199 Project::WriteConfigurationFile ()
204 buf
= (char*) malloc ( 10*1024 );
206 throw OutOfMemoryException ();
209 s
= s
+ sprintf ( s
, "/* Automatically generated. " );
210 s
= s
+ sprintf ( s
, "Edit config.xml to change configuration */\n" );
211 s
= s
+ sprintf ( s
, "#ifndef __INCLUDE_CONFIG_H\n" );
212 s
= s
+ sprintf ( s
, "#define __INCLUDE_CONFIG_H\n" );
214 SetConfigurationOption ( s
, "ARCH" );
215 SetConfigurationOption ( s
, "OPTIMIZED" );
216 SetConfigurationOption ( s
, "MP", new string ( "UP" ) );
217 SetConfigurationOption ( s
, "ACPI" );
218 SetConfigurationOption ( s
, "_3GB" );
220 s
= s
+ sprintf ( s
, "#endif /* __INCLUDE_CONFIG_H */\n" );
222 FileSupportCode::WriteIfChanged ( buf
, "include" + sSep
+ "roscfg.h" );
228 Project::ExecuteInvocations ()
230 for ( size_t i
= 0; i
< modules
.size (); i
++ )
231 modules
[i
]->InvokeModule ();
238 head
= XMLLoadFile ( xmlfile
, path
, xmlbuildfiles
);
240 for ( size_t i
= 0; i
< head
->subElements
.size (); i
++ )
242 if ( head
->subElements
[i
]->name
== "project" )
244 node
= head
->subElements
[i
];
252 node
= head
->subElements
[0];
254 throw XMLInvalidBuildFileException (
256 "Document contains no 'project' tag." );
260 Project::ProcessXML ( const string
& path
)
262 const XMLAttribute
*att
;
263 if ( node
->name
!= "project" )
264 throw Exception ( "internal tool error: Project::ProcessXML() called with non-<project> node" );
266 att
= node
->GetAttribute ( "name", false );
272 att
= node
->GetAttribute ( "makefile", true );
274 makefile
= att
->value
;
277 for ( i
= 0; i
< node
->subElements
.size (); i
++ )
279 ParseContext parseContext
;
280 ProcessXMLSubElement ( *node
->subElements
[i
], path
, parseContext
);
283 non_if_data
.ProcessXML ();
285 non_if_data
.ExtractModules( modules
);
287 for ( i
= 0; i
< non_if_data
.ifs
.size (); i
++ )
289 const Property
*property
=
290 LookupProperty( non_if_data
.ifs
[i
]->property
);
292 if( !property
) continue;
295 (non_if_data
.ifs
[i
]->negated
&&
296 (property
->value
!= non_if_data
.ifs
[i
]->value
)) ||
297 (property
->value
== non_if_data
.ifs
[i
]->value
);
299 non_if_data
.ifs
[i
]->data
.ExtractModules( modules
);
302 If
* if_data
= non_if_data
.ifs
[i
];
303 non_if_data
.ifs
.erase ( non_if_data
.ifs
.begin () + i
);
308 for ( i
= 0; i
< linkerFlags
.size (); i
++ )
309 linkerFlags
[i
]->ProcessXML ();
310 for ( i
= 0; i
< modules
.size (); i
++ )
311 modules
[i
]->ProcessXML ();
312 for ( i
= 0; i
< cdfiles
.size (); i
++ )
313 cdfiles
[i
]->ProcessXML ();
314 for ( i
= 0; i
< installfiles
.size (); i
++ )
315 installfiles
[i
]->ProcessXML ();
319 Project::ProcessXMLSubElement ( const XMLElement
& e
,
321 ParseContext
& parseContext
)
323 bool subs_invalid
= false;
324 If
* pOldIf
= parseContext
.ifData
;
326 string
subpath(path
);
327 if ( e
.name
== "module" )
329 Module
* module
= new Module ( *this, e
, path
);
330 if ( LocateModule ( module
->name
) )
331 throw XMLInvalidBuildFileException (
333 "module name conflict: '%s' (originally defined at %s)",
334 module
->name
.c_str(),
335 module
->node
.location
.c_str() );
336 if ( parseContext
.ifData
)
337 parseContext
.ifData
->data
.modules
.push_back( module
);
339 non_if_data
.modules
.push_back ( module
);
340 return; // defer processing until later
342 else if ( e
.name
== "cdfile" )
344 CDFile
* cdfile
= new CDFile ( *this, e
, path
);
345 cdfiles
.push_back ( cdfile
);
348 else if ( e
.name
== "installfile" )
350 InstallFile
* installfile
= new InstallFile ( *this, e
, path
);
351 installfiles
.push_back ( installfile
);
354 else if ( e
.name
== "directory" )
356 const XMLAttribute
* att
= e
.GetAttribute ( "name", true );
358 subpath
= GetSubPath ( e
.location
, path
, att
->value
);
360 else if ( e
.name
== "include" )
362 Include
* include
= new Include ( *this, &e
);
363 if ( parseContext
.ifData
)
364 parseContext
.ifData
->data
.includes
.push_back ( include
);
366 non_if_data
.includes
.push_back ( include
);
369 else if ( e
.name
== "define" )
371 Define
* define
= new Define ( *this, e
);
372 if ( parseContext
.ifData
)
373 parseContext
.ifData
->data
.defines
.push_back ( define
);
375 non_if_data
.defines
.push_back ( define
);
378 else if ( e
.name
== "compilerflag" )
380 CompilerFlag
* pCompilerFlag
= new CompilerFlag ( *this, e
);
381 if ( parseContext
.ifData
)
382 parseContext
.ifData
->data
.compilerFlags
.push_back ( pCompilerFlag
);
384 non_if_data
.compilerFlags
.push_back ( pCompilerFlag
);
387 else if ( e
.name
== "linkerflag" )
389 linkerFlags
.push_back ( new LinkerFlag ( *this, e
) );
392 else if ( e
.name
== "if" )
394 parseContext
.ifData
= new If ( e
, *this, NULL
);
396 pOldIf
->data
.ifs
.push_back ( parseContext
.ifData
);
398 non_if_data
.ifs
.push_back ( parseContext
.ifData
);
399 subs_invalid
= false;
401 else if ( e
.name
== "ifnot" )
403 parseContext
.ifData
= new If ( e
, *this, NULL
, true );
405 pOldIf
->data
.ifs
.push_back ( parseContext
.ifData
);
407 non_if_data
.ifs
.push_back ( parseContext
.ifData
);
408 subs_invalid
= false;
410 else if ( e
.name
== "property" )
412 Property
* property
= new Property ( e
, *this, NULL
);
413 if ( parseContext
.ifData
)
414 parseContext
.ifData
->data
.properties
.push_back ( property
);
416 non_if_data
.properties
.push_back ( property
);
418 if ( subs_invalid
&& e
.subElements
.size() )
420 throw XMLInvalidBuildFileException (
422 "<%s> cannot have sub-elements",
425 for ( size_t i
= 0; i
< e
.subElements
.size (); i
++ )
426 ProcessXMLSubElement ( *e
.subElements
[i
], subpath
, parseContext
);
428 parseContext
.ifData
= pOldIf
;
432 Project::LocateModule ( const string
& name
)
434 for ( size_t i
= 0; i
< modules
.size (); i
++ )
436 if (modules
[i
]->name
== name
)
444 Project::LocateModule ( const string
& name
) const
446 for ( size_t i
= 0; i
< modules
.size (); i
++ )
448 if ( modules
[i
]->name
== name
)
456 Project::GetProjectFilename () const