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 for ( i
= 0; i
< modules
.size (); i
++ )
102 for ( i
= 0; i
< linkerFlags
.size (); i
++ )
103 delete linkerFlags
[i
];
104 for ( i
= 0; i
< cdfiles
.size (); i
++ )
106 for ( i
= 0; i
< installfiles
.size (); i
++ )
107 delete installfiles
[i
];
112 Project::LookupProperty ( const string
& name
) const
114 for ( size_t i
= 0; i
< non_if_data
.properties
.size (); i
++ )
116 const Property
* property
= non_if_data
.properties
[i
];
117 if ( property
->name
== name
)
124 Project::ResolveNextProperty ( string
& s
) const
126 size_t i
= s
.find ( "${" );
127 if ( i
== string::npos
)
129 if ( i
!= string::npos
)
132 if ( s
[i
+ 1] == '{' )
136 size_t j
= s
.find ( endCharacter
);
137 if ( j
!= string::npos
)
139 int propertyNameLength
= j
- i
- 2;
140 string propertyName
= s
.substr ( i
+ 2, propertyNameLength
);
141 const Property
* property
= LookupProperty ( propertyName
);
142 if ( property
!= NULL
)
143 return s
.replace ( i
, propertyNameLength
+ 3, property
->value
);
150 Project::ResolveProperties ( const string
& s
) const
157 s2
= ResolveNextProperty ( s3
);
158 } while ( s2
!= s3
);
163 Project::SetConfigurationOption ( char* s
,
165 string
* alternativeName
)
167 const Property
* property
= LookupProperty ( name
);
168 if ( property
!= NULL
&& property
->value
.length () > 0 )
172 property
->name
.c_str (),
173 property
->value
.c_str () );
175 else if ( property
!= NULL
)
179 property
->name
.c_str () );
181 else if ( alternativeName
!= NULL
)
185 alternativeName
->c_str () );
190 Project::SetConfigurationOption ( char* s
,
193 SetConfigurationOption ( s
, name
, NULL
);
197 Project::WriteConfigurationFile ()
202 buf
= (char*) malloc ( 10*1024 );
204 throw OutOfMemoryException ();
207 s
= s
+ sprintf ( s
, "/* Automatically generated. " );
208 s
= s
+ sprintf ( s
, "Edit config.xml to change configuration */\n" );
209 s
= s
+ sprintf ( s
, "#ifndef __INCLUDE_CONFIG_H\n" );
210 s
= s
+ sprintf ( s
, "#define __INCLUDE_CONFIG_H\n" );
212 SetConfigurationOption ( s
, "ARCH" );
213 SetConfigurationOption ( s
, "OPTIMIZED" );
214 SetConfigurationOption ( s
, "MP", new string ( "UP" ) );
215 SetConfigurationOption ( s
, "ACPI" );
216 SetConfigurationOption ( s
, "_3GB" );
218 s
= s
+ sprintf ( s
, "#endif /* __INCLUDE_CONFIG_H */\n" );
220 FileSupportCode::WriteIfChanged ( buf
, "include" + sSep
+ "roscfg.h" );
226 Project::ExecuteInvocations ()
228 for ( size_t i
= 0; i
< modules
.size (); i
++ )
229 modules
[i
]->InvokeModule ();
236 head
= XMLLoadFile ( xmlfile
, path
, xmlbuildfiles
);
238 for ( size_t i
= 0; i
< head
->subElements
.size (); i
++ )
240 if ( head
->subElements
[i
]->name
== "project" )
242 node
= head
->subElements
[i
];
244 this->ProcessXML ( path
);
250 node
= head
->subElements
[0];
252 throw XMLInvalidBuildFileException (
254 "Document contains no 'project' tag." );
258 Project::ProcessXML ( const string
& path
)
260 const XMLAttribute
*att
;
261 if ( node
->name
!= "project" )
262 throw Exception ( "internal tool error: Project::ProcessXML() called with non-<project> node" );
264 att
= node
->GetAttribute ( "name", false );
270 att
= node
->GetAttribute ( "makefile", true );
272 makefile
= att
->value
;
275 for ( i
= 0; i
< node
->subElements
.size (); i
++ )
277 ParseContext parseContext
;
278 ProcessXMLSubElement ( *node
->subElements
[i
], path
, parseContext
);
280 for ( i
= 0; i
< modules
.size (); i
++ )
281 modules
[i
]->ProcessXML ();
282 for ( i
= 0; i
< linkerFlags
.size (); i
++ )
283 linkerFlags
[i
]->ProcessXML ();
284 non_if_data
.ProcessXML ();
285 for ( i
= 0; i
< cdfiles
.size (); i
++ )
286 cdfiles
[i
]->ProcessXML ();
287 for ( i
= 0; i
< installfiles
.size (); i
++ )
288 installfiles
[i
]->ProcessXML ();
292 Project::ProcessXMLSubElement ( const XMLElement
& e
,
294 ParseContext
& parseContext
)
296 bool subs_invalid
= false;
297 string
subpath(path
);
298 if ( e
.name
== "module" )
300 if ( parseContext
.ifData
)
301 throw XMLInvalidBuildFileException (
303 "<module> is not a valid sub-element of <if>" );
304 Module
* module
= new Module ( *this, e
, path
);
305 if ( LocateModule ( module
->name
) )
306 throw XMLInvalidBuildFileException (
308 "module name conflict: '%s' (originally defined at %s)",
309 module
->name
.c_str(),
310 module
->node
.location
.c_str() );
311 modules
.push_back ( module
);
312 return; // defer processing until later
314 else if ( e
.name
== "cdfile" )
316 CDFile
* cdfile
= new CDFile ( *this, e
, path
);
317 cdfiles
.push_back ( cdfile
);
320 else if ( e
.name
== "installfile" )
322 InstallFile
* installfile
= new InstallFile ( *this, e
, path
);
323 installfiles
.push_back ( installfile
);
326 else if ( e
.name
== "directory" )
328 const XMLAttribute
* att
= e
.GetAttribute ( "name", true );
330 subpath
= GetSubPath ( e
.location
, path
, att
->value
);
332 else if ( e
.name
== "include" )
334 Include
* include
= new Include ( *this, &e
);
335 if ( parseContext
.ifData
)
336 parseContext
.ifData
->data
.includes
.push_back ( include
);
338 non_if_data
.includes
.push_back ( include
);
341 else if ( e
.name
== "define" )
343 Define
* define
= new Define ( *this, e
);
344 if ( parseContext
.ifData
)
345 parseContext
.ifData
->data
.defines
.push_back ( define
);
347 non_if_data
.defines
.push_back ( define
);
350 else if ( e
.name
== "compilerflag" )
352 CompilerFlag
* pCompilerFlag
= new CompilerFlag ( *this, e
);
353 if ( parseContext
.ifData
)
354 parseContext
.ifData
->data
.compilerFlags
.push_back ( pCompilerFlag
);
356 non_if_data
.compilerFlags
.push_back ( pCompilerFlag
);
359 else if ( e
.name
== "linkerflag" )
361 linkerFlags
.push_back ( new LinkerFlag ( *this, e
) );
364 else if ( e
.name
== "if" )
366 If
* pOldIf
= parseContext
.ifData
;
367 parseContext
.ifData
= new If ( e
, *this, NULL
);
369 pOldIf
->data
.ifs
.push_back ( parseContext
.ifData
);
371 non_if_data
.ifs
.push_back ( parseContext
.ifData
);
372 subs_invalid
= false;
374 else if ( e
.name
== "ifnot" )
376 If
* pOldIf
= parseContext
.ifData
;
377 parseContext
.ifData
= new If ( e
, *this, NULL
, true );
379 pOldIf
->data
.ifs
.push_back ( parseContext
.ifData
);
381 non_if_data
.ifs
.push_back ( parseContext
.ifData
);
382 subs_invalid
= false;
384 else if ( e
.name
== "property" )
386 Property
* property
= new Property ( e
, *this, NULL
);
387 if ( parseContext
.ifData
)
388 parseContext
.ifData
->data
.properties
.push_back ( property
);
390 non_if_data
.properties
.push_back ( property
);
392 if ( subs_invalid
&& e
.subElements
.size() )
394 throw XMLInvalidBuildFileException (
396 "<%s> cannot have sub-elements",
399 for ( size_t i
= 0; i
< e
.subElements
.size (); i
++ )
400 ProcessXMLSubElement ( *e
.subElements
[i
], subpath
, parseContext
);
404 Project::LocateModule ( const string
& name
)
406 for ( size_t i
= 0; i
< modules
.size (); i
++ )
408 if (modules
[i
]->name
== name
)
416 Project::LocateModule ( const string
& name
) const
418 for ( size_t i
= 0; i
< modules
.size (); i
++ )
420 if ( modules
[i
]->name
== name
)
428 Project::GetProjectFilename () const