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 along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 const XMLAttribute
* att
;
335 att
= e
.GetAttribute ( "compilerset", false );
339 CompilerSet compilerSet
;
341 if ( att
->value
== "msc" )
342 compilerSet
= MicrosoftC
;
343 else if ( att
->value
== "gcc" )
344 compilerSet
= GnuGcc
;
346 throw InvalidAttributeValueException (
351 if ( compilerSet
!= configuration
.Compiler
)
355 att
= e
.GetAttribute ( "linkerset", false );
361 if ( att
->value
== "mslink" )
362 linkerSet
= MicrosoftLink
;
363 else if ( att
->value
== "ld" )
366 throw InvalidAttributeValueException (
371 if ( linkerSet
!= configuration
.Linker
)
375 bool subs_invalid
= false;
377 string
subpath(path
);
378 if ( e
.name
== "module" )
380 Module
* module
= new Module ( *this, e
, path
);
381 if ( LocateModule ( module
->name
) )
382 throw XMLInvalidBuildFileException (
384 "module name conflict: '%s' (originally defined at %s)",
385 module
->name
.c_str(),
386 module
->node
.location
.c_str() );
387 non_if_data
.modules
.push_back ( module
);
388 return; // defer processing until later
390 else if ( e
.name
== "cdfile" )
392 CDFile
* cdfile
= new CDFile ( *this, e
, path
);
393 cdfiles
.push_back ( cdfile
);
396 else if ( e
.name
== "installfile" )
398 InstallFile
* installfile
= new InstallFile ( *this, e
, path
);
399 installfiles
.push_back ( installfile
);
402 else if ( e
.name
== "directory" )
404 const XMLAttribute
* att
= e
.GetAttribute ( "name", true );
406 subpath
= GetSubPath ( *this, e
.location
, path
, att
->value
);
408 else if ( e
.name
== "include" )
410 const XMLAttribute
* host
= e
.GetAttribute("host", false);
411 Include
* include
= new Include ( *this, &e
);
413 if(host
&& host
->value
== "true")
414 host_non_if_data
.includes
.push_back(include
);
416 non_if_data
.includes
.push_back ( include
);
420 else if ( e
.name
== "define" || e
.name
== "redefine" )
422 const XMLAttribute
* host
= e
.GetAttribute("host", false);
423 Define
* define
= new Define ( *this, e
);
425 if(host
&& host
->value
== "true")
426 host_non_if_data
.defines
.push_back(define
);
428 non_if_data
.defines
.push_back ( define
);
432 else if ( e
.name
== "compilerflag" )
434 CompilerFlag
* pCompilerFlag
= new CompilerFlag ( *this, e
);
435 non_if_data
.compilerFlags
.push_back ( pCompilerFlag
);
438 else if ( e
.name
== "linkerflag" )
440 linkerFlags
.push_back ( new LinkerFlag ( *this, e
) );
443 else if ( e
.name
== "if" || e
.name
== "ifnot" )
445 const XMLAttribute
* name
;
446 name
= e
.GetAttribute ( "property", true );
448 const Property
*property
= LookupProperty( name
->value
);
449 const string
*PropertyValue
;
450 const string EmptyString
;
454 PropertyValue
= &property
->value
;
458 // Property does not exist, treat it as being empty
459 PropertyValue
= &EmptyString
;
462 const XMLAttribute
* value
;
463 value
= e
.GetAttribute ( "value", true );
466 bool negate
= ( e
.name
== "ifnot" );
467 bool equality
= ( *PropertyValue
== value
->value
);
468 if ( equality
== negate
)
470 // Failed, skip this element
471 if ( configuration
.Verbose
)
472 printf("Skipping 'If' at %s\n", e
.location
.c_str () );
475 subs_invalid
= false;
477 else if ( e
.name
== "property" )
479 Property
* property
= new Property ( e
, *this, NULL
);
480 non_if_data
.properties
.insert ( std::make_pair ( property
->name
, property
) );
482 if ( subs_invalid
&& e
.subElements
.size() )
484 throw XMLInvalidBuildFileException (
486 "<%s> cannot have sub-elements",
489 for ( size_t i
= 0; i
< e
.subElements
.size (); i
++ )
490 ProcessXMLSubElement ( *e
.subElements
[i
], subpath
, parseContext
);
494 Project::LocateModule ( const string
& name
)
496 std::map
<std::string
, Module
*>::const_iterator p
= modules
.find(name
);
498 if ( p
== modules
.end() )
505 Project::LocateModule ( const string
& name
) const
507 std::map
<std::string
, Module
*>::const_iterator p
= modules
.find(name
);
509 if ( p
== modules
.end() )
516 Project::GetProjectFilename () const
522 Project::GetCompilerSet () const
524 switch ( configuration
.Compiler
)
526 case GnuGcc
: return "gcc";
527 case MicrosoftC
: return "msc";
528 default: assert ( false );
533 Project::GetLinkerSet () const
535 switch ( configuration
.Linker
)
537 case GnuLd
: return "ld";
538 case MicrosoftLink
: return "mslink";
539 default: assert ( false );