OSDN Git Service

git-svn-id: svn+ssh://svn.sourceforge.jp/svnroot/filelock/trunk@17 c6214a2a-ec3a...
[filelock/repo.git] / filelock / src / main / lib / launch4j-2.1.5-win32 / src / net / sf / launch4j / Builder.java
1 /*
2         Launch4j (http://launch4j.sourceforge.net/)
3         Cross-platform Java application wrapper for creating Windows native executables.
4
5         Copyright (C) 2004, 2006 Grzegorz Kowal
6
7         This program is free software; you can redistribute it and/or modify
8         it under the terms of the GNU General Public License as published by
9         the Free Software Foundation; either version 2 of the License, or
10         (at your option) any later version.
11
12         This program is distributed in the hope that it will be useful,
13         but WITHOUT ANY WARRANTY; without even the implied warranty of
14         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15         GNU General Public License for more details.
16
17         You should have received a copy of the GNU General Public License
18         along with this program; if not, write to the Free Software
19         Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20 */
21
22 /*
23  * Created on 2005-04-24
24  */
25 package net.sf.launch4j;
26
27 import java.io.File;
28 import java.io.FileInputStream;
29 import java.io.FileOutputStream;
30 import java.io.IOException;
31
32 import net.sf.launch4j.binding.InvariantViolationException;
33 import net.sf.launch4j.config.Config;
34 import net.sf.launch4j.config.ConfigPersister;
35
36 /**
37  * @author Copyright (C) 2005 Grzegorz Kowal
38  */
39 public class Builder {
40         private final Log _log;
41
42         public Builder(Log log) {
43                 _log = log;
44         }
45
46         /**
47          * @return Output file path.
48          */
49         public File build() throws BuilderException {
50                 final Config c = ConfigPersister.getInstance().getConfig();
51                 try {
52                         c.validate();
53                 } catch (InvariantViolationException e) {
54                         throw new BuilderException(e.getMessage());
55                 }
56                 File rc = null;
57                 File ro = null;
58                 File outfile = null;
59                 FileInputStream is = null;
60                 FileOutputStream os = null;
61                 final RcBuilder rcb = new RcBuilder();
62                 try {
63                         File basedir = Util.getJarBasedir();
64                         rc = rcb.build(c);
65                         ro = Util.createTempFile("o"); //$NON-NLS-1$
66                         outfile = ConfigPersister.getInstance().getOutputFile();
67
68                         Cmd resCmd = new Cmd(basedir);
69                         resCmd.addExe("windres") //$NON-NLS-1$
70                                         .add(Util.WINDOWS_OS ? "--preprocessor=type" : "--preprocessor=cat") //$NON-NLS-1$ //$NON-NLS-2$
71                                         .add("-J rc -O coff -F pe-i386") //$NON-NLS-1$
72                                         .addAbsFile(rc)
73                                         .addAbsFile(ro);
74                         _log.append(Messages.getString("Builder.compiling.resources")); //$NON-NLS-1$
75                         resCmd.exec(_log);
76
77                         Cmd ldCmd = new Cmd(basedir);
78                         ldCmd.addExe("ld") //$NON-NLS-1$
79                                         .add("-mi386pe") //$NON-NLS-1$
80                                         .add("--oformat pei-i386") //$NON-NLS-1$
81                                         .add((c.getHeaderType() == Config.GUI_HEADER)
82                                                         ? "--subsystem windows" : "--subsystem console") //$NON-NLS-1$ //$NON-NLS-2$
83                                         .add("-s")              // strip symbols //$NON-NLS-1$
84                                         .addFiles(c.getHeaderObjects())
85                                         .addAbsFile(ro)
86                                         .addFiles(c.getLibs())
87                                         .add("-o") //$NON-NLS-1$
88                                         .addAbsFile(outfile);
89                         _log.append(Messages.getString("Builder.linking")); //$NON-NLS-1$
90                         ldCmd.exec(_log);
91
92                         if (!c.isDontWrapJar()) {
93                                 _log.append(Messages.getString("Builder.wrapping")); //$NON-NLS-1$
94                                 int len;
95                                 byte[] buffer = new byte[1024];
96                                 is = new FileInputStream(
97                                                 Util.getAbsoluteFile(ConfigPersister.getInstance().getConfigPath(),     c.getJar()));
98                                 os = new FileOutputStream(outfile, true);
99                                 while ((len = is.read(buffer)) > 0) {
100                                         os.write(buffer, 0, len);
101                                 }
102                         }
103                         _log.append(Messages.getString("Builder.success") + outfile.getPath()); //$NON-NLS-1$
104                         return outfile;
105                 } catch (IOException e) {
106                         Util.delete(outfile);
107                         _log.append(e.getMessage());
108                         throw new BuilderException(e);
109                 } catch (ExecException e) {
110                         Util.delete(outfile);
111                         String msg = e.getMessage(); 
112                         if (msg != null && msg.indexOf("windres") != -1) { //$NON-NLS-1$
113                                 _log.append(Messages.getString("Builder.generated.resource.file")); //$NON-NLS-1$
114                                 _log.append(rcb.getContent());
115                         }
116                         throw new BuilderException(e);
117                 } finally {
118                         Util.close(is);
119                         Util.close(os);
120                         Util.delete(rc);
121                         Util.delete(ro);
122                 }
123         }
124 }
125
126 class Cmd {
127         private final StringBuffer _sb = new StringBuffer();
128         private final File _basedir;
129         private final File _bindir;
130         private final boolean _quote;
131
132         public Cmd(File basedir) {
133                 _basedir = basedir;
134                 _quote = basedir.getPath().indexOf(' ') != -1;
135                 String path = System.getProperty("launch4j.bindir");
136                 if (path == null) {
137                         _bindir = new File(basedir, "bin");
138                 } else {
139                         File bindir = new File(path);
140                         _bindir = bindir.isAbsolute() ? bindir : new File(basedir, path);
141                 }
142         }
143
144         public Cmd add(String s) {
145                 space();
146                 _sb.append(s);
147                 return this;
148         }
149
150         public Cmd addAbsFile(File file) {
151                 space();
152                 boolean quote = file.getPath().indexOf(' ') != -1;
153                 if (quote) {
154                         _sb.append('"');
155                 }
156                 _sb.append(file.getPath());
157                 if (quote) {
158                         _sb.append('"');
159                 }
160                 return this;
161         }
162
163         public Cmd addFile(String pathname) {
164                 space();
165                 if (_quote) {
166                         _sb.append('"');
167                 }
168                 _sb.append(new File(_basedir, pathname).getPath());
169                 if (_quote) {
170                         _sb.append('"');
171                 }
172                 return this;
173         }
174
175         public Cmd addExe(String pathname) {
176                 space();
177                 if (_quote) {
178                         _sb.append('"');
179                 }
180                 if (Util.WINDOWS_OS) {
181                         pathname += ".exe";     //$NON-NLS-1$
182                 }
183                 _sb.append(new File(_bindir, pathname).getPath());
184                 if (_quote) {
185                         _sb.append('"');
186                 }
187                 return this;
188         }
189
190         public Cmd addFiles(String[] files) {
191                 for (int i = 0; i < files.length; i++) {
192                         addFile(files[i]);
193                 }
194                 return this;
195         }
196
197         private void space() {
198                 if (_sb.length() > 0) {
199                         _sb.append(' ');
200                 }
201         }
202         
203         public String toString() {
204                 return _sb.toString();
205         }
206
207         public void exec(Log log) throws ExecException {
208                 Util.exec(_sb.toString(), log);
209         }
210 }