1 | /*
|
---|
2 | * Copyright 2001-2005 The Apache Software Foundation
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | *
|
---|
16 | */
|
---|
17 |
|
---|
18 | package org.apache.tools.ant.taskdefs.compilers;
|
---|
19 |
|
---|
20 |
|
---|
21 | import org.apache.tools.ant.BuildException;
|
---|
22 | import org.apache.tools.ant.Project;
|
---|
23 | import org.apache.tools.ant.types.Commandline;
|
---|
24 | import org.apache.tools.ant.types.Path;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * The implementation of the gcj compiler.
|
---|
28 | * This is primarily a cut-and-paste from the jikes.
|
---|
29 | *
|
---|
30 | * @since Ant 1.4
|
---|
31 | */
|
---|
32 | public class Gcj extends DefaultCompilerAdapter {
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Performs a compile using the gcj compiler.
|
---|
36 | */
|
---|
37 | public boolean execute() throws BuildException {
|
---|
38 | Commandline cmd;
|
---|
39 | attributes.log("Using gcj compiler", Project.MSG_VERBOSE);
|
---|
40 | cmd = setupGCJCommand();
|
---|
41 |
|
---|
42 | int firstFileName = cmd.size();
|
---|
43 | logAndAddFilesToCompile(cmd);
|
---|
44 |
|
---|
45 | return
|
---|
46 | executeExternalCompile(cmd.getCommandline(), firstFileName) == 0;
|
---|
47 | }
|
---|
48 |
|
---|
49 | protected Commandline setupGCJCommand() {
|
---|
50 | Commandline cmd = new Commandline();
|
---|
51 | Path classpath = new Path(project);
|
---|
52 |
|
---|
53 | // gcj doesn't support bootclasspath dir (-bootclasspath)
|
---|
54 | // so we'll emulate it for compatibility and convenience.
|
---|
55 | if (bootclasspath != null) {
|
---|
56 | classpath.append(bootclasspath);
|
---|
57 | }
|
---|
58 |
|
---|
59 | // gcj doesn't support an extension dir (-extdir)
|
---|
60 | // so we'll emulate it for compatibility and convenience.
|
---|
61 | classpath.addExtdirs(extdirs);
|
---|
62 |
|
---|
63 | classpath.append(getCompileClasspath());
|
---|
64 |
|
---|
65 | // Gcj has no option for source-path so we
|
---|
66 | // will add it to classpath.
|
---|
67 | if (compileSourcepath != null) {
|
---|
68 | classpath.append(compileSourcepath);
|
---|
69 | } else {
|
---|
70 | classpath.append(src);
|
---|
71 | }
|
---|
72 |
|
---|
73 | String exec = getJavac().getExecutable();
|
---|
74 | cmd.setExecutable(exec == null ? "gcj" : exec);
|
---|
75 |
|
---|
76 | if (destDir != null) {
|
---|
77 | cmd.createArgument().setValue("-d");
|
---|
78 | cmd.createArgument().setFile(destDir);
|
---|
79 |
|
---|
80 | if (!destDir.exists() && !destDir.mkdirs()) {
|
---|
81 | throw new BuildException("Can't make output directories. "
|
---|
82 | + "Maybe permission is wrong. ");
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | cmd.createArgument().setValue("-classpath");
|
---|
87 | cmd.createArgument().setPath(classpath);
|
---|
88 |
|
---|
89 | if (encoding != null) {
|
---|
90 | cmd.createArgument().setValue("--encoding=" + encoding);
|
---|
91 | }
|
---|
92 | if (debug) {
|
---|
93 | cmd.createArgument().setValue("-g1");
|
---|
94 | }
|
---|
95 | if (optimize) {
|
---|
96 | cmd.createArgument().setValue("-O");
|
---|
97 | }
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * gcj should be set for generate class.
|
---|
101 | * ... if no 'compile to native' argument is passed
|
---|
102 | */
|
---|
103 | if (!isNativeBuild()) {
|
---|
104 | cmd.createArgument().setValue("-C");
|
---|
105 | }
|
---|
106 |
|
---|
107 | addCurrentCompilerArgs(cmd);
|
---|
108 |
|
---|
109 | return cmd;
|
---|
110 | }
|
---|
111 |
|
---|
112 | /**
|
---|
113 | * Whether any of the arguments given via <compilerarg>
|
---|
114 | * implies that compilation to native code is requested.
|
---|
115 | *
|
---|
116 | * @since Ant 1.6.2
|
---|
117 | */
|
---|
118 | public boolean isNativeBuild() {
|
---|
119 | boolean nativeBuild = false;
|
---|
120 | String[] additionalArguments = getJavac().getCurrentCompilerArgs();
|
---|
121 | int argsLength = 0;
|
---|
122 | while (!nativeBuild && argsLength < additionalArguments.length) {
|
---|
123 | int conflictLength = 0;
|
---|
124 | while (!nativeBuild
|
---|
125 | && conflictLength < CONFLICT_WITH_DASH_C.length) {
|
---|
126 | nativeBuild = (additionalArguments[argsLength].startsWith
|
---|
127 | (CONFLICT_WITH_DASH_C[conflictLength]));
|
---|
128 | conflictLength++;
|
---|
129 | }
|
---|
130 | argsLength++;
|
---|
131 | }
|
---|
132 | return nativeBuild;
|
---|
133 | }
|
---|
134 |
|
---|
135 | private static final String [] CONFLICT_WITH_DASH_C = {
|
---|
136 | "-o" , "--main=", "-D", "-fjni", "-L"
|
---|
137 | };
|
---|
138 |
|
---|
139 | }
|
---|