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
);
103 GetBooleanValue ( const string
& value
)
111 IfableData::~IfableData()
114 for ( i
= 0; i
< files
.size(); i
++ )
116 for ( i
= 0; i
< includes
.size(); i
++ )
118 for ( i
= 0; i
< defines
.size(); i
++ )
120 for ( i
= 0; i
< libraries
.size(); i
++ )
122 for ( i
= 0; i
< properties
.size(); i
++ )
123 delete properties
[i
];
124 for ( i
= 0; i
< compilerFlags
.size(); i
++ )
125 delete compilerFlags
[i
];
126 for ( i
= 0; i
< ifs
.size(); i
++ )
130 void IfableData::ProcessXML ()
133 for ( i
= 0; i
< files
.size (); i
++ )
134 files
[i
]->ProcessXML ();
135 for ( i
= 0; i
< includes
.size (); i
++ )
136 includes
[i
]->ProcessXML ();
137 for ( i
= 0; i
< defines
.size (); i
++ )
138 defines
[i
]->ProcessXML ();
139 for ( i
= 0; i
< libraries
.size (); i
++ )
140 libraries
[i
]->ProcessXML ();
141 for ( i
= 0; i
< properties
.size(); i
++ )
142 properties
[i
]->ProcessXML ();
143 for ( i
= 0; i
< compilerFlags
.size(); i
++ )
144 compilerFlags
[i
]->ProcessXML ();
145 for ( i
= 0; i
< ifs
.size (); i
++ )
146 ifs
[i
]->ProcessXML ();
149 Module::Module ( const Project
& project
,
150 const XMLElement
& moduleNode
,
151 const string
& modulePath
)
154 importLibrary (NULL
),
160 if ( node
.name
!= "module" )
161 throw InvalidOperationException ( __FILE__
,
163 "Module created with non-<module> node" );
165 xmlbuildFile
= Path::RelativeFromWorkingDirectory ( moduleNode
.xmlFile
->filename () );
167 path
= FixSeparator ( modulePath
);
171 const XMLAttribute
* att
= moduleNode
.GetAttribute ( "if", false );
173 enabled
= GetBooleanValue ( project
.ResolveProperties ( att
->value
) );
175 att
= moduleNode
.GetAttribute ( "ifnot", false );
177 enabled
= !GetBooleanValue ( project
.ResolveProperties ( att
->value
) );
179 att
= moduleNode
.GetAttribute ( "name", true );
183 att
= moduleNode
.GetAttribute ( "type", true );
185 type
= GetModuleType ( node
.location
, *att
);
187 att
= moduleNode
.GetAttribute ( "extension", false );
189 extension
= att
->value
;
191 extension
= GetDefaultModuleExtension ();
193 att
= moduleNode
.GetAttribute ( "entrypoint", false );
195 entrypoint
= att
->value
;
197 entrypoint
= GetDefaultModuleEntrypoint ();
199 att
= moduleNode
.GetAttribute ( "baseaddress", false );
201 baseaddress
= att
->value
;
203 baseaddress
= GetDefaultModuleBaseaddress ();
205 att
= moduleNode
.GetAttribute ( "mangledsymbols", false );
208 const char* p
= att
->value
.c_str();
209 if ( !stricmp ( p
, "true" ) || !stricmp ( p
, "yes" ) )
210 mangledSymbols
= true;
211 else if ( !stricmp ( p
, "false" ) || !stricmp ( p
, "no" ) )
212 mangledSymbols
= false;
215 throw InvalidAttributeValueException (
222 mangledSymbols
= false;
224 att
= moduleNode
.GetAttribute ( "host", false );
227 const char* p
= att
->value
.c_str();
228 if ( !stricmp ( p
, "true" ) || !stricmp ( p
, "yes" ) )
230 else if ( !stricmp ( p
, "false" ) || !stricmp ( p
, "no" ) )
234 throw InvalidAttributeValueException (
241 att
= moduleNode
.GetAttribute ( "prefix", false );
245 att
= moduleNode
.GetAttribute ( "installbase", false );
247 installBase
= att
->value
;
251 att
= moduleNode
.GetAttribute ( "installname", false );
253 installName
= att
->value
;
257 att
= moduleNode
.GetAttribute ( "usewrc", false );
259 useWRC
= att
->value
== "true";
263 att
= moduleNode
.GetAttribute ( "warnings", false );
265 enableWarnings
= att
->value
== "true";
267 enableWarnings
= false;
273 for ( i
= 0; i
< invocations
.size(); i
++ )
274 delete invocations
[i
];
275 for ( i
= 0; i
< dependencies
.size(); i
++ )
276 delete dependencies
[i
];
277 for ( i
= 0; i
< compilerFlags
.size(); i
++ )
278 delete compilerFlags
[i
];
279 for ( i
= 0; i
< linkerFlags
.size(); i
++ )
280 delete linkerFlags
[i
];
281 for ( i
= 0; i
< stubbedComponents
.size(); i
++ )
282 delete stubbedComponents
[i
];
291 for ( i
= 0; i
< node
.subElements
.size(); i
++ )
292 ProcessXMLSubElement ( *node
.subElements
[i
], path
);
293 for ( i
= 0; i
< invocations
.size(); i
++ )
294 invocations
[i
]->ProcessXML ();
295 for ( i
= 0; i
< dependencies
.size(); i
++ )
296 dependencies
[i
]->ProcessXML ();
297 for ( i
= 0; i
< compilerFlags
.size(); i
++ )
298 compilerFlags
[i
]->ProcessXML();
299 for ( i
= 0; i
< linkerFlags
.size(); i
++ )
300 linkerFlags
[i
]->ProcessXML();
301 for ( i
= 0; i
< stubbedComponents
.size(); i
++ )
302 stubbedComponents
[i
]->ProcessXML();
303 non_if_data
.ProcessXML();
309 Module::ProcessXMLSubElement ( const XMLElement
& e
,
313 bool subs_invalid
= false;
314 string
subpath ( path
);
315 if ( e
.name
== "file" && e
.value
.size () > 0 )
318 const XMLAttribute
* att
= e
.GetAttribute ( "first", false );
321 if ( !stricmp ( att
->value
.c_str(), "true" ) )
323 else if ( stricmp ( att
->value
.c_str(), "false" ) )
324 throw InvalidBuildFileException (
326 "attribute 'first' of <file> element can only be 'true' or 'false'" );
328 string switches
= "";
329 att
= e
.GetAttribute ( "switches", false );
331 switches
= att
->value
;
334 // check for c++ file
335 string ext
= GetExtension ( e
.value
);
336 if ( !stricmp ( ext
.c_str(), ".cpp" ) )
338 else if ( !stricmp ( ext
.c_str(), ".cc" ) )
340 else if ( !stricmp ( ext
.c_str(), ".cxx" ) )
343 File
* pFile
= new File ( FixSeparator ( path
+ CSEP
+ e
.value
),
348 pIf
->data
.files
.push_back ( pFile
);
350 non_if_data
.files
.push_back ( pFile
);
353 else if ( e
.name
== "library" && e
.value
.size () )
355 Library
* pLibrary
= new Library ( e
, *this, e
.value
);
357 pIf
->data
.libraries
.push_back ( pLibrary
);
359 non_if_data
.libraries
.push_back ( pLibrary
);
362 else if ( e
.name
== "directory" )
364 const XMLAttribute
* att
= e
.GetAttribute ( "name", true );
366 subpath
= GetSubPath ( e
.location
, path
, att
->value
);
368 else if ( e
.name
== "include" )
370 Include
* include
= new Include ( project
, this, e
);
372 pIf
->data
.includes
.push_back ( include
);
374 non_if_data
.includes
.push_back ( include
);
377 else if ( e
.name
== "define" )
379 Define
* pDefine
= new Define ( project
, this, e
);
381 pIf
->data
.defines
.push_back ( pDefine
);
383 non_if_data
.defines
.push_back ( pDefine
);
386 else if ( e
.name
== "invoke" )
389 throw InvalidBuildFileException (
391 "<invoke> is not a valid sub-element of <if>" );
392 invocations
.push_back ( new Invoke ( e
, *this ) );
393 subs_invalid
= false;
395 else if ( e
.name
== "dependency" )
398 throw InvalidBuildFileException (
400 "<dependency> is not a valid sub-element of <if>" );
401 dependencies
.push_back ( new Dependency ( e
, *this ) );
404 else if ( e
.name
== "importlibrary" )
407 throw InvalidBuildFileException (
409 "<importlibrary> is not a valid sub-element of <if>" );
411 throw InvalidBuildFileException (
413 "Only one <importlibrary> is valid per module" );
414 importLibrary
= new ImportLibrary ( e
, *this );
417 else if ( e
.name
== "if" )
420 pIf
= new If ( e
, project
, this );
422 pOldIf
->data
.ifs
.push_back ( pIf
);
424 non_if_data
.ifs
.push_back ( pIf
);
425 subs_invalid
= false;
427 else if ( e
.name
== "compilerflag" )
429 CompilerFlag
* pCompilerFlag
= new CompilerFlag ( project
, this, e
);
431 pIf
->data
.compilerFlags
.push_back ( pCompilerFlag
);
433 non_if_data
.compilerFlags
.push_back ( pCompilerFlag
);
436 else if ( e
.name
== "linkerflag" )
438 linkerFlags
.push_back ( new LinkerFlag ( project
, this, e
) );
441 else if ( e
.name
== "component" )
443 stubbedComponents
.push_back ( new StubbedComponent ( this, e
) );
444 subs_invalid
= false;
446 else if ( e
.name
== "property" )
448 throw InvalidBuildFileException (
450 "<property> is not a valid sub-element of <module>" );
452 else if ( e
.name
== "bootstrap" )
454 bootstrap
= new Bootstrap ( project
, this, e
);
457 else if ( e
.name
== "pch" )
460 throw InvalidBuildFileException (
462 "<pch> is not a valid sub-element of <if>" );
464 throw InvalidBuildFileException (
466 "Only one <pch> is valid per module" );
468 e
, *this, File ( FixSeparator ( path
+ CSEP
+ e
.value
), false, "", true ) );
471 if ( subs_invalid
&& e
.subElements
.size() > 0 )
472 throw InvalidBuildFileException (
474 "<%s> cannot have sub-elements",
476 for ( size_t i
= 0; i
< e
.subElements
.size (); i
++ )
477 ProcessXMLSubElement ( *e
.subElements
[i
], subpath
, pIf
);
481 Module::GetModuleType ( const string
& location
, const XMLAttribute
& attribute
)
483 if ( attribute
.value
== "buildtool" )
485 if ( attribute
.value
== "staticlibrary" )
486 return StaticLibrary
;
487 if ( attribute
.value
== "objectlibrary" )
488 return ObjectLibrary
;
489 if ( attribute
.value
== "kernel" )
491 if ( attribute
.value
== "kernelmodedll" )
492 return KernelModeDLL
;
493 if ( attribute
.value
== "kernelmodedriver" )
494 return KernelModeDriver
;
495 if ( attribute
.value
== "nativedll" )
497 if ( attribute
.value
== "nativecui" )
499 if ( attribute
.value
== "win32dll" )
501 if ( attribute
.value
== "win32cui" )
503 if ( attribute
.value
== "win32gui" )
505 if ( attribute
.value
== "bootloader" )
507 if ( attribute
.value
== "bootsector" )
509 if ( attribute
.value
== "iso" )
511 if ( attribute
.value
== "liveiso" )
513 if ( attribute
.value
== "test" )
515 if ( attribute
.value
== "rpcserver" )
517 if ( attribute
.value
== "rpcclient" )
519 throw InvalidAttributeValueException ( location
,
525 Module::GetDefaultModuleExtension () const
544 case KernelModeDriver
:
559 throw InvalidOperationException ( __FILE__
,
564 Module::GetDefaultModuleEntrypoint () const
569 return "_NtProcessStartup";
571 return "_DriverEntry@8";
573 return "_DllMainCRTStartup@12";
575 return "_NtProcessStartup@4";
577 return "_DllMain@12";
580 return "_mainCRTStartup";
582 return "_WinMainCRTStartup";
583 case KernelModeDriver
:
584 return "_DriverEntry@8";
596 throw InvalidOperationException ( __FILE__
,
601 Module::GetDefaultModuleBaseaddress () const
617 case KernelModeDriver
:
630 throw InvalidOperationException ( __FILE__
,
635 Module::HasImportLibrary () const
637 return importLibrary
!= NULL
;
641 Module::IsDLL () const
649 case KernelModeDriver
:
666 throw InvalidOperationException ( __FILE__
,
671 Module::GenerateInOutputTree () const
679 case KernelModeDriver
:
696 throw InvalidOperationException ( __FILE__
,
701 Module::GetTargetName () const
703 return name
+ extension
;
707 Module::GetDependencyPath () const
709 if ( HasImportLibrary () )
710 return ReplaceExtension ( GetPathWithPrefix ( "lib" ), ".a" );
716 Module::GetBasePath () const
722 Module::GetPath () const
724 if ( path
.length() > 0 )
725 return path
+ CSEP
+ GetTargetName ();
727 return GetTargetName ();
731 Module::GetPathWithPrefix ( const string
& prefix
) const
733 return path
+ CSEP
+ prefix
+ GetTargetName ();
737 Module::GetInvocationTarget ( const int index
) const
739 return ssprintf ( "%s_invoke_%d",
745 Module::HasFileWithExtension (
746 const IfableData
& data
,
747 const std::string
& extension
) const
750 for ( i
= 0; i
< data
.files
.size (); i
++ )
752 File
& file
= *data
.files
[i
];
753 string file_ext
= GetExtension ( file
.name
);
754 if ( !stricmp ( file_ext
.c_str (), extension
.c_str () ) )
757 for ( i
= 0; i
< data
.ifs
.size (); i
++ )
759 if ( HasFileWithExtension ( data
.ifs
[i
]->data
, extension
) )
766 Module::InvokeModule () const
768 for ( size_t i
= 0; i
< invocations
.size (); i
++ )
770 Invoke
& invoke
= *invocations
[i
];
771 string command
= invoke
.invokeModule
->GetPath () + " " + invoke
.GetParameters ();
772 printf ( "Executing '%s'\n\n", command
.c_str () );
773 int exitcode
= system ( command
.c_str () );
775 throw InvocationFailedException ( command
,
781 File::File ( const string
& _name
, bool _first
,
782 std::string _switches
,
783 bool _isPreCompiledHeader
)
787 isPreCompiledHeader(_isPreCompiledHeader
)
797 File::IsGeneratedFile () const
799 string extension
= GetExtension ( name
);
800 return ( extension
== ".spec" || extension
== ".SPEC" );
804 Library::Library ( const XMLElement
& _node
,
805 const Module
& _module
,
806 const string
& _name
)
810 imported_module(_module
.project
.LocateModule(_name
))
812 if ( module
.name
== name
)
813 throw InvalidBuildFileException (
815 "module '%s' cannot link against itself",
817 if ( !imported_module
)
818 throw InvalidBuildFileException (
820 "module '%s' trying to import non-existant module '%s'",
826 Library::ProcessXML()
828 if ( !module
.project
.LocateModule ( name
) )
829 throw InvalidBuildFileException (
831 "module '%s' is trying to link against non-existant module '%s'",
837 Invoke::Invoke ( const XMLElement
& _node
,
838 const Module
& _module
)
847 const XMLAttribute
* att
= node
.GetAttribute ( "module", false );
849 invokeModule
= &module
;
852 invokeModule
= module
.project
.LocateModule ( att
->value
);
853 if ( invokeModule
== NULL
)
854 throw InvalidBuildFileException (
856 "module '%s' is trying to invoke non-existant module '%s'",
858 att
->value
.c_str() );
861 for ( size_t i
= 0; i
< node
.subElements
.size (); i
++ )
862 ProcessXMLSubElement ( *node
.subElements
[i
] );
866 Invoke::ProcessXMLSubElement ( const XMLElement
& e
)
868 bool subs_invalid
= false;
869 if ( e
.name
== "input" )
871 for ( size_t i
= 0; i
< e
.subElements
.size (); i
++ )
872 ProcessXMLSubElementInput ( *e
.subElements
[i
] );
874 else if ( e
.name
== "output" )
876 for ( size_t i
= 0; i
< e
.subElements
.size (); i
++ )
877 ProcessXMLSubElementOutput ( *e
.subElements
[i
] );
879 if ( subs_invalid
&& e
.subElements
.size() > 0 )
880 throw InvalidBuildFileException ( e
.location
,
881 "<%s> cannot have sub-elements",
886 Invoke::ProcessXMLSubElementInput ( const XMLElement
& e
)
888 bool subs_invalid
= false;
889 if ( e
.name
== "inputfile" && e
.value
.size () > 0 )
891 input
.push_back ( new InvokeFile ( e
, FixSeparator ( module
.path
+ CSEP
+ e
.value
) ) );
894 if ( subs_invalid
&& e
.subElements
.size() > 0 )
895 throw InvalidBuildFileException ( e
.location
,
896 "<%s> cannot have sub-elements",
901 Invoke::ProcessXMLSubElementOutput ( const XMLElement
& e
)
903 bool subs_invalid
= false;
904 if ( e
.name
== "outputfile" && e
.value
.size () > 0 )
906 output
.push_back ( new InvokeFile ( e
, FixSeparator ( module
.path
+ CSEP
+ e
.value
) ) );
909 if ( subs_invalid
&& e
.subElements
.size() > 0 )
910 throw InvalidBuildFileException (
912 "<%s> cannot have sub-elements",
917 Invoke::GetTargets ( string_list
& targets
) const
919 for ( size_t i
= 0; i
< output
.size (); i
++ )
921 InvokeFile
& file
= *output
[i
];
922 targets
.push_back ( NormalizeFilename ( file
.name
) );
927 Invoke::GetParameters () const
929 string
parameters ( "" );
931 for ( i
= 0; i
< output
.size (); i
++ )
933 if ( parameters
.length () > 0)
935 InvokeFile
& invokeFile
= *output
[i
];
936 if ( invokeFile
.switches
.length () > 0 )
938 parameters
+= invokeFile
.switches
+ " ";
940 parameters
+= invokeFile
.name
;
943 for ( i
= 0; i
< input
.size (); i
++ )
945 if ( parameters
.length () > 0 )
947 InvokeFile
& invokeFile
= *input
[i
];
948 if ( invokeFile
.switches
.length () > 0 )
950 parameters
+= invokeFile
.switches
;
953 parameters
+= invokeFile
.name
;
960 InvokeFile::InvokeFile ( const XMLElement
& _node
,
961 const string
& _name
)
965 const XMLAttribute
* att
= _node
.GetAttribute ( "switches", false );
967 switches
= att
->value
;
973 InvokeFile::ProcessXML()
978 Dependency::Dependency ( const XMLElement
& _node
,
979 const Module
& _module
)
982 dependencyModule (NULL
)
987 Dependency::ProcessXML()
989 dependencyModule
= module
.project
.LocateModule ( node
.value
);
990 if ( dependencyModule
== NULL
)
991 throw InvalidBuildFileException ( node
.location
,
992 "module '%s' depend on non-existant module '%s'",
994 node
.value
.c_str() );
998 ImportLibrary::ImportLibrary ( const XMLElement
& _node
,
999 const Module
& _module
)
1003 const XMLAttribute
* att
= _node
.GetAttribute ( "basename", false );
1005 basename
= att
->value
;
1007 basename
= module
.name
;
1009 att
= _node
.GetAttribute ( "definition", true );
1011 definition
= FixSeparator(att
->value
);
1015 If::If ( const XMLElement
& node_
,
1016 const Project
& project_
,
1017 const Module
* module_
)
1018 : node(node_
), project(project_
), module(module_
)
1020 const XMLAttribute
* att
;
1022 att
= node
.GetAttribute ( "property", true );
1024 property
= att
->value
;
1026 att
= node
.GetAttribute ( "value", true );
1041 Property::Property ( const XMLElement
& node_
,
1042 const Project
& project_
,
1043 const Module
* module_
)
1044 : node(node_
), project(project_
), module(module_
)
1046 const XMLAttribute
* att
;
1048 att
= node
.GetAttribute ( "name", true );
1052 att
= node
.GetAttribute ( "value", true );
1058 Property::ProcessXML()
1064 const XMLElement
& node_
,
1065 const Module
& module_
,
1067 : node(node_
), module(module_
), file(file_
)
1072 PchFile::ProcessXML()