1 /*
2  * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  *
23  */
24 
25 package build.tools.projectcreator;
26 
27 import static java.nio.file.FileVisitResult.CONTINUE;
28 
29 import java.io.IOException;
30 import java.nio.file.FileSystems;
31 import java.nio.file.FileVisitResult;
32 import java.nio.file.Files;
33 import java.nio.file.Path;
34 import java.nio.file.attribute.BasicFileAttributes;
35 import java.util.Stack;
36 import java.util.Vector;
37 
38 public class FileTreeCreatorVC10 extends FileTreeCreator {
39 
FileTreeCreatorVC10(Path startDir, Vector<BuildConfig> allConfigs, WinGammaPlatformVC10 wg)40       public FileTreeCreatorVC10(Path startDir, Vector<BuildConfig> allConfigs, WinGammaPlatformVC10 wg) {
41          super(startDir, allConfigs, wg);
42       }
43 
44       @Override
visitFile(Path file, BasicFileAttributes attr)45       public FileVisitResult visitFile(Path file, BasicFileAttributes attr) {
46          DirAttributes currentFileAttr = attributes.peek().clone();
47          boolean usePch = false;
48          boolean disablePch = false;
49          boolean useIgnore = false;
50          boolean isAltSrc = false;  // only needed as a debugging crumb
51          boolean isReplacedByAltSrc = false;
52          String fileName = file.getFileName().toString();
53 
54          // TODO hideFile
55 
56          // usePch applies to all configs for a file.
57          if (fileName.equals(BuildConfig.getFieldString(null, "UseToGeneratePch"))) {
58             usePch = true;
59          }
60 
61          String fileLoc = vcProjLocation.relativize(file).toString();
62 
63          // isAltSrc and isReplacedByAltSrc applies to all configs for a file
64          if (BuildConfig.matchesRelativeAltSrcInclude(
65                file.toAbsolutePath().toString())) {
66             // current file is an alternate source file so track it
67             isAltSrc = true;
68             BuildConfig.trackRelativeAltSrcFile(
69                 file.toAbsolutePath().toString());
70          } else if (BuildConfig.matchesRelativeAltSrcFile(
71                     file.toAbsolutePath().toString())) {
72             // current file is a regular file that matches an alternate
73             // source file so yack about replacing the regular file
74             isReplacedByAltSrc = true;
75             System.out.println("INFO: alternate source file '" +
76                                BuildConfig.getMatchingRelativeAltSrcFile(
77                                    file.toAbsolutePath().toString()) +
78                                "' replaces '" + fileLoc + "'");
79          }
80 
81          for (BuildConfig cfg : allConfigs) {
82             if (cfg.lookupHashFieldInContext("IgnoreFile", fileName) != null) {
83                useIgnore = true;
84                currentFileAttr.setIgnore(cfg);
85             } else if (cfg.matchesIgnoredPath(file.toAbsolutePath().toString())) {
86                useIgnore = true;
87                currentFileAttr.setIgnore(cfg);
88             }
89 
90             if (cfg.lookupHashFieldInContext("DisablePch", fileName) != null) {
91                disablePch = true;
92                currentFileAttr.setDisablePch(cfg);
93             }
94 
95             Vector<String> rv = new Vector<String>();
96             cfg.collectRelevantVectors(rv, "AdditionalFile");
97             for(String addFile : rv) {
98                if (addFile.equals(fileName)) {
99                   // supress any ignore
100                   // TODO - may need some adjustments
101                   if (file.toAbsolutePath().toString().contains(cfg.get("Flavour"))) {
102                      currentFileAttr.removeFromIgnored(cfg);
103                   }
104                }
105             }
106          }
107 
108          String tagName = wg10.getFileTagFromSuffix(fileName);
109 
110          if (!useIgnore && !disablePch && !usePch && !isReplacedByAltSrc) {
111             wg.tag(tagName, new String[] { "Include", fileLoc});
112          } else {
113             wg.startTag(
114                   tagName,
115                   new String[] { "Include", fileLoc});
116 
117             for (BuildConfig cfg : allConfigs) {
118                boolean ignore = currentFileAttr.hasIgnore(cfg);
119                if (ignore) {
120                   wg.tagData("ExcludedFromBuild", "true", "Condition", "'$(Configuration)|$(Platform)'=='" + cfg.get("Name") + "'");
121                }
122                if (usePch) {
123                   wg.tagData("PrecompiledHeader", "Create", "Condition", "'$(Configuration)|$(Platform)'=='" + cfg.get("Name") + "'");
124                }
125                if (disablePch) {
126                   wg.tag("PrecompiledHeader", "Condition", "'$(Configuration)|$(Platform)'=='" + cfg.get("Name") + "'");
127                }
128                if (isReplacedByAltSrc) {
129                   wg.tagData("ExcludedFromBuild", "true", "Condition",
130                              "'$(Configuration)|$(Platform)'=='" +
131                              cfg.get("Name") + "'");
132                }
133             }
134             wg.endTag();
135          }
136 
137          String filter = startDir.relativize(file.getParent().toAbsolutePath()).toString();
138          wg10.addFilterDependency(fileLoc, filter);
139 
140          return CONTINUE;
141       }
142 
143       @Override
preVisitDirectory(Path path, BasicFileAttributes attrs)144       public FileVisitResult preVisitDirectory(Path path, BasicFileAttributes attrs)
145             throws IOException {
146          Boolean hide = false;
147          // TODO remove attrs, if path is matched in this dir, then it is too in every subdir.
148          // And we will check anyway
149          DirAttributes newAttr = attributes.peek().clone();
150 
151          // check per config ignorePaths!
152          for (BuildConfig cfg : allConfigs) {
153             if (cfg.matchesIgnoredPath(path.toAbsolutePath().toString())) {
154                newAttr.setIgnore(cfg);
155             }
156 
157             // Hide is always on all configs. And additional files are never hiddden
158             if (cfg.matchesHidePath(path.toAbsolutePath().toString())) {
159                hide = true;
160                break;
161             }
162          }
163 
164          if (!hide) {
165             String name = startDir.relativize(path.toAbsolutePath()).toString();
166             if (!"".equals(name)) {
167                wg10.addFilter(name);
168             }
169 
170             attributes.push(newAttr);
171             return super.preVisitDirectory(path, attrs);
172          } else {
173             return FileVisitResult.SKIP_SUBTREE;
174          }
175       }
176 
177       @Override
postVisitDirectory(Path dir, IOException exc)178       public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
179          //end matching attributes set by ignorepath
180          attributes.pop();
181          return CONTINUE;
182       }
183 
184       @Override
visitFileFailed(Path file, IOException exc)185       public FileVisitResult visitFileFailed(Path file, IOException exc) {
186          return CONTINUE;
187       }
188 
writeFileTree()189       public void writeFileTree() throws IOException {
190          Files.walkFileTree(this.startDir, this);
191       }
192 }
193