001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.cnd.discovery.wizard.bridge;
043:
044: import java.io.File;
045: import java.io.IOException;
046: import java.util.ArrayList;
047: import java.util.HashMap;
048: import java.util.HashSet;
049: import java.util.List;
050: import java.util.Map;
051: import java.util.Set;
052: import java.util.Vector;
053: import javax.swing.SwingUtilities;
054: import org.netbeans.api.project.Project;
055: import org.netbeans.modules.cnd.api.compilers.CompilerSet;
056: import org.netbeans.modules.cnd.api.compilers.CompilerSetManager;
057: import org.netbeans.modules.cnd.api.compilers.Tool;
058: import org.netbeans.modules.cnd.api.utils.IpeUtils;
059: import org.netbeans.modules.cnd.makeproject.api.ProjectGenerator;
060: import org.netbeans.modules.cnd.makeproject.api.compilers.BasicCompiler;
061: import org.netbeans.modules.cnd.makeproject.api.configurations.BasicCompilerConfiguration;
062: import org.netbeans.modules.cnd.makeproject.api.configurations.BooleanConfiguration;
063: import org.netbeans.modules.cnd.makeproject.api.configurations.CCCCompilerConfiguration;
064: import org.netbeans.modules.cnd.makeproject.api.configurations.CCCompilerConfiguration;
065: import org.netbeans.modules.cnd.makeproject.api.configurations.CCompilerConfiguration;
066: import org.netbeans.modules.cnd.makeproject.api.configurations.Configuration;
067: import org.netbeans.modules.cnd.makeproject.api.configurations.ConfigurationDescriptorProvider;
068: import org.netbeans.modules.cnd.makeproject.api.configurations.Folder;
069: import org.netbeans.modules.cnd.makeproject.api.configurations.FolderConfiguration;
070: import org.netbeans.modules.cnd.makeproject.api.configurations.Item;
071: import org.netbeans.modules.cnd.makeproject.api.configurations.ItemConfiguration;
072: import org.netbeans.modules.cnd.makeproject.api.configurations.MakeConfiguration;
073: import org.netbeans.modules.cnd.makeproject.api.configurations.MakeConfigurationDescriptor;
074: import org.netbeans.modules.cnd.makeproject.api.remote.FilePathAdaptor;
075: import org.openide.util.Utilities;
076:
077: /**
078: *
079: * @author Alexander Simon
080: */
081: public class ProjectBridge {
082: private String baseFolder;
083: private MakeConfigurationDescriptor makeConfigurationDescriptor;
084: private Project project;
085: private Set<Project> resultSet = new HashSet<Project>();
086: private Map<String, Item> canonicalItems;
087:
088: public ProjectBridge(Project project) {
089: this .project = project;
090: baseFolder = File.separator
091: + project.getProjectDirectory().getPath();
092: resultSet.add(project);
093: ConfigurationDescriptorProvider pdp = project.getLookup()
094: .lookup(ConfigurationDescriptorProvider.class);
095: makeConfigurationDescriptor = (MakeConfigurationDescriptor) pdp
096: .getConfigurationDescriptor();
097: }
098:
099: public ProjectBridge(String baseFolder) throws IOException {
100: this .baseFolder = baseFolder;
101: MakeConfiguration extConf = new MakeConfiguration(baseFolder,
102: "Default", MakeConfiguration.TYPE_MAKEFILE); // NOI18N
103: String workingDir = baseFolder;
104: String workingDirRel = IpeUtils.toRelativePath(baseFolder,
105: FilePathAdaptor.naturalize(workingDir));
106: workingDirRel = FilePathAdaptor.normalize(workingDirRel);
107: extConf.getMakefileConfiguration().getBuildCommandWorkingDir()
108: .setValue(workingDirRel);
109: project = ProjectGenerator.createBlankProject(
110: "DiscoveryProject", baseFolder,
111: new MakeConfiguration[] { extConf }, true); // NOI18N
112: resultSet.add(project);
113: ConfigurationDescriptorProvider pdp = project.getLookup()
114: .lookup(ConfigurationDescriptorProvider.class);
115: makeConfigurationDescriptor = (MakeConfigurationDescriptor) pdp
116: .getConfigurationDescriptor();
117: }
118:
119: public Folder createFolder(Folder parent, String name) {
120: return new Folder(makeConfigurationDescriptor, parent, name,
121: name, true);
122: }
123:
124: /**
125: * Create new item. Path is converted to relative.
126: */
127: public Item createItem(String path) {
128: return new Item(getRelativepath(path));
129: }
130:
131: /**
132: * Find project item by relative path.
133: */
134: public Item getProjectItem(String path) {
135: Item item = makeConfigurationDescriptor
136: .findProjectItemByPath(path);
137: if (item == null) {
138: if (!IpeUtils.isPathAbsolute(path)) {
139: path = IpeUtils.toAbsolutePath(baseFolder, path);
140: }
141: item = findByCanonicalName(path);
142: }
143: return item;
144: }
145:
146: private Item findByCanonicalName(String path) {
147: if (canonicalItems == null) {
148: canonicalItems = new HashMap<String, Item>();
149: for (Item item : makeConfigurationDescriptor
150: .getProjectItems()) {
151: canonicalItems.put(item.getCanonicalFile()
152: .getAbsolutePath(), item);
153: }
154: }
155: return canonicalItems.get(path);
156: }
157:
158: public Object getAuxObject(Item item) {
159: MakeConfiguration makeConfiguration = (MakeConfiguration) item
160: .getFolder().getConfigurationDescriptor().getConfs()
161: .getActive();
162: ItemConfiguration itemConfiguration = item
163: .getItemConfiguration(makeConfiguration); //ItemConfiguration)makeConfiguration.getAuxObject(ItemConfiguration.getId(item.getPath()));
164: return itemConfiguration;
165: }
166:
167: public void setAuxObject(Item item, Object pao) {
168: if (pao instanceof ItemConfiguration) {
169: ItemConfiguration conf = (ItemConfiguration) pao;
170: MakeConfiguration makeConfiguration = (MakeConfiguration) item
171: .getFolder().getConfigurationDescriptor()
172: .getConfs().getActive();
173: ItemConfiguration itemConfiguration = item
174: .getItemConfiguration(makeConfiguration); //ItemConfiguration)makeConfiguration.getAuxObject(ItemConfiguration.getId(item.getPath()));
175: itemConfiguration.setCCCompilerConfiguration(conf
176: .getCCCompilerConfiguration());
177: itemConfiguration.setCCompilerConfiguration(conf
178: .getCCompilerConfiguration());
179: itemConfiguration.setCustomToolConfiguration(conf
180: .getCustomToolConfiguration());
181: }
182: }
183:
184: /**
185: * Convert absolute path to relative.
186: * Converter does some simplifications:
187: * /some/../ => /
188: * /./ => /
189: */
190: public String getRelativepath(String path) {
191: if (Utilities.isWindows()) {
192: path = path.replace('/', File.separatorChar);
193: }
194: path = IpeUtils.toRelativePath(makeConfigurationDescriptor
195: .getBaseDir(), path);
196: path = FilePathAdaptor.mapToRemote(path);
197: path = cutLocalRelative(path);
198: path = FilePathAdaptor.normalize(path);
199: return path;
200: }
201:
202: private static final String PATTERN_1 = File.separator + "."
203: + File.separator; // NOI18N
204: private static final String PATTERN_2 = File.separator + "."; // NOI18N
205: private static final String PATTERN_3 = File.separator + ".."
206: + File.separator; // NOI18N
207: private static final String PATTERN_4 = File.separator + ".."; // NOI18N
208:
209: private String cutLocalRelative(String path) {
210: String pattern = PATTERN_1;
211: while (true) {
212: int i = path.indexOf(pattern);
213: if (i < 0) {
214: break;
215: }
216: path = path.substring(0, i + 1)
217: + path.substring(i + pattern.length());
218: }
219: pattern = PATTERN_2;
220: if (path.endsWith(pattern)) {
221: path = path.substring(0, path.length() - pattern.length());
222: }
223: pattern = PATTERN_3;
224: while (true) {
225: int i = path.indexOf(pattern);
226: if (i < 0) {
227: break;
228: }
229: int k = -1;
230: for (int j = i - 1; j >= 0; j--) {
231: if (path.charAt(j) == File.separatorChar) {
232: k = j;
233: break;
234: }
235: }
236: if (k < 0) {
237: break;
238: }
239: path = path.substring(0, k + 1)
240: + path.substring(i + pattern.length());
241: }
242: pattern = PATTERN_4;
243: if (path.endsWith(pattern)) {
244: int k = -1;
245: for (int j = path.length() - pattern.length() - 1; j >= 0; j--) {
246: if (path.charAt(j) == File.separatorChar) {
247: k = j;
248: break;
249: }
250: }
251: if (k > 0) {
252: path = path.substring(0, k);
253: }
254: }
255: return path;
256: }
257:
258: public Item[] getAllSources() {
259: return makeConfigurationDescriptor.getProjectItems();
260: }
261:
262: public Folder getRoot() {
263: Folder folder = makeConfigurationDescriptor.getLogicalFolders();
264: Vector sources = folder.getFolders();
265: List<Folder> roots = new ArrayList<Folder>();
266: for (Object o : sources) {
267: Folder sub = (Folder) o;
268: if (sub.isProjectFiles()) {
269: if (MakeConfigurationDescriptor.SOURCE_FILES_FOLDER
270: .equals(sub.getName())) {
271: Vector v = sub.getFolders();
272: for (Object e : v) {
273: Folder s = (Folder) e;
274: if (s.isProjectFiles()) {
275: roots.add(s);
276: }
277: }
278: }
279: }
280: }
281: if (roots.size() > 0) {
282: return roots.get(0);
283: }
284: return folder;
285: }
286:
287: public Set getResult() {
288: makeConfigurationDescriptor.save();
289: if (SwingUtilities.isEventDispatchThread()) {
290: makeConfigurationDescriptor.checkForChangedItems(project,
291: null, null);
292: } else {
293: SwingUtilities.invokeLater(new Runnable() {
294: public void run() {
295: makeConfigurationDescriptor.checkForChangedItems(
296: project, null, null);
297: }
298: });
299: }
300: return resultSet;
301: }
302:
303: public void setupProject(List includes, List macros, boolean isCPP) {
304: Configuration c = makeConfigurationDescriptor.getConfs()
305: .getActive();
306: if (c instanceof MakeConfiguration) {
307: MakeConfiguration extConf = (MakeConfiguration) c;
308: if (isCPP) {
309: extConf.getCCCompilerConfiguration()
310: .getIncludeDirectories().setValue(includes);
311: extConf.getCCCompilerConfiguration()
312: .getPreprocessorConfiguration()
313: .setValue(macros);
314: extConf.getCCCompilerConfiguration()
315: .getIncludeDirectories().setDirty(true);
316: extConf.getCCCompilerConfiguration()
317: .getPreprocessorConfiguration().setDirty(true);
318: } else {
319: extConf.getCCompilerConfiguration()
320: .getIncludeDirectories().setValue(includes);
321: extConf.getCCompilerConfiguration()
322: .getPreprocessorConfiguration()
323: .setValue(macros);
324: extConf.getCCompilerConfiguration()
325: .getIncludeDirectories().setDirty(true);
326: extConf.getCCompilerConfiguration()
327: .getPreprocessorConfiguration().setDirty(true);
328: }
329: }
330: makeConfigurationDescriptor.setModified();
331: }
332:
333: public void setupFolder(List includes, boolean inheriteIncludes,
334: List macros, boolean inheriteMacros, boolean isCPP,
335: Folder folder) {
336: MakeConfiguration makeConfiguration = (MakeConfiguration) folder
337: .getConfigurationDescriptor().getConfs().getActive();
338: //FolderConfiguration folderConfiguration = (FolderConfiguration)makeConfiguration.getAuxObject(folder.getId());
339: FolderConfiguration folderConfiguration = folder
340: .getFolderConfiguration(makeConfiguration);
341: if (folderConfiguration == null) {
342: return;
343: }
344: if (isCPP) {
345: CCCompilerConfiguration ccCompilerConfiguration = folderConfiguration
346: .getCCCompilerConfiguration();
347: if (ccCompilerConfiguration != null) {
348: ccCompilerConfiguration.getIncludeDirectories()
349: .setValue(includes);
350: ccCompilerConfiguration.getInheritIncludes().setValue(
351: inheriteIncludes);
352: ccCompilerConfiguration.getPreprocessorConfiguration()
353: .setValue(macros);
354: ccCompilerConfiguration.getInheritPreprocessor()
355: .setValue(inheriteMacros);
356: }
357: } else {
358: CCompilerConfiguration cCompilerConfiguration = folderConfiguration
359: .getCCompilerConfiguration();
360: if (cCompilerConfiguration != null) {
361: cCompilerConfiguration.getIncludeDirectories()
362: .setValue(includes);
363: cCompilerConfiguration.getInheritIncludes().setValue(
364: inheriteIncludes);
365: cCompilerConfiguration.getPreprocessorConfiguration()
366: .setValue(macros);
367: cCompilerConfiguration.getInheritPreprocessor()
368: .setValue(inheriteMacros);
369: }
370: }
371: }
372:
373: public void setExclude(Item item, boolean exclude) {
374: MakeConfiguration makeConfiguration = (MakeConfiguration) item
375: .getFolder().getConfigurationDescriptor().getConfs()
376: .getActive();
377: ItemConfiguration itemConfiguration = item
378: .getItemConfiguration(makeConfiguration); //ItemConfiguration)makeConfiguration.getAuxObject(ItemConfiguration.getId(item.getPath()));
379: if (itemConfiguration == null) {
380: return;
381: }
382: BooleanConfiguration excl = itemConfiguration.getExcluded();
383: if (excl.getValue() ^ exclude) {
384: excl.setValue(exclude);
385: }
386: //itemConfiguration.setTool(Tool.CustomTool);
387: }
388:
389: public void setHeaderTool(Item item) {
390: MakeConfiguration makeConfiguration = (MakeConfiguration) item
391: .getFolder().getConfigurationDescriptor().getConfs()
392: .getActive();
393: ItemConfiguration itemConfiguration = item
394: .getItemConfiguration(makeConfiguration); //ItemConfiguration)makeConfiguration.getAuxObject(ItemConfiguration.getId(item.getPath()));
395: if (itemConfiguration == null) {
396: return;
397: }
398: if (itemConfiguration.getTool() == Tool.CCCompiler
399: || itemConfiguration.getTool() == Tool.CCompiler) {
400: itemConfiguration.setTool(Tool.CustomTool);
401: }
402: }
403:
404: public void setSourceTool(Item item, boolean isCPP) {
405: MakeConfiguration makeConfiguration = (MakeConfiguration) item
406: .getFolder().getConfigurationDescriptor().getConfs()
407: .getActive();
408: ItemConfiguration itemConfiguration = item
409: .getItemConfiguration(makeConfiguration); //ItemConfiguration)makeConfiguration.getAuxObject(ItemConfiguration.getId(item.getPath()));
410: if (itemConfiguration == null) {
411: return;
412: }
413: if (isCPP) {
414: if (itemConfiguration.getTool() != Tool.CCCompiler) {
415: itemConfiguration.setTool(Tool.CCCompiler);
416: }
417: } else {
418: if (itemConfiguration.getTool() != Tool.CCompiler) {
419: itemConfiguration.setTool(Tool.CCompiler);
420: }
421: }
422: }
423:
424: public void setupFile(String compilepath, List includes,
425: boolean inheriteIncludes, List macros,
426: boolean inheriteMacros, Item item) {
427: MakeConfiguration makeConfiguration = (MakeConfiguration) item
428: .getFolder().getConfigurationDescriptor().getConfs()
429: .getActive();
430: ItemConfiguration itemConfiguration = item
431: .getItemConfiguration(makeConfiguration); //ItemConfiguration)makeConfiguration.getAuxObject(ItemConfiguration.getId(item.getPath()));
432: if (itemConfiguration == null
433: || !itemConfiguration.isCompilerToolConfiguration()) {
434: return;
435: }
436: BooleanConfiguration excl = itemConfiguration.getExcluded();
437: if (excl.getValue()) {
438: excl.setValue(false);
439: }
440: BasicCompilerConfiguration compilerConfiguration = itemConfiguration
441: .getCompilerConfiguration();
442: if (compilerConfiguration instanceof CCCCompilerConfiguration) {
443: CCCCompilerConfiguration cccCompilerConfiguration = (CCCCompilerConfiguration) compilerConfiguration;
444: cccCompilerConfiguration.getIncludeDirectories().setValue(
445: includes);
446: cccCompilerConfiguration.getInheritIncludes().setValue(
447: inheriteIncludes);
448: cccCompilerConfiguration.getPreprocessorConfiguration()
449: .setValue(macros);
450: cccCompilerConfiguration.getInheritPreprocessor().setValue(
451: inheriteMacros);
452: }
453: }
454:
455: private CompilerSet getCompilerSet() {
456: MakeConfiguration makeConfiguration = (MakeConfiguration) makeConfigurationDescriptor
457: .getConfs().getActive();
458: return CompilerSetManager.getDefault().getCompilerSet(
459: makeConfiguration.getCompilerSet().getValue());
460: }
461:
462: public String getCompilerFlavor() {
463: return getCompilerSet().getCompilerFlavor().toString();
464: }
465:
466: public String getCompilerDirectory() {
467: return getCompilerSet().getDirectory();
468: }
469:
470: private List<String> systemIncludePathsC;
471: private List<String> systemIncludePathsCpp;
472:
473: public List<String> getSystemIncludePaths(boolean isCPP) {
474: List<String> systemIncludePaths;
475: if (isCPP) {
476: systemIncludePaths = systemIncludePathsCpp;
477: } else {
478: systemIncludePaths = systemIncludePathsC;
479: }
480: if (systemIncludePaths == null) {
481: systemIncludePaths = new ArrayList<String>();
482: CompilerSet compilerSet = getCompilerSet();
483: BasicCompiler compiler;
484: if (isCPP) {
485: compiler = (BasicCompiler) compilerSet
486: .getTool(Tool.CCCompiler);
487: } else {
488: compiler = (BasicCompiler) compilerSet
489: .getTool(Tool.CCompiler);
490: }
491: for (Object o : compiler.getSystemIncludeDirectories()) {
492: String path = (String) o;
493: systemIncludePaths.add(fixWindowsPath(path));
494: }
495: if (isCPP) {
496: systemIncludePathsCpp = systemIncludePaths;
497: } else {
498: systemIncludePathsC = systemIncludePaths;
499: }
500: }
501: return systemIncludePaths;
502: }
503:
504: private static final String CYG_DRIVE_UNIX = "/cygdrive/"; // NOI18N
505:
506: private String fixWindowsPath(String path) {
507: if (Utilities.isWindows()) {
508: // use unix style path
509: path = path.replace('\\', '/');
510: // fix /cygdrive/d/gcc/bin/../lib/gcc/i686-pc-cygwin/3.4.4/include
511: int i = path.indexOf(CYG_DRIVE_UNIX);
512: if (i >= 0
513: && path.length() > i + CYG_DRIVE_UNIX.length() + 1) {
514: path = Character.toUpperCase(path.charAt(i
515: + CYG_DRIVE_UNIX.length()))
516: + ":" + // NOI18N
517: path.substring(i + CYG_DRIVE_UNIX.length() + 1);
518: }
519: }
520: return path;
521: }
522:
523: private Map<String, String> systemMacroDefinitionsC;
524: private Map<String, String> systemMacroDefinitionsCpp;
525:
526: public Map<String, String> getSystemMacroDefinitions(boolean isCPP) {
527: Map<String, String> systemMacroDefinitions;
528: if (isCPP) {
529: systemMacroDefinitions = systemMacroDefinitionsCpp;
530: } else {
531: systemMacroDefinitions = systemMacroDefinitionsC;
532: }
533: if (systemMacroDefinitions == null) {
534: systemMacroDefinitions = new HashMap<String, String>();
535: CompilerSet compilerSet = getCompilerSet();
536: BasicCompiler compiler;
537: if (isCPP) {
538: compiler = (BasicCompiler) compilerSet
539: .getTool(Tool.CCCompiler);
540: } else {
541: compiler = (BasicCompiler) compilerSet
542: .getTool(Tool.CCompiler);
543: }
544: for (Object o : compiler.getSystemPreprocessorSymbols()) {
545: String macro = (String) o;
546: int i = macro.indexOf('=');
547: if (i > 0) {
548: systemMacroDefinitions.put(macro.substring(0, i),
549: macro.substring(i + 1).trim());
550: } else {
551: systemMacroDefinitions.put(macro, null);
552: }
553: }
554: if (isCPP) {
555: systemMacroDefinitionsCpp = systemMacroDefinitions;
556: } else {
557: systemMacroDefinitionsC = systemMacroDefinitions;
558: }
559: }
560: return systemMacroDefinitions;
561: }
562: }
|