001: /*
002: Launch4j (http://launch4j.sourceforge.net/)
003: Cross-platform Java application wrapper for creating Windows native executables.
004:
005: Copyright (c) 2004, 2007 Grzegorz Kowal
006:
007: All rights reserved.
008:
009: Redistribution and use in source and binary forms, with or without modification,
010: are permitted provided that the following conditions are met:
011:
012: * Redistributions of source code must retain the above copyright notice,
013: this list of conditions and the following disclaimer.
014: * Redistributions in binary form must reproduce the above copyright notice,
015: this list of conditions and the following disclaimer in the documentation
016: and/or other materials provided with the distribution.
017: * Neither the name of the Launch4j nor the names of its contributors
018: may be used to endorse or promote products derived from this software without
019: specific prior written permission.
020:
021: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
022: "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
023: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
024: A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
025: CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
026: EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
027: PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
028: PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
029: LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
030: NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
031: SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
032: */
033:
034: /*
035: * Created on 2005-04-24
036: */
037: package net.sf.launch4j;
038:
039: import java.io.File;
040: import java.io.FileInputStream;
041: import java.io.FileOutputStream;
042: import java.io.IOException;
043: import java.util.ArrayList;
044: import java.util.Iterator;
045: import java.util.List;
046: import java.util.StringTokenizer;
047:
048: import net.sf.launch4j.binding.InvariantViolationException;
049: import net.sf.launch4j.config.Config;
050: import net.sf.launch4j.config.ConfigPersister;
051:
052: /**
053: * @author Copyright (C) 2005 Grzegorz Kowal
054: */
055: public class Builder {
056: private final Log _log;
057: private final File _basedir;
058:
059: public Builder(Log log) {
060: _log = log;
061: _basedir = Util.getJarBasedir();
062: }
063:
064: public Builder(Log log, File basedir) {
065: _log = log;
066: _basedir = basedir;
067: }
068:
069: /**
070: * @return Output file path.
071: */
072: public File build() throws BuilderException {
073: final Config c = ConfigPersister.getInstance().getConfig();
074: try {
075: c.validate();
076: } catch (InvariantViolationException e) {
077: throw new BuilderException(e.getMessage());
078: }
079: File rc = null;
080: File ro = null;
081: File outfile = null;
082: FileInputStream is = null;
083: FileOutputStream os = null;
084: final RcBuilder rcb = new RcBuilder();
085: try {
086: rc = rcb.build(c);
087: ro = Util.createTempFile("o");
088: outfile = ConfigPersister.getInstance().getOutputFile();
089:
090: Cmd resCmd = new Cmd(_basedir);
091: resCmd.addExe("windres").add(
092: Util.WINDOWS_OS ? "--preprocessor=type"
093: : "--preprocessor=cat").add(
094: "-J rc -O coff -F pe-i386").addAbsFile(rc)
095: .addAbsFile(ro);
096: _log.append(Messages
097: .getString("Builder.compiling.resources"));
098: resCmd.exec(_log);
099:
100: Cmd ldCmd = new Cmd(_basedir);
101: ldCmd
102: .addExe("ld")
103: .add("-mi386pe")
104: .add("--oformat pei-i386")
105: .add(
106: (c.getHeaderType()
107: .equals(Config.GUI_HEADER)) ? "--subsystem windows"
108: : "--subsystem console")
109: .add("-s")
110: // strip symbols
111: .addFiles(c.getHeaderObjects()).addAbsFile(ro)
112: .addFiles(c.getLibs()).add("-o")
113: .addAbsFile(outfile);
114: _log.append(Messages.getString("Builder.linking"));
115: ldCmd.exec(_log);
116:
117: if (!c.isDontWrapJar()) {
118: _log.append(Messages.getString("Builder.wrapping"));
119: int len;
120: byte[] buffer = new byte[1024];
121: is = new FileInputStream(Util.getAbsoluteFile(
122: ConfigPersister.getInstance().getConfigPath(),
123: c.getJar()));
124: os = new FileOutputStream(outfile, true);
125: while ((len = is.read(buffer)) > 0) {
126: os.write(buffer, 0, len);
127: }
128: }
129: _log.append(Messages.getString("Builder.success")
130: + outfile.getPath());
131: return outfile;
132: } catch (IOException e) {
133: Util.delete(outfile);
134: _log.append(e.getMessage());
135: throw new BuilderException(e);
136: } catch (ExecException e) {
137: Util.delete(outfile);
138: String msg = e.getMessage();
139: if (msg != null && msg.indexOf("windres") != -1) {
140: if (e.getErrLine() != -1) {
141: _log.append(Messages.getString(
142: "Builder.line.has.errors", String.valueOf(e
143: .getErrLine())));
144: _log.append(rcb.getLine(e.getErrLine()));
145: } else {
146: _log
147: .append(Messages
148: .getString("Builder.generated.resource.file"));
149: _log.append(rcb.getContent());
150: }
151: }
152: throw new BuilderException(e);
153: } finally {
154: Util.close(is);
155: Util.close(os);
156: Util.delete(rc);
157: Util.delete(ro);
158: }
159: }
160: }
161:
162: class Cmd {
163: private final List _cmd = new ArrayList();
164: private final File _basedir;
165: private final File _bindir;
166:
167: public Cmd(File basedir) {
168: _basedir = basedir;
169: String path = System.getProperty("launch4j.bindir");
170: if (path == null) {
171: _bindir = new File(basedir, "bin");
172: } else {
173: File bindir = new File(path);
174: _bindir = bindir.isAbsolute() ? bindir : new File(basedir,
175: path);
176: }
177: }
178:
179: public Cmd add(String s) {
180: StringTokenizer st = new StringTokenizer(s);
181: while (st.hasMoreTokens()) {
182: _cmd.add(st.nextToken());
183: }
184: return this ;
185: }
186:
187: public Cmd addAbsFile(File file) {
188: _cmd.add(file.getPath());
189: return this ;
190: }
191:
192: public Cmd addFile(String pathname) {
193: _cmd.add(new File(_basedir, pathname).getPath());
194: return this ;
195: }
196:
197: public Cmd addExe(String pathname) {
198: if (Util.WINDOWS_OS) {
199: pathname += ".exe";
200: }
201: _cmd.add(new File(_bindir, pathname).getPath());
202: return this ;
203: }
204:
205: public Cmd addFiles(List files) {
206: for (Iterator iter = files.iterator(); iter.hasNext();) {
207: addFile((String) iter.next());
208: }
209: return this ;
210: }
211:
212: public void exec(Log log) throws ExecException {
213: String[] cmd = (String[]) _cmd.toArray(new String[_cmd.size()]);
214: Util.exec(cmd, log);
215: }
216: }
|