1 package org.codehaus.mojo.natives.mingw;
2
3 import java.io.File;
4 import org.codehaus.plexus.util.cli.Commandline;
5
6 import org.codehaus.mojo.natives.NativeBuildException;
7 import org.codehaus.mojo.natives.compiler.AbstractCompiler;
8 import org.codehaus.mojo.natives.compiler.CompilerConfiguration;
9 import org.codehaus.mojo.natives.parser.Parser;
10 import org.codehaus.mojo.natives.parser.CParser;
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 public abstract class AbstractGccCompiler extends AbstractCompiler {
36
37
38
39
40
41 private Parser parser = new CParser();
42
43 protected abstract String getOutputFileOption();
44
45 protected Parser getParser() {
46 return this.parser;
47 }
48
49
50
51
52 protected Commandline getCommandLine(File srcFile, File destFile, CompilerConfiguration config)
53 throws NativeBuildException {
54
55 if (config.getExecutable() == null) {
56 config.setExecutable("gcc");
57 }
58
59 Commandline cl = new Commandline();
60
61 cl.setExecutable(config.getExecutable());
62
63 if (config.getWorkingDirectory() != null) {
64 cl.setWorkingDirectory(config.getWorkingDirectory().getPath());
65 }
66
67 this.setStartOptions(cl, config);
68
69 this.setIncludePaths(cl, config.getIncludePaths());
70
71 this.setIncludePaths(cl, config.getSystemIncludePaths());
72
73 this.setMiddleOptions(cl, config);
74
75 this.setOutputArgs(cl, destFile);
76
77 this.setSourceArgs(cl, srcFile);
78
79 this.setEndOptions(cl, config);
80
81 return cl;
82 }
83
84 private void setOptions(Commandline cl, String[] options) {
85 if (options != null) {
86 for (int i = 0; i < options.length; ++i) {
87 cl.createArg().setValue(options[i]);
88 }
89 }
90 }
91
92 private void setStartOptions(Commandline cl, CompilerConfiguration config) {
93 this.setOptions(cl, config.getStartOptions());
94 }
95
96 private void setMiddleOptions(Commandline cl, CompilerConfiguration config) {
97 this.setOptions(cl, config.getMiddleOptions());
98 }
99
100 private void setEndOptions(Commandline cl, CompilerConfiguration config) {
101 this.setOptions(cl, config.getEndOptions());
102 }
103
104 private void setIncludePaths(Commandline cl, File[] includePaths) {
105 if (includePaths != null) {
106 for (int i = 0; i < includePaths.length; ++i) {
107 cl.createArg().setValue("-I" + includePaths[i].getPath());
108 }
109 }
110 }
111
112 private void setOutputArgs(Commandline cl, File outputFile) {
113 String outputFileOption = this.getOutputFileOption();
114
115 if (outputFileOption.endsWith(" ")) {
116 cl.createArg().setValue(outputFileOption.trim());
117 cl.createArg().setValue(outputFile.getPath());
118 } else {
119 cl.createArg().setValue(outputFileOption + outputFile.getPath());
120 }
121 }
122
123 private void setSourceArgs(Commandline cl, File srcFile) {
124 cl.createArg().setValue("-c");
125 cl.createArg().setValue(srcFile.getPath());
126 }
127 }