[RTL]
[reactos.git] / reactos / tools / rbuild / compilationunitsupportcode.cpp
1 /*
2 * Copyright (C) 2005 Casper S. Hornstrup
3 *
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.
8 *
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.
13 *
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.
17 */
18 #include "pch.h"
19 #include <assert.h>
20
21 #include "rbuild.h"
22
23 using std::string;
24 using std::vector;
25
26 CompilationUnitSupportCode::CompilationUnitSupportCode ( const Project& project )
27 : project ( project )
28 {
29 }
30
31 CompilationUnitSupportCode::~CompilationUnitSupportCode ()
32 {
33 }
34
35 void
36 CompilationUnitSupportCode::Generate ( bool verbose )
37 {
38 for( std::map<std::string, Module*>::const_iterator p = project.modules.begin(); p != project.modules.end(); ++ p )
39 {
40 GenerateForModule ( *p->second,
41 verbose );
42 }
43 }
44
45 void
46 CompilationUnitSupportCode::GenerateForModule ( Module& module,
47 bool verbose )
48 {
49 if ( verbose )
50 {
51 printf ( "\nGenerating compilation unit support code for %s",
52 module.name.c_str () );
53 }
54
55 for ( size_t i = 0; i < module.non_if_data.compilationUnits.size () ; i++ )
56 {
57 CompilationUnit& compilationUnit = *module.non_if_data.compilationUnits[i];
58 if ( compilationUnit.GetFiles ().size () <= 1 )
59 continue;
60 WriteCompilationUnitFile ( module, compilationUnit );
61 }
62 }
63
64 string
65 CompilationUnitSupportCode::GetCompilationUnitFilename ( Module& module,
66 CompilationUnit& compilationUnit )
67 {
68 return NormalizeFilename ( Environment::GetIntermediatePath () + sSep + compilationUnit.name );
69 }
70
71 void
72 CompilationUnitSupportCode::WriteCompilationUnitFile ( Module& module,
73 CompilationUnit& compilationUnit )
74 {
75 char* buf;
76 char* s;
77
78 buf = (char*) malloc ( 512*1024 );
79 if ( buf == NULL )
80 throw OutOfMemoryException ();
81
82 s = buf;
83 s = s + sprintf ( s, "/* This file is automatically generated. */\n" );
84 s = s + sprintf ( s, "#define ONE_COMPILATION_UNIT\n" );
85
86 for ( size_t i = 0; i < compilationUnit.GetFiles ().size () ; i++ )
87 {
88 const File& file = *compilationUnit.GetFiles ()[i];
89 s = s + sprintf ( s, "#include <%s/%s>\n", ChangeSeparator ( file.file.relative_path, '\\', '/' ).c_str (), file.file.name.c_str () );
90 }
91
92 s = s + sprintf ( s, "\n" );
93
94 FileSupportCode::WriteIfChanged ( buf, GetCompilationUnitFilename ( module, compilationUnit ) );
95
96 free ( buf );
97 }