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"
90 class TestSupportCode
;
91 class AutomaticDependency
;
96 class StubbedComponent
;
98 class CompilationUnit
;
102 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
);
138 void Add ( const char* subdir
);
139 void GenerateTree ( DirectoryLocation root
,
141 void CreateRule ( FILE* f
,
142 const std::string
& parent
);
144 bool mkdir_p ( const char* path
);
145 bool CreateDirectory ( const std::string
& path
);
146 std::string
EscapeSpaces ( const std::string
& path
);
147 void GenerateTree ( const std::string
& parent
,
159 bool AutomaticDependencies
;
160 bool CheckDependenciesForModuleOnly
;
161 bool CompilationUnitsEnabled
;
162 bool PrecompiledHeadersEnabled
;
163 std::string CheckDependenciesForModuleOnlyModule
;
164 std::string VSProjectVersion
;
165 std::string VSConfigurationType
;
166 bool UseVSVersionInPath
;
167 bool UseConfigurationInPath
;
168 bool MakeHandlesInstallDirectories
;
169 bool GenerateProxyMakefilesInSourceTree
;
176 static std::string
GetVariable ( const std::string
& name
);
177 static std::string
GetArch ();
178 static std::string
GetIntermediatePath ();
179 static std::string
GetOutputPath ();
180 static std::string
GetCdOutputPath ();
181 static std::string
GetInstallPath ();
182 static std::string
GetAutomakeFile ( const std::string
& defaultFile
);
183 static std::string
GetEnvironmentVariablePathOrDefault ( const std::string
& name
,
184 const std::string
& defaultValue
);
188 class FileSupportCode
191 static void WriteIfChanged ( char* outbuf
,
192 const std::string
& filename
,
193 bool ignoreError
= false );
200 CompilationUnit
* compilationUnit
;
208 std::vector
<CompilationUnit
*> compilationUnits
;
209 std::vector
<File
*> files
;
210 std::vector
<Include
*> includes
;
211 std::vector
<Define
*> defines
;
212 std::vector
<Library
*> libraries
;
213 std::map
<std::string
, Property
*> properties
;
214 std::vector
<Module
*> modules
;
215 std::vector
<CompilerFlag
*> compilerFlags
;
216 int asmFiles
; // number of .asm files in compilationUnits
221 void ExtractModules( std::map
<std::string
, 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::map
<std::string
, Module
*> modules
;
238 IfableData non_if_data
;
239 IfableData host_non_if_data
;
241 Project ( const Configuration
& configuration
,
242 const std::string
& filename
,
243 const std::map
<std::string
, std::string
>* properties
= NULL
);
245 void SetBackend ( Backend
* backend
) { _backend
= backend
; }
246 Backend
& GetBackend() { return *_backend
; }
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;
254 const Property
* LookupProperty ( const std::string
& name
) const;
256 std::string
ResolveNextProperty ( const std::string
& s
) const;
258 void ProcessXMLSubElement ( const XMLElement
& e
,
259 const std::string
& path
,
260 ParseContext
& parseContext
);
262 // disable copy semantics
263 Project ( const Project
& );
264 Project
& operator = ( const Project
& );
302 TypeDontCare
, // always at the end
315 CompilerTypeDontCare
,
323 DirectoryLocation directory
;
324 std::string relative_path
;
327 FileLocation ( const DirectoryLocation directory
,
328 const std::string
& relative_path
,
329 const std::string
& name
,
330 const XMLElement
*node
= NULL
);
332 FileLocation ( const FileLocation
& other
);
338 const Project
& project
;
339 const XMLElement
& node
;
340 std::string xmlbuildFile
;
343 std::string extension
;
344 std::string baseaddress
;
346 std::string buildtype
;
348 ImportLibrary
* importLibrary
;
350 Bootsector
* bootSector
;
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
;
367 std::string aliasedModuleName
;
371 FileLocation
*output
; // "path/foo.exe"
372 FileLocation
*dependency
; // "path/foo.exe" or "path/libfoo.a"
373 FileLocation
*install
;
374 std::string description
;
376 std::string layoutId
;
377 std::string layoutNameResId
;
379 Module ( const Project
& project
,
380 const XMLElement
& moduleNode
,
381 const std::string
& modulePath
);
383 ModuleType
GetModuleType ( const std::string
& location
,
384 const XMLAttribute
& attribute
);
385 bool HasImportLibrary () const;
387 std::string
GetPathWithPrefix ( const std::string
& prefix
) const; // "path/prefixfoo.exe"
388 std::string
GetPathToBaseDir() const; // "../" offset to rootdirectory
389 std::string
GetEntryPoint(bool leadingUnderscore
) const;
390 void GetTargets ( string_list
& ) const;
391 std::string
GetInvocationTarget ( const int index
) const;
392 bool HasFileWithExtension ( const IfableData
&, const std::string
& extension
) const;
393 void InvokeModule () const;
396 void SetImportLibrary ( ImportLibrary
* importLibrary
);
397 DirectoryLocation
GetTargetDirectoryTree () const;
398 std::string
GetDefaultModuleExtension () const;
399 std::string
GetDefaultModuleEntrypoint () const;
400 std::string
GetDefaultModuleBaseaddress () const;
401 std::string entrypoint
;
402 void ProcessXMLSubElement ( const XMLElement
& e
,
403 DirectoryLocation directory
,
404 const std::string
& relative_path
,
405 ParseContext
& parseContext
);
412 FileLocation
*directory
;
414 Include ( const Project
& project
,
415 const XMLElement
* includeNode
);
416 Include ( const Project
& project
,
417 const XMLElement
* includeNode
,
418 const Module
* module
);
419 Include ( const Project
& project
,
420 DirectoryLocation directory
,
421 const std::string
& relative_path
);
425 const Project
& project
;
426 const XMLElement
* node
;
427 const Module
* module
;
428 DirectoryLocation
GetDefaultDirectoryTree ( const Module
* module
) const;
435 const Project
& project
;
436 const Module
* module
;
437 const XMLElement
* node
;
443 Define ( const Project
& project
,
444 const XMLElement
& defineNode
);
445 Define ( const Project
& project
,
446 const Module
* module
,
447 const XMLElement
& defineNode
);
448 Define ( const Project
& project
,
449 const Module
* module
,
450 const std::string
& name_
,
451 const std::string
& backend_
= "" );
464 std::string switches
;
465 bool isPreCompiledHeader
;
467 File ( DirectoryLocation directory
,
468 const std::string
& relative_path
,
469 const std::string
& name
,
471 const std::string
& _switches
,
472 bool _isPreCompiledHeader
);
475 std::string
GetFullPath () const;
481 const XMLElement
*node
;
483 const Module
& module
;
485 const Module
* importedModule
;
487 Library ( const XMLElement
& _node
,
488 const Module
& _module
,
489 const std::string
& _name
);
490 Library ( const Module
& _module
,
491 const std::string
& _name
);
500 const XMLElement
& node
;
501 const Module
& module
;
502 const Module
* invokeModule
;
503 std::vector
<InvokeFile
*> input
;
504 std::vector
<InvokeFile
*> output
;
506 Invoke ( const XMLElement
& _node
,
507 const Module
& _module
);
510 void GetTargets ( string_list
& targets
) const;
511 std::string
GetParameters () const;
513 void ProcessXMLSubElement ( const XMLElement
& e
);
514 void ProcessXMLSubElementInput ( const XMLElement
& e
);
515 void ProcessXMLSubElementOutput ( const XMLElement
& e
);
522 const XMLElement
& node
;
524 std::string switches
;
526 InvokeFile ( const XMLElement
& _node
,
527 const std::string
& _name
);
536 const XMLElement
& node
;
537 const Module
& module
;
538 const Module
* dependencyModule
;
540 Dependency ( const XMLElement
& _node
,
541 const Module
& _module
);
549 const XMLElement
& node
;
550 const Module
* module
;
551 const Module
* bootSectorModule
;
553 Bootsector ( const XMLElement
& _node
,
554 const Module
* _module
);
558 bool IsSupportedModuleType ( ModuleType type
);
564 const XMLElement
& node
;
565 const Module
& module
;
567 std::string description
;
569 std::string copyright
;
574 Metadata ( const XMLElement
& _node
,
575 const Module
& _module
);
580 class ImportLibrary
: public XmlNode
583 const Module
* module
;
585 FileLocation
*source
;
587 ImportLibrary ( const Project
& project
,
588 const XMLElement
& node
,
589 const Module
* module
);
597 const Project
& project
;
598 const Module
* module
;
599 const XMLElement
& node
;
601 CompilerType compiler
;
603 CompilerFlag ( const Project
& project
,
604 const XMLElement
& compilerFlagNode
);
605 CompilerFlag ( const Project
& project
,
606 const Module
* module
,
607 const XMLElement
& compilerFlagNode
);
618 const Project
& project
;
619 const Module
* module
;
620 const XMLElement
& node
;
623 LinkerFlag ( const Project
& project
,
624 const XMLElement
& linkerFlagNode
);
625 LinkerFlag ( const Project
& project
,
626 const Module
* module
,
627 const XMLElement
& linkerFlagNode
);
638 const XMLElement
& node
;
639 const Module
& module
;
640 const FileLocation
*file
;
642 LinkerScript ( const XMLElement
& node
,
643 const Module
& module
,
644 const FileLocation
*file
);
653 const Project
& project
;
654 const Module
* module
;
655 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
);
821 static std::string
ReplaceVariable ( const std::string
& name
,
822 const std::string
& value
,
827 class InstallFile
: public XmlNode
830 FileLocation
*source
;
831 FileLocation
*target
;
833 InstallFile ( const Project
& project
,
834 const XMLElement
& bootstrapNode
,
835 const std::string
& path
);
843 const XMLElement
& node
;
844 const Module
& module
;
845 const FileLocation
*file
;
848 const XMLElement
& node
,
849 const Module
& module
,
850 const FileLocation
*file
);
856 class StubbedComponent
859 const Module
* module
;
860 const XMLElement
& node
;
862 std::vector
<StubbedSymbol
*> symbols
;
864 StubbedComponent ( const Module
* module_
,
865 const XMLElement
& stubbedComponentNode
);
866 ~StubbedComponent ();
868 void ProcessXMLSubElement ( const XMLElement
& e
);
875 const XMLElement
& node
;
878 std::string strippedName
;
880 StubbedSymbol ( const XMLElement
& stubbedSymbolNode
);
884 std::string
StripSymbol ( std::string symbol
);
888 class CompilationUnit
893 CompilationUnit ( const File
* file
);
894 CompilationUnit ( const Project
* project
,
895 const Module
* module
,
896 const XMLElement
* node
);
899 bool IsGeneratedFile () const;
900 bool HasFileWithExtension ( const std::string
& extension
) const;
901 bool IsFirstFile () const;
902 const FileLocation
& GetFilename () const;
903 const std::string
& GetSwitches () const;
904 void AddFile ( const File
* file
);
905 const std::vector
<const File
*> GetFiles () const;
907 const Project
* project
;
908 const Module
* module
;
909 const XMLElement
* node
;
910 std::vector
<const File
*> files
;
911 FileLocation
*default_name
;
915 class CompilationUnitSupportCode
918 const Project
& project
;
920 CompilationUnitSupportCode ( const Project
& project
);
921 ~CompilationUnitSupportCode ();
922 void Generate ( bool verbose
);
924 void GenerateForModule ( Module
& module
,
926 std::string
GetCompilationUnitFilename ( Module
& module
,
927 CompilationUnit
& compilationUnit
);
928 void WriteCompilationUnitFile ( Module
& module
,
929 CompilationUnit
& compilationUnit
);
933 enum AutoRegisterType
940 class AutoRegister
: public XmlNode
943 const Module
* module
;
944 std::string infSection
;
945 AutoRegisterType type
;
946 AutoRegister ( const Project
& project_
,
947 const Module
* module_
,
948 const XMLElement
& node_
);
950 bool IsSupportedModuleType ( ModuleType type
);
951 AutoRegisterType
GetAutoRegisterType( const std::string
& type
);
956 class SysSetupGenerator
959 const Project
& project
;
960 SysSetupGenerator ( const Project
& project
);
961 ~SysSetupGenerator ();
964 std::string
GetDirectoryId ( const Module
& module
);
965 std::string
GetFlags ( const Module
& module
);
966 void Generate ( HINF inf
,
967 const Module
& module
);
972 InitializeEnvironment ();
975 Right ( const std::string
& s
, size_t n
);
978 Replace ( const std::string
& s
, const std::string
& find
, const std::string
& with
);
981 ChangeSeparator ( const std::string
& s
,
982 const char fromSeparator
,
983 const char toSeparator
);
986 FixSeparator ( const std::string
& s
);
989 FixSeparatorForSystemCommand ( const std::string
& s
);
992 DosSeparator ( const std::string
& s
);
996 const std::string
& filename
,
997 const std::string
& newExtension
);
1001 const Project
& project
,
1002 const std::string
& location
,
1003 const std::string
& path
,
1004 const std::string
& att_value
);
1007 GetExtension ( const FileLocation
& file
);
1010 NormalizeFilename ( const std::string
& filename
);
1013 ToLower ( std::string filename
);
1015 #endif /* __RBUILD_H */