10 FixSeparator ( const string
& s
)
13 char* p
= strchr ( &s2
[0], CBAD_SEP
);
17 p
= strchr ( p
, CBAD_SEP
);
24 const string
& filename
,
25 const string
& newExtension
)
27 size_t index
= filename
.find_last_of ( '/' );
28 if ( index
== string::npos
)
30 size_t index2
= filename
.find_last_of ( '\\' );
31 if ( index2
!= string::npos
&& index2
> index
)
33 string tmp
= filename
.substr( index
/*, filename.size() - index*/ );
34 size_t ext_index
= tmp
.find_last_of( '.' );
35 if ( ext_index
!= string::npos
)
36 return filename
.substr ( 0, index
+ ext_index
) + newExtension
;
37 return filename
+ newExtension
;
42 const string
& location
,
44 const string
& att_value
)
46 if ( !att_value
.size() )
47 throw InvalidBuildFileException (
49 "<directory> tag has empty 'name' attribute" );
50 if ( strpbrk ( att_value
.c_str (), "/\\?*:<>|" ) )
51 throw InvalidBuildFileException (
53 "<directory> tag has invalid characters in 'name' attribute" );
56 return FixSeparator(path
+ CSEP
+ att_value
);
60 GetExtension ( const string
& filename
)
62 size_t index
= filename
.find_last_of ( '/' );
63 if (index
== string::npos
) index
= 0;
64 string tmp
= filename
.substr( index
, filename
.size() - index
);
65 size_t ext_index
= tmp
.find_last_of( '.' );
66 if (ext_index
!= string::npos
)
67 return filename
.substr ( index
+ ext_index
, filename
.size() );
72 GetDirectory ( const string
& filename
)
74 size_t index
= filename
.find_last_of ( CSEP
);
75 if ( index
== string::npos
)
78 return filename
.substr ( 0, index
);
82 NormalizeFilename ( const string
& filename
)
85 string normalizedPath
= path
.Fixup ( filename
, true );
86 string relativeNormalizedPath
= path
.RelativeFromWorkingDirectory ( normalizedPath
);
87 return FixSeparator ( relativeNormalizedPath
);
90 IfableData::~IfableData()
93 for ( i
= 0; i
< files
.size(); i
++ )
95 for ( i
= 0; i
< includes
.size(); i
++ )
97 for ( i
= 0; i
< defines
.size(); i
++ )
99 for ( i
= 0; i
< libraries
.size(); i
++ )
101 for ( i
= 0; i
< properties
.size(); i
++ )
102 delete properties
[i
];
103 for ( i
= 0; i
< ifs
.size(); i
++ )
107 void IfableData::ProcessXML ()
110 for ( i
= 0; i
< files
.size (); i
++ )
111 files
[i
]->ProcessXML ();
112 for ( i
= 0; i
< includes
.size (); i
++ )
113 includes
[i
]->ProcessXML ();
114 for ( i
= 0; i
< defines
.size (); i
++ )
115 defines
[i
]->ProcessXML ();
116 for ( i
= 0; i
< libraries
.size (); i
++ )
117 libraries
[i
]->ProcessXML ();
118 for ( i
= 0; i
< properties
.size(); i
++ )
119 properties
[i
]->ProcessXML ();
120 for ( i
= 0; i
< ifs
.size (); i
++ )
121 ifs
[i
]->ProcessXML ();
124 Module::Module ( const Project
& project
,
125 const XMLElement
& moduleNode
,
126 const string
& modulePath
)
129 importLibrary (NULL
),
135 if ( node
.name
!= "module" )
136 throw InvalidOperationException ( __FILE__
,
138 "Module created with non-<module> node" );
140 path
= FixSeparator ( modulePath
);
142 const XMLAttribute
* att
= moduleNode
.GetAttribute ( "name", true );
146 att
= moduleNode
.GetAttribute ( "type", true );
148 type
= GetModuleType ( node
.location
, *att
);
150 att
= moduleNode
.GetAttribute ( "extension", false );
152 extension
= att
->value
;
154 extension
= GetDefaultModuleExtension ();
156 att
= moduleNode
.GetAttribute ( "entrypoint", false );
158 entrypoint
= att
->value
;
160 entrypoint
= GetDefaultModuleEntrypoint ();
162 att
= moduleNode
.GetAttribute ( "baseaddress", false );
164 baseaddress
= att
->value
;
166 baseaddress
= GetDefaultModuleBaseaddress ();
168 att
= moduleNode
.GetAttribute ( "mangledsymbols", false );
171 const char* p
= att
->value
.c_str();
172 if ( !stricmp ( p
, "true" ) || !stricmp ( p
, "yes" ) )
173 mangledSymbols
= true;
174 else if ( !stricmp ( p
, "false" ) || !stricmp ( p
, "no" ) )
175 mangledSymbols
= false;
178 throw InvalidAttributeValueException (
185 mangledSymbols
= false;
187 att
= moduleNode
.GetAttribute ( "host", false );
190 const char* p
= att
->value
.c_str();
191 if ( !stricmp ( p
, "true" ) || !stricmp ( p
, "yes" ) )
193 else if ( !stricmp ( p
, "false" ) || !stricmp ( p
, "no" ) )
197 throw InvalidAttributeValueException (
204 att
= moduleNode
.GetAttribute ( "prefix", false );
208 att
= moduleNode
.GetAttribute ( "installbase", false );
210 installBase
= att
->value
;
214 att
= moduleNode
.GetAttribute ( "installname", false );
216 installName
= att
->value
;
220 att
= moduleNode
.GetAttribute ( "usewrc", false );
222 useWRC
= att
->value
== "true";
230 for ( i
= 0; i
< invocations
.size(); i
++ )
231 delete invocations
[i
];
232 for ( i
= 0; i
< dependencies
.size(); i
++ )
233 delete dependencies
[i
];
234 for ( i
= 0; i
< compilerFlags
.size(); i
++ )
235 delete compilerFlags
[i
];
236 for ( i
= 0; i
< linkerFlags
.size(); i
++ )
237 delete linkerFlags
[i
];
238 for ( i
= 0; i
< stubbedComponents
.size(); i
++ )
239 delete stubbedComponents
[i
];
248 for ( i
= 0; i
< node
.subElements
.size(); i
++ )
249 ProcessXMLSubElement ( *node
.subElements
[i
], path
);
250 for ( i
= 0; i
< invocations
.size(); i
++ )
251 invocations
[i
]->ProcessXML ();
252 for ( i
= 0; i
< dependencies
.size(); i
++ )
253 dependencies
[i
]->ProcessXML ();
254 for ( i
= 0; i
< compilerFlags
.size(); i
++ )
255 compilerFlags
[i
]->ProcessXML();
256 for ( i
= 0; i
< linkerFlags
.size(); i
++ )
257 linkerFlags
[i
]->ProcessXML();
258 for ( i
= 0; i
< stubbedComponents
.size(); i
++ )
259 stubbedComponents
[i
]->ProcessXML();
260 non_if_data
.ProcessXML();
266 Module::ProcessXMLSubElement ( const XMLElement
& e
,
270 bool subs_invalid
= false;
271 string
subpath ( path
);
272 if ( e
.name
== "file" && e
.value
.size () > 0 )
275 const XMLAttribute
* att
= e
.GetAttribute ( "first", false );
278 if ( !stricmp ( att
->value
.c_str(), "true" ) )
280 else if ( stricmp ( att
->value
.c_str(), "false" ) )
281 throw InvalidBuildFileException (
283 "attribute 'first' of <file> element can only be 'true' or 'false'" );
287 // check for c++ file
288 string ext
= GetExtension ( e
.value
);
289 if ( !stricmp ( ext
.c_str(), ".cpp" ) )
291 else if ( !stricmp ( ext
.c_str(), ".cc" ) )
293 else if ( !stricmp ( ext
.c_str(), ".cxx" ) )
296 File
* pFile
= new File ( FixSeparator ( path
+ CSEP
+ e
.value
), first
);
298 pIf
->data
.files
.push_back ( pFile
);
300 non_if_data
.files
.push_back ( pFile
);
303 else if ( e
.name
== "library" && e
.value
.size () )
305 Library
* pLibrary
= new Library ( e
, *this, e
.value
);
307 pIf
->data
.libraries
.push_back ( pLibrary
);
309 non_if_data
.libraries
.push_back ( pLibrary
);
312 else if ( e
.name
== "directory" )
314 const XMLAttribute
* att
= e
.GetAttribute ( "name", true );
316 subpath
= GetSubPath ( e
.location
, path
, att
->value
);
318 else if ( e
.name
== "include" )
320 Include
* include
= new Include ( project
, this, e
);
322 pIf
->data
.includes
.push_back ( include
);
324 non_if_data
.includes
.push_back ( include
);
327 else if ( e
.name
== "define" )
329 Define
* pDefine
= new Define ( project
, this, e
);
331 pIf
->data
.defines
.push_back ( pDefine
);
333 non_if_data
.defines
.push_back ( pDefine
);
336 else if ( e
.name
== "invoke" )
339 throw InvalidBuildFileException (
341 "<invoke> is not a valid sub-element of <if>" );
342 invocations
.push_back ( new Invoke ( e
, *this ) );
343 subs_invalid
= false;
345 else if ( e
.name
== "dependency" )
348 throw InvalidBuildFileException (
350 "<dependency> is not a valid sub-element of <if>" );
351 dependencies
.push_back ( new Dependency ( e
, *this ) );
354 else if ( e
.name
== "importlibrary" )
357 throw InvalidBuildFileException (
359 "<importlibrary> is not a valid sub-element of <if>" );
361 throw InvalidBuildFileException (
363 "Only one <importlibrary> is valid per module" );
364 importLibrary
= new ImportLibrary ( e
, *this );
367 else if ( e
.name
== "if" )
370 pIf
= new If ( e
, project
, this );
372 pOldIf
->data
.ifs
.push_back ( pIf
);
374 non_if_data
.ifs
.push_back ( pIf
);
375 subs_invalid
= false;
377 else if ( e
.name
== "compilerflag" )
379 compilerFlags
.push_back ( new CompilerFlag ( project
, this, e
) );
382 else if ( e
.name
== "linkerflag" )
384 linkerFlags
.push_back ( new LinkerFlag ( project
, this, e
) );
387 else if ( e
.name
== "component" )
389 stubbedComponents
.push_back ( new StubbedComponent ( this, e
) );
390 subs_invalid
= false;
392 else if ( e
.name
== "property" )
394 throw InvalidBuildFileException (
396 "<property> is not a valid sub-element of <module>" );
398 else if ( e
.name
== "bootstrap" )
400 bootstrap
= new Bootstrap ( project
, this, e
);
403 else if ( e
.name
== "pch" )
406 throw InvalidBuildFileException (
408 "<pch> is not a valid sub-element of <if>" );
410 throw InvalidBuildFileException (
412 "Only one <pch> is valid per module" );
414 e
, *this, FixSeparator ( path
+ CSEP
+ e
.value
) );
417 if ( subs_invalid
&& e
.subElements
.size() > 0 )
418 throw InvalidBuildFileException (
420 "<%s> cannot have sub-elements",
422 for ( size_t i
= 0; i
< e
.subElements
.size (); i
++ )
423 ProcessXMLSubElement ( *e
.subElements
[i
], subpath
, pIf
);
427 Module::GetModuleType ( const string
& location
, const XMLAttribute
& attribute
)
429 if ( attribute
.value
== "buildtool" )
431 if ( attribute
.value
== "staticlibrary" )
432 return StaticLibrary
;
433 if ( attribute
.value
== "objectlibrary" )
434 return ObjectLibrary
;
435 if ( attribute
.value
== "kernel" )
437 if ( attribute
.value
== "kernelmodedll" )
438 return KernelModeDLL
;
439 if ( attribute
.value
== "kernelmodedriver" )
440 return KernelModeDriver
;
441 if ( attribute
.value
== "nativedll" )
443 if ( attribute
.value
== "nativecui" )
445 if ( attribute
.value
== "win32dll" )
447 if ( attribute
.value
== "win32cui" )
449 if ( attribute
.value
== "win32gui" )
451 if ( attribute
.value
== "bootloader" )
453 if ( attribute
.value
== "bootsector" )
455 if ( attribute
.value
== "iso" )
457 if ( attribute
.value
== "test" )
459 if ( attribute
.value
== "rpcserver" )
461 if ( attribute
.value
== "rpcclient" )
463 throw InvalidAttributeValueException ( location
,
469 Module::GetDefaultModuleExtension () const
488 case KernelModeDriver
:
502 throw InvalidOperationException ( __FILE__
,
507 Module::GetDefaultModuleEntrypoint () const
512 return "_NtProcessStartup";
514 return "_DriverEntry@8";
516 return "_DllMainCRTStartup@12";
518 return "_NtProcessStartup@4";
520 return "_DllMain@12";
523 return "_mainCRTStartup";
525 return "_WinMainCRTStartup";
526 case KernelModeDriver
:
527 return "_DriverEntry@8";
538 throw InvalidOperationException ( __FILE__
,
543 Module::GetDefaultModuleBaseaddress () const
562 case KernelModeDriver
:
574 throw InvalidOperationException ( __FILE__
,
579 Module::HasImportLibrary () const
581 return importLibrary
!= NULL
;
585 Module::GetTargetName () const
587 return name
+ extension
;
591 Module::GetDependencyPath () const
593 if ( HasImportLibrary () )
595 return ReplaceExtension ( GetPath(), ".a" );
602 Module::GetBasePath () const
608 Module::GetPath () const
610 if ( path
.length() > 0 )
611 return path
+ CSEP
+ GetTargetName ();
613 return GetTargetName ();
617 Module::GetPathWithPrefix ( const string
& prefix
) const
619 return path
+ CSEP
+ prefix
+ GetTargetName ();
623 Module::GetInvocationTarget ( const int index
) const
625 return ssprintf ( "%s_invoke_%d",
631 Module::HasFileWithExtension (
632 const IfableData
& data
,
633 const std::string
& extension
) const
636 for ( i
= 0; i
< data
.files
.size (); i
++ )
638 File
& file
= *data
.files
[i
];
639 string file_ext
= GetExtension ( file
.name
);
640 if ( !stricmp ( file_ext
.c_str (), extension
.c_str () ) )
643 for ( i
= 0; i
< data
.ifs
.size (); i
++ )
645 if ( HasFileWithExtension ( data
.ifs
[i
]->data
, extension
) )
652 Module::InvokeModule () const
654 for ( size_t i
= 0; i
< invocations
.size (); i
++ )
656 Invoke
& invoke
= *invocations
[i
];
657 string command
= invoke
.invokeModule
->GetPath () + " " + invoke
.GetParameters ();
658 printf ( "Executing '%s'\n\n", command
.c_str () );
659 int exitcode
= system ( command
.c_str () );
661 throw InvocationFailedException ( command
,
667 File::File ( const string
& _name
, bool _first
)
668 : name(_name
), first(_first
)
678 Library::Library ( const XMLElement
& _node
,
679 const Module
& _module
,
680 const string
& _name
)
684 imported_module(_module
.project
.LocateModule(_name
))
686 if ( module
.name
== name
)
687 throw InvalidBuildFileException (
689 "module '%s' cannot link against itself",
691 if ( !imported_module
)
692 throw InvalidBuildFileException (
694 "module '%s' trying to import non-existant module '%s'",
700 Library::ProcessXML()
702 if ( !module
.project
.LocateModule ( name
) )
703 throw InvalidBuildFileException (
705 "module '%s' is trying to link against non-existant module '%s'",
711 Invoke::Invoke ( const XMLElement
& _node
,
712 const Module
& _module
)
721 const XMLAttribute
* att
= node
.GetAttribute ( "module", false );
723 invokeModule
= &module
;
726 invokeModule
= module
.project
.LocateModule ( att
->value
);
727 if ( invokeModule
== NULL
)
728 throw InvalidBuildFileException (
730 "module '%s' is trying to invoke non-existant module '%s'",
732 att
->value
.c_str() );
735 for ( size_t i
= 0; i
< node
.subElements
.size (); i
++ )
736 ProcessXMLSubElement ( *node
.subElements
[i
] );
740 Invoke::ProcessXMLSubElement ( const XMLElement
& e
)
742 bool subs_invalid
= false;
743 if ( e
.name
== "input" )
745 for ( size_t i
= 0; i
< e
.subElements
.size (); i
++ )
746 ProcessXMLSubElementInput ( *e
.subElements
[i
] );
748 else if ( e
.name
== "output" )
750 for ( size_t i
= 0; i
< e
.subElements
.size (); i
++ )
751 ProcessXMLSubElementOutput ( *e
.subElements
[i
] );
753 if ( subs_invalid
&& e
.subElements
.size() > 0 )
754 throw InvalidBuildFileException ( e
.location
,
755 "<%s> cannot have sub-elements",
760 Invoke::ProcessXMLSubElementInput ( const XMLElement
& e
)
762 bool subs_invalid
= false;
763 if ( e
.name
== "inputfile" && e
.value
.size () > 0 )
765 input
.push_back ( new InvokeFile ( e
, FixSeparator ( module
.path
+ CSEP
+ e
.value
) ) );
768 if ( subs_invalid
&& e
.subElements
.size() > 0 )
769 throw InvalidBuildFileException ( e
.location
,
770 "<%s> cannot have sub-elements",
775 Invoke::ProcessXMLSubElementOutput ( const XMLElement
& e
)
777 bool subs_invalid
= false;
778 if ( e
.name
== "outputfile" && e
.value
.size () > 0 )
780 output
.push_back ( new InvokeFile ( e
, FixSeparator ( module
.path
+ CSEP
+ e
.value
) ) );
783 if ( subs_invalid
&& e
.subElements
.size() > 0 )
784 throw InvalidBuildFileException (
786 "<%s> cannot have sub-elements",
791 Invoke::GetTargets ( string_list
& targets
) const
793 for ( size_t i
= 0; i
< output
.size (); i
++ )
795 InvokeFile
& file
= *output
[i
];
796 targets
.push_back ( NormalizeFilename ( file
.name
) );
801 Invoke::GetParameters () const
803 string
parameters ( "" );
805 for ( i
= 0; i
< output
.size (); i
++ )
807 if ( parameters
.length () > 0)
809 InvokeFile
& invokeFile
= *output
[i
];
810 if ( invokeFile
.switches
.length () > 0 )
812 parameters
+= invokeFile
.switches
+ " ";
814 parameters
+= invokeFile
.name
;
817 for ( i
= 0; i
< input
.size (); i
++ )
819 if ( parameters
.length () > 0 )
821 InvokeFile
& invokeFile
= *input
[i
];
822 if ( invokeFile
.switches
.length () > 0 )
824 parameters
+= invokeFile
.switches
;
827 parameters
+= invokeFile
.name
;
834 InvokeFile::InvokeFile ( const XMLElement
& _node
,
835 const string
& _name
)
839 const XMLAttribute
* att
= _node
.GetAttribute ( "switches", false );
841 switches
= att
->value
;
847 InvokeFile::ProcessXML()
852 Dependency::Dependency ( const XMLElement
& _node
,
853 const Module
& _module
)
856 dependencyModule (NULL
)
861 Dependency::ProcessXML()
863 dependencyModule
= module
.project
.LocateModule ( node
.value
);
864 if ( dependencyModule
== NULL
)
865 throw InvalidBuildFileException ( node
.location
,
866 "module '%s' depend on non-existant module '%s'",
868 node
.value
.c_str() );
872 ImportLibrary::ImportLibrary ( const XMLElement
& _node
,
873 const Module
& _module
)
877 const XMLAttribute
* att
= _node
.GetAttribute ( "basename", false );
879 basename
= att
->value
;
881 basename
= module
.name
;
883 att
= _node
.GetAttribute ( "definition", true );
885 definition
= FixSeparator(att
->value
);
889 If::If ( const XMLElement
& node_
,
890 const Project
& project_
,
891 const Module
* module_
)
892 : node(node_
), project(project_
), module(module_
)
894 const XMLAttribute
* att
;
896 att
= node
.GetAttribute ( "property", true );
898 property
= att
->value
;
900 att
= node
.GetAttribute ( "value", true );
915 Property::Property ( const XMLElement
& node_
,
916 const Project
& project_
,
917 const Module
* module_
)
918 : node(node_
), project(project_
), module(module_
)
920 const XMLAttribute
* att
;
922 att
= node
.GetAttribute ( "name", true );
926 att
= node
.GetAttribute ( "value", true );
932 Property::ProcessXML()
938 const XMLElement
& node_
,
939 const Module
& module_
,
940 const string
& header_
)
941 : node(node_
), module(module_
), header(header_
)
946 PchFile::ProcessXML()