c.s.tools.ide.analysis.modernize/src/com/sun/tools/ide/analysis/modernize/options/ClangAnalyzerOptions.java
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/c.s.tools.ide.analysis.modernize/src/com/sun/tools/ide/analysis/modernize/options/ClangAnalyzerOptions.java Wed Jun 07 20:23:29 2017 +0300
1.3 @@ -0,0 +1,169 @@
1.4 +/*
1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
1.6 + *
1.7 + * Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved.
1.8 + *
1.9 + * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
1.10 + * Other names may be trademarks of their respective owners.
1.11 + *
1.12 + * The contents of this file are subject to the terms of either the GNU
1.13 + * General Public License Version 2 only ("GPL") or the Common
1.14 + * Development and Distribution License("CDDL") (collectively, the
1.15 + * "License"). You may not use this file except in compliance with the
1.16 + * License. You can obtain a copy of the License at
1.17 + * http://www.netbeans.org/cddl-gplv2.html
1.18 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
1.19 + * specific language governing permissions and limitations under the
1.20 + * License. When distributing the software, include this License Header
1.21 + * Notice in each file and include the License file at
1.22 + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
1.23 + * particular file as subject to the "Classpath" exception as provided
1.24 + * by Oracle in the GPL Version 2 section of the License file that
1.25 + * accompanied this code. If applicable, add the following below the
1.26 + * License Header, with the fields enclosed by brackets [] replaced by
1.27 + * your own identifying information:
1.28 + * "Portions Copyrighted [year] [name of copyright owner]"
1.29 + *
1.30 + * If you wish your version of this file to be governed by only the CDDL
1.31 + * or only the GPL Version 2, indicate your decision by adding
1.32 + * "[Contributor] elects to include this software in this distribution
1.33 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
1.34 + * single choice of license, a recipient has the option to distribute
1.35 + * your version of this file under either the CDDL, the GPL Version 2 or
1.36 + * to extend the choice of license to its licensees as provided above.
1.37 + * However, if you add GPL Version 2 code and therefore, elected the GPL
1.38 + * Version 2 license, then the option applies only if the new code is
1.39 + * made subject to such option by the copyright holder.
1.40 + *
1.41 + * Contributor(s): Ilia Gromov
1.42 + */
1.43 +
1.44 +package com.sun.tools.ide.analysis.modernize.options;
1.45 +
1.46 +import java.io.File;
1.47 +import java.io.IOException;
1.48 +import java.util.Collections;
1.49 +import org.netbeans.modules.nativeexecution.api.ExecutionEnvironmentFactory;
1.50 +import org.netbeans.modules.nativeexecution.api.HostInfo;
1.51 +import org.netbeans.modules.nativeexecution.api.util.ConnectionManager;
1.52 +import org.netbeans.modules.nativeexecution.api.util.HostInfoUtils;
1.53 +import org.openide.modules.InstalledFileLocator;
1.54 +
1.55 +/**
1.56 + *
1.57 + * @author Ilia Gromov
1.58 + */
1.59 +public class ClangAnalyzerOptions {
1.60 +
1.61 + public static final String CLANG_BINARY_NAME = "clang-tidy"; //NOI18N
1.62 + public static final String CLANG_BINARY_PATH = "clang-tidy-path"; //NOI18N
1.63 +
1.64 + private static String findInPaths() {
1.65 + String binaryName = CLANG_BINARY_NAME + "-" + getCodeBase(); //NOI18N
1.66 + String result = HostInfoUtils.searchFile(ExecutionEnvironmentFactory.getLocal(), Collections.<String>emptyList(), binaryName, true); // NOI18N
1.67 + return result;
1.68 + }
1.69 +
1.70 + public static String getClangAnalyzerPath() {
1.71 + String result = AnalyzerPreferences.getPreferences().get(CLANG_BINARY_PATH, ""); //NOI18N
1.72 + String oldValue = result;
1.73 + if (result.isEmpty()) {
1.74 + String toolPath = System.getProperty("devstudio.clangtidy.path"); //NOI18N
1.75 + if (toolPath != null) {
1.76 + result = toolPath;
1.77 + }
1.78 + }
1.79 + if (result.isEmpty()) {
1.80 + final String codeBase = getCodeBase();
1.81 + String relativePath = String.format("%s/%s-%s", CLANG_BINARY_NAME, CLANG_BINARY_NAME, codeBase); //NOI18N
1.82 + File toolFile = InstalledFileLocator.getDefault().locate(relativePath, codeBase, false);
1.83 + if (toolFile != null && toolFile.exists()) {
1.84 + toolFile.setExecutable(true);
1.85 + System.out.println(toolFile.canExecute());
1.86 + result = toolFile.getAbsolutePath();
1.87 + }
1.88 + }
1.89 + if (result.isEmpty()) {
1.90 + String toolPath = findInPaths();
1.91 + if (toolPath != null) {
1.92 + result = toolPath;
1.93 + }
1.94 + }
1.95 + if (result.isEmpty()) {
1.96 + return null;
1.97 + } else {
1.98 + if (!oldValue.equals(result)) {
1.99 + AnalyzerPreferences.getPreferences().put(CLANG_BINARY_PATH, result);
1.100 + }
1.101 + }
1.102 + return result;
1.103 + }
1.104 +
1.105 + public static String getMissingModuleName() {
1.106 + return "org.netbeans.modules.analysis.clangtidy." + getCodeBase(); //NOI18N
1.107 + }
1.108 +
1.109 + public static String getCodeBase() {
1.110 + HostInfo hostInfo = null;
1.111 + try {
1.112 + hostInfo = HostInfoUtils.getHostInfo(ExecutionEnvironmentFactory.getLocal());
1.113 + } catch (IOException ex) {
1.114 + ex.printStackTrace(System.err);
1.115 + } catch (ConnectionManager.CancellationException ex) {
1.116 + ex.printStackTrace(System.err);
1.117 + }
1.118 + String module = null;
1.119 + if (hostInfo != null) {
1.120 + switch (hostInfo.getOS().getFamily()) {
1.121 + case SUNOS:
1.122 + switch (hostInfo.getCpuFamily()) {
1.123 + case X86:
1.124 + switch (hostInfo.getOS().getBitness()) {
1.125 + case _32:
1.126 + module = "SunOS_x86"; // NOI18N
1.127 + break;
1.128 + case _64:
1.129 + module = "SunOS_x86_64"; // NOI18N
1.130 + break;
1.131 + }
1.132 + break;
1.133 + case SPARC:
1.134 + module = "SunOS_sparc"; // NOI18N
1.135 + break;
1.136 + }
1.137 + break;
1.138 + case LINUX:
1.139 + switch (hostInfo.getOS().getBitness()) {
1.140 + case _32:
1.141 + module = "Linux_x86"; // NOI18N
1.142 + break;
1.143 + case _64:
1.144 + module = "Linux_x86_64"; // NOI18N
1.145 + break;
1.146 + }
1.147 + break;
1.148 + case WINDOWS:
1.149 + switch (hostInfo.getOS().getBitness()) {
1.150 + case _32:
1.151 + module = "Windows_x86"; // NOI18N
1.152 + break;
1.153 + case _64:
1.154 + module = "Windows_x86_64"; // NOI18N
1.155 + break;
1.156 + }
1.157 + break;
1.158 + case MACOSX:
1.159 + switch (hostInfo.getOS().getBitness()) {
1.160 + case _32:
1.161 + module = "MacOSX_x86"; // NOI18N
1.162 + break;
1.163 + case _64:
1.164 + module = "MacOSX_x86_64"; // NOI18N
1.165 + break;
1.166 + }
1.167 + break;
1.168 + }
1.169 + }
1.170 + return module;
1.171 + }
1.172 +}