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 Right ( const string
& s
, size_t n
)
31 return string ( &s
[s
.size()-n
] );
35 Replace ( const string
& s
, const string
& find
, const string
& with
)
38 const char* p
= s
.c_str();
41 const char* p2
= strstr ( p
, find
.c_str() );
45 ret
+= string ( p
, p2
-p
);
55 FixSeparator ( const string
& s
)
58 char* p
= strchr ( &s2
[0], CBAD_SEP
);
62 p
= strchr ( p
, CBAD_SEP
);
68 DosSeparator ( const string
& s
)
71 char* p
= strchr ( &s2
[0], '/' );
75 p
= strchr ( p
, '/' );
82 const string
& filename
,
83 const string
& newExtension
)
85 size_t index
= filename
.find_last_of ( '/' );
86 if ( index
== string::npos
)
88 size_t index2
= filename
.find_last_of ( '\\' );
89 if ( index2
!= string::npos
&& index2
> index
)
91 string tmp
= filename
.substr( index
/*, filename.size() - index*/ );
92 size_t ext_index
= tmp
.find_last_of( '.' );
93 if ( ext_index
!= string::npos
)
94 return filename
.substr ( 0, index
+ ext_index
) + newExtension
;
95 return filename
+ newExtension
;
100 const string
& location
,
102 const string
& att_value
)
104 if ( !att_value
.size() )
105 throw InvalidBuildFileException (
107 "<directory> tag has empty 'name' attribute" );
108 if ( strpbrk ( att_value
.c_str (), "/\\?*:<>|" ) )
109 throw InvalidBuildFileException (
111 "<directory> tag has invalid characters in 'name' attribute" );
114 return FixSeparator(path
+ CSEP
+ att_value
);
118 GetExtension ( const string
& filename
)
120 size_t index
= filename
.find_last_of ( '/' );
121 if (index
== string::npos
) index
= 0;
122 string tmp
= filename
.substr( index
, filename
.size() - index
);
123 size_t ext_index
= tmp
.find_last_of( '.' );
124 if (ext_index
!= string::npos
)
125 return filename
.substr ( index
+ ext_index
, filename
.size() );
130 GetDirectory ( const string
& filename
)
132 size_t index
= filename
.find_last_of ( CSEP
);
133 if ( index
== string::npos
)
136 return filename
.substr ( 0, index
);
140 GetFilename ( const string
& filename
)
142 size_t index
= filename
.find_last_of ( CSEP
);
143 if ( index
== string::npos
)
146 return filename
.substr ( index
+ 1, filename
.length () - index
);
150 NormalizeFilename ( const string
& filename
)
152 if ( filename
== "" )
155 string normalizedPath
= path
.Fixup ( filename
, true );
156 string relativeNormalizedPath
= path
.RelativeFromWorkingDirectory ( normalizedPath
);
157 return FixSeparator ( relativeNormalizedPath
);
161 GetBooleanValue ( const string
& value
)
169 IfableData::~IfableData()
172 for ( i
= 0; i
< files
.size(); i
++ )
174 for ( i
= 0; i
< includes
.size(); i
++ )
176 for ( i
= 0; i
< defines
.size(); i
++ )
178 for ( i
= 0; i
< libraries
.size(); i
++ )
180 for ( i
= 0; i
< properties
.size(); i
++ )
181 delete properties
[i
];
182 for ( i
= 0; i
< compilerFlags
.size(); i
++ )
183 delete compilerFlags
[i
];
184 for ( i
= 0; i
< ifs
.size(); i
++ )
188 void IfableData::ProcessXML ()
191 for ( i
= 0; i
< files
.size (); i
++ )
192 files
[i
]->ProcessXML ();
193 for ( i
= 0; i
< includes
.size (); i
++ )
194 includes
[i
]->ProcessXML ();
195 for ( i
= 0; i
< defines
.size (); i
++ )
196 defines
[i
]->ProcessXML ();
197 for ( i
= 0; i
< libraries
.size (); i
++ )
198 libraries
[i
]->ProcessXML ();
199 for ( i
= 0; i
< properties
.size(); i
++ )
200 properties
[i
]->ProcessXML ();
201 for ( i
= 0; i
< compilerFlags
.size(); i
++ )
202 compilerFlags
[i
]->ProcessXML ();
203 for ( i
= 0; i
< ifs
.size (); i
++ )
204 ifs
[i
]->ProcessXML ();
207 Module::Module ( const Project
& project
,
208 const XMLElement
& moduleNode
,
209 const string
& modulePath
)
212 importLibrary (NULL
),
218 if ( node
.name
!= "module" )
219 throw InvalidOperationException ( __FILE__
,
221 "Module created with non-<module> node" );
223 xmlbuildFile
= Path::RelativeFromWorkingDirectory ( moduleNode
.xmlFile
->filename () );
225 path
= FixSeparator ( modulePath
);
229 const XMLAttribute
* att
= moduleNode
.GetAttribute ( "if", false );
231 enabled
= GetBooleanValue ( project
.ResolveProperties ( att
->value
) );
233 att
= moduleNode
.GetAttribute ( "ifnot", false );
235 enabled
= !GetBooleanValue ( project
.ResolveProperties ( att
->value
) );
237 att
= moduleNode
.GetAttribute ( "name", true );
241 att
= moduleNode
.GetAttribute ( "type", true );
243 type
= GetModuleType ( node
.location
, *att
);
245 att
= moduleNode
.GetAttribute ( "extension", false );
247 extension
= att
->value
;
249 extension
= GetDefaultModuleExtension ();
251 att
= moduleNode
.GetAttribute ( "entrypoint", false );
253 entrypoint
= att
->value
;
255 entrypoint
= GetDefaultModuleEntrypoint ();
257 att
= moduleNode
.GetAttribute ( "baseaddress", false );
259 baseaddress
= att
->value
;
261 baseaddress
= GetDefaultModuleBaseaddress ();
263 att
= moduleNode
.GetAttribute ( "mangledsymbols", false );
266 const char* p
= att
->value
.c_str();
267 if ( !stricmp ( p
, "true" ) || !stricmp ( p
, "yes" ) )
268 mangledSymbols
= true;
269 else if ( !stricmp ( p
, "false" ) || !stricmp ( p
, "no" ) )
270 mangledSymbols
= false;
273 throw InvalidAttributeValueException (
280 mangledSymbols
= false;
282 att
= moduleNode
.GetAttribute ( "host", false );
285 const char* p
= att
->value
.c_str();
286 if ( !stricmp ( p
, "true" ) || !stricmp ( p
, "yes" ) )
288 else if ( !stricmp ( p
, "false" ) || !stricmp ( p
, "no" ) )
292 throw InvalidAttributeValueException (
299 att
= moduleNode
.GetAttribute ( "prefix", false );
303 att
= moduleNode
.GetAttribute ( "installbase", false );
305 installBase
= att
->value
;
309 att
= moduleNode
.GetAttribute ( "installname", false );
311 installName
= att
->value
;
315 att
= moduleNode
.GetAttribute ( "usewrc", false );
317 useWRC
= att
->value
== "true";
321 att
= moduleNode
.GetAttribute ( "warnings", false );
323 enableWarnings
= att
->value
== "true";
325 enableWarnings
= false;
327 att
= moduleNode
.GetAttribute ( "aliasof", false );
328 if ( type
== Alias
&& att
!= NULL
)
329 aliasedModuleName
= att
->value
;
331 aliasedModuleName
= "";
337 for ( i
= 0; i
< invocations
.size(); i
++ )
338 delete invocations
[i
];
339 for ( i
= 0; i
< dependencies
.size(); i
++ )
340 delete dependencies
[i
];
341 for ( i
= 0; i
< compilerFlags
.size(); i
++ )
342 delete compilerFlags
[i
];
343 for ( i
= 0; i
< linkerFlags
.size(); i
++ )
344 delete linkerFlags
[i
];
345 for ( i
= 0; i
< stubbedComponents
.size(); i
++ )
346 delete stubbedComponents
[i
];
356 if ( aliasedModuleName
== name
)
357 throw InvalidBuildFileException (
359 "module '%s' cannot link against itself",
361 const Module
* m
= project
.LocateModule ( aliasedModuleName
);
363 throw InvalidBuildFileException (
365 "module '%s' trying to alias non-existant module '%s'",
367 aliasedModuleName
.c_str() );
371 for ( i
= 0; i
< node
.subElements
.size(); i
++ )
372 ProcessXMLSubElement ( *node
.subElements
[i
], path
);
373 for ( i
= 0; i
< invocations
.size(); i
++ )
374 invocations
[i
]->ProcessXML ();
375 for ( i
= 0; i
< dependencies
.size(); i
++ )
376 dependencies
[i
]->ProcessXML ();
377 for ( i
= 0; i
< compilerFlags
.size(); i
++ )
378 compilerFlags
[i
]->ProcessXML();
379 for ( i
= 0; i
< linkerFlags
.size(); i
++ )
380 linkerFlags
[i
]->ProcessXML();
381 for ( i
= 0; i
< stubbedComponents
.size(); i
++ )
382 stubbedComponents
[i
]->ProcessXML();
383 non_if_data
.ProcessXML();
389 Module::ProcessXMLSubElement ( const XMLElement
& e
,
393 bool subs_invalid
= false;
394 string
subpath ( path
);
395 if ( e
.name
== "file" && e
.value
.size () > 0 )
398 const XMLAttribute
* att
= e
.GetAttribute ( "first", false );
401 if ( !stricmp ( att
->value
.c_str(), "true" ) )
403 else if ( stricmp ( att
->value
.c_str(), "false" ) )
404 throw InvalidBuildFileException (
406 "attribute 'first' of <file> element can only be 'true' or 'false'" );
408 string switches
= "";
409 att
= e
.GetAttribute ( "switches", false );
411 switches
= att
->value
;
414 // check for c++ file
415 string ext
= GetExtension ( e
.value
);
416 if ( !stricmp ( ext
.c_str(), ".cpp" ) )
418 else if ( !stricmp ( ext
.c_str(), ".cc" ) )
420 else if ( !stricmp ( ext
.c_str(), ".cxx" ) )
423 File
* pFile
= new File ( FixSeparator ( path
+ CSEP
+ e
.value
),
428 pIf
->data
.files
.push_back ( pFile
);
430 non_if_data
.files
.push_back ( pFile
);
433 else if ( e
.name
== "library" && e
.value
.size () )
435 Library
* pLibrary
= new Library ( e
, *this, e
.value
);
437 pIf
->data
.libraries
.push_back ( pLibrary
);
439 non_if_data
.libraries
.push_back ( pLibrary
);
442 else if ( e
.name
== "directory" )
444 const XMLAttribute
* att
= e
.GetAttribute ( "name", true );
446 subpath
= GetSubPath ( e
.location
, path
, att
->value
);
448 else if ( e
.name
== "include" )
450 Include
* include
= new Include ( project
, this, &e
);
452 pIf
->data
.includes
.push_back ( include
);
454 non_if_data
.includes
.push_back ( include
);
457 else if ( e
.name
== "define" )
459 Define
* pDefine
= new Define ( project
, this, e
);
461 pIf
->data
.defines
.push_back ( pDefine
);
463 non_if_data
.defines
.push_back ( pDefine
);
466 else if ( e
.name
== "invoke" )
469 throw InvalidBuildFileException (
471 "<invoke> is not a valid sub-element of <if>" );
472 invocations
.push_back ( new Invoke ( e
, *this ) );
473 subs_invalid
= false;
475 else if ( e
.name
== "dependency" )
478 throw InvalidBuildFileException (
480 "<dependency> is not a valid sub-element of <if>" );
481 dependencies
.push_back ( new Dependency ( e
, *this ) );
484 else if ( e
.name
== "importlibrary" )
487 throw InvalidBuildFileException (
489 "<importlibrary> is not a valid sub-element of <if>" );
491 throw InvalidBuildFileException (
493 "Only one <importlibrary> is valid per module" );
494 importLibrary
= new ImportLibrary ( e
, *this );
497 else if ( e
.name
== "if" )
500 pIf
= new If ( e
, project
, this );
502 pOldIf
->data
.ifs
.push_back ( pIf
);
504 non_if_data
.ifs
.push_back ( pIf
);
505 subs_invalid
= false;
507 else if ( e
.name
== "ifnot" )
510 pIf
= new If ( e
, project
, this, true );
512 pOldIf
->data
.ifs
.push_back ( pIf
);
514 non_if_data
.ifs
.push_back ( pIf
);
515 subs_invalid
= false;
517 else if ( e
.name
== "compilerflag" )
519 CompilerFlag
* pCompilerFlag
= new CompilerFlag ( project
, this, e
);
521 pIf
->data
.compilerFlags
.push_back ( pCompilerFlag
);
523 non_if_data
.compilerFlags
.push_back ( pCompilerFlag
);
526 else if ( e
.name
== "linkerflag" )
528 linkerFlags
.push_back ( new LinkerFlag ( project
, this, e
) );
531 else if ( e
.name
== "component" )
533 stubbedComponents
.push_back ( new StubbedComponent ( this, e
) );
534 subs_invalid
= false;
536 else if ( e
.name
== "property" )
538 throw InvalidBuildFileException (
540 "<property> is not a valid sub-element of <module>" );
542 else if ( e
.name
== "bootstrap" )
544 bootstrap
= new Bootstrap ( project
, this, e
);
547 else if ( e
.name
== "pch" )
550 throw InvalidBuildFileException (
552 "<pch> is not a valid sub-element of <if>" );
554 throw InvalidBuildFileException (
556 "Only one <pch> is valid per module" );
558 e
, *this, File ( FixSeparator ( path
+ CSEP
+ e
.value
), false, "", true ) );
561 if ( subs_invalid
&& e
.subElements
.size() > 0 )
562 throw InvalidBuildFileException (
564 "<%s> cannot have sub-elements",
566 for ( size_t i
= 0; i
< e
.subElements
.size (); i
++ )
567 ProcessXMLSubElement ( *e
.subElements
[i
], subpath
, pIf
);
571 Module::GetModuleType ( const string
& location
, const XMLAttribute
& attribute
)
573 if ( attribute
.value
== "buildtool" )
575 if ( attribute
.value
== "staticlibrary" )
576 return StaticLibrary
;
577 if ( attribute
.value
== "objectlibrary" )
578 return ObjectLibrary
;
579 if ( attribute
.value
== "kernel" )
581 if ( attribute
.value
== "kernelmodedll" )
582 return KernelModeDLL
;
583 if ( attribute
.value
== "kernelmodedriver" )
584 return KernelModeDriver
;
585 if ( attribute
.value
== "nativedll" )
587 if ( attribute
.value
== "nativecui" )
589 if ( attribute
.value
== "win32dll" )
591 if ( attribute
.value
== "win32cui" )
593 if ( attribute
.value
== "win32gui" )
595 if ( attribute
.value
== "bootloader" )
597 if ( attribute
.value
== "bootsector" )
599 if ( attribute
.value
== "iso" )
601 if ( attribute
.value
== "liveiso" )
603 if ( attribute
.value
== "test" )
605 if ( attribute
.value
== "rpcserver" )
607 if ( attribute
.value
== "rpcclient" )
609 if ( attribute
.value
== "alias" )
611 throw InvalidAttributeValueException ( location
,
617 Module::GetDefaultModuleExtension () const
636 case KernelModeDriver
:
653 throw InvalidOperationException ( __FILE__
,
658 Module::GetDefaultModuleEntrypoint () const
663 return "_NtProcessStartup";
665 return "_DriverEntry@8";
667 return "_DllMainCRTStartup@12";
669 return "_NtProcessStartup@4";
671 return "_DllMain@12";
674 return "_mainCRTStartup";
676 return "_WinMainCRTStartup";
677 case KernelModeDriver
:
678 return "_DriverEntry@8";
691 throw InvalidOperationException ( __FILE__
,
696 Module::GetDefaultModuleBaseaddress () const
712 case KernelModeDriver
:
726 throw InvalidOperationException ( __FILE__
,
731 Module::HasImportLibrary () const
733 return importLibrary
!= NULL
;
737 Module::IsDLL () const
745 case KernelModeDriver
:
763 throw InvalidOperationException ( __FILE__
,
768 Module::GenerateInOutputTree () const
776 case KernelModeDriver
:
794 throw InvalidOperationException ( __FILE__
,
799 Module::GetTargetName () const
801 return name
+ extension
;
805 Module::GetDependencyPath () const
807 if ( HasImportLibrary () )
808 return ReplaceExtension ( GetPathWithPrefix ( "lib" ), ".a" );
814 Module::GetBasePath () const
820 Module::GetPath () const
822 if ( path
.length() > 0 )
823 return path
+ CSEP
+ GetTargetName ();
825 return GetTargetName ();
829 Module::GetPathWithPrefix ( const string
& prefix
) const
831 return path
+ CSEP
+ prefix
+ GetTargetName ();
835 Module::GetInvocationTarget ( const int index
) const
837 return ssprintf ( "%s_invoke_%d",
843 Module::HasFileWithExtension (
844 const IfableData
& data
,
845 const std::string
& extension
) const
848 for ( i
= 0; i
< data
.files
.size (); i
++ )
850 File
& file
= *data
.files
[i
];
851 string file_ext
= GetExtension ( file
.name
);
852 if ( !stricmp ( file_ext
.c_str (), extension
.c_str () ) )
855 for ( i
= 0; i
< data
.ifs
.size (); i
++ )
857 if ( HasFileWithExtension ( data
.ifs
[i
]->data
, extension
) )
864 Module::InvokeModule () const
866 for ( size_t i
= 0; i
< invocations
.size (); i
++ )
868 Invoke
& invoke
= *invocations
[i
];
869 string command
= invoke
.invokeModule
->GetPath () + " " + invoke
.GetParameters ();
870 printf ( "Executing '%s'\n\n", command
.c_str () );
871 int exitcode
= system ( command
.c_str () );
873 throw InvocationFailedException ( command
,
879 File::File ( const string
& _name
, bool _first
,
880 std::string _switches
,
881 bool _isPreCompiledHeader
)
885 isPreCompiledHeader(_isPreCompiledHeader
)
895 File::IsGeneratedFile () const
897 string extension
= GetExtension ( name
);
898 return ( extension
== ".spec" || extension
== ".SPEC" );
902 Library::Library ( const XMLElement
& _node
,
903 const Module
& _module
,
904 const string
& _name
)
908 importedModule(_module
.project
.LocateModule(_name
))
910 if ( module
.name
== name
)
911 throw InvalidBuildFileException (
913 "module '%s' cannot link against itself",
915 if ( !importedModule
)
916 throw InvalidBuildFileException (
918 "module '%s' trying to import non-existant module '%s'",
924 Library::ProcessXML()
926 if ( !module
.project
.LocateModule ( name
) )
927 throw InvalidBuildFileException (
929 "module '%s' is trying to link against non-existant module '%s'",
935 Invoke::Invoke ( const XMLElement
& _node
,
936 const Module
& _module
)
945 const XMLAttribute
* att
= node
.GetAttribute ( "module", false );
947 invokeModule
= &module
;
950 invokeModule
= module
.project
.LocateModule ( att
->value
);
951 if ( invokeModule
== NULL
)
952 throw InvalidBuildFileException (
954 "module '%s' is trying to invoke non-existant module '%s'",
956 att
->value
.c_str() );
959 for ( size_t i
= 0; i
< node
.subElements
.size (); i
++ )
960 ProcessXMLSubElement ( *node
.subElements
[i
] );
964 Invoke::ProcessXMLSubElement ( const XMLElement
& e
)
966 bool subs_invalid
= false;
967 if ( e
.name
== "input" )
969 for ( size_t i
= 0; i
< e
.subElements
.size (); i
++ )
970 ProcessXMLSubElementInput ( *e
.subElements
[i
] );
972 else if ( e
.name
== "output" )
974 for ( size_t i
= 0; i
< e
.subElements
.size (); i
++ )
975 ProcessXMLSubElementOutput ( *e
.subElements
[i
] );
977 if ( subs_invalid
&& e
.subElements
.size() > 0 )
978 throw InvalidBuildFileException ( e
.location
,
979 "<%s> cannot have sub-elements",
984 Invoke::ProcessXMLSubElementInput ( const XMLElement
& e
)
986 bool subs_invalid
= false;
987 if ( e
.name
== "inputfile" && e
.value
.size () > 0 )
989 input
.push_back ( new InvokeFile ( e
, FixSeparator ( module
.path
+ CSEP
+ e
.value
) ) );
992 if ( subs_invalid
&& e
.subElements
.size() > 0 )
993 throw InvalidBuildFileException ( e
.location
,
994 "<%s> cannot have sub-elements",
999 Invoke::ProcessXMLSubElementOutput ( const XMLElement
& e
)
1001 bool subs_invalid
= false;
1002 if ( e
.name
== "outputfile" && e
.value
.size () > 0 )
1004 output
.push_back ( new InvokeFile ( e
, FixSeparator ( module
.path
+ CSEP
+ e
.value
) ) );
1005 subs_invalid
= true;
1007 if ( subs_invalid
&& e
.subElements
.size() > 0 )
1008 throw InvalidBuildFileException (
1010 "<%s> cannot have sub-elements",
1015 Invoke::GetTargets ( string_list
& targets
) const
1017 for ( size_t i
= 0; i
< output
.size (); i
++ )
1019 InvokeFile
& file
= *output
[i
];
1020 targets
.push_back ( NormalizeFilename ( file
.name
) );
1025 Invoke::GetParameters () const
1027 string
parameters ( "" );
1029 for ( i
= 0; i
< output
.size (); i
++ )
1031 if ( parameters
.length () > 0)
1033 InvokeFile
& invokeFile
= *output
[i
];
1034 if ( invokeFile
.switches
.length () > 0 )
1036 parameters
+= invokeFile
.switches
+ " ";
1038 parameters
+= invokeFile
.name
;
1041 for ( i
= 0; i
< input
.size (); i
++ )
1043 if ( parameters
.length () > 0 )
1045 InvokeFile
& invokeFile
= *input
[i
];
1046 if ( invokeFile
.switches
.length () > 0 )
1048 parameters
+= invokeFile
.switches
;
1051 parameters
+= invokeFile
.name
;
1058 InvokeFile::InvokeFile ( const XMLElement
& _node
,
1059 const string
& _name
)
1063 const XMLAttribute
* att
= _node
.GetAttribute ( "switches", false );
1065 switches
= att
->value
;
1071 InvokeFile::ProcessXML()
1076 Dependency::Dependency ( const XMLElement
& _node
,
1077 const Module
& _module
)
1080 dependencyModule (NULL
)
1085 Dependency::ProcessXML()
1087 dependencyModule
= module
.project
.LocateModule ( node
.value
);
1088 if ( dependencyModule
== NULL
)
1089 throw InvalidBuildFileException ( node
.location
,
1090 "module '%s' depend on non-existant module '%s'",
1091 module
.name
.c_str(),
1092 node
.value
.c_str() );
1096 ImportLibrary::ImportLibrary ( const XMLElement
& _node
,
1097 const Module
& _module
)
1101 const XMLAttribute
* att
= _node
.GetAttribute ( "basename", false );
1103 basename
= att
->value
;
1105 basename
= module
.name
;
1107 att
= _node
.GetAttribute ( "definition", true );
1109 definition
= FixSeparator(att
->value
);
1113 If::If ( const XMLElement
& node_
,
1114 const Project
& project_
,
1115 const Module
* module_
,
1116 const bool negated_
)
1117 : node(node_
), project(project_
), module(module_
), negated(negated_
)
1119 const XMLAttribute
* att
;
1121 att
= node
.GetAttribute ( "property", true );
1123 property
= att
->value
;
1125 att
= node
.GetAttribute ( "value", true );
1140 Property::Property ( const XMLElement
& node_
,
1141 const Project
& project_
,
1142 const Module
* module_
)
1143 : node(node_
), project(project_
), module(module_
)
1145 const XMLAttribute
* att
;
1147 att
= node
.GetAttribute ( "name", true );
1151 att
= node
.GetAttribute ( "value", true );
1157 Property::ProcessXML()
1163 const XMLElement
& node_
,
1164 const Module
& module_
,
1166 : node(node_
), module(module_
), file(file_
)
1171 PchFile::ProcessXML()