]> git.proxmox.com Git - mirror_edk2.git/blobdiff - Tools/Java/Source/Cpptasks/net/sf/antcontrib/cpptasks/userdefine/CommandLineUserDefine.java
Restructuring for better separation of Tool packages.
[mirror_edk2.git] / Tools / Java / Source / Cpptasks / net / sf / antcontrib / cpptasks / userdefine / CommandLineUserDefine.java
diff --git a/Tools/Java/Source/Cpptasks/net/sf/antcontrib/cpptasks/userdefine/CommandLineUserDefine.java b/Tools/Java/Source/Cpptasks/net/sf/antcontrib/cpptasks/userdefine/CommandLineUserDefine.java
new file mode 100644 (file)
index 0000000..56e82de
--- /dev/null
@@ -0,0 +1,269 @@
+/*\r
+ * \r
+ * Copyright 2001-2004 The Ant-Contrib project\r
+ *\r
+ *  Licensed under the Apache License, Version 2.0 (the "License");\r
+ *  you may not use this file except in compliance with the License.\r
+ *  You may obtain a copy of the License at\r
+ *\r
+ *      http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ *  Unless required by applicable law or agreed to in writing, software\r
+ *  distributed under the License is distributed on an "AS IS" BASIS,\r
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ *  See the License for the specific language governing permissions and\r
+ *  limitations under the License.\r
+ */\r
+package net.sf.antcontrib.cpptasks.userdefine;\r
+\r
+import java.io.File;\r
+import java.util.Iterator;\r
+import java.util.LinkedHashSet;\r
+import java.util.Map;\r
+import java.util.Set;\r
+import java.util.StringTokenizer;\r
+import java.util.Vector;\r
+\r
+import net.sf.antcontrib.cpptasks.CCTask;\r
+import net.sf.antcontrib.cpptasks.CUtil;\r
+import net.sf.antcontrib.cpptasks.types.CommandLineArgument;\r
+import net.sf.antcontrib.cpptasks.types.ConditionalFileSet;\r
+\r
+import org.apache.tools.ant.BuildException;\r
+import org.apache.tools.ant.DirectoryScanner;\r
+import org.apache.tools.ant.Project;\r
+import org.apache.tools.ant.types.Environment;\r
+import org.apache.tools.ant.types.Path;\r
+import org.apache.tools.ant.types.Environment.Variable;\r
+\r
+/**\r
+ * \r
+ */\r
+public class CommandLineUserDefine {\r
+\r
+    String includePathDelimiter = null;\r
+\r
+    String outputDelimiter = null;\r
+    \r
+    private static String pathName = null;\r
+    \r
+    public void command(CCTask cctask, UserDefineDef userdefine) {\r
+        boolean isGccCommand = userdefine.getFamily().equalsIgnoreCase("GCC");\r
+        File workdir;\r
+        Project project = cctask.getProject();\r
+        if (userdefine.getWorkdir() == null) {\r
+            workdir = new File(".");\r
+        } else {\r
+            workdir = userdefine.getWorkdir();\r
+        }\r
+\r
+        //\r
+        // generate cmdline= command + args + includepath + endargs + outfile\r
+        // \r
+        Vector args = new Vector();\r
+        Vector argsWithoutSpace = new Vector();\r
+        Vector endargs = new Vector();\r
+        Vector endargsWithoutSpace = new Vector();\r
+        Vector includePath = new Vector();\r
+\r
+        //\r
+        // get Args.\r
+        //\r
+        CommandLineArgument[] argument = userdefine.getActiveProcessorArgs();\r
+        for (int j = 0; j < argument.length; j++) {\r
+            if (argument[j].getLocation() == 0) {\r
+                args.addElement(argument[j].getValue());\r
+            } else {\r
+                endargs.addElement(argument[j].getValue());\r
+            }\r
+        }\r
+\r
+        //\r
+        // get include path.\r
+        //\r
+        String[] incPath = userdefine.getActiveIncludePaths();\r
+        for (int j = 0; j < incPath.length; j++) {\r
+            includePath.addElement(includePathDelimiter + incPath[j]);\r
+        }\r
+\r
+        //\r
+        // Remove space in args and endargs.\r
+        //\r
+        for (int i = 0; i < args.size(); i++) {\r
+            String str = (String) args.get(i);\r
+            StringTokenizer st = new StringTokenizer(str, " \t");\r
+            while (st.hasMoreTokens()) {\r
+                argsWithoutSpace.addElement(st.nextToken());\r
+            }\r
+        }\r
+        for (int i = 0; i < endargs.size(); i++) {\r
+            String str = (String) endargs.get(i);\r
+            StringTokenizer st = new StringTokenizer(str, " \t");\r
+            while (st.hasMoreTokens()) {\r
+                endargsWithoutSpace.addElement(st.nextToken());\r
+            }\r
+        }\r
+\r
+        int cmdLen = 0;\r
+        //\r
+        // command + args + endargs + includepath + sourcefile\r
+        //\r
+        cmdLen = 1 + argsWithoutSpace.size() + endargsWithoutSpace.size()\r
+                        + includePath.size() + 1;\r
+        String[] libSet = userdefine.getLibset();\r
+        if (libSet != null && libSet.length > 0) {\r
+            cmdLen = cmdLen + libSet.length;\r
+            if (isGccCommand) {\r
+                cmdLen += 2; // we need -( and -) to group libs for GCC\r
+            }\r
+        }\r
+\r
+        //\r
+        // In gcc the "cr" flag should follow space then add outputfile name,\r
+        // otherwise\r
+        // it will pop error.\r
+        // TBD\r
+        if (outputDelimiter != null && userdefine.getOutputFile() != null\r
+                        && outputDelimiter.trim().length() > 0) {\r
+            if (outputDelimiter.trim().equalsIgnoreCase("-cr")) {\r
+                cmdLen = cmdLen + 2;\r
+            } else {\r
+                cmdLen++;\r
+            }\r
+        }\r
+\r
+        //\r
+        // for every source file\r
+        // if file is header file, just skip it (add later)\r
+        //\r
+        Vector srcSets = userdefine.getSrcSets();\r
+\r
+        //\r
+        // if have source file append source file in command line.\r
+        //\r
+        Set allSrcFiles = new LinkedHashSet();\r
+\r
+        for (int i = 0; i < srcSets.size(); i++) {\r
+            ConditionalFileSet srcSet = (ConditionalFileSet) srcSets\r
+                            .elementAt(i);\r
+            if (srcSet.isActive()) {\r
+                //\r
+                // Find matching source files\r
+                //\r
+                DirectoryScanner scanner = srcSet.getDirectoryScanner(project);\r
+                //\r
+                // Check each source file - see if it needs compilation\r
+                //\r
+                String[] fileNames = scanner.getIncludedFiles();\r
+                for (int j = 0; j < fileNames.length; j++) {\r
+                    allSrcFiles.add(scanner.getBasedir() + "/" + fileNames[j]);\r
+                    if (isGccCommand) {\r
+                        System.out.println("[" + userdefine.getType() + "] "\r
+                                        + fileNames[j]);\r
+                    }\r
+                }\r
+            }\r
+        }\r
+\r
+        String[] fileNames = (String[]) allSrcFiles\r
+                        .toArray(new String[allSrcFiles.size()]);\r
+        String[] cmd = new String[cmdLen - 1 + fileNames.length];\r
+        int index = 0;\r
+        cmd[index++] = userdefine.getCmd();\r
+\r
+        Iterator iter = argsWithoutSpace.iterator();\r
+        while (iter.hasNext()) {\r
+            cmd[index++] = project.replaceProperties((String) iter.next());\r
+        }\r
+\r
+        iter = endargsWithoutSpace.iterator();\r
+        while (iter.hasNext()) {\r
+            cmd[index++] = project.replaceProperties((String) iter.next());\r
+        }\r
+\r
+        //\r
+        // Add outputFileFlag and output file to cmd\r
+        //\r
+        if (outputDelimiter != null && userdefine.getOutputFile() != null\r
+                        && outputDelimiter.length() > 0) {\r
+            if (outputDelimiter.trim().equalsIgnoreCase("-cr")) {\r
+                cmd[index++] = outputDelimiter;\r
+                cmd[index++] = userdefine.getOutputFile();\r
+            } else {\r
+                cmd[index++] = outputDelimiter + userdefine.getOutputFile();\r
+            }\r
+        }\r
+\r
+        iter = includePath.iterator();\r
+        while (iter.hasNext()) {\r
+            cmd[index++] = (String) iter.next();\r
+        }\r
+\r
+        if (libSet != null && libSet.length > 0) {\r
+            if (isGccCommand) {\r
+                cmd[index++] = "-(";\r
+            }\r
+            for (int k = 0; k < libSet.length; k++) {\r
+                cmd[index++] = libSet[k];\r
+            }\r
+            if (isGccCommand) {\r
+                cmd[index++] = "-)";\r
+            }\r
+        }\r
+        for (int j = 0; j < fileNames.length; j++) {\r
+            cmd[index++] = fileNames[j];\r
+        }\r
+        \r
+//        StringBuffer logLine = new StringBuffer();\r
+//        for(int i = 0; i < cmd.length; i++) {\r
+//            logLine.append(cmd[i] + " ");\r
+//        }\r
+//        project.log(logLine.toString(), Project.MSG_VERBOSE);\r
+\r
+        int retval = 0;\r
+        \r
+        if (userdefine.getDpath() == null || userdefine.getDpath().trim().length() == 0) {\r
+            retval = runCommand(cctask, workdir, cmd, null);\r
+        } else {\r
+            String existPath = System.getenv(getPathName());\r
+            Environment newEnv = new Environment();\r
+            Variable var = new Variable();\r
+            var.setKey(getPathName());\r
+            var.setPath(new Path(project, userdefine.getDpath() + ";" + existPath));\r
+            newEnv.addVariable(var);\r
+            retval = runCommand(cctask, workdir, cmd, newEnv);\r
+        }\r
+        \r
+\r
+        if (retval != 0) {\r
+            throw new BuildException(userdefine.getCmd()\r
+                            + " failed with return code " + retval, cctask\r
+                            .getLocation());\r
+        }\r
+    }\r
+    \r
+    private String getPathName() {\r
+        if (pathName != null) {\r
+            return pathName;\r
+        }\r
+        Map allEnv = System.getenv();\r
+        Iterator iter = allEnv.keySet().iterator();\r
+        while (iter.hasNext()) {\r
+            String key = (String)iter.next();\r
+            if(key.equalsIgnoreCase("PATH")) {\r
+                pathName = key;\r
+                break ;\r
+            }\r
+        }\r
+        return pathName;\r
+    }\r
+\r
+    protected int runCommand(CCTask task, File workingDir, String[] cmdline, Environment env)\r
+                    throws BuildException {\r
+        //\r
+        // Write command to File\r
+        //\r
+        return CUtil.runCommand(task, workingDir, cmdline, false, env);\r
+\r
+    }\r
+}\r