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 along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 "9.00"
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
,
151 enum DependenciesType
154 AutomaticDependencies
,
184 DependenciesType Dependencies
;
185 bool CheckDependenciesForModuleOnly
;
186 bool CompilationUnitsEnabled
;
187 bool PrecompiledHeadersEnabled
;
188 std::string CheckDependenciesForModuleOnlyModule
;
189 std::string VSProjectVersion
;
190 std::string VSConfigurationType
;
191 bool UseVSVersionInPath
;
192 bool UseConfigurationInPath
;
193 bool MakeHandlesInstallDirectories
;
194 bool GenerateProxyMakefilesInSourceTree
;
196 CompilerSet Compiler
;
203 static std::string
GetVariable ( const std::string
& name
);
204 static std::string
GetArch ();
205 static std::string
GetIntermediatePath ();
206 static std::string
GetOutputPath ();
207 static std::string
GetSourcePath ();
208 static std::string
GetCdOutputPath ();
209 static std::string
GetInstallPath ();
210 static std::string
GetAutomakeFile ( const std::string
& defaultFile
);
211 static std::string
GetEnvironmentVariablePathOrDefault ( const std::string
& name
,
212 const std::string
& defaultValue
);
216 class FileSupportCode
219 static void WriteIfChanged ( char* outbuf
,
220 const std::string
& filename
,
221 bool ignoreError
= false );
228 CompilationUnit
* compilationUnit
;
236 std::vector
<CompilationUnit
*> compilationUnits
;
237 std::vector
<File
*> files
;
238 std::vector
<Include
*> includes
;
239 std::vector
<Define
*> defines
;
240 std::vector
<Library
*> libraries
;
241 std::map
<std::string
, Property
*> properties
;
242 std::vector
<Module
*> modules
;
243 std::vector
<CompilerFlag
*> compilerFlags
;
244 int asmFiles
; // number of .asm files in compilationUnits
249 void ExtractModules( std::map
<std::string
, Module
*> &modules
);
255 XMLElement
*node
, *head
;
258 const Configuration
& configuration
;
260 std::string makefile
;
261 XMLIncludes xmlbuildfiles
;
262 std::vector
<LinkerFlag
*> linkerFlags
;
263 std::vector
<CDFile
*> cdfiles
;
264 std::vector
<InstallFile
*> installfiles
;
265 std::map
<std::string
, Module
*> modules
;
266 IfableData non_if_data
;
267 IfableData host_non_if_data
;
269 bool allowWarningsSet
;
271 Project ( const Configuration
& configuration
,
272 const std::string
& filename
,
273 const std::map
<std::string
, std::string
>* properties
= NULL
);
275 void SetBackend ( Backend
* backend
) { _backend
= backend
; }
276 Backend
& GetBackend() { return *_backend
; }
277 void ExecuteInvocations ();
279 void ProcessXML ( const std::string
& path
);
280 Module
* LocateModule ( const std::string
& name
);
281 const Module
* LocateModule ( const std::string
& name
) const;
282 const std::string
& GetProjectFilename () const;
283 std::string
ResolveProperties ( const std::string
& s
) const;
284 const Property
* LookupProperty ( const std::string
& name
) const;
285 std::string
GetCompilerSet () const;
286 std::string
GetLinkerSet () const;
288 std::string
ResolveNextProperty ( const std::string
& s
) const;
290 void ProcessXMLSubElement ( const XMLElement
& e
,
291 const std::string
& path
,
292 ParseContext
& parseContext
);
294 // disable copy semantics
295 Project ( const Project
& );
296 Project
& operator = ( const Project
& );
333 TypeDontCare
, // always at the end
360 DirectoryLocation directory
;
361 std::string relative_path
;
364 FileLocation ( const DirectoryLocation directory
,
365 const std::string
& relative_path
,
366 const std::string
& name
,
367 const XMLElement
*node
= NULL
);
369 FileLocation ( const FileLocation
& other
);
375 const Project
& project
;
376 const XMLElement
& node
;
377 std::string xmlbuildFile
;
380 std::string extension
;
381 std::string baseaddress
;
383 std::string buildtype
;
385 ImportLibrary
* importLibrary
;
386 ImportLibrary
* delayImportLibrary
;
388 Bootsector
* bootSector
;
390 bool isDefaultEntryPoint
;
391 Bootstrap
* bootstrap
;
392 AutoRegister
* autoRegister
; // <autoregister> node
393 IfableData non_if_data
;
394 std::vector
<Invoke
*> invocations
;
395 std::vector
<Dependency
*> dependencies
;
396 std::vector
<CompilerFlag
*> compilerFlags
;
397 std::vector
<LinkerFlag
*> linkerFlags
;
398 std::vector
<StubbedComponent
*> stubbedComponents
;
399 std::vector
<CDFile
*> cdfiles
;
400 LinkerScript
* linkerScript
;
404 std::string aliasedModuleName
;
411 FileLocation
*output
; // "path/foo.exe"
412 FileLocation
*dependency
; // "path/foo.exe" or "path/libfoo.a"
413 FileLocation
*install
;
414 std::string description
;
416 std::string layoutId
;
417 std::string layoutNameResId
;
419 Module ( const Project
& project
,
420 const XMLElement
& moduleNode
,
421 const std::string
& modulePath
);
423 ModuleType
GetModuleType ( const std::string
& location
,
424 const XMLAttribute
& attribute
);
425 bool HasImportLibrary () const;
427 std::string
GetPathWithPrefix ( const std::string
& prefix
) const; // "path/prefixfoo.exe"
428 std::string
GetPathToBaseDir() const; // "../" offset to rootdirectory
429 std::string
GetEntryPoint() const;
430 void GetTargets ( string_list
& ) const;
431 std::string
GetInvocationTarget ( const int index
) const;
432 bool HasFileWithExtension ( const IfableData
&, const std::string
& extension
) const;
433 void InvokeModule () const;
435 std::string
GetDllName() const;
436 SpecFileType
IsSpecDefinitionFile () const;
438 void SetImportLibrary ( ImportLibrary
* importLibrary
);
439 void SetDelayImportLibrary ( ImportLibrary
* importLibrary
);
440 DirectoryLocation
GetTargetDirectoryTree () const;
441 std::string
GetDefaultModuleExtension () const;
442 std::string
GetDefaultModuleEntrypoint () const;
443 std::string
GetDefaultModuleBaseaddress () const;
444 std::string
GetDefaultModuleCRT () const;
445 bool GetDefaultModuleIsCRT () const;
446 std::string entrypoint
;
447 void ProcessXMLSubElement ( const XMLElement
& e
,
448 DirectoryLocation directory
,
449 const std::string
& relative_path
,
450 ParseContext
& parseContext
);
451 bool GetBooleanAttribute ( const XMLElement
& moduleNode
,
453 bool default_value
= false );
456 class ToolsetDirective
462 void ParseToolsets ( const Project
& project
, const XMLElement
& node
);
465 virtual ~ToolsetDirective() { }
466 bool IsEnabled () const;
469 class CompilerDirective
472 std::bitset
<CompilerTypesCount
> compilersSet
;
476 void ParseCompilers ( const XMLElement
& node
, const std::string
& defaultValue
);
479 CompilerDirective (): enabled ( true ) { }
480 virtual ~CompilerDirective() { }
481 void SetCompiler ( CompilerType compiler
);
482 void UnsetCompiler ( CompilerType compiler
);
483 void SetAllCompilers ();
484 void UnsetAllCompilers ();
485 bool IsCompilerSet ( CompilerType compiler
) const;
488 class Include
: public CompilerDirective
, public ToolsetDirective
491 FileLocation
*directory
;
493 Include ( const Project
& project
,
494 const XMLElement
* includeNode
);
495 Include ( const Project
& project
,
496 const XMLElement
* includeNode
,
497 const Module
* module
);
498 Include ( const Project
& project
,
499 DirectoryLocation directory
,
500 const std::string
& relative_path
);
504 const Project
& project
;
505 const XMLElement
* node
;
506 const Module
* module
;
507 DirectoryLocation
GetDefaultDirectoryTree ( const Module
* module
) const;
512 class Define
: public CompilerDirective
, public ToolsetDirective
515 const Project
& project
;
516 const Module
* module
;
517 const XMLElement
* node
;
519 std::string arguments
;
524 Define ( const Project
& project
,
525 const XMLElement
& defineNode
);
526 Define ( const Project
& project
,
527 const Module
* module
,
528 const XMLElement
& defineNode
);
529 Define ( const Project
& project
,
530 const Module
* module
,
531 const std::string
& name_
,
532 const std::string
& backend_
= "",
533 bool redefine_
= false );
546 std::string switches
;
547 bool isPreCompiledHeader
;
549 File ( DirectoryLocation directory
,
550 const std::string
& relative_path
,
551 const std::string
& name
,
553 const std::string
& _switches
,
554 bool _isPreCompiledHeader
);
557 std::string
GetFullPath () const;
563 const XMLElement
*node
;
565 const Module
& module
;
567 const Module
* importedModule
;
570 Library ( const XMLElement
& _node
,
571 const Module
& _module
,
572 const std::string
& _name
);
573 Library ( const Module
& _module
,
574 const std::string
& _name
);
583 const XMLElement
& node
;
584 const Module
& module
;
585 const Module
* invokeModule
;
586 std::vector
<InvokeFile
*> input
;
587 std::vector
<InvokeFile
*> output
;
589 Invoke ( const XMLElement
& _node
,
590 const Module
& _module
);
593 void GetTargets ( string_list
& targets
) const;
594 std::string
GetParameters () const;
596 void ProcessXMLSubElement ( const XMLElement
& e
);
597 void ProcessXMLSubElementInput ( const XMLElement
& e
);
598 void ProcessXMLSubElementOutput ( const XMLElement
& e
);
605 const XMLElement
& node
;
607 std::string switches
;
609 InvokeFile ( const XMLElement
& _node
,
610 const std::string
& _name
);
619 const XMLElement
& node
;
620 const Module
& module
;
621 const Module
* dependencyModule
;
623 Dependency ( const XMLElement
& _node
,
624 const Module
& _module
);
632 const XMLElement
& node
;
633 const Module
* module
;
634 const Module
* bootSectorModule
;
636 Bootsector ( const XMLElement
& _node
,
637 const Module
* _module
);
641 bool IsSupportedModuleType ( ModuleType type
);
647 const XMLElement
& node
;
648 const Module
& module
;
650 std::string description
;
652 std::string copyright
;
657 Metadata ( const XMLElement
& _node
,
658 const Module
& _module
);
663 class ImportLibrary
: public XmlNode
666 const Module
* module
;
668 FileLocation
*source
;
669 FileLocation
*target
;
671 ImportLibrary ( const Project
& project
,
672 const XMLElement
& node
,
673 const Module
* module
,
679 class CompilerFlag
: public CompilerDirective
, public ToolsetDirective
682 const Project
& project
;
683 const Module
* module
;
684 const XMLElement
& node
;
687 CompilerFlag ( const Project
& project
,
688 const XMLElement
& compilerFlagNode
);
689 CompilerFlag ( const Project
& project
,
690 const Module
* module
,
691 const XMLElement
& compilerFlagNode
);
699 class LinkerFlag
: public ToolsetDirective
702 const Project
& project
;
703 const Module
* module
;
704 const XMLElement
& node
;
707 LinkerFlag ( const Project
& project
,
708 const XMLElement
& linkerFlagNode
);
709 LinkerFlag ( const Project
& project
,
710 const Module
* module
,
711 const XMLElement
& linkerFlagNode
);
722 const XMLElement
& node
;
723 const Module
& module
;
724 const FileLocation
*file
;
726 LinkerScript ( const XMLElement
& node
,
727 const Module
& module
,
728 const FileLocation
*file
);
737 const Project
& project
;
738 const Module
* module
;
739 std::string name
, value
;
742 Property ( const XMLElement
& node_
,
743 const Project
& project_
,
744 const Module
* module_
);
746 Property ( const Project
& project_
,
747 const Module
* module_
,
748 const std::string
& name_
,
749 const std::string
& value_
);
755 class TestSupportCode
758 const Project
& project
;
760 TestSupportCode ( const Project
& project
);
762 void GenerateTestSupportCode ( bool verbose
);
764 bool IsTestModule ( const Module
& module
);
765 void GenerateTestSupportCodeForModule ( Module
& module
,
767 std::string
GetHooksFilename ( Module
& module
);
768 char* WriteStubbedSymbolToHooksFile ( char* buffer
,
769 const StubbedComponent
& component
,
770 const StubbedSymbol
& symbol
);
771 char* WriteStubbedComponentToHooksFile ( char* buffer
,
772 const StubbedComponent
& component
);
773 void WriteHooksFile ( Module
& module
);
774 std::string
GetStubsFilename ( Module
& module
);
775 char* WriteStubbedSymbolToStubsFile ( char* buffer
,
776 const StubbedComponent
& component
,
777 const StubbedSymbol
& symbol
,
779 char* WriteStubbedComponentToStubsFile ( char* buffer
,
780 const StubbedComponent
& component
,
782 void WriteStubsFile ( Module
& module
);
783 std::string
GetStartupFilename ( Module
& module
);
784 bool IsUnknownCharacter ( char ch
);
785 std::string
GetTestDispatcherName ( std::string filename
);
786 bool IsTestFile ( std::string
& filename
) const;
787 void GetSourceFilenames ( string_list
& list
,
788 Module
& module
) const;
789 char* WriteTestDispatcherPrototypesToStartupFile ( char* buffer
,
791 char* WriteRegisterTestsFunctionToStartupFile ( char* buffer
,
793 void WriteStartupFile ( Module
& module
);
800 SourceFile ( AutomaticDependency
* automaticDependency
,
801 const Module
& module
,
803 SourceFile
* parent
);
805 std::vector
<SourceFile
*> files
; /* List of files included in this file */
807 AutomaticDependency
* automaticDependency
;
808 const Module
& module
;
809 std::vector
<SourceFile
*> parents
; /* List of files, this file is included from */
810 time_t lastWriteTime
;
811 time_t youngestLastWriteTime
; /* Youngest last write time of this file and all children */
812 SourceFile
* youngestFile
;
816 void SkipWhitespace ();
817 bool ReadInclude ( std::string
& filename
,
818 bool& searchCurrentDirectory
,
820 bool IsIncludedFrom ( const File
& file
);
821 SourceFile
* ParseFile(const File
& file
);
822 bool CanProcessFile ( const File
& file
);
823 bool IsParentOf ( const SourceFile
* parent
,
824 const SourceFile
* child
);
831 class AutomaticDependency
833 friend class SourceFileTest
;
835 const Project
& project
;
837 AutomaticDependency ( const Project
& project
);
838 ~AutomaticDependency ();
839 bool LocateIncludedFile ( const FileLocation
& directory
,
840 const std::string
& includedFilename
);
841 bool LocateIncludedFile ( SourceFile
* sourceFile
,
842 const Module
& module
,
843 const std::string
& includedFilename
,
844 bool searchCurrentDirectory
,
846 File
& resolvedFile
);
847 SourceFile
* RetrieveFromCacheOrParse ( const Module
& module
,
849 SourceFile
* parentSourceFile
);
850 SourceFile
* RetrieveFromCache ( const File
& file
);
851 void CheckAutomaticDependencies ( bool verbose
);
852 void CheckAutomaticDependenciesForModule ( Module
& module
,
855 void GetModulesToCheck ( Module
& module
, std::vector
<const Module
*>& modules
);
856 void CheckAutomaticDependencies ( const Module
& module
,
858 void CheckAutomaticDependenciesForFile ( SourceFile
* sourceFile
);
859 void GetIncludeDirectories ( std::vector
<Include
*>& includes
,
860 const Module
& module
);
861 void GetModuleFiles ( const Module
& module
,
862 std::vector
<File
*>& files
) const;
864 void ParseFiles ( const Module
& module
);
865 void ParseFile ( const Module
& module
,
867 std::map
<std::string
, SourceFile
*> sourcefile_map
;
874 const Project
& project
;
875 const Module
* module
;
876 const XMLElement
& node
;
878 std::string nameoncd
;
880 Bootstrap ( const Project
& project
,
881 const Module
* module
,
882 const XMLElement
& bootstrapNode
);
886 bool IsSupportedModuleType ( ModuleType type
);
888 static std::string
ReplaceVariable ( const std::string
& name
,
889 const std::string
& value
,
894 class CDFile
: public XmlNode
897 FileLocation
*source
;
898 FileLocation
*target
;
900 CDFile ( const Project
& project
,
901 const XMLElement
& bootstrapNode
,
902 const std::string
& path
);
905 static std::string
ReplaceVariable ( const std::string
& name
,
906 const std::string
& value
,
911 class InstallFile
: public XmlNode
914 FileLocation
*source
;
915 FileLocation
*target
;
917 InstallFile ( const Project
& project
,
918 const XMLElement
& bootstrapNode
,
919 const std::string
& path
);
927 const XMLElement
& node
;
928 const Module
& module
;
929 const FileLocation
*file
;
932 const XMLElement
& node
,
933 const Module
& module
,
934 const FileLocation
*file
);
940 class StubbedComponent
943 const Module
* module
;
944 const XMLElement
& node
;
946 std::vector
<StubbedSymbol
*> symbols
;
948 StubbedComponent ( const Module
* module_
,
949 const XMLElement
& stubbedComponentNode
);
950 ~StubbedComponent ();
952 void ProcessXMLSubElement ( const XMLElement
& e
);
959 const XMLElement
& node
;
962 std::string strippedName
;
964 StubbedSymbol ( const XMLElement
& stubbedSymbolNode
);
968 std::string
StripSymbol ( std::string symbol
);
972 class CompilationUnit
977 CompilationUnit ( const File
* file
);
978 CompilationUnit ( const Project
* project
,
979 const Module
* module
,
980 const XMLElement
* node
);
983 bool IsGeneratedFile () const;
984 bool HasFileWithExtension ( const std::string
& extension
) const;
985 bool IsFirstFile () const;
986 const FileLocation
& GetFilename () const;
987 const std::string
& GetSwitches () const;
988 void AddFile ( const File
* file
);
989 const std::vector
<const File
*> GetFiles () const;
991 const Project
* project
;
992 const Module
* module
;
993 const XMLElement
* node
;
994 std::vector
<const File
*> files
;
995 FileLocation
*default_name
;
999 class CompilationUnitSupportCode
1002 const Project
& project
;
1004 CompilationUnitSupportCode ( const Project
& project
);
1005 ~CompilationUnitSupportCode ();
1006 void Generate ( bool verbose
);
1008 void GenerateForModule ( Module
& module
,
1010 std::string
GetCompilationUnitFilename ( Module
& module
,
1011 CompilationUnit
& compilationUnit
);
1012 void WriteCompilationUnitFile ( Module
& module
,
1013 CompilationUnit
& compilationUnit
);
1017 enum AutoRegisterType
1024 class AutoRegister
: public XmlNode
1027 const Module
* module
;
1028 std::string infSection
;
1029 AutoRegisterType type
;
1030 AutoRegister ( const Project
& project_
,
1031 const Module
* module_
,
1032 const XMLElement
& node_
);
1034 bool IsSupportedModuleType ( ModuleType type
);
1035 AutoRegisterType
GetAutoRegisterType( const std::string
& type
);
1040 class SysSetupGenerator
1043 const Project
& project
;
1044 SysSetupGenerator ( const Project
& project
);
1045 ~SysSetupGenerator ();
1048 std::string
GetDirectoryId ( const Module
& module
);
1049 std::string
GetFlags ( const Module
& module
);
1050 void Generate ( HINF inf
,
1051 const Module
& module
);
1056 InitializeEnvironment ();
1059 Right ( const std::string
& s
, size_t n
);
1062 Replace ( const std::string
& s
, const std::string
& find
, const std::string
& with
);
1065 ChangeSeparator ( const std::string
& s
,
1066 const char fromSeparator
,
1067 const char toSeparator
);
1070 FixSeparator ( const std::string
& s
);
1073 FixSeparatorForSystemCommand ( const std::string
& s
);
1076 DosSeparator ( const std::string
& s
);
1080 const std::string
& filename
,
1081 const std::string
& newExtension
);
1085 const Project
& project
,
1086 const std::string
& location
,
1087 const std::string
& path
,
1088 const std::string
& att_value
);
1091 GetExtension ( const std::string
& filename
);
1094 GetExtension ( const FileLocation
& file
);
1097 NormalizeFilename ( const std::string
& filename
);
1100 ToLower ( std::string filename
);