|
| 1 | +/******************************************************************************** |
| 2 | + * Copyright (c) 2023, 2024 Renesas Electronics Corp. and others. |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Eclipse Public License 2.0 which is available at |
| 6 | + * http://www.eclipse.org/legal/epl-2.0. |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: EPL-2.0 |
| 9 | + ********************************************************************************/ |
| 10 | +package org.eclipse.cdt.managedbuilder.core.jsoncdb.generator; |
| 11 | + |
| 12 | +import java.util.Collections; |
| 13 | +import java.util.HashMap; |
| 14 | +import java.util.List; |
| 15 | +import java.util.Map; |
| 16 | + |
| 17 | +import org.eclipse.cdt.managedbuilder.core.IConfiguration; |
| 18 | +import org.eclipse.cdt.managedbuilder.core.IToolChain; |
| 19 | +import org.eclipse.cdt.managedbuilder.core.jsoncdb.CompilationDatabaseInformation; |
| 20 | +import org.eclipse.cdt.managedbuilder.core.jsoncdb.ICompilationDatabaseContributor; |
| 21 | +import org.eclipse.core.runtime.Assert; |
| 22 | +import org.eclipse.core.runtime.CoreException; |
| 23 | +import org.eclipse.core.runtime.IConfigurationElement; |
| 24 | +import org.eclipse.core.runtime.IExtension; |
| 25 | +import org.eclipse.core.runtime.IExtensionPoint; |
| 26 | +import org.eclipse.core.runtime.Platform; |
| 27 | +import org.eclipse.core.runtime.Status; |
| 28 | +import org.eclipse.jdt.annotation.NonNull; |
| 29 | + |
| 30 | +final class CompilationDatabaseContributionManager { |
| 31 | + private static final String ATTRIB_RUNNER = "runner"; //$NON-NLS-1$ |
| 32 | + private static final String ATTRIB_TOOLCHAIN_ID = "toolchainID"; //$NON-NLS-1$ |
| 33 | + private static final String ID_COMPILATIONDATABASE = "compilationDatabase"; //$NON-NLS-1$ |
| 34 | + private static final String EXTENSION_ID = "compilationDatabaseContributor"; //$NON-NLS-1$ |
| 35 | + @NonNull |
| 36 | + private final Map<String, ICompilationDatabaseContributor> loadedInstances; |
| 37 | + |
| 38 | + private class EmptyCompilationDatabaseContributor implements ICompilationDatabaseContributor { |
| 39 | + |
| 40 | + @Override |
| 41 | + public final @NonNull List<CompilationDatabaseInformation> getAdditionalFiles(@NonNull IConfiguration config) { |
| 42 | + return Collections.emptyList(); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + private Map<String, IConfigurationElement> factoryExtensions; |
| 47 | + private static CompilationDatabaseContributionManager instance; |
| 48 | + |
| 49 | + private CompilationDatabaseContributionManager() { |
| 50 | + factoryExtensions = new HashMap<>(); |
| 51 | + loadedInstances = new HashMap<>(); |
| 52 | + initalise(); |
| 53 | + } |
| 54 | + |
| 55 | + public static synchronized CompilationDatabaseContributionManager getInstance() { |
| 56 | + if (CompilationDatabaseContributionManager.instance == null) { |
| 57 | + CompilationDatabaseContributionManager.instance = new CompilationDatabaseContributionManager(); |
| 58 | + } |
| 59 | + return CompilationDatabaseContributionManager.instance; |
| 60 | + } |
| 61 | + |
| 62 | + private void initalise() { |
| 63 | + Map<String, IConfigurationElement> loadedExtension = new HashMap<>(); |
| 64 | + |
| 65 | + IExtensionPoint extension = Platform.getExtensionRegistry().getExtensionPoint( |
| 66 | + "org.eclipse.cdt.managedbuilder.core", CompilationDatabaseContributionManager.EXTENSION_ID); //$NON-NLS-1$ |
| 67 | + if (extension != null) { |
| 68 | + IExtension[] extensions = extension.getExtensions(); |
| 69 | + for (IExtension extension2 : extensions) { |
| 70 | + IConfigurationElement[] configElements = extension2.getConfigurationElements(); |
| 71 | + for (IConfigurationElement configElement : configElements) { |
| 72 | + if (CompilationDatabaseContributionManager.ID_COMPILATIONDATABASE.equals(configElement.getName())) { // $NON-NLS-1$ |
| 73 | + String toolchainId = configElement |
| 74 | + .getAttribute(CompilationDatabaseContributionManager.ATTRIB_TOOLCHAIN_ID); |
| 75 | + String className = configElement |
| 76 | + .getAttribute(CompilationDatabaseContributionManager.ATTRIB_RUNNER); |
| 77 | + if (toolchainId != null && className != null) { |
| 78 | + loadedExtension.put(toolchainId, configElement); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + factoryExtensions = loadedExtension; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Get the Code Assist tool with the specified id |
| 90 | + * |
| 91 | + * @param id |
| 92 | + * @return Tool extension or a list with no element if not found. |
| 93 | + * @throws CoreException |
| 94 | + */ |
| 95 | + |
| 96 | + @NonNull |
| 97 | + public synchronized ICompilationDatabaseContributor getCompilationDatabaseContributor( |
| 98 | + @NonNull IConfiguration config) { |
| 99 | + IToolChain toolChain = config.getToolChain(); |
| 100 | + while (toolChain != null) { |
| 101 | + String toolchainId = toolChain.getBaseId(); |
| 102 | + if (loadedInstances.containsKey(toolchainId)) { |
| 103 | + ICompilationDatabaseContributor contributor = loadedInstances.get(toolchainId); |
| 104 | + Assert.isNotNull(contributor); |
| 105 | + return contributor; |
| 106 | + } else if (factoryExtensions.containsKey(toolchainId)) { |
| 107 | + return createCdbInstance(toolchainId); |
| 108 | + } else { |
| 109 | + toolChain = toolChain.getSuperClass(); |
| 110 | + } |
| 111 | + } |
| 112 | + return new EmptyCompilationDatabaseContributor(); |
| 113 | + } |
| 114 | + |
| 115 | + @NonNull |
| 116 | + private ICompilationDatabaseContributor createCdbInstance(String toolchainId) { |
| 117 | + IConfigurationElement ele = factoryExtensions.get(toolchainId); |
| 118 | + if (ele != null) { |
| 119 | + ICompilationDatabaseContributor loaded = null; |
| 120 | + try { |
| 121 | + loaded = (ICompilationDatabaseContributor) ele |
| 122 | + .createExecutableExtension(CompilationDatabaseContributionManager.ATTRIB_RUNNER); |
| 123 | + |
| 124 | + } catch (CoreException e) { |
| 125 | + Platform.getLog(getClass()).log(Status.error("Not able to create instance", e)); //$NON-NLS-1$ |
| 126 | + } |
| 127 | + if (loaded == null) { |
| 128 | + loaded = new EmptyCompilationDatabaseContributor(); |
| 129 | + } |
| 130 | + loadedInstances.put(toolchainId, loaded); |
| 131 | + return loaded; |
| 132 | + } |
| 133 | + |
| 134 | + return new EmptyCompilationDatabaseContributor(); |
| 135 | + } |
| 136 | + |
| 137 | +} |
0 commit comments