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.
30 #include <sys/utime.h>
41 #include "exception.h"
44 class Backend
; // forward declaration
46 typedef std::vector
<std::string
> string_list
;
48 extern std::string ExePrefix
;
49 extern std::string ExePostfix
;
50 extern std::string sSep
;
51 extern std::string sBadSep
;
56 #define DEF_EXEPREFIX ""
57 #define DEF_EXEPOSTFIX ".exe"
59 #define DEF_CBAD_SEP '/'
61 #define DEF_SBAD_SEP "/"
63 #define DEF_EXEPREFIX "./"
64 #define DEF_EXEPOSTFIX ""
66 #define DEF_CBAD_SEP '\\'
68 #define DEF_SBAD_SEP "\\"
71 #define MS_VS_DEF_VERSION "7.10"
91 class TestSupportCode
;
92 class AutomaticDependency
;
97 class StubbedComponent
;
99 class CompilationUnit
;
103 class SourceFileTest
;
106 typedef std::map
<std::string
,Directory
*> directory_map
;
111 const Project
& project
;
112 const XMLElement
& node
;
114 XmlNode ( const Project
& project_
,
115 const XMLElement
& node_
);
119 virtual void ProcessXML();
122 enum DirectoryLocation
125 IntermediateDirectory
,
135 directory_map subdirs
;
136 Directory ( const std::string
& name
);
137 void Add ( const char* subdir
);
138 void GenerateTree ( DirectoryLocation root
,
140 void CreateRule ( FILE* f
,
141 const std::string
& parent
);
143 bool mkdir_p ( const char* path
);
144 bool CreateDirectory ( const std::string
& path
);
145 std::string
EscapeSpaces ( const std::string
& path
);
146 void GenerateTree ( const std::string
& parent
,
158 bool AutomaticDependencies
;
159 bool CheckDependenciesForModuleOnly
;
160 bool CompilationUnitsEnabled
;
161 std::string CheckDependenciesForModuleOnlyModule
;
162 std::string VSProjectVersion
;
163 std::string VSConfigurationType
;
164 bool UseVSVersionInPath
;
165 bool UseConfigurationInPath
;
166 bool MakeHandlesInstallDirectories
;
167 bool GenerateProxyMakefilesInSourceTree
;
174 static std::string
GetVariable ( const std::string
& name
);
175 static std::string
GetArch ();
176 static std::string
GetIntermediatePath ();
177 static std::string
GetOutputPath ();
178 static std::string
GetCdOutputPath ();
179 static std::string
GetInstallPath ();
180 static std::string
GetAutomakeFile ( const std::string
& defaultFile
);
181 static std::string
GetEnvironmentVariablePathOrDefault ( const std::string
& name
,
182 const std::string
& defaultValue
);
186 class FileSupportCode
189 static void WriteIfChanged ( char* outbuf
,
190 const std::string
& filename
,
191 bool ignoreError
= false );
199 CompilationUnit
* compilationUnit
;
207 std::vector
<CompilationUnit
*> compilationUnits
;
208 std::vector
<File
*> files
;
209 std::vector
<Include
*> includes
;
210 std::vector
<Define
*> defines
;
211 std::vector
<Library
*> libraries
;
212 std::vector
<Property
*> properties
;
213 std::vector
<Module
*> modules
;
214 std::vector
<CompilerFlag
*> compilerFlags
;
215 std::vector
<If
*> ifs
;
216 int asmFiles
; // number of .asm files in compilationUnits
221 void ExtractModules( std::vector
<Module
*> &modules
);
227 XMLElement
*node
, *head
;
230 const Configuration
& configuration
;
232 std::string makefile
;
233 XMLIncludes xmlbuildfiles
;
234 std::vector
<LinkerFlag
*> linkerFlags
;
235 std::vector
<CDFile
*> cdfiles
;
236 std::vector
<InstallFile
*> installfiles
;
237 std::vector
<Module
*> modules
;
238 IfableData non_if_data
;
240 Project ( const Configuration
& configuration
,
241 const std::string
& filename
,
242 const std::map
<std::string
, std::string
>* properties
= NULL
);
244 void SetBackend ( Backend
* backend
) { _backend
= backend
; }
245 Backend
& GetBackend() { return *_backend
; }
246 void WriteConfigurationFile ();
247 void ExecuteInvocations ();
249 void ProcessXML ( const std::string
& path
);
250 Module
* LocateModule ( const std::string
& name
);
251 const Module
* LocateModule ( const std::string
& name
) const;
252 const std::string
& GetProjectFilename () const;
253 std::string
ResolveProperties ( const std::string
& s
) const;
255 std::string
ResolveNextProperty ( const std::string
& s
) const;
256 const Property
* LookupProperty ( const std::string
& name
) const;
257 void SetConfigurationOption ( char* s
,
259 std::string
* alternativeName
);
260 void SetConfigurationOption ( char* s
,
263 void ProcessXMLSubElement ( const XMLElement
& e
,
264 const std::string
& path
,
265 ParseContext
& parseContext
);
267 // disable copy semantics
268 Project ( const Project
& );
269 Project
& operator = ( const Project
& );
280 KernelModeDriver
= 5,
300 EmbeddedTypeLib
= 26,
316 CompilerTypeDontCare
,
324 DirectoryLocation directory
;
325 std::string relative_path
;
328 FileLocation ( const DirectoryLocation directory
,
329 const std::string
& relative_path
,
330 const std::string
& name
,
331 const XMLElement
*node
= NULL
);
333 FileLocation ( const FileLocation
& other
);
339 const Project
& project
;
340 const XMLElement
& node
;
341 std::string xmlbuildFile
;
344 std::string extension
;
345 std::string baseaddress
;
347 std::string buildtype
;
349 ImportLibrary
* importLibrary
;
352 bool underscoreSymbols
;
354 bool isDefaultEntryPoint
;
355 Bootstrap
* bootstrap
;
356 AutoRegister
* autoRegister
; // <autoregister> node
357 IfableData non_if_data
;
358 std::vector
<Invoke
*> invocations
;
359 std::vector
<Dependency
*> dependencies
;
360 std::vector
<CompilerFlag
*> compilerFlags
;
361 std::vector
<LinkerFlag
*> linkerFlags
;
362 std::vector
<StubbedComponent
*> stubbedComponents
;
363 LinkerScript
* linkerScript
;
368 std::string aliasedModuleName
;
372 FileLocation
*output
; // "path/foo.exe"
373 FileLocation
*dependency
; // "path/foo.exe" or "path/libfoo.a"
374 FileLocation
*install
;
376 Module ( const Project
& project
,
377 const XMLElement
& moduleNode
,
378 const std::string
& modulePath
);
380 ModuleType
GetModuleType ( const std::string
& location
,
381 const XMLAttribute
& attribute
);
382 bool HasImportLibrary () const;
384 std::string
GetPathWithPrefix ( const std::string
& prefix
) const; // "path/prefixfoo.exe"
385 std::string
GetPathToBaseDir() const; // "../" offset to rootdirectory
386 std::string
GetEntryPoint(bool leadingUnderscore
) const;
387 void GetTargets ( string_list
& ) const;
388 std::string
GetInvocationTarget ( const int index
) const;
389 bool HasFileWithExtension ( const IfableData
&, const std::string
& extension
) const;
390 void InvokeModule () const;
393 void SetImportLibrary ( ImportLibrary
* importLibrary
);
394 DirectoryLocation
GetTargetDirectoryTree () const;
395 std::string
GetDefaultModuleExtension () const;
396 std::string
GetDefaultModuleEntrypoint () const;
397 std::string
GetDefaultModuleBaseaddress () const;
398 std::string entrypoint
;
399 void ProcessXMLSubElement ( const XMLElement
& e
,
400 DirectoryLocation directory
,
401 const std::string
& relative_path
,
402 ParseContext
& parseContext
);
409 FileLocation
*directory
;
411 Include ( const Project
& project
,
412 const XMLElement
* includeNode
);
413 Include ( const Project
& project
,
414 const XMLElement
* includeNode
,
415 const Module
* module
);
416 Include ( const Project
& project
,
417 DirectoryLocation directory
,
418 const std::string
& relative_path
);
422 const Project
& project
;
423 const XMLElement
* node
;
424 const Module
* module
;
425 DirectoryLocation
GetDefaultDirectoryTree ( const Module
* module
) const;
432 const Project
& project
;
433 const Module
* module
;
434 const XMLElement
* node
;
440 Define ( const Project
& project
,
441 const XMLElement
& defineNode
);
442 Define ( const Project
& project
,
443 const Module
* module
,
444 const XMLElement
& defineNode
);
445 Define ( const Project
& project
,
446 const Module
* module
,
447 const std::string
& name_
,
448 const std::string
& backend_
= "" );
461 std::string switches
;
462 bool isPreCompiledHeader
;
464 File ( DirectoryLocation directory
,
465 const std::string
& relative_path
,
466 const std::string
& name
,
468 const std::string
& _switches
,
469 bool _isPreCompiledHeader
);
472 std::string
GetFullPath () const;
478 const XMLElement
*node
;
480 const Module
& module
;
482 const Module
* importedModule
;
484 Library ( const XMLElement
& _node
,
485 const Module
& _module
,
486 const std::string
& _name
);
487 Library ( const Module
& _module
,
488 const std::string
& _name
);
497 const XMLElement
& node
;
498 const Module
& module
;
499 const Module
* invokeModule
;
500 std::vector
<InvokeFile
*> input
;
501 std::vector
<InvokeFile
*> output
;
503 Invoke ( const XMLElement
& _node
,
504 const Module
& _module
);
507 void GetTargets ( string_list
& targets
) const;
508 std::string
GetParameters () const;
510 void ProcessXMLSubElement ( const XMLElement
& e
);
511 void ProcessXMLSubElementInput ( const XMLElement
& e
);
512 void ProcessXMLSubElementOutput ( const XMLElement
& e
);
519 const XMLElement
& node
;
521 std::string switches
;
523 InvokeFile ( const XMLElement
& _node
,
524 const std::string
& _name
);
533 const XMLElement
& node
;
534 const Module
& module
;
535 const Module
* dependencyModule
;
537 Dependency ( const XMLElement
& _node
,
538 const Module
& _module
);
546 const XMLElement
& node
;
547 const Module
& module
;
549 std::string description
;
551 std::string copyright
;
556 Metadata ( const XMLElement
& _node
,
557 const Module
& _module
);
562 class ImportLibrary
: public XmlNode
565 const Module
& module
;
567 FileLocation
*source
;
569 ImportLibrary ( const Project
& project
,
570 const XMLElement
& node
,
571 const Module
& module
);
578 const XMLElement
& node
;
579 const Project
& project
;
580 const Module
* module
;
582 std::string property
, value
;
585 If ( const XMLElement
& node_
,
586 const Project
& project_
,
587 const Module
* module_
,
588 const bool negated_
= false );
598 const Project
& project
;
599 const Module
* module
;
600 const XMLElement
& node
;
602 CompilerType compiler
;
604 CompilerFlag ( const Project
& project
,
605 const XMLElement
& compilerFlagNode
);
606 CompilerFlag ( const Project
& project
,
607 const Module
* module
,
608 const XMLElement
& compilerFlagNode
);
619 const Project
& project
;
620 const Module
* module
;
621 const XMLElement
& node
;
624 LinkerFlag ( const Project
& project
,
625 const XMLElement
& linkerFlagNode
);
626 LinkerFlag ( const Project
& project
,
627 const Module
* module
,
628 const XMLElement
& linkerFlagNode
);
639 const XMLElement
& node
;
640 const Module
& module
;
643 LinkerScript ( const XMLElement
& node
,
644 const Module
& module
,
645 const FileLocation
& file
);
654 const Project
& project
;
655 const Module
* module
;
656 std::string name
, value
;
658 Property ( const XMLElement
& node_
,
659 const Project
& project_
,
660 const Module
* module_
);
662 Property ( const Project
& project_
,
663 const Module
* module_
,
664 const std::string
& name_
,
665 const std::string
& value_
);
671 class TestSupportCode
674 const Project
& project
;
676 TestSupportCode ( const Project
& project
);
678 void GenerateTestSupportCode ( bool verbose
);
680 bool IsTestModule ( const Module
& module
);
681 void GenerateTestSupportCodeForModule ( Module
& module
,
683 std::string
GetHooksFilename ( Module
& module
);
684 char* WriteStubbedSymbolToHooksFile ( char* buffer
,
685 const StubbedComponent
& component
,
686 const StubbedSymbol
& symbol
);
687 char* WriteStubbedComponentToHooksFile ( char* buffer
,
688 const StubbedComponent
& component
);
689 void WriteHooksFile ( Module
& module
);
690 std::string
GetStubsFilename ( Module
& module
);
691 char* WriteStubbedSymbolToStubsFile ( char* buffer
,
692 const StubbedComponent
& component
,
693 const StubbedSymbol
& symbol
,
695 char* WriteStubbedComponentToStubsFile ( char* buffer
,
696 const StubbedComponent
& component
,
698 void WriteStubsFile ( Module
& module
);
699 std::string
GetStartupFilename ( Module
& module
);
700 bool IsUnknownCharacter ( char ch
);
701 std::string
GetTestDispatcherName ( std::string filename
);
702 bool IsTestFile ( std::string
& filename
) const;
703 void GetSourceFilenames ( string_list
& list
,
704 Module
& module
) const;
705 char* WriteTestDispatcherPrototypesToStartupFile ( char* buffer
,
707 char* WriteRegisterTestsFunctionToStartupFile ( char* buffer
,
709 void WriteStartupFile ( Module
& module
);
716 SourceFile ( AutomaticDependency
* automaticDependency
,
717 const Module
& module
,
719 SourceFile
* parent
);
721 std::vector
<SourceFile
*> files
; /* List of files included in this file */
723 AutomaticDependency
* automaticDependency
;
724 const Module
& module
;
725 std::vector
<SourceFile
*> parents
; /* List of files, this file is included from */
726 time_t lastWriteTime
;
727 time_t youngestLastWriteTime
; /* Youngest last write time of this file and all children */
728 SourceFile
* youngestFile
;
732 void SkipWhitespace ();
733 bool ReadInclude ( std::string
& filename
,
734 bool& searchCurrentDirectory
,
736 bool IsIncludedFrom ( const File
& file
);
737 SourceFile
* ParseFile(const File
& file
);
738 bool CanProcessFile ( const File
& file
);
739 bool IsParentOf ( const SourceFile
* parent
,
740 const SourceFile
* child
);
747 class AutomaticDependency
749 friend class SourceFileTest
;
751 const Project
& project
;
753 AutomaticDependency ( const Project
& project
);
754 ~AutomaticDependency ();
755 bool LocateIncludedFile ( const FileLocation
& directory
,
756 const std::string
& includedFilename
);
757 bool LocateIncludedFile ( SourceFile
* sourceFile
,
758 const Module
& module
,
759 const std::string
& includedFilename
,
760 bool searchCurrentDirectory
,
762 File
& resolvedFile
);
763 SourceFile
* RetrieveFromCacheOrParse ( const Module
& module
,
765 SourceFile
* parentSourceFile
);
766 SourceFile
* RetrieveFromCache ( const File
& file
);
767 void CheckAutomaticDependencies ( bool verbose
);
768 void CheckAutomaticDependenciesForModule ( Module
& module
,
771 void GetModulesToCheck ( Module
& module
, std::vector
<const Module
*>& modules
);
772 void CheckAutomaticDependencies ( const Module
& module
,
774 void CheckAutomaticDependenciesForFile ( SourceFile
* sourceFile
);
775 void GetIncludeDirectories ( std::vector
<Include
*>& includes
,
776 const Module
& module
);
777 void GetModuleFiles ( const Module
& module
,
778 std::vector
<File
*>& files
) const;
780 void ParseFiles ( const Module
& module
);
781 void ParseFile ( const Module
& module
,
783 std::map
<std::string
, SourceFile
*> sourcefile_map
;
790 const Project
& project
;
791 const Module
* module
;
792 const XMLElement
& node
;
794 std::string nameoncd
;
796 Bootstrap ( const Project
& project
,
797 const Module
* module
,
798 const XMLElement
& bootstrapNode
);
802 bool IsSupportedModuleType ( ModuleType type
);
804 static std::string
ReplaceVariable ( const std::string
& name
,
805 const std::string
& value
,
810 class CDFile
: public XmlNode
813 FileLocation
*source
;
814 FileLocation
*target
;
816 CDFile ( const Project
& project
,
817 const XMLElement
& bootstrapNode
,
818 const std::string
& path
);
820 static std::string
ReplaceVariable ( const std::string
& name
,
821 const std::string
& value
,
826 class InstallFile
: public XmlNode
829 FileLocation
*source
;
830 FileLocation
*target
;
832 InstallFile ( const Project
& project
,
833 const XMLElement
& bootstrapNode
,
834 const std::string
& path
);
841 const XMLElement
& node
;
842 const Module
& module
;
846 const XMLElement
& node
,
847 const Module
& module
,
848 const FileLocation
& file
);
853 class StubbedComponent
856 const Module
* module
;
857 const XMLElement
& node
;
859 std::vector
<StubbedSymbol
*> symbols
;
861 StubbedComponent ( const Module
* module_
,
862 const XMLElement
& stubbedComponentNode
);
863 ~StubbedComponent ();
865 void ProcessXMLSubElement ( const XMLElement
& e
);
872 const XMLElement
& node
;
875 std::string strippedName
;
877 StubbedSymbol ( const XMLElement
& stubbedSymbolNode
);
881 std::string
StripSymbol ( std::string symbol
);
885 class CompilationUnit
890 CompilationUnit ( const File
* file
);
891 CompilationUnit ( const Project
* project
,
892 const Module
* module
,
893 const XMLElement
* node
);
896 bool IsGeneratedFile () const;
897 bool HasFileWithExtension ( const std::string
& extension
) const;
898 bool IsFirstFile () const;
899 const FileLocation
& GetFilename () const;
900 const std::string
& GetSwitches () const;
901 void AddFile ( const File
* file
);
902 const std::vector
<const File
*> GetFiles () const;
904 const Project
* project
;
905 const Module
* module
;
906 const XMLElement
* node
;
907 std::vector
<const File
*> files
;
908 FileLocation
*default_name
;
912 class CompilationUnitSupportCode
915 const Project
& project
;
917 CompilationUnitSupportCode ( const Project
& project
);
918 ~CompilationUnitSupportCode ();
919 void Generate ( bool verbose
);
921 void GenerateForModule ( Module
& module
,
923 std::string
GetCompilationUnitFilename ( Module
& module
,
924 CompilationUnit
& compilationUnit
);
925 void WriteCompilationUnitFile ( Module
& module
,
926 CompilationUnit
& compilationUnit
);
930 enum AutoRegisterType
937 class AutoRegister
: public XmlNode
940 const Module
* module
;
941 std::string infSection
;
942 AutoRegisterType type
;
943 AutoRegister ( const Project
& project_
,
944 const Module
* module_
,
945 const XMLElement
& node_
);
947 bool IsSupportedModuleType ( ModuleType type
);
948 AutoRegisterType
GetAutoRegisterType( const std::string
& type
);
953 class SysSetupGenerator
956 const Project
& project
;
957 SysSetupGenerator ( const Project
& project
);
958 ~SysSetupGenerator ();
961 std::string
GetDirectoryId ( const Module
& module
);
962 std::string
GetFlags ( const Module
& module
);
963 void Generate ( HINF inf
,
964 const Module
& module
);
969 InitializeEnvironment ();
972 Right ( const std::string
& s
, size_t n
);
975 Replace ( const std::string
& s
, const std::string
& find
, const std::string
& with
);
978 ChangeSeparator ( const std::string
& s
,
979 const char fromSeparator
,
980 const char toSeparator
);
983 FixSeparator ( const std::string
& s
);
986 FixSeparatorForSystemCommand ( const std::string
& s
);
989 DosSeparator ( const std::string
& s
);
993 const std::string
& filename
,
994 const std::string
& newExtension
);
998 const Project
& project
,
999 const std::string
& location
,
1000 const std::string
& path
,
1001 const std::string
& att_value
);
1004 GetExtension ( const FileLocation
& file
);
1007 NormalizeFilename ( const std::string
& filename
);
1010 ToLower ( std::string filename
);
1012 #endif /* __RBUILD_H */