3c146f0246b6f50c867b29d4016ddce03f75824f
[mirror_edk2.git] / Tools / Source / GenBuild / org / tianocore / build / global / OutputManager.java
1 /** @file
2 OutputManager class.
3
4 OutputManager class set output directories for every module by BUILD_MODE.
5
6 Copyright (c) 2006, Intel Corporation
7 All rights reserved. This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
11
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14 **/
15 package org.tianocore.build.global;
16
17 import org.apache.tools.ant.Project;
18 import java.io.File;
19
20 /**
21 OutputManager class is used to setup output directories (BIN_DIR, DEST_DIR_OUTPUT,
22 DEST_DIR_DEBUG).
23
24 @since GenBuild 1.0
25 **/
26 public class OutputManager {
27
28 ///
29 /// means intermediate files will put under Module's dir
30 ///
31 private String MODULE = "MODULE";
32
33 ///
34 /// mean intermediate files will put under a unify dir
35 ///
36 private String UNIFIED = "UNIFIED";
37
38
39 private String userdir;
40
41 private String type;
42 ///
43 /// Singleton Design Pattern
44 ///
45 private static OutputManager object;
46
47 public synchronized static OutputManager getInstance() {
48 if ( object == null ) {
49 object = new OutputManager();
50 }
51 return object;
52 }
53
54 public void setup(String userdir, String type) {
55 this.userdir = userdir;
56 this.type = type;
57 }
58
59 /**
60 Setup BIN_DIR, DEST_DIR_OUTPUT and DEST_DIR_OUTPUT, following are the rules:
61
62 <p>Divide all output files into two types: one is final files, such as FFS
63 file for driver module while LIB file for library module; another is
64 intermediate files, such AutoGen.c, OBJ files, Section files and so on.
65
66 <p>In FPD, OutputDirectory element is used to specify where to put the output
67 files to. There are two mode (MODULE | UNIFIED). MODULE mode means that all
68 output files will put to the module directory while UNIFIED mode means that
69 all output files will put together. Default is UNIFIED mode.
70
71 <p>BUILD_DIR is the base directory for current module build. By default,
72 BUILD_DIR is PLATFORM_DIR/Build in UNIFIED mode while is MODULE_DIR/Build
73 in MODULE mode. Of course, user can customize BUILD_DIR. If user-defined
74 BUILD_DIR is relative path, then look as related to WORKSPACE_DIR.
75
76 <p>Then, BIN_DIR is BUILD_DIR/TARGET/TOOLCHAIN/ARCH;
77
78 <p>FV_DIR is BUILD_DIR/TARGET/TOOLCHAIN/FV;
79
80 <p>DEST_DIR_DEBUG | DEST_DIR_OUTPUT is:
81 BIN_DIR/PACKAGE_RELATIVE_DIR/MODULE_RELATIVE_DIR/DEBUG | OUTPUT
82
83
84 @param project current ANT build Project
85 @param userdir user-defined directory
86 @param type the module build type (MODULE or UNIFIED)
87 **/
88 public void update(Project project) {
89 //
90 // Default mode is UNIFIED.
91 //
92 if (type != null && type.equalsIgnoreCase(MODULE)) {
93 type = MODULE;
94 }
95 else {
96 type = UNIFIED;
97 }
98
99 //
100 // default BUILD_DIR value
101 //
102 String buildDir;
103 if(type.equals(MODULE)){
104 buildDir = project.getProperty("MODULE_DIR") + File.separatorChar + "Build";
105 }
106 else {
107 buildDir = project.getProperty("PLATFORM_DIR") + File.separatorChar + "Build";
108 }
109
110 //
111 // If user define BUILD_DIR
112 //
113 if (userdir != null && ! userdir.equals("")) {
114 File buildFile = new File(userdir);
115 if (buildFile.isAbsolute()){
116 buildDir = userdir;
117 }
118 //
119 // If path is not absolute, then look as related to WORKSPACE_DIR
120 //
121 else {
122 buildDir = GlobalData.getWorkspacePath() + File.separatorChar + userdir;
123 }
124 }
125
126 //
127 // Define BIN_DIR and FV_DIR
128 //
129 String binDir = buildDir + File.separatorChar + project.getProperty("TARGET")
130 + File.separatorChar + project.getProperty("TOOLCHAIN")
131 + File.separatorChar + project.getProperty("ARCH") ;
132
133 String fvDir = buildDir + File.separatorChar + project.getProperty("TARGET")
134 + File.separatorChar + project.getProperty("TOOLCHAIN")
135 + File.separatorChar + "FV";
136
137 //
138 // Define DEST_DIR_OUTPUT and DEST_DIR_DEBUG
139 //
140 String destDir = binDir + File.separatorChar + project.getProperty("PACKAGE_RELATIVE_DIR")
141 + File.separatorChar + project.getProperty("MODULE_RELATIVE_DIR");
142
143 //
144 // Set properties
145 //
146 project.setProperty("BUILD_DIR", buildDir.replaceAll("(\\\\)", "/"));
147 project.setProperty("FV_DIR", fvDir.replaceAll("(\\\\)", "/"));
148 project.setProperty("BIN_DIR", binDir.replaceAll("(\\\\)", "/"));
149 project.setProperty("DEST_DIR_DEBUG", (destDir + File.separatorChar + "DEBUG").replaceAll("(\\\\)", "/"));
150 project.setProperty("DEST_DIR_OUTPUT", (destDir + File.separatorChar + "OUTPUT").replaceAll("(\\\\)", "/"));
151
152 //
153 // Create all directory if necessary
154 //
155 (new File(buildDir)).mkdirs();
156 (new File(fvDir)).mkdirs();
157 (new File(binDir)).mkdirs();
158 (new File(destDir + File.separatorChar + "DEBUG")).mkdirs();
159 (new File(destDir + File.separatorChar + "OUTPUT")).mkdirs();
160 }
161
162 public boolean prepareBuildDir(Project project){
163 boolean isUnified = true;
164
165 if (type.equalsIgnoreCase("MODULE")) {
166 isUnified = false;
167 }
168
169 String buildDir = project.getProperty("PLATFORM_DIR") + File.separatorChar + "Build";
170 //
171 // If user define BUILD_DIR
172 //
173 if (userdir != null && ! userdir.equals("")) {
174 File buildFile = new File(userdir);
175 if (buildFile.isAbsolute()){
176 buildDir = userdir;
177 }
178 //
179 // If path is not absolute, then look as related to WORKSPACE_DIR
180 //
181 else {
182 buildDir = GlobalData.getWorkspacePath() + File.separatorChar + userdir;
183 }
184 }
185
186 //
187 // Set to property
188 //
189 project.setProperty("BUILD_DIR", buildDir.replaceAll("(\\\\)", "/"));
190
191 //
192 // Create all directory if necessary
193 //
194 (new File(buildDir)).mkdirs();
195 return isUnified;
196 }
197
198 }