2 * Copyright (C) 2005 Casper S. Hornstrup
3 * Copyright (C) 2008 Hervé Poussineau
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "backend/backend.h"
29 Environment::GetVariable ( const string
& name
)
31 char* value
= getenv ( name
.c_str () );
32 if ( value
!= NULL
&& strlen ( value
) > 0 )
33 return ssprintf ( "%s",
40 Environment::GetArch ()
42 return GetEnvironmentVariablePathOrDefault ( "ROS_ARCH", "i386" );
46 Environment::GetEnvironmentVariablePathOrDefault ( const string
& name
,
47 const string
& defaultValue
)
49 const string
& environmentVariableValue
= Environment::GetVariable ( name
);
50 if ( environmentVariableValue
.length () > 0 )
51 return NormalizeFilename ( environmentVariableValue
);
57 Environment::GetIntermediatePath ()
59 string defaultIntermediate
=
60 string( "obj-" ) + GetArch ();
61 return GetEnvironmentVariablePathOrDefault ( "ROS_INTERMEDIATE",
62 defaultIntermediate
);
66 Environment::GetOutputPath ()
68 string defaultOutput
=
69 string( "output-" ) + GetArch ();
70 return GetEnvironmentVariablePathOrDefault ( "ROS_OUTPUT",
75 Environment::GetInstallPath ()
77 string defaultInstall
= GetCdOutputPath ();
78 return GetEnvironmentVariablePathOrDefault ( "ROS_INSTALL",
83 Environment::GetCdOutputPath ()
85 return GetEnvironmentVariablePathOrDefault ( "ROS_CDOUTPUT",
90 Environment::GetAutomakeFile ( const std::string
& defaultFile
)
92 return GetEnvironmentVariablePathOrDefault ( "ROS_AUTOMAKE",
96 ParseContext::ParseContext ()
97 : compilationUnit (NULL
)
102 FileLocation::FileLocation ( const DirectoryLocation directory
,
103 const std::string
& relative_path
,
104 const std::string
& name
,
105 const XMLElement
*node
)
106 : directory ( directory
),
107 relative_path ( NormalizeFilename ( relative_path
) ),
110 if ( relative_path
[0] == '/' ||
111 relative_path
[0] == '\\' ||
112 relative_path
.find ( '$' ) != string::npos
||
113 ( relative_path
.length () > 1 && ( relative_path
[1] == ':' ||
114 relative_path
.find_last_of ( "/\\" ) == relative_path
.length () - 1 ) ) ||
115 ( relative_path
.length () > 3 && relative_path
.find ( ':' ) != string::npos
)
119 throw InvalidOperationException ( __FILE__
,
121 "Invalid relative path '%s' at %s",
122 relative_path
.c_str (),
123 node
->location
.c_str () );
125 throw InvalidOperationException ( __FILE__
,
127 "Invalid relative path '%s'",
128 relative_path
.c_str () );
131 if ( name
.find_first_of ( "/\\:" ) != string::npos
)
134 throw InvalidOperationException ( __FILE__
,
136 "Invalid file name '%s' at %s",
138 node
->location
.c_str () );
140 throw InvalidOperationException ( __FILE__
,
142 "Invalid file name '%s'",
148 FileLocation::FileLocation ( const FileLocation
& other
)
149 : directory ( other
.directory
),
150 relative_path ( other
.relative_path
),
156 Project::Project ( const Configuration
& configuration
,
157 const string
& filename
,
158 const std::map
<std::string
, std::string
>* properties
)
159 : xmlfile (filename
),
162 configuration (configuration
)
168 std::map
<string
, string
>::const_iterator it
;
169 for (it
= properties
->begin (); it
!= properties
->end (); it
++)
171 const Property
*existing
= LookupProperty( it
->first
);
174 Property
* property
= new Property ( *this, NULL
, it
->first
, it
->second
);
175 non_if_data
.properties
.insert ( std::make_pair ( property
->name
, property
) );
188 #ifdef NOT_NEEDED_SINCE_THESE_ARE_CLEANED_BY_IFABLE_DATA
189 for ( i
= 0; i
< modules
.size (); i
++ )
192 for ( i
= 0; i
< linkerFlags
.size (); i
++ )
193 delete linkerFlags
[i
];
194 for ( i
= 0; i
< cdfiles
.size (); i
++ )
196 for ( i
= 0; i
< installfiles
.size (); i
++ )
197 delete installfiles
[i
];
203 Project::LookupProperty ( const string
& name
) const
205 std::map
<std::string
, Property
*>::const_iterator p
= non_if_data
.properties
.find(name
);
207 if ( p
== non_if_data
.properties
.end () )
214 Project::ResolveNextProperty ( const string
& s
) const
216 size_t i
= s
.find ( "${" );
217 if ( i
== string::npos
)
219 if ( i
!= string::npos
)
222 if ( s
[i
+ 1] == '{' )
226 size_t j
= s
.find ( endCharacter
);
227 if ( j
!= string::npos
)
229 int propertyNameLength
= j
- i
- 2;
230 string propertyName
= s
.substr ( i
+ 2, propertyNameLength
);
231 const Property
* property
= LookupProperty ( propertyName
);
232 if ( property
!= NULL
)
233 return string ( s
).replace ( i
, propertyNameLength
+ 3, property
->value
);
240 Project::ResolveProperties ( const string
& s
) const
247 s2
= ResolveNextProperty ( s3
);
248 } while ( s2
!= s3
);
253 Project::ExecuteInvocations ()
255 for( std::map
<std::string
, Module
*>::const_iterator p
= modules
.begin(); p
!= modules
.end(); ++ p
)
256 p
->second
->InvokeModule ();
263 head
= XMLLoadFile ( xmlfile
, path
, xmlbuildfiles
);
265 for ( size_t i
= 0; i
< head
->subElements
.size (); i
++ )
267 if ( head
->subElements
[i
]->name
== "project" )
269 node
= head
->subElements
[i
];
277 node
= head
->subElements
[0];
279 throw XMLInvalidBuildFileException (
281 "Document contains no 'project' tag." );
285 Project::ProcessXML ( const string
& path
)
287 const XMLAttribute
*att
;
288 if ( node
->name
!= "project" )
289 throw Exception ( "internal tool error: Project::ProcessXML() called with non-<project> node" );
291 att
= node
->GetAttribute ( "name", false );
297 att
= node
->GetAttribute ( "makefile", true );
299 makefile
= Environment::GetAutomakeFile ( att
->value
);
301 att
= node
->GetAttribute ( "allowwarnings", false );
302 allowWarningsSet
= att
!= NULL
;
304 allowWarnings
= att
->value
== "true";
307 for ( i
= 0; i
< node
->subElements
.size (); i
++ )
309 ParseContext parseContext
;
310 ProcessXMLSubElement ( *node
->subElements
[i
], path
, parseContext
);
313 non_if_data
.ProcessXML ();
314 host_non_if_data
.ProcessXML ();
316 non_if_data
.ExtractModules( modules
);
318 for ( i
= 0; i
< linkerFlags
.size (); i
++ )
319 linkerFlags
[i
]->ProcessXML ();
320 for( std::map
<std::string
, Module
*>::const_iterator p
= modules
.begin(); p
!= modules
.end(); ++ p
)
321 p
->second
->ProcessXML ();
322 for ( i
= 0; i
< cdfiles
.size (); i
++ )
323 cdfiles
[i
]->ProcessXML ();
324 for ( i
= 0; i
< installfiles
.size (); i
++ )
325 installfiles
[i
]->ProcessXML ();
329 Project::ProcessXMLSubElement ( const XMLElement
& e
,
331 ParseContext
& parseContext
)
333 bool subs_invalid
= false;
335 string
subpath(path
);
336 if ( e
.name
== "module" )
338 Module
* module
= new Module ( *this, e
, path
);
339 if ( LocateModule ( module
->name
) )
340 throw XMLInvalidBuildFileException (
342 "module name conflict: '%s' (originally defined at %s)",
343 module
->name
.c_str(),
344 module
->node
.location
.c_str() );
345 non_if_data
.modules
.push_back ( module
);
346 return; // defer processing until later
348 else if ( e
.name
== "cdfile" )
350 CDFile
* cdfile
= new CDFile ( *this, e
, path
);
351 cdfiles
.push_back ( cdfile
);
354 else if ( e
.name
== "installfile" )
356 InstallFile
* installfile
= new InstallFile ( *this, e
, path
);
357 installfiles
.push_back ( installfile
);
360 else if ( e
.name
== "directory" )
362 const XMLAttribute
* att
= e
.GetAttribute ( "name", true );
364 subpath
= GetSubPath ( *this, e
.location
, path
, att
->value
);
366 else if ( e
.name
== "include" )
368 const XMLAttribute
* host
= e
.GetAttribute("host", false);
369 Include
* include
= new Include ( *this, &e
);
371 if(host
&& host
->value
== "true")
372 host_non_if_data
.includes
.push_back(include
);
374 non_if_data
.includes
.push_back ( include
);
378 else if ( e
.name
== "define" || e
.name
== "redefine" )
380 const XMLAttribute
* host
= e
.GetAttribute("host", false);
381 Define
* define
= new Define ( *this, e
);
383 if(host
&& host
->value
== "true")
384 host_non_if_data
.defines
.push_back(define
);
386 non_if_data
.defines
.push_back ( define
);
390 else if ( e
.name
== "compilerflag" )
392 CompilerFlag
* pCompilerFlag
= new CompilerFlag ( *this, e
);
393 non_if_data
.compilerFlags
.push_back ( pCompilerFlag
);
396 else if ( e
.name
== "linkerflag" )
398 linkerFlags
.push_back ( new LinkerFlag ( *this, e
) );
401 else if ( e
.name
== "if" || e
.name
== "ifnot" )
403 const XMLAttribute
* name
;
404 name
= e
.GetAttribute ( "property", true );
406 const Property
*property
= LookupProperty( name
->value
);
407 const string
*PropertyValue
;
408 const string EmptyString
;
412 PropertyValue
= &property
->value
;
416 // Property does not exist, treat it as being empty
417 PropertyValue
= &EmptyString
;
420 const XMLAttribute
* value
;
421 value
= e
.GetAttribute ( "value", true );
424 bool negate
= ( e
.name
== "ifnot" );
425 bool equality
= ( *PropertyValue
== value
->value
);
426 if ( equality
== negate
)
428 // Failed, skip this element
429 if ( configuration
.Verbose
)
430 printf("Skipping 'If' at %s\n", e
.location
.c_str () );
433 subs_invalid
= false;
435 else if ( e
.name
== "property" )
437 Property
* property
= new Property ( e
, *this, NULL
);
438 non_if_data
.properties
.insert ( std::make_pair ( property
->name
, property
) );
440 if ( subs_invalid
&& e
.subElements
.size() )
442 throw XMLInvalidBuildFileException (
444 "<%s> cannot have sub-elements",
447 for ( size_t i
= 0; i
< e
.subElements
.size (); i
++ )
448 ProcessXMLSubElement ( *e
.subElements
[i
], subpath
, parseContext
);
452 Project::LocateModule ( const string
& name
)
454 std::map
<std::string
, Module
*>::const_iterator p
= modules
.find(name
);
456 if ( p
== modules
.end() )
463 Project::LocateModule ( const string
& name
) const
465 std::map
<std::string
, Module
*>::const_iterator p
= modules
.find(name
);
467 if ( p
== modules
.end() )
474 Project::GetProjectFilename () const