View Javadoc

1   /*
2    * #%L
3    * Native ARchive plugin for Maven
4    * %%
5    * Copyright (C) 2002 - 2014 NAR Maven Plugin developers.
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * 
11   * http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package com.github.maven_nar.cpptasks.sun;
21  
22  import java.io.File;
23  import java.util.Vector;
24  
25  import com.github.maven_nar.cpptasks.CUtil;
26  import com.github.maven_nar.cpptasks.OptimizationEnum;
27  import com.github.maven_nar.cpptasks.compiler.LinkType;
28  import com.github.maven_nar.cpptasks.compiler.Linker;
29  import com.github.maven_nar.cpptasks.gcc.GccCompatibleCCompiler;
30  
31  /**
32   * Adapter for the Sun (r) Forte (tm) C++ compiler
33   *
34   * @author Curt Arnold
35   */
36  public final class ForteCCCompiler extends GccCompatibleCCompiler {
37    private final static String[] headerExtensions = new String[] {
38        ".h", ".hpp", ".inl"
39    };
40    private final static String[] sourceExtensions = new String[] {
41        ".c", ".cc", ".cxx", ".cpp", ".c++", ".i", ".s"
42    };
43  
44    private static final ForteCCCompiler instance = new ForteCCCompiler("CC", sourceExtensions, headerExtensions);
45  
46    /**
47     * Gets singleton instance of this class
48     */
49    public static ForteCCCompiler getInstance() {
50      return instance;
51    }
52  
53    private String identifier;
54    private File[] includePath;
55  
56    /**
57     * Private constructor. Use ForteCCCompiler.getInstance() to get singleton
58     * instance of this class.
59     */
60    private ForteCCCompiler(final String command, final String[] sourceExtensions, final String[] headerExtensions) {
61      super(command, "-V", sourceExtensions, headerExtensions, false, null, false, null);
62    }
63  
64    @Override
65    public void addImpliedArgs(final Vector<String> args, final boolean debug, final boolean multithreaded,
66        final boolean exceptions, final LinkType linkType, final Boolean rtti, final OptimizationEnum optimization) {
67      args.addElement("-c");
68      if (debug) {
69        args.addElement("-g");
70      }
71      if (optimization != null) {
72        if (optimization.isSpeed()) {
73          args.addElement("-xO2");
74        }
75      }
76      if (rtti != null) {
77        if (rtti.booleanValue()) {
78          args.addElement("-features=rtti");
79        } else {
80          args.addElement("-features=no%rtti");
81        }
82      }
83      if (multithreaded) {
84        args.addElement("-mt");
85      }
86      if (linkType.isSharedLibrary()) {
87        args.addElement("-KPIC");
88      }
89  
90    }
91  
92    @Override
93    public void addWarningSwitch(final Vector<String> args, final int level) {
94      switch (level) {
95        case 0:
96          args.addElement("-w");
97          break;
98        case 1:
99        case 2:
100         break;
101       case 3:
102         args.addElement("+w");
103         break;
104       case 4:
105         args.addElement("+w2");
106         break;
107       case 5:
108         args.addElement("+w2");
109         args.addElement("-xwe");
110     }
111   }
112 
113   @Override
114   public File[] getEnvironmentIncludePath() {
115     if (this.includePath == null) {
116       final File ccLoc = CUtil.getExecutableLocation("CC");
117       if (ccLoc != null) {
118         final File compilerIncludeDir = new File(new File(ccLoc, "../include").getAbsolutePath());
119         if (compilerIncludeDir.exists()) {
120           this.includePath = new File[2];
121           this.includePath[0] = compilerIncludeDir;
122         }
123       }
124       if (this.includePath == null) {
125         this.includePath = new File[1];
126       }
127       this.includePath[this.includePath.length - 1] = new File("/usr/include");
128     }
129     return this.includePath;
130   }
131 
132   @Override
133   public Linker getLinker(final LinkType linkType) {
134     return ForteCCLinker.getInstance().getLinker(linkType);
135   }
136 
137   @Override
138   public int getMaximumCommandLength() {
139     return Integer.MAX_VALUE;
140   }
141 }