Option to disable compilation units
[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
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, 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 ( size_t i = 0; i < project.modules.size (); i++ )
39 {
40 GenerateForModule ( *project.modules[i],
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.files.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 if ( module.pch )
86 s = s + sprintf ( s, "#include <%s>\n", ChangeSeparator ( module.pch->file.name, '\\', '/' ).c_str () );
87
88 for ( size_t i = 0; i < compilationUnit.files.size () ; i++ )
89 {
90 File& file = *compilationUnit.files[i];
91 s = s + sprintf ( s, "#include <%s>\n", ChangeSeparator ( file.name, '\\', '/' ).c_str () );
92 }
93
94 s = s + sprintf ( s, "\n" );
95
96 FileSupportCode::WriteIfChanged ( buf, GetCompilationUnitFilename ( module, compilationUnit ) );
97
98 free ( buf );
99 }