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 GetFilename ( const string
& filename
)
84 size_t index
= filename
.find_last_of ( CSEP
);
85 if ( index
== string::npos
)
88 return filename
.substr ( index
+ 1, filename
.length () - index
);
92 NormalizeFilename ( const string
& filename
)
97 string normalizedPath
= path
.Fixup ( filename
, true );
98 string relativeNormalizedPath
= path
.RelativeFromWorkingDirectory ( normalizedPath
);
99 return FixSeparator ( relativeNormalizedPath
);
102 IfableData::~IfableData()
105 for ( i
= 0; i
< files
.size(); i
++ )
107 for ( i
= 0; i
< includes
.size(); i
++ )
109 for ( i
= 0; i
< defines
.size(); i
++ )
111 for ( i
= 0; i
< libraries
.size(); i
++ )
113 for ( i
= 0; i
< properties
.size(); i
++ )
114 delete properties
[i
];
115 for ( i
= 0; i
< compilerFlags
.size(); i
++ )
116 delete compilerFlags
[i
];
117 for ( i
= 0; i
< ifs
.size(); i
++ )
121 void IfableData::ProcessXML ()
124 for ( i
= 0; i
< files
.size (); i
++ )
125 files
[i
]->ProcessXML ();
126 for ( i
= 0; i
< includes
.size (); i
++ )
127 includes
[i
]->ProcessXML ();
128 for ( i
= 0; i
< defines
.size (); i
++ )
129 defines
[i
]->ProcessXML ();
130 for ( i
= 0; i
< libraries
.size (); i
++ )
131 libraries
[i
]->ProcessXML ();
132 for ( i
= 0; i
< properties
.size(); i
++ )
133 properties
[i
]->ProcessXML ();
134 for ( i
= 0; i
< compilerFlags
.size(); i
++ )
135 compilerFlags
[i
]->ProcessXML ();
136 for ( i
= 0; i
< ifs
.size (); i
++ )
137 ifs
[i
]->ProcessXML ();
140 Module::Module ( const Project
& project
,
141 const XMLElement
& moduleNode
,
142 const string
& modulePath
)
145 importLibrary (NULL
),
151 if ( node
.name
!= "module" )
152 throw InvalidOperationException ( __FILE__
,
154 "Module created with non-<module> node" );
156 xmlbuildFile
= Path::RelativeFromWorkingDirectory ( moduleNode
.xmlFile
->filename() );
158 path
= FixSeparator ( modulePath
);
160 const XMLAttribute
* att
= moduleNode
.GetAttribute ( "name", true );
164 att
= moduleNode
.GetAttribute ( "type", true );
166 type
= GetModuleType ( node
.location
, *att
);
168 att
= moduleNode
.GetAttribute ( "extension", false );
170 extension
= att
->value
;
172 extension
= GetDefaultModuleExtension ();
174 att
= moduleNode
.GetAttribute ( "entrypoint", false );
176 entrypoint
= att
->value
;
178 entrypoint
= GetDefaultModuleEntrypoint ();
180 att
= moduleNode
.GetAttribute ( "baseaddress", false );
182 baseaddress
= att
->value
;
184 baseaddress
= GetDefaultModuleBaseaddress ();
186 att
= moduleNode
.GetAttribute ( "mangledsymbols", false );
189 const char* p
= att
->value
.c_str();
190 if ( !stricmp ( p
, "true" ) || !stricmp ( p
, "yes" ) )
191 mangledSymbols
= true;
192 else if ( !stricmp ( p
, "false" ) || !stricmp ( p
, "no" ) )
193 mangledSymbols
= false;
196 throw InvalidAttributeValueException (
203 mangledSymbols
= false;
205 att
= moduleNode
.GetAttribute ( "host", false );
208 const char* p
= att
->value
.c_str();
209 if ( !stricmp ( p
, "true" ) || !stricmp ( p
, "yes" ) )
211 else if ( !stricmp ( p
, "false" ) || !stricmp ( p
, "no" ) )
215 throw InvalidAttributeValueException (
222 att
= moduleNode
.GetAttribute ( "prefix", false );
226 att
= moduleNode
.GetAttribute ( "installbase", false );
228 installBase
= att
->value
;
232 att
= moduleNode
.GetAttribute ( "installname", false );
234 installName
= att
->value
;
238 att
= moduleNode
.GetAttribute ( "usewrc", false );
240 useWRC
= att
->value
== "true";
244 att
= moduleNode
.GetAttribute ( "warnings", false );
246 enableWarnings
= att
->value
== "true";
248 enableWarnings
= false;
254 for ( i
= 0; i
< invocations
.size(); i
++ )
255 delete invocations
[i
];
256 for ( i
= 0; i
< dependencies
.size(); i
++ )
257 delete dependencies
[i
];
258 for ( i
= 0; i
< compilerFlags
.size(); i
++ )
259 delete compilerFlags
[i
];
260 for ( i
= 0; i
< linkerFlags
.size(); i
++ )
261 delete linkerFlags
[i
];
262 for ( i
= 0; i
< stubbedComponents
.size(); i
++ )
263 delete stubbedComponents
[i
];
272 for ( i
= 0; i
< node
.subElements
.size(); i
++ )
273 ProcessXMLSubElement ( *node
.subElements
[i
], path
);
274 for ( i
= 0; i
< invocations
.size(); i
++ )
275 invocations
[i
]->ProcessXML ();
276 for ( i
= 0; i
< dependencies
.size(); i
++ )
277 dependencies
[i
]->ProcessXML ();
278 for ( i
= 0; i
< compilerFlags
.size(); i
++ )
279 compilerFlags
[i
]->ProcessXML();
280 for ( i
= 0; i
< linkerFlags
.size(); i
++ )
281 linkerFlags
[i
]->ProcessXML();
282 for ( i
= 0; i
< stubbedComponents
.size(); i
++ )
283 stubbedComponents
[i
]->ProcessXML();
284 non_if_data
.ProcessXML();
290 Module::ProcessXMLSubElement ( const XMLElement
& e
,
294 bool subs_invalid
= false;
295 string
subpath ( path
);
296 if ( e
.name
== "file" && e
.value
.size () > 0 )
299 const XMLAttribute
* att
= e
.GetAttribute ( "first", false );
302 if ( !stricmp ( att
->value
.c_str(), "true" ) )
304 else if ( stricmp ( att
->value
.c_str(), "false" ) )
305 throw InvalidBuildFileException (
307 "attribute 'first' of <file> element can only be 'true' or 'false'" );
309 string switches
= "";
310 att
= e
.GetAttribute ( "switches", false );
312 switches
= att
->value
;
315 // check for c++ file
316 string ext
= GetExtension ( e
.value
);
317 if ( !stricmp ( ext
.c_str(), ".cpp" ) )
319 else if ( !stricmp ( ext
.c_str(), ".cc" ) )
321 else if ( !stricmp ( ext
.c_str(), ".cxx" ) )
324 File
* pFile
= new File ( FixSeparator ( path
+ CSEP
+ e
.value
),
328 pIf
->data
.files
.push_back ( pFile
);
330 non_if_data
.files
.push_back ( pFile
);
333 else if ( e
.name
== "library" && e
.value
.size () )
335 Library
* pLibrary
= new Library ( e
, *this, e
.value
);
337 pIf
->data
.libraries
.push_back ( pLibrary
);
339 non_if_data
.libraries
.push_back ( pLibrary
);
342 else if ( e
.name
== "directory" )
344 const XMLAttribute
* att
= e
.GetAttribute ( "name", true );
346 subpath
= GetSubPath ( e
.location
, path
, att
->value
);
348 else if ( e
.name
== "include" )
350 Include
* include
= new Include ( project
, this, e
);
352 pIf
->data
.includes
.push_back ( include
);
354 non_if_data
.includes
.push_back ( include
);
357 else if ( e
.name
== "define" )
359 Define
* pDefine
= new Define ( project
, this, e
);
361 pIf
->data
.defines
.push_back ( pDefine
);
363 non_if_data
.defines
.push_back ( pDefine
);
366 else if ( e
.name
== "invoke" )
369 throw InvalidBuildFileException (
371 "<invoke> is not a valid sub-element of <if>" );
372 invocations
.push_back ( new Invoke ( e
, *this ) );
373 subs_invalid
= false;
375 else if ( e
.name
== "dependency" )
378 throw InvalidBuildFileException (
380 "<dependency> is not a valid sub-element of <if>" );
381 dependencies
.push_back ( new Dependency ( e
, *this ) );
384 else if ( e
.name
== "importlibrary" )
387 throw InvalidBuildFileException (
389 "<importlibrary> is not a valid sub-element of <if>" );
391 throw InvalidBuildFileException (
393 "Only one <importlibrary> is valid per module" );
394 importLibrary
= new ImportLibrary ( e
, *this );
397 else if ( e
.name
== "if" )
400 pIf
= new If ( e
, project
, this );
402 pOldIf
->data
.ifs
.push_back ( pIf
);
404 non_if_data
.ifs
.push_back ( pIf
);
405 subs_invalid
= false;
407 else if ( e
.name
== "compilerflag" )
409 CompilerFlag
* pCompilerFlag
= new CompilerFlag ( project
, this, e
);
411 pIf
->data
.compilerFlags
.push_back ( pCompilerFlag
);
413 non_if_data
.compilerFlags
.push_back ( pCompilerFlag
);
416 else if ( e
.name
== "linkerflag" )
418 linkerFlags
.push_back ( new LinkerFlag ( project
, this, e
) );
421 else if ( e
.name
== "component" )
423 stubbedComponents
.push_back ( new StubbedComponent ( this, e
) );
424 subs_invalid
= false;
426 else if ( e
.name
== "property" )
428 throw InvalidBuildFileException (
430 "<property> is not a valid sub-element of <module>" );
432 else if ( e
.name
== "bootstrap" )
434 bootstrap
= new Bootstrap ( project
, this, e
);
437 else if ( e
.name
== "pch" )
440 throw InvalidBuildFileException (
442 "<pch> is not a valid sub-element of <if>" );
444 throw InvalidBuildFileException (
446 "Only one <pch> is valid per module" );
448 e
, *this, FixSeparator ( path
+ CSEP
+ e
.value
) );
451 if ( subs_invalid
&& e
.subElements
.size() > 0 )
452 throw InvalidBuildFileException (
454 "<%s> cannot have sub-elements",
456 for ( size_t i
= 0; i
< e
.subElements
.size (); i
++ )
457 ProcessXMLSubElement ( *e
.subElements
[i
], subpath
, pIf
);
461 Module::GetModuleType ( const string
& location
, const XMLAttribute
& attribute
)
463 if ( attribute
.value
== "buildtool" )
465 if ( attribute
.value
== "staticlibrary" )
466 return StaticLibrary
;
467 if ( attribute
.value
== "objectlibrary" )
468 return ObjectLibrary
;
469 if ( attribute
.value
== "kernel" )
471 if ( attribute
.value
== "kernelmodedll" )
472 return KernelModeDLL
;
473 if ( attribute
.value
== "kernelmodedriver" )
474 return KernelModeDriver
;
475 if ( attribute
.value
== "nativedll" )
477 if ( attribute
.value
== "nativecui" )
479 if ( attribute
.value
== "win32dll" )
481 if ( attribute
.value
== "win32cui" )
483 if ( attribute
.value
== "win32gui" )
485 if ( attribute
.value
== "bootloader" )
487 if ( attribute
.value
== "bootsector" )
489 if ( attribute
.value
== "iso" )
491 if ( attribute
.value
== "liveiso" )
493 if ( attribute
.value
== "test" )
495 if ( attribute
.value
== "rpcserver" )
497 if ( attribute
.value
== "rpcclient" )
499 throw InvalidAttributeValueException ( location
,
505 Module::GetDefaultModuleExtension () const
524 case KernelModeDriver
:
539 throw InvalidOperationException ( __FILE__
,
544 Module::GetDefaultModuleEntrypoint () const
549 return "_NtProcessStartup";
551 return "_DriverEntry@8";
553 return "_DllMainCRTStartup@12";
555 return "_NtProcessStartup@4";
557 return "_DllMain@12";
560 return "_mainCRTStartup";
562 return "_WinMainCRTStartup";
563 case KernelModeDriver
:
564 return "_DriverEntry@8";
576 throw InvalidOperationException ( __FILE__
,
581 Module::GetDefaultModuleBaseaddress () const
597 case KernelModeDriver
:
610 throw InvalidOperationException ( __FILE__
,
615 Module::HasImportLibrary () const
617 return importLibrary
!= NULL
;
621 Module::IsDLL () const
629 case KernelModeDriver
:
646 throw InvalidOperationException ( __FILE__
,
651 Module::GetTargetName () const
653 return name
+ extension
;
657 Module::GetDependencyPath () const
659 if ( HasImportLibrary () )
660 return ReplaceExtension ( GetPathWithPrefix ( "lib" ), ".a" );
666 Module::GetBasePath () const
672 Module::GetPath () const
674 if ( path
.length() > 0 )
675 return path
+ CSEP
+ GetTargetName ();
677 return GetTargetName ();
681 Module::GetPathWithPrefix ( const string
& prefix
) const
683 return path
+ CSEP
+ prefix
+ GetTargetName ();
687 Module::GetInvocationTarget ( const int index
) const
689 return ssprintf ( "%s_invoke_%d",
695 Module::HasFileWithExtension (
696 const IfableData
& data
,
697 const std::string
& extension
) const
700 for ( i
= 0; i
< data
.files
.size (); i
++ )
702 File
& file
= *data
.files
[i
];
703 string file_ext
= GetExtension ( file
.name
);
704 if ( !stricmp ( file_ext
.c_str (), extension
.c_str () ) )
707 for ( i
= 0; i
< data
.ifs
.size (); i
++ )
709 if ( HasFileWithExtension ( data
.ifs
[i
]->data
, extension
) )
716 Module::InvokeModule () const
718 for ( size_t i
= 0; i
< invocations
.size (); i
++ )
720 Invoke
& invoke
= *invocations
[i
];
721 string command
= invoke
.invokeModule
->GetPath () + " " + invoke
.GetParameters ();
722 printf ( "Executing '%s'\n\n", command
.c_str () );
723 int exitcode
= system ( command
.c_str () );
725 throw InvocationFailedException ( command
,
731 File::File ( const string
& _name
, bool _first
,
732 std::string _switches
)
745 File::IsGeneratedFile () const
747 string extension
= GetExtension ( name
);
748 return ( extension
== ".spec" || extension
== ".SPEC" );
752 Library::Library ( const XMLElement
& _node
,
753 const Module
& _module
,
754 const string
& _name
)
758 imported_module(_module
.project
.LocateModule(_name
))
760 if ( module
.name
== name
)
761 throw InvalidBuildFileException (
763 "module '%s' cannot link against itself",
765 if ( !imported_module
)
766 throw InvalidBuildFileException (
768 "module '%s' trying to import non-existant module '%s'",
774 Library::ProcessXML()
776 if ( !module
.project
.LocateModule ( name
) )
777 throw InvalidBuildFileException (
779 "module '%s' is trying to link against non-existant module '%s'",
785 Invoke::Invoke ( const XMLElement
& _node
,
786 const Module
& _module
)
795 const XMLAttribute
* att
= node
.GetAttribute ( "module", false );
797 invokeModule
= &module
;
800 invokeModule
= module
.project
.LocateModule ( att
->value
);
801 if ( invokeModule
== NULL
)
802 throw InvalidBuildFileException (
804 "module '%s' is trying to invoke non-existant module '%s'",
806 att
->value
.c_str() );
809 for ( size_t i
= 0; i
< node
.subElements
.size (); i
++ )
810 ProcessXMLSubElement ( *node
.subElements
[i
] );
814 Invoke::ProcessXMLSubElement ( const XMLElement
& e
)
816 bool subs_invalid
= false;
817 if ( e
.name
== "input" )
819 for ( size_t i
= 0; i
< e
.subElements
.size (); i
++ )
820 ProcessXMLSubElementInput ( *e
.subElements
[i
] );
822 else if ( e
.name
== "output" )
824 for ( size_t i
= 0; i
< e
.subElements
.size (); i
++ )
825 ProcessXMLSubElementOutput ( *e
.subElements
[i
] );
827 if ( subs_invalid
&& e
.subElements
.size() > 0 )
828 throw InvalidBuildFileException ( e
.location
,
829 "<%s> cannot have sub-elements",
834 Invoke::ProcessXMLSubElementInput ( const XMLElement
& e
)
836 bool subs_invalid
= false;
837 if ( e
.name
== "inputfile" && e
.value
.size () > 0 )
839 input
.push_back ( new InvokeFile ( e
, FixSeparator ( module
.path
+ CSEP
+ e
.value
) ) );
842 if ( subs_invalid
&& e
.subElements
.size() > 0 )
843 throw InvalidBuildFileException ( e
.location
,
844 "<%s> cannot have sub-elements",
849 Invoke::ProcessXMLSubElementOutput ( const XMLElement
& e
)
851 bool subs_invalid
= false;
852 if ( e
.name
== "outputfile" && e
.value
.size () > 0 )
854 output
.push_back ( new InvokeFile ( e
, FixSeparator ( module
.path
+ CSEP
+ e
.value
) ) );
857 if ( subs_invalid
&& e
.subElements
.size() > 0 )
858 throw InvalidBuildFileException (
860 "<%s> cannot have sub-elements",
865 Invoke::GetTargets ( string_list
& targets
) const
867 for ( size_t i
= 0; i
< output
.size (); i
++ )
869 InvokeFile
& file
= *output
[i
];
870 targets
.push_back ( NormalizeFilename ( file
.name
) );
875 Invoke::GetParameters () const
877 string
parameters ( "" );
879 for ( i
= 0; i
< output
.size (); i
++ )
881 if ( parameters
.length () > 0)
883 InvokeFile
& invokeFile
= *output
[i
];
884 if ( invokeFile
.switches
.length () > 0 )
886 parameters
+= invokeFile
.switches
+ " ";
888 parameters
+= invokeFile
.name
;
891 for ( i
= 0; i
< input
.size (); i
++ )
893 if ( parameters
.length () > 0 )
895 InvokeFile
& invokeFile
= *input
[i
];
896 if ( invokeFile
.switches
.length () > 0 )
898 parameters
+= invokeFile
.switches
;
901 parameters
+= invokeFile
.name
;
908 InvokeFile::InvokeFile ( const XMLElement
& _node
,
909 const string
& _name
)
913 const XMLAttribute
* att
= _node
.GetAttribute ( "switches", false );
915 switches
= att
->value
;
921 InvokeFile::ProcessXML()
926 Dependency::Dependency ( const XMLElement
& _node
,
927 const Module
& _module
)
930 dependencyModule (NULL
)
935 Dependency::ProcessXML()
937 dependencyModule
= module
.project
.LocateModule ( node
.value
);
938 if ( dependencyModule
== NULL
)
939 throw InvalidBuildFileException ( node
.location
,
940 "module '%s' depend on non-existant module '%s'",
942 node
.value
.c_str() );
946 ImportLibrary::ImportLibrary ( const XMLElement
& _node
,
947 const Module
& _module
)
951 const XMLAttribute
* att
= _node
.GetAttribute ( "basename", false );
953 basename
= att
->value
;
955 basename
= module
.name
;
957 att
= _node
.GetAttribute ( "definition", true );
959 definition
= FixSeparator(att
->value
);
963 If::If ( const XMLElement
& node_
,
964 const Project
& project_
,
965 const Module
* module_
)
966 : node(node_
), project(project_
), module(module_
)
968 const XMLAttribute
* att
;
970 att
= node
.GetAttribute ( "property", true );
972 property
= att
->value
;
974 att
= node
.GetAttribute ( "value", true );
989 Property::Property ( const XMLElement
& node_
,
990 const Project
& project_
,
991 const Module
* module_
)
992 : node(node_
), project(project_
), module(module_
)
994 const XMLAttribute
* att
;
996 att
= node
.GetAttribute ( "name", true );
1000 att
= node
.GetAttribute ( "value", true );
1006 Property::ProcessXML()
1012 const XMLElement
& node_
,
1013 const Module
& module_
,
1014 const string
& header_
)
1015 : node(node_
), module(module_
), header(header_
)
1020 PchFile::ProcessXML()