4 #include "../../rbuild.h"
6 #include "modulehandler.h"
13 typedef set
<string
> set_string
;
15 map
<ModuleType
,MingwModuleHandler
*>*
16 MingwModuleHandler::handler_map
= NULL
;
18 MingwModuleHandler::directory_set
;
20 MingwModuleHandler::ref
= 0;
23 MingwModuleHandler::fMakefile
= NULL
;
25 MingwModuleHandler::MingwModuleHandler ( ModuleType moduletype
)
28 handler_map
= new map
<ModuleType
,MingwModuleHandler
*>;
29 (*handler_map
)[moduletype
] = this;
32 MingwModuleHandler::~MingwModuleHandler()
42 MingwModuleHandler::PassThruCacheDirectory( const string
&file
) const
44 directory_set
.insert( ReplaceExtension( GetDirectory( file
), "" ) );
49 MingwModuleHandler::SetMakefile ( FILE* f
)
55 MingwModuleHandler::LookupHandler ( const string
& location
,
56 ModuleType moduletype
)
59 throw Exception ( "internal tool error: no registered module handlers" );
60 MingwModuleHandler
* h
= (*handler_map
)[moduletype
];
63 throw UnknownModuleTypeException ( location
, moduletype
);
70 MingwModuleHandler::GetWorkingDirectory () const
76 MingwModuleHandler::GetDirectory ( const string
& filename
) const
78 size_t index
= filename
.find_last_of ( '/' );
79 if (index
== string::npos
)
82 return filename
.substr ( 0, index
);
86 MingwModuleHandler::GetExtension ( const string
& filename
) const
88 size_t index
= filename
.find_last_of ( '/' );
89 if (index
== string::npos
) index
= 0;
90 string tmp
= filename
.substr( index
, filename
.size() - index
);
91 size_t ext_index
= tmp
.find_last_of( '.' );
92 if (ext_index
!= string::npos
)
93 return filename
.substr ( index
+ ext_index
, filename
.size() );
98 MingwModuleHandler::GetBasename ( const string
& filename
) const
100 size_t index
= filename
.find_last_of ( '.' );
101 if (index
!= string::npos
)
102 return filename
.substr ( 0, index
);
107 MingwModuleHandler::ReplaceExtension ( const string
& filename
,
108 const string
& newExtension
) const
110 size_t index
= filename
.find_last_of ( '/' );
111 if (index
== string::npos
) index
= 0;
112 string tmp
= filename
.substr( index
, filename
.size() - index
);
113 size_t ext_index
= tmp
.find_last_of( '.' );
114 if (ext_index
!= string::npos
)
115 return filename
.substr ( 0, index
+ ext_index
) + newExtension
;
116 return filename
+ newExtension
;
120 MingwModuleHandler::GetActualSourceFilename ( const string
& filename
) const
122 string extension
= GetExtension ( filename
);
123 if ( extension
== ".spec" || extension
== "SPEC" )
125 string basename
= GetBasename( filename
);
126 return basename
+ ".stubs.c";
133 MingwModuleHandler::GetModuleArchiveFilename ( const Module
& module
) const
135 return ReplaceExtension ( FixupTargetFilename ( module
.GetPath () ),
140 MingwModuleHandler::IsGeneratedFile ( const File
& file
) const
142 string extension
= GetExtension ( file
.name
);
143 if ( extension
== ".spec" || extension
== "SPEC" )
150 MingwModuleHandler::GetImportLibraryDependencies ( const Module
& module
) const
152 if ( module
.libraries
.size () == 0 )
155 string
dependencies ( "" );
156 for ( size_t i
= 0; i
< module
.libraries
.size (); i
++ )
158 if ( dependencies
.size () > 0 )
160 const Module
* importedModule
= module
.project
.LocateModule ( module
.libraries
[i
]->name
);
161 assert ( importedModule
!= NULL
);
162 dependencies
+= PassThruCacheDirectory ( FixupTargetFilename ( importedModule
->GetDependencyPath () ) ).c_str ();
168 MingwModuleHandler::GetModuleDependencies ( const Module
& module
) const
170 if ( module
.dependencies
.size () == 0 )
173 string
dependencies ( "" );
174 for ( size_t i
= 0; i
< module
.dependencies
.size (); i
++ )
176 if ( dependencies
.size () > 0 )
178 const Dependency
* dependency
= module
.dependencies
[i
];
179 const Module
* dependencyModule
= dependency
->dependencyModule
;
180 dependencies
+= dependencyModule
->GetTargets ();
182 string definitionDependencies
= GetDefinitionDependencies ( module
);
183 if ( dependencies
.length () > 0 && definitionDependencies
.length () > 0 )
184 dependencies
+= " " + definitionDependencies
;
185 else if ( definitionDependencies
.length () > 0 )
186 dependencies
= definitionDependencies
;
191 MingwModuleHandler::GetAllDependencies ( const Module
& module
) const
193 string dependencies
= GetImportLibraryDependencies ( module
);
194 string s
= GetModuleDependencies ( module
);
204 MingwModuleHandler::GetSourceFilenames ( const Module
& module
,
205 bool includeGeneratedFiles
) const
209 string
sourceFilenames ( "" );
210 for ( i
= 0; i
< module
.files
.size (); i
++ )
212 if ( includeGeneratedFiles
|| !IsGeneratedFile ( *module
.files
[i
] ) )
213 sourceFilenames
+= " " + GetActualSourceFilename ( module
.files
[i
]->name
);
215 vector
<If
*> ifs
= module
.ifs
;
216 for ( i
= 0; i
< ifs
.size(); i
++ )
220 for ( j
= 0; j
< rIf
.ifs
.size(); j
++ )
221 ifs
.push_back ( rIf
.ifs
[j
] );
222 for ( j
= 0; j
< rIf
.files
.size(); j
++ )
224 if ( includeGeneratedFiles
|| !IsGeneratedFile ( *rIf
.files
[j
] ) )
225 sourceFilenames
+= " " + GetActualSourceFilename ( rIf
.files
[j
]->name
);
228 return sourceFilenames
;
232 MingwModuleHandler::GetSourceFilenames ( const Module
& module
) const
234 return GetSourceFilenames ( module
,
239 MingwModuleHandler::GetSourceFilenamesWithoutGeneratedFiles ( const Module
& module
) const
241 return GetSourceFilenames ( module
,
246 MingwModuleHandler::GetObjectFilename ( const string
& sourceFilename
) const
249 string extension
= GetExtension ( sourceFilename
);
250 if ( extension
== ".rc" || extension
== ".RC" )
251 newExtension
= ".coff";
252 else if ( extension
== ".spec" || extension
== ".SPEC" )
253 newExtension
= ".stubs.o";
256 return PassThruCacheDirectory (
257 FixupTargetFilename (
258 ReplaceExtension ( sourceFilename
, newExtension
) ) );
262 MingwModuleHandler::GetObjectFilenames ( const Module
& module
) const
264 if ( module
.files
.size () == 0 )
267 string
objectFilenames ( "" );
268 for ( size_t i
= 0; i
< module
.files
.size (); i
++ )
270 if ( objectFilenames
.size () > 0 )
271 objectFilenames
+= " ";
272 objectFilenames
+= GetObjectFilename ( module
.files
[i
]->name
);
274 return objectFilenames
;
278 MingwModuleHandler::GenerateDirectoryTargets() const
280 set_string::iterator i
;
281 fprintf( fMakefile
, "ifneq ($(ROS_INTERMEDIATE),)\ndirectories::" );
283 for ( i
= directory_set
.begin();
284 i
!= directory_set
.end();
287 fprintf ( fMakefile
, " %s", i
->c_str() );
290 fprintf( fMakefile
, "\n\n" );
292 for ( i
= directory_set
.begin();
293 i
!= directory_set
.end();
296 fprintf ( fMakefile
, "%s ", i
->c_str() );
300 "::\n\t${mkdir} $@\n\n"
305 directory_set
.clear();
309 MingwModuleHandler::GenerateGccDefineParametersFromVector ( const vector
<Define
*>& defines
) const
312 for ( size_t i
= 0; i
< defines
.size (); i
++ )
314 Define
& define
= *defines
[i
];
315 if (parameters
.length () > 0)
318 parameters
+= define
.name
;
319 if (define
.value
.length () > 0)
322 parameters
+= define
.value
;
329 MingwModuleHandler::GenerateGccDefineParameters ( const Module
& module
) const
331 string parameters
= GenerateGccDefineParametersFromVector ( module
.project
.defines
);
332 string s
= GenerateGccDefineParametersFromVector ( module
.defines
);
333 if ( s
.length () > 0 )
342 MingwModuleHandler::ConcatenatePaths ( const string
& path1
,
343 const string
& path2
) const
345 if ( ( path1
.length () == 0 ) || ( path1
== "." ) || ( path1
== "./" ) )
347 if ( path1
[path1
.length ()] == CSEP
)
348 return path1
+ path2
;
350 return path1
+ CSEP
+ path2
;
354 MingwModuleHandler::GenerateGccIncludeParametersFromVector ( const vector
<Include
*>& includes
) const
357 for ( size_t i
= 0; i
< includes
.size (); i
++ )
359 Include
& include
= *includes
[i
];
360 if (parameters
.length () > 0)
362 parameters
+= "-I" + include
.directory
;
368 MingwModuleHandler::GenerateGccIncludeParameters ( const Module
& module
) const
370 string parameters
= GenerateGccIncludeParametersFromVector ( module
.includes
);
371 string s
= GenerateGccIncludeParametersFromVector ( module
.project
.includes
);
372 if ( s
.length () > 0 )
382 MingwModuleHandler::GenerateLinkerParametersFromVector ( const vector
<LinkerFlag
*>& linkerFlags
) const
385 for ( size_t i
= 0; i
< linkerFlags
.size (); i
++ )
387 LinkerFlag
& linkerFlag
= *linkerFlags
[i
];
388 if ( parameters
.length () > 0 )
390 parameters
+= linkerFlag
.flag
;
396 MingwModuleHandler::GenerateLinkerParameters ( const Module
& module
) const
398 return GenerateLinkerParametersFromVector ( module
.linkerFlags
);
402 MingwModuleHandler::GenerateMacro ( const char* assignmentOperation
,
404 const vector
<Include
*>& includes
,
405 const vector
<Define
*>& defines
) const
413 assignmentOperation
);
414 for ( i
= 0; i
< includes
.size(); i
++ )
419 includes
[i
]->directory
.c_str() );
421 for ( i
= 0; i
< defines
.size(); i
++ )
423 Define
& d
= *defines
[i
];
428 if ( d
.value
.size() )
434 fprintf ( fMakefile
, "\n" );
438 MingwModuleHandler::GenerateMacros (
439 const char* assignmentOperation
,
440 const vector
<File
*>& files
,
441 const vector
<Include
*>& includes
,
442 const vector
<Define
*>& defines
,
443 const vector
<LinkerFlag
*>* linkerFlags
,
444 const vector
<If
*>& ifs
,
445 const string
& cflags_macro
,
446 const string
& nasmflags_macro
,
447 const string
& windresflags_macro
,
448 const string
& linkerflags_macro
,
449 const string
& objs_macro
) const
453 if ( includes
.size() || defines
.size() )
455 GenerateMacro ( assignmentOperation
,
459 GenerateMacro ( assignmentOperation
,
465 if ( linkerFlags
!= NULL
)
467 string linkerParameters
= GenerateLinkerParametersFromVector ( *linkerFlags
);
468 if ( linkerParameters
.size () > 0 )
473 linkerflags_macro
.c_str (),
475 linkerParameters
.c_str() );
481 for ( i
= 0; i
< files
.size(); i
++ )
483 if ( files
[i
]->first
)
488 GetObjectFilename(files
[i
]->name
).c_str(),
489 objs_macro
.c_str() );
496 assignmentOperation
);
497 for ( i
= 0; i
< files
.size(); i
++ )
499 string extension
= GetExtension ( files
[i
]->name
);
500 if ( extension
!= ".spec"
501 && extension
!= ".SPEC"
502 && !files
[i
]->first
)
507 ( i
%10 == 9 ? "\\\n\t" : " " ),
508 GetObjectFilename(files
[i
]->name
).c_str() );
511 fprintf ( fMakefile
, "\n" );
514 for ( i
= 0; i
< ifs
.size(); i
++ )
517 if ( rIf
.defines
.size() || rIf
.includes
.size() || rIf
.files
.size() || rIf
.ifs
.size() )
521 "ifeq (\"$(%s)\",\"%s\")\n",
522 rIf
.property
.c_str(),
544 MingwModuleHandler::GenerateMacros (
545 const Module
& module
,
546 const string
& cflags_macro
,
547 const string
& nasmflags_macro
,
548 const string
& windresflags_macro
,
549 const string
& linkerflags_macro
,
550 const string
& objs_macro
) const
564 fprintf ( fMakefile
, "\n" );
568 "%s += $(PROJECT_CFLAGS)\n\n",
569 cflags_macro
.c_str () );
573 "%s += $(PROJECT_RCFLAGS)\n\n",
574 windresflags_macro
.c_str () );
578 "%s_LFLAGS += $(PROJECT_LFLAGS)\n\n",
579 module
.name
.c_str () );
583 MingwModuleHandler::GenerateGccCommand ( const Module
& module
,
584 const string
& sourceFilename
,
586 const string
& cflagsMacro
) const
588 string objectFilename
= GetObjectFilename ( sourceFilename
);
591 objectFilename
.c_str (),
592 sourceFilename
.c_str () );
594 "\t%s -c %s -o %s %s\n",
596 sourceFilename
.c_str (),
597 objectFilename
.c_str (),
598 cflagsMacro
.c_str () );
602 MingwModuleHandler::GenerateGccAssemblerCommand ( const Module
& module
,
603 const string
& sourceFilename
,
605 const string
& cflagsMacro
) const
607 string objectFilename
= GetObjectFilename ( sourceFilename
);
610 objectFilename
.c_str (),
611 sourceFilename
.c_str () );
613 "\t%s -x assembler-with-cpp -c %s -o %s -D__ASM__ %s\n",
615 sourceFilename
.c_str (),
616 objectFilename
.c_str (),
617 cflagsMacro
.c_str () );
621 MingwModuleHandler::GenerateNasmCommand ( const Module
& module
,
622 const string
& sourceFilename
,
623 const string
& nasmflagsMacro
) const
625 string objectFilename
= GetObjectFilename ( sourceFilename
);
628 objectFilename
.c_str (),
629 sourceFilename
.c_str () );
631 "\t%s -f win32 %s -o %s %s\n",
633 sourceFilename
.c_str (),
634 objectFilename
.c_str (),
635 nasmflagsMacro
.c_str () );
639 MingwModuleHandler::GenerateWindresCommand ( const Module
& module
,
640 const string
& sourceFilename
,
641 const string
& windresflagsMacro
) const
643 string objectFilename
= GetObjectFilename ( sourceFilename
);
646 objectFilename
.c_str (),
647 sourceFilename
.c_str () );
649 "\t%s %s -o %s ${%s}\n",
651 sourceFilename
.c_str (),
652 objectFilename
.c_str (),
653 windresflagsMacro
.c_str () );
657 MingwModuleHandler::GenerateWinebuildCommands ( const Module
& module
,
658 const string
& sourceFilename
) const
660 string basename
= GetBasename ( sourceFilename
);
664 sourceFilename
.c_str () );
666 "\t%s --def=%s -o %s.spec.def\n",
668 sourceFilename
.c_str (),
674 sourceFilename
.c_str () );
676 "\t%s --pedll=%s -o %s.stubs.c\n",
678 sourceFilename
.c_str (),
683 MingwModuleHandler::GenerateCommands ( const Module
& module
,
684 const string
& sourceFilename
,
686 const string
& cflagsMacro
,
687 const string
& nasmflagsMacro
,
688 const string
& windresflagsMacro
) const
690 string extension
= GetExtension ( sourceFilename
);
691 if ( extension
== ".c" || extension
== ".C" )
693 GenerateGccCommand ( module
,
699 else if ( extension
== ".s" || extension
== ".S" )
701 GenerateGccAssemblerCommand ( module
,
707 else if ( extension
== ".asm" || extension
== ".ASM" )
709 GenerateNasmCommand ( module
,
714 else if ( extension
== ".rc" || extension
== ".RC" )
716 GenerateWindresCommand ( module
,
721 else if ( extension
== ".spec" || extension
== ".SPEC" )
723 GenerateWinebuildCommands ( module
,
725 GenerateGccCommand ( module
,
726 GetActualSourceFilename ( sourceFilename
),
732 throw InvalidOperationException ( __FILE__
,
734 "Unsupported filename extension '%s' in file '%s'",
736 sourceFilename
.c_str () );
740 MingwModuleHandler::GenerateLinkerCommand ( const Module
& module
,
741 const string
& linker
,
742 const string
& linkerParameters
,
743 const string
& objectFilenames
) const
745 string
targetName ( module
.GetTargetName () );
746 string
target ( FixupTargetFilename ( module
.GetPath () ) );
747 string importLibraryDependencies
= GetImportLibraryDependencies ( module
);
748 if ( module
.importLibrary
!= NULL
)
750 static string
ros_junk ( "$(ROS_TEMPORARY)" );
751 string base_tmp
= ros_junk
+ module
.name
+ ".base.tmp";
752 string junk_tmp
= ros_junk
+ module
.name
+ ".junk.tmp";
753 string temp_exp
= ros_junk
+ module
.name
+ ".temp.exp";
756 "\t%s %s -Wl,--base-file,%s -o %s %s %s %s\n",
758 linkerParameters
.c_str (),
761 objectFilenames
.c_str (),
762 importLibraryDependencies
.c_str (),
763 GetLinkerMacro ( module
).c_str () );
770 "\t${dlltool} --dllname %s --base-file %s --def %s --output-exp %s --kill-at\n",
773 ( module
.GetBasePath () + SSEP
+ module
.importLibrary
->definition
).c_str (),
781 "\t%s %s %s -o %s %s %s %s\n\n",
783 linkerParameters
.c_str (),
786 objectFilenames
.c_str (),
787 importLibraryDependencies
.c_str (),
788 GetLinkerMacro ( module
).c_str () );
797 "\t%s %s -o %s %s %s %s\n\n",
799 linkerParameters
.c_str (),
801 objectFilenames
.c_str (),
802 importLibraryDependencies
.c_str (),
803 GetLinkerMacro ( module
).c_str () );
808 MingwModuleHandler::GenerateObjectFileTargets ( const Module
& module
,
809 const vector
<File
*>& files
,
810 const vector
<If
*>& ifs
,
812 const string
& cflagsMacro
,
813 const string
& nasmflagsMacro
,
814 const string
& windresflagsMacro
) const
818 for ( i
= 0; i
< files
.size (); i
++ )
820 string sourceFilename
= files
[i
]->name
;
821 GenerateCommands ( module
,
831 for ( i
= 0; i
< ifs
.size(); i
++ )
833 GenerateObjectFileTargets ( module
,
844 MingwModuleHandler::GenerateObjectFileTargets ( const Module
& module
,
846 const string
& cflagsMacro
,
847 const string
& nasmflagsMacro
,
848 const string
& windresflagsMacro
) const
850 GenerateObjectFileTargets ( module
,
857 fprintf ( fMakefile
, "\n" );
861 MingwModuleHandler::GetCleanTargets ( vector
<string
>& out
,
862 const vector
<File
*>& files
,
863 const vector
<If
*>& ifs
) const
867 for ( i
= 0; i
< files
.size(); i
++ )
868 out
.push_back ( GetObjectFilename(files
[i
]->name
) );
870 for ( i
= 0; i
< ifs
.size(); i
++ )
871 GetCleanTargets ( out
, ifs
[i
]->files
, ifs
[i
]->ifs
);
875 MingwModuleHandler::GenerateArchiveTarget ( const Module
& module
,
877 const string
& objs_macro
) const
879 string archiveFilename
= GetModuleArchiveFilename ( module
);
883 archiveFilename
.c_str (),
884 objs_macro
.c_str ());
887 "\t%s -rc %s %s\n\n",
889 archiveFilename
.c_str (),
890 objs_macro
.c_str ());
892 return archiveFilename
;
896 MingwModuleHandler::GetCFlagsMacro ( const Module
& module
) const
898 return ssprintf ( "$(%s_CFLAGS)",
899 module
.name
.c_str () );
903 MingwModuleHandler::GetObjectsMacro ( const Module
& module
) const
905 return ssprintf ( "$(%s_OBJS)",
906 module
.name
.c_str () );
910 MingwModuleHandler::GetLinkerMacro ( const Module
& module
) const
912 return ssprintf ( "$(%s_LFLAGS)",
913 module
.name
.c_str () );
917 MingwModuleHandler::GenerateMacrosAndTargets (
918 const Module
& module
,
921 const string
* cflags
) const
923 string cflagsMacro
= ssprintf ("%s_CFLAGS", module
.name
.c_str ());
924 string nasmflagsMacro
= ssprintf ("%s_NASMFLAGS", module
.name
.c_str ());
925 string windresflagsMacro
= ssprintf ("%s_RCFLAGS", module
.name
.c_str ());
926 string linkerFlagsMacro
= ssprintf ("%s_LFLAGS", module
.name
.c_str ());
927 string objectsMacro
= ssprintf ("%s_OBJS", module
.name
.c_str ());
929 GenerateMacros ( module
,
936 if ( cflags
!= NULL
)
940 cflagsMacro
.c_str (),
944 // generate phony target for module name
945 fprintf ( fMakefile
, ".PHONY: %s\n",
946 module
.name
.c_str () );
947 fprintf ( fMakefile
, "%s: %s\n\n",
948 module
.name
.c_str (),
949 FixupTargetFilename ( module
.GetPath () ).c_str () );
951 // future references to the macros will be to get their values
952 cflagsMacro
= ssprintf ("$(%s)", cflagsMacro
.c_str ());
953 nasmflagsMacro
= ssprintf ("$(%s)", nasmflagsMacro
.c_str ());
954 objectsMacro
= ssprintf ("$(%s)", objectsMacro
.c_str ());
956 string ar_target
= GenerateArchiveTarget ( module
, ar
, objectsMacro
);
957 GenerateObjectFileTargets ( module
,
963 vector
<string
> clean_files
;
964 clean_files
.push_back ( FixupTargetFilename(module
.GetPath()) );
965 clean_files
.push_back ( ar_target
);
966 GetCleanTargets ( clean_files
, module
.files
, module
.ifs
);
968 fprintf ( fMakefile
, "clean::\n\t-@$(rm)" );
969 for ( size_t i
= 0; i
< clean_files
.size(); i
++ )
972 fprintf ( fMakefile
, " 2>$(NUL)\n\t-@$(rm)" );
973 fprintf ( fMakefile
, " %s", clean_files
[i
].c_str() );
975 fprintf ( fMakefile
, " 2>$(NUL)\n\n" );
979 MingwModuleHandler::GenerateMacrosAndTargetsHost ( const Module
& module
) const
981 GenerateMacrosAndTargets ( module
, "${host_gcc}", "${host_ar}", NULL
);
985 MingwModuleHandler::GenerateMacrosAndTargetsTarget ( const Module
& module
) const
987 GenerateMacrosAndTargetsTarget ( module
,
992 MingwModuleHandler::GenerateMacrosAndTargetsTarget ( const Module
& module
,
993 const string
* clags
) const
995 GenerateMacrosAndTargets ( module
, "${gcc}", "${ar}", clags
);
999 MingwModuleHandler::GetInvocationDependencies ( const Module
& module
) const
1001 string dependencies
;
1002 for ( size_t i
= 0; i
< module
.invocations
.size (); i
++ )
1004 Invoke
& invoke
= *module
.invocations
[i
];
1005 if (invoke
.invokeModule
== &module
)
1006 /* Protect against circular dependencies */
1008 if ( dependencies
.length () > 0 )
1009 dependencies
+= " ";
1010 dependencies
+= invoke
.GetTargets ();
1012 return dependencies
;
1016 MingwModuleHandler::GetInvocationParameters ( const Invoke
& invoke
) const
1018 string
parameters ( "" );
1020 for (i
= 0; i
< invoke
.output
.size (); i
++)
1022 if (parameters
.length () > 0)
1024 InvokeFile
& invokeFile
= *invoke
.output
[i
];
1025 if (invokeFile
.switches
.length () > 0)
1027 parameters
+= invokeFile
.switches
;
1030 parameters
+= invokeFile
.name
;
1033 for (i
= 0; i
< invoke
.input
.size (); i
++)
1035 if (parameters
.length () > 0)
1037 InvokeFile
& invokeFile
= *invoke
.input
[i
];
1038 if (invokeFile
.switches
.length () > 0)
1040 parameters
+= invokeFile
.switches
;
1043 parameters
+= invokeFile
.name
;
1050 MingwModuleHandler::GenerateInvocations ( const Module
& module
) const
1052 if ( module
.invocations
.size () == 0 )
1055 for ( size_t i
= 0; i
< module
.invocations
.size (); i
++ )
1057 const Invoke
& invoke
= *module
.invocations
[i
];
1059 if ( invoke
.invokeModule
->type
!= BuildTool
)
1061 throw InvalidBuildFileException ( module
.node
.location
,
1062 "Only modules of type buildtool can be invoked." );
1065 string invokeTarget
= module
.GetInvocationTarget ( i
);
1066 fprintf ( fMakefile
,
1068 invokeTarget
.c_str () );
1069 fprintf ( fMakefile
,
1071 invokeTarget
.c_str (),
1072 invoke
.GetTargets ().c_str () );
1073 fprintf ( fMakefile
,
1075 invoke
.GetTargets ().c_str (),
1076 FixupTargetFilename ( invoke
.invokeModule
->GetPath () ).c_str () );
1077 fprintf ( fMakefile
,
1079 FixupTargetFilename ( invoke
.invokeModule
->GetPath () ).c_str (),
1080 GetInvocationParameters ( invoke
).c_str () );
1085 MingwModuleHandler::GetPreconditionDependenciesName ( const Module
& module
) const
1087 return ssprintf ( "%s_precondition",
1088 module
.name
.c_str () );
1092 MingwModuleHandler::GeneratePreconditionDependencies ( const Module
& module
) const
1094 string preconditionDependenciesName
= GetPreconditionDependenciesName ( module
);
1095 string sourceFilenames
= GetSourceFilenamesWithoutGeneratedFiles ( module
);
1096 string dependencies
= GetModuleDependencies ( module
);
1097 string s
= GetInvocationDependencies ( module
);
1098 if ( s
.length () > 0 )
1100 if ( dependencies
.length () > 0 )
1101 dependencies
+= " ";
1105 fprintf ( fMakefile
,
1107 preconditionDependenciesName
.c_str () );
1108 fprintf ( fMakefile
,
1110 preconditionDependenciesName
.c_str (),
1111 dependencies
.c_str () );
1112 const char* p
= sourceFilenames
.c_str();
1113 const char* end
= p
+ strlen(p
);
1116 const char* p2
= &p
[512];
1119 while ( p2
> p
&& !isspace(*p2
) )
1123 p2
= strpbrk ( p
, " \t" );
1127 fprintf ( fMakefile
,
1131 preconditionDependenciesName
.c_str ());
1133 p
+= strspn ( p
, " \t" );
1135 fprintf ( fMakefile
, "\n" );
1139 MingwModuleHandler::GenerateImportLibraryTargetIfNeeded ( const Module
& module
) const
1141 if ( module
.importLibrary
!= NULL
)
1143 string definitionDependencies
= GetDefinitionDependencies ( module
);
1144 fprintf ( fMakefile
, "%s: %s\n",
1145 FixupTargetFilename( module
.GetDependencyPath () ).c_str (),
1146 definitionDependencies
.c_str () );
1148 fprintf ( fMakefile
,
1149 "\t${dlltool} --dllname %s --def %s --output-lib %s --kill-at\n\n",
1150 module
.GetTargetName ().c_str (),
1151 ( module
.GetBasePath () + SSEP
+ module
.importLibrary
->definition
).c_str (),
1152 FixupTargetFilename ( module
.GetDependencyPath () ).c_str () );
1157 MingwModuleHandler::GetSpecObjectDependencies ( const string
& filename
) const
1159 string basename
= GetBasename ( filename
);
1160 return basename
+ ".spec.def" + " " + basename
+ ".stubs.c";
1164 MingwModuleHandler::GetDefinitionDependencies ( const Module
& module
) const
1166 string dependencies
;
1167 for ( size_t i
= 0; i
< module
.files
.size (); i
++ )
1169 File
& file
= *module
.files
[i
];
1170 string extension
= GetExtension ( file
.name
);
1171 if ( extension
== ".spec" || extension
== ".SPEC" )
1173 if ( dependencies
.length () > 0 )
1174 dependencies
+= " ";
1175 dependencies
+= GetSpecObjectDependencies ( file
.name
);
1178 return dependencies
;
1182 MingwModuleHandler::GetLinkingDependencies ( const Module
& module
) const
1184 string dependencies
= GetImportLibraryDependencies ( module
);
1185 string s
= GetDefinitionDependencies ( module
);
1186 if ( s
.length () > 0 )
1188 dependencies
+= " ";
1191 return dependencies
;
1195 static MingwBuildToolModuleHandler buildtool_handler
;
1197 MingwBuildToolModuleHandler::MingwBuildToolModuleHandler()
1198 : MingwModuleHandler ( BuildTool
)
1203 MingwBuildToolModuleHandler::Process ( const Module
& module
)
1205 GeneratePreconditionDependencies ( module
);
1206 GenerateBuildToolModuleTarget ( module
);
1207 GenerateInvocations ( module
);
1211 MingwBuildToolModuleHandler::GenerateBuildToolModuleTarget ( const Module
& module
)
1213 string
target ( FixupTargetFilename ( module
.GetPath () ) );
1214 string archiveFilename
= GetModuleArchiveFilename ( module
);
1216 GenerateMacrosAndTargetsHost ( module
);
1218 fprintf ( fMakefile
, "%s: %s\n",
1220 archiveFilename
.c_str () );
1221 fprintf ( fMakefile
,
1222 "\t${host_gcc} %s -o %s %s\n\n",
1223 GetLinkerMacro ( module
).c_str (),
1225 archiveFilename
.c_str () );
1228 static MingwKernelModuleHandler kernelmodule_handler
;
1230 MingwKernelModuleHandler::MingwKernelModuleHandler ()
1231 : MingwModuleHandler ( Kernel
)
1236 MingwKernelModuleHandler::Process ( const Module
& module
)
1238 GeneratePreconditionDependencies ( module
);
1239 GenerateKernelModuleTarget ( module
);
1240 GenerateInvocations ( module
);
1244 MingwKernelModuleHandler::GenerateKernelModuleTarget ( const Module
& module
)
1246 static string
ros_junk ( "$(ROS_TEMPORARY)" );
1247 string
targetName ( module
.GetTargetName () );
1248 string
target ( FixupTargetFilename (module
.GetPath ()) );
1249 string workingDirectory
= GetWorkingDirectory ();
1250 string objectsMacro
= GetObjectsMacro ( module
);
1251 string importLibraryDependencies
= GetImportLibraryDependencies ( module
);
1252 string base_tmp
= ros_junk
+ module
.name
+ ".base.tmp";
1253 string junk_tmp
= ros_junk
+ module
.name
+ ".junk.tmp";
1254 string temp_exp
= ros_junk
+ module
.name
+ ".temp.exp";
1255 string gccOptions
= ssprintf ("-Wl,-T,%s" SSEP
"ntoskrnl.lnk -Wl,--subsystem,native -Wl,--entry,_NtProcessStartup -Wl,--image-base,0xC0000000 -Wl,--file-alignment,0x1000 -Wl,--section-alignment,0x1000 -nostartfiles -mdll",
1256 module
.GetBasePath ().c_str () );
1258 GenerateMacrosAndTargetsTarget ( module
);
1260 GenerateImportLibraryTargetIfNeeded ( module
);
1262 fprintf ( fMakefile
, "%s: %s %s\n",
1264 objectsMacro
.c_str (),
1265 importLibraryDependencies
.c_str () );
1266 fprintf ( fMakefile
,
1267 "\t${gcc} %s %s -Wl,--base-file,%s -o %s %s %s\n",
1268 GetLinkerMacro ( module
).c_str (),
1269 gccOptions
.c_str (),
1272 objectsMacro
.c_str (),
1273 importLibraryDependencies
.c_str () );
1274 fprintf ( fMakefile
,
1276 junk_tmp
.c_str () );
1277 fprintf ( fMakefile
,
1278 "\t${dlltool} --dllname %s --base-file %s --def ntoskrnl/ntoskrnl.def --output-exp %s --kill-at\n",
1279 targetName
.c_str (),
1281 temp_exp
.c_str () );
1282 fprintf ( fMakefile
,
1284 base_tmp
.c_str () );
1285 fprintf ( fMakefile
,
1286 "\t${gcc} %s %s -Wl,%s -o %s %s %s\n",
1287 GetLinkerMacro ( module
).c_str (),
1288 gccOptions
.c_str (),
1291 objectsMacro
.c_str (),
1292 importLibraryDependencies
.c_str () );
1293 fprintf ( fMakefile
,
1295 temp_exp
.c_str () );
1299 static MingwStaticLibraryModuleHandler staticlibrary_handler
;
1301 MingwStaticLibraryModuleHandler::MingwStaticLibraryModuleHandler ()
1302 : MingwModuleHandler ( StaticLibrary
)
1307 MingwStaticLibraryModuleHandler::Process ( const Module
& module
)
1309 GeneratePreconditionDependencies ( module
);
1310 GenerateStaticLibraryModuleTarget ( module
);
1311 GenerateInvocations ( module
);
1315 MingwStaticLibraryModuleHandler::GenerateStaticLibraryModuleTarget ( const Module
& module
)
1317 GenerateMacrosAndTargetsTarget ( module
);
1321 static MingwKernelModeDLLModuleHandler kernelmodedll_handler
;
1323 MingwKernelModeDLLModuleHandler::MingwKernelModeDLLModuleHandler ()
1324 : MingwModuleHandler ( KernelModeDLL
)
1329 MingwKernelModeDLLModuleHandler::Process ( const Module
& module
)
1331 GeneratePreconditionDependencies ( module
);
1332 GenerateKernelModeDLLModuleTarget ( module
);
1333 GenerateInvocations ( module
);
1337 MingwKernelModeDLLModuleHandler::GenerateKernelModeDLLModuleTarget ( const Module
& module
)
1339 static string
ros_junk ( "$(ROS_TEMPORARY)" );
1340 string
target ( FixupTargetFilename ( module
.GetPath () ) );
1341 string workingDirectory
= GetWorkingDirectory ( );
1342 string archiveFilename
= GetModuleArchiveFilename ( module
);
1343 string importLibraryDependencies
= GetImportLibraryDependencies ( module
);
1345 GenerateImportLibraryTargetIfNeeded ( module
);
1347 if ( module
.files
.size () > 0 )
1349 GenerateMacrosAndTargetsTarget ( module
);
1351 fprintf ( fMakefile
, "%s: %s %s\n",
1353 archiveFilename
.c_str (),
1354 importLibraryDependencies
.c_str () );
1356 string
linkerParameters ( "-Wl,--subsystem,native -Wl,--entry,_DriverEntry@8 -Wl,--image-base,0x10000 -Wl,--file-alignment,0x1000 -Wl,--section-alignment,0x1000 -nostartfiles -mdll" );
1357 GenerateLinkerCommand ( module
,
1364 fprintf ( fMakefile
, ".PHONY: %s\n\n",
1366 fprintf ( fMakefile
, "%s:\n",
1372 static MingwKernelModeDriverModuleHandler kernelmodedriver_handler
;
1374 MingwKernelModeDriverModuleHandler::MingwKernelModeDriverModuleHandler ()
1375 : MingwModuleHandler ( KernelModeDriver
)
1380 MingwKernelModeDriverModuleHandler::Process ( const Module
& module
)
1382 GeneratePreconditionDependencies ( module
);
1383 GenerateKernelModeDriverModuleTarget ( module
);
1384 GenerateInvocations ( module
);
1389 MingwKernelModeDriverModuleHandler::GenerateKernelModeDriverModuleTarget ( const Module
& module
)
1391 static string
ros_junk ( "$(ROS_TEMPORARY)" );
1392 string
target ( PassThruCacheDirectory( FixupTargetFilename ( module
.GetPath () ) ) );
1393 string workingDirectory
= GetWorkingDirectory ( );
1394 string archiveFilename
= GetModuleArchiveFilename ( module
);
1395 string importLibraryDependencies
= GetImportLibraryDependencies ( module
);
1397 GenerateImportLibraryTargetIfNeeded ( module
);
1399 if ( module
.files
.size () > 0 )
1401 string
* cflags
= new string ( "-D__NTDRIVER__" );
1402 GenerateMacrosAndTargetsTarget ( module
,
1406 fprintf ( fMakefile
, "%s: %s %s\n",
1408 archiveFilename
.c_str (),
1409 importLibraryDependencies
.c_str () );
1411 string
linkerParameters ( "-Wl,--subsystem,native -Wl,--entry,_DriverEntry@8 -Wl,--image-base,0x10000 -Wl,--file-alignment,0x1000 -Wl,--section-alignment,0x1000 -nostartfiles -mdll" );
1412 GenerateLinkerCommand ( module
,
1419 fprintf ( fMakefile
, ".PHONY: %s\n\n",
1421 fprintf ( fMakefile
, "%s:\n",
1427 static MingwNativeDLLModuleHandler nativedll_handler
;
1429 MingwNativeDLLModuleHandler::MingwNativeDLLModuleHandler ()
1430 : MingwModuleHandler ( NativeDLL
)
1435 MingwNativeDLLModuleHandler::Process ( const Module
& module
)
1437 GeneratePreconditionDependencies ( module
);
1438 GenerateNativeDLLModuleTarget ( module
);
1439 GenerateInvocations ( module
);
1443 MingwNativeDLLModuleHandler::GenerateNativeDLLModuleTarget ( const Module
& module
)
1445 static string
ros_junk ( "$(ROS_TEMPORARY)" );
1446 string
target ( FixupTargetFilename ( module
.GetPath () ) );
1447 string workingDirectory
= GetWorkingDirectory ( );
1448 string objectFilenames
= GetObjectFilenames ( module
);
1449 string archiveFilename
= GetModuleArchiveFilename ( module
);
1450 string importLibraryDependencies
= GetImportLibraryDependencies ( module
);
1452 GenerateImportLibraryTargetIfNeeded ( module
);
1454 if ( module
.files
.size () > 0 )
1457 fprintf ( fMakefile
,
1458 "\t${dlltool} --dllname %s --def %s --output-lib %s --kill-at\n\n",
1459 module
.GetTargetName ().c_str (),
1460 (module
.GetBasePath () + SSEP
+ module
.importLibrary
->definition
).c_str (),
1461 FixupTargetFilename ( module
.GetDependencyPath () ).c_str () );
1464 if (module
.files
.size () > 0)
1466 GenerateMacrosAndTargetsTarget ( module
);
1468 fprintf ( fMakefile
, "%s: %s %s\n",
1470 archiveFilename
.c_str (),
1471 importLibraryDependencies
.c_str () );
1473 string
linkerParameters ( "-Wl,--subsystem,native -Wl,--entry,_DllMainCRTStartup@12 -Wl,--image-base,0x10000 -Wl,--file-alignment,0x1000 -Wl,--section-alignment,0x1000 -nostartfiles -nostdlib -mdll" );
1474 GenerateLinkerCommand ( module
,
1481 fprintf ( fMakefile
, ".PHONY: %s\n\n",
1483 fprintf ( fMakefile
, "%s:\n\n",
1489 static MingwWin32DLLModuleHandler win32dll_handler
;
1491 MingwWin32DLLModuleHandler::MingwWin32DLLModuleHandler ()
1492 : MingwModuleHandler ( Win32DLL
)
1497 MingwWin32DLLModuleHandler::Process ( const Module
& module
)
1499 GenerateExtractWineDLLResourcesTarget ( module
);
1500 GeneratePreconditionDependencies ( module
);
1501 GenerateWin32DLLModuleTarget ( module
);
1502 GenerateInvocations ( module
);
1506 MingwWin32DLLModuleHandler::GenerateExtractWineDLLResourcesTarget ( const Module
& module
)
1508 fprintf ( fMakefile
, ".PHONY: %s_extractresources\n\n",
1509 module
.name
.c_str () );
1510 fprintf ( fMakefile
, "%s_extractresources: bin2res\n",
1511 module
.name
.c_str () );
1512 for ( size_t i
= 0; i
< module
.files
.size (); i
++ )
1514 File
& file
= *module
.files
[i
];
1515 string extension
= GetExtension ( file
.name
);
1516 if ( extension
== ".rc" || extension
== ".RC" )
1518 string resource
= FixupTargetFilename ( file
.name
);
1519 fprintf ( fMakefile
, "\t@echo ${bin2res} -f -x %s\n",
1520 resource
.c_str () );
1523 fprintf ( fMakefile
, "\n");
1527 MingwWin32DLLModuleHandler::GenerateWin32DLLModuleTarget ( const Module
& module
)
1529 static string
ros_junk ( "$(ROS_TEMPORARY)" );
1530 string
target ( FixupTargetFilename ( module
.GetPath () ) );
1531 string workingDirectory
= GetWorkingDirectory ( );
1532 string objectFilenames
= GetObjectFilenames ( module
);
1533 string linkingDependencies
= GetLinkingDependencies ( module
);
1535 GenerateImportLibraryTargetIfNeeded ( module
);
1536 if ( module
.files
.size () > 0 )
1538 GenerateMacrosAndTargetsTarget ( module
);
1540 fprintf ( fMakefile
, "%s: %s %s\n",
1542 objectFilenames
.c_str (),
1543 linkingDependencies
.c_str () );
1545 string
linkerParameters ( "-Wl,--subsystem,console -Wl,--entry,_DllMain@12 -Wl,--image-base,0x10000 -Wl,--file-alignment,0x1000 -Wl,--section-alignment,0x1000 -mdll" );
1546 GenerateLinkerCommand ( module
,
1553 fprintf ( fMakefile
, ".PHONY: %s\n\n",
1555 fprintf ( fMakefile
, "%s:\n\n",
1561 static MingwWin32GUIModuleHandler win32gui_handler
;
1563 MingwWin32GUIModuleHandler::MingwWin32GUIModuleHandler ()
1564 : MingwModuleHandler ( Win32GUI
)
1569 MingwWin32GUIModuleHandler::Process ( const Module
& module
)
1571 GeneratePreconditionDependencies ( module
);
1572 GenerateWin32GUIModuleTarget ( module
);
1573 GenerateInvocations ( module
);
1577 MingwWin32GUIModuleHandler::GenerateWin32GUIModuleTarget ( const Module
& module
)
1579 static string
ros_junk ( "$(ROS_TEMPORARY)" );
1580 string
target ( FixupTargetFilename ( module
.GetPath () ) );
1581 string workingDirectory
= GetWorkingDirectory ( );
1582 string objectFilenames
= GetObjectFilenames ( module
);
1583 string importLibraryDependencies
= GetImportLibraryDependencies ( module
);
1585 GenerateImportLibraryTargetIfNeeded ( module
);
1587 if ( module
.files
.size () > 0 )
1589 GenerateMacrosAndTargetsTarget ( module
);
1591 fprintf ( fMakefile
, "%s: %s %s\n",
1593 objectFilenames
.c_str (),
1594 importLibraryDependencies
.c_str () );
1596 string
linkerParameters ( "-Wl,--subsystem,windows -Wl,--entry,_WinMainCRTStartup -Wl,--image-base,0x00400000 -Wl,--file-alignment,0x1000 -Wl,--section-alignment,0x1000" );
1597 GenerateLinkerCommand ( module
,
1604 fprintf ( fMakefile
, ".PHONY: %s\n\n",
1606 fprintf ( fMakefile
, "%s:\n\n",