Path: blob/master/sourcetools/com.ibm.uma/com/ibm/uma/om/Module.java
6004 views
/*******************************************************************************1* Copyright (c) 2001, 2017 IBM Corp. and others2*3* This program and the accompanying materials are made available under4* the terms of the Eclipse Public License 2.0 which accompanies this5* distribution and is available at https://www.eclipse.org/legal/epl-2.0/6* or the Apache License, Version 2.0 which accompanies this distribution and7* is available at https://www.apache.org/licenses/LICENSE-2.0.8*9* This Source Code may also be made available under the following10* Secondary Licenses when the conditions for such availability set11* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU12* General Public License, version 2 with the GNU Classpath13* Exception [1] and GNU General Public License, version 2 with the14* OpenJDK Assembly Exception [2].15*16* [1] https://www.gnu.org/software/classpath/license.html17* [2] http://openjdk.java.net/legal/assembly-exception.html18*19* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception20*******************************************************************************/21package com.ibm.uma.om;22import java.util.Hashtable;23import java.util.Vector;2425import com.ibm.uma.UMA;26import com.ibm.uma.UMAException;2728public class Module extends PredicateList {29Vector<Artifact> artifacts = new Vector<Artifact>();30Hashtable<String,Exports> exports = new Hashtable<String, Exports>();31Hashtable<String, Objects> objects = new Hashtable<String, Objects>();32Hashtable<String, Flags> flags = new Hashtable<String, Flags>();33String modulePath;34String fullName;35boolean rootModule = false;36int moduleDepth;3738public Module(String modulePath) {39super(modulePath);40this.modulePath = modulePath;4142String rootDir = UMA.getUma().getRootDirectory();43String cleanedPath = modulePath.replace("\\", "/");44String[] pathParts = cleanedPath.substring(rootDir.length()).split("/");45if ( pathParts.length == 1 || pathParts[pathParts.length-2].equals("")) {46rootModule = true;47this.fullName = "";48this.moduleDepth = 0;49} else {50this.fullName = modulePath.substring(rootDir.length(),modulePath.length()-UMA.getUma().getConfiguration().getMetadataFilename().length()-1);51this.moduleDepth = pathParts.length-1;52}5354//System.out.println("modulePath " + modulePath + "\nfullName " + this.fullName + "\nmoduleDepth " + this.moduleDepth );55}5657public void addArtifact(Artifact artifact) throws UMAException {58if ( artifact.getType() == Artifact.TYPE_SUBDIR ) {59artifacts.insertElementAt(artifact,0);60} else {61artifacts.add(artifact);62}63}6465public void addExports(String group, Exports exps) {66if ( exports.containsKey(group) ) {67exports.get(group).addExports(exps);68} else {69exports.put(group, exps);70}71}7273public void addObjects(String group, Objects objs) {74objects.put(group, objs);75}7677public void addFlags(String group, Flags f) {78flags.put(group, f);79}8081public boolean isSet(String moduleProperty) {82return false;83}8485public String getValue(String moduleProperty) {86return "";87}8889public boolean isInPhase(int phase) throws UMAException {90for ( int j=0; j<artifacts.size(); j++ ) {91Artifact artifact = artifacts.elementAt(j);92if ( !artifact.evaluate()) continue;93if ( artifact.isInPhase(phase) ) return true;94}95return false;96}9798public boolean containsArtifact(String artifactName) throws UMAException {99if ( artifactName == null ) return false;100for ( Artifact artifact : artifacts ) {101if ( !artifact.evaluate() ) continue;102switch (artifact.getType()) {103case Artifact.TYPE_BUNDLE:104case Artifact.TYPE_SHARED:105case Artifact.TYPE_EXECUTABLE:106case Artifact.TYPE_STATIC:107if ( artifact.getTargetName().equals(artifactName)) return true;108break;109case Artifact.TYPE_SUBDIR:110SubdirArtifact subdirArtifact = (SubdirArtifact) artifact;111if ( subdirArtifact.getSubdirModule().containsArtifact(artifactName) ) return true;112break;113}114115}116return false;117}118119public boolean containsNonStaticArtifacts() throws UMAException {120for ( Artifact artifact : artifacts ) {121if ( !artifact.evaluate() ) continue;122switch (artifact.getType()) {123case Artifact.TYPE_EXECUTABLE: //Fall-Thru124case Artifact.TYPE_SHARED: //Fall-Thru125case Artifact.TYPE_BUNDLE:126return true;127case Artifact.TYPE_SUBDIR:128SubdirArtifact subdirArtifact = (SubdirArtifact) artifact;129if ( subdirArtifact.getSubdirModule().containsNonStaticArtifacts() ) return true;130break;131}132133}134return false;135}136137public Vector<Artifact> getArtifacts() {138return artifacts;139}140141public Vector<Artifact> getSubdirArtifacts() throws UMAException {142Vector<Artifact> subdirs = new Vector<Artifact>();143boolean artifactNotAdded = true;144int iterations = 0;145while( artifactNotAdded ) {146artifactNotAdded = false;147iterations ++;148if ( iterations > artifacts.size() ) {149throw new UMAException("Error: dependency loop detected in tree rooted at " + this.getFullName());150}151for ( Artifact a : artifacts ) {152if ( a.getType() != Artifact.TYPE_SUBDIR ) continue;153if ( subdirs.contains(a) ) continue;154int index = -2;155for ( String lib : a.getAllLibrariesInTree()) {156for ( Artifact b : artifacts ) {157if ( b == a || b.getType() != Artifact.TYPE_SUBDIR ) continue;158if ( b.isLibDefinedInTree(lib) ) {159// A library used by the current subdir 'a' is defined160// inside of subdir 'b', thus 'a' goes before 'b'.161// determine the index of 'b' and see if we are already162// scheduled to go in earlier.163int index2 = subdirs.indexOf(b);164if ( (index2 > index && index != -1) || index2 == -1 ) {165index = index2;166}167//System.out.println( a.getTargetName() + " [" + index2 + "] depends on " + b.getTargetName());168}169}170}171if ( index == -1 ) {172// artifact has dependencies not yet inserted.173artifactNotAdded = true;174} else if ( index == -2 ) {175// Found no dependencies, so insert a end176subdirs.insertElementAt(a, 0);177} else {178// insert before any dependencies179subdirs.insertElementAt(a, index+1);180}181}182}183return subdirs;184}185186public Vector<Artifact> getStaticArtifacts() throws UMAException {187Vector<Artifact> statics = new Vector<Artifact>();188for ( Artifact a : artifacts ) {189if ( a.getType() == Artifact.TYPE_STATIC) {190statics.add(a);191}192}193return statics;194}195196public Vector<Artifact> getSharedArtifacts() throws UMAException {197Vector<Artifact> shareds = new Vector<Artifact>();198for ( Artifact a : artifacts ) {199if ( a.getType() == Artifact.TYPE_SHARED || a.getType() == Artifact.TYPE_BUNDLE) {200shareds.add(a);201}202}203return shareds;204}205206public Vector<Artifact> getExecutableArtifacts() throws UMAException {207Vector<Artifact> executables = new Vector<Artifact>();208for ( Artifact a : artifacts ) {209if ( a.getType() == Artifact.TYPE_EXECUTABLE) {210executables.add(a);211}212}213return executables;214}215216public Hashtable<String,Exports> getExports() {217return exports;218}219220public Hashtable<String,Objects> getObjects() {221return objects;222}223224public Hashtable<String,Flags> getFlags() {225return flags;226}227228public String getModulePath() {229return modulePath;230}231232public String getFullName() {233return fullName;234}235236public String getLogicalFullName() {237if (rootModule) return "root";238return "root/" + fullName;239}240241public int getModuleDepth() {242return moduleDepth;243}244245public String moduleNameAtDepth(int depth) {246String[] moduleNames = getLogicalFullName().split("/");247return moduleNames[depth];248}249250public boolean isTopLevel() {251if ( rootModule ) return true;252return false;253}254255public String getTopLevelName() {256return moduleNameAtDepth(0);257}258259public String[] getParents() {260int modDepth = getModuleDepth();261if ( modDepth == 0 ) return null;262String[] parents = new String[modDepth];263String parent = getTopLevelName();264parents[0] = parent;265for ( int depth = 1; depth < modDepth; depth++ ) {266parent = parent + "/" + moduleNameAtDepth(depth);267parents[depth] = parent;268}269return parents;270}271272public boolean containsEnabledArtifacts() throws UMAException {273for ( Artifact artifact : artifacts ) {274if ( !artifact.evaluate() ) continue;275if ( artifact.getType() == Artifact.TYPE_SUBDIR ) {276SubdirArtifact subdirArtifact = (SubdirArtifact) artifact;277if ( subdirArtifact.getSubdirModule().containsEnabledArtifacts() ) return true;278} else {279return true;280}281}282return false;283284}285286public boolean requiresDispatchMakefile() throws UMAException {287int numEnabledArtifacts = 0;288for ( Artifact artifact : artifacts ) {289if ( !artifact.evaluate() ) continue;290if ( artifact.getType() == Artifact.TYPE_SUBDIR ) {291SubdirArtifact subdirArtifact = (SubdirArtifact) artifact;292if ( subdirArtifact.getSubdirModule().containsEnabledArtifacts() ) return true;293} else {294numEnabledArtifacts ++;295}296}297if ( numEnabledArtifacts > 1 ) return true;298return false;299}300301public Vector<Artifact> getAllArtifactsInTree() throws UMAException {302Vector<Artifact> allArtifacts = new Vector<Artifact>();303for ( Artifact artifact : artifacts ) {304if ( !artifact.evaluate() ) continue;305if (artifact.getType() == Artifact.TYPE_REFERENCE) continue;306if (artifact.getType() == Artifact.TYPE_SUBDIR) {307Module subdirModule = ((SubdirArtifact)artifact).getSubdirModule();308allArtifacts.addAll(subdirModule.getAllArtifactsInTree());309} else {310allArtifacts.add(artifact);311}312}313return allArtifacts;314}315316public String[] getAllLibrariesInTree() {317Vector<String> allLibs = new Vector<String>();318for ( Artifact artifact : artifacts ) {319for ( String lib : artifact.getAllLibrariesInTree() ) {320allLibs.add(lib);321}322}323String[] libs = new String[allLibs.size()];324allLibs.toArray(libs);325return libs;326}327328public boolean isLibDefinedInTree( String lib ) {329for ( Artifact artifact : artifacts ) {330if ( artifact.isLibDefinedInTree(lib) ) return true;331}332return false;333}334}335336337338