1 21 22 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 import java.util.Iterator ; 32 import java.util.List ; 33 34 import net.sf.launch4j.binding.InvariantViolationException; 35 import net.sf.launch4j.config.Config; 36 import net.sf.launch4j.config.ConfigPersister; 37 38 41 public class Builder { 42 private final Log _log; 43 private final File _basedir; 44 45 public Builder(Log log) { 46 _log = log; 47 _basedir = Util.getJarBasedir(); 48 } 49 50 public Builder(Log log, File basedir) { 51 _log = log; 52 _basedir = basedir; 53 } 54 55 58 public File build() throws BuilderException { 59 final Config c = ConfigPersister.getInstance().getConfig(); 60 try { 61 c.validate(); 62 } catch (InvariantViolationException e) { 63 throw new BuilderException(e.getMessage()); 64 } 65 File rc = null; 66 File ro = null; 67 File outfile = null; 68 FileInputStream is = null; 69 FileOutputStream os = null; 70 final RcBuilder rcb = new RcBuilder(); 71 try { 72 rc = rcb.build(c); 73 ro = Util.createTempFile("o"); 74 outfile = ConfigPersister.getInstance().getOutputFile(); 75 76 Cmd resCmd = new Cmd(_basedir); 77 resCmd.addExe("windres") 78 .add(Util.WINDOWS_OS ? "--preprocessor=type" : "--preprocessor=cat") 79 .add("-J rc -O coff -F pe-i386") 80 .addAbsFile(rc) 81 .addAbsFile(ro); 82 _log.append(Messages.getString("Builder.compiling.resources")); 83 resCmd.exec(_log); 84 85 Cmd ldCmd = new Cmd(_basedir); 86 ldCmd.addExe("ld") 87 .add("-mi386pe") 88 .add("--oformat pei-i386") 89 .add((c.getHeaderType().equals(Config.GUI_HEADER)) 90 ? "--subsystem windows" : "--subsystem console") 91 .add("-s") .addFiles(c.getHeaderObjects()) 93 .addAbsFile(ro) 94 .addFiles(c.getLibs()) 95 .add("-o") 96 .addAbsFile(outfile); 97 _log.append(Messages.getString("Builder.linking")); 98 ldCmd.exec(_log); 99 100 if (!c.isDontWrapJar()) { 101 _log.append(Messages.getString("Builder.wrapping")); 102 int len; 103 byte[] buffer = new byte[1024]; 104 is = new FileInputStream (Util.getAbsoluteFile( 105 ConfigPersister.getInstance().getConfigPath(), c.getJar())); 106 os = new FileOutputStream (outfile, true); 107 while ((len = is.read(buffer)) > 0) { 108 os.write(buffer, 0, len); 109 } 110 } 111 _log.append(Messages.getString("Builder.success") + outfile.getPath()); 112 return outfile; 113 } catch (IOException e) { 114 Util.delete(outfile); 115 _log.append(e.getMessage()); 116 throw new BuilderException(e); 117 } catch (ExecException e) { 118 Util.delete(outfile); 119 String msg = e.getMessage(); 120 if (msg != null && msg.indexOf("windres") != -1) { 121 if (e.getErrLine() != -1) { 122 _log.append(Messages.getString("Builder.line.has.errors", 123 String.valueOf(e.getErrLine()))); 124 _log.append(rcb.getLine(e.getErrLine())); 125 } else { 126 _log.append(Messages.getString("Builder.generated.resource.file")); 127 _log.append(rcb.getContent()); 128 } 129 } 130 throw new BuilderException(e); 131 } finally { 132 Util.close(is); 133 Util.close(os); 134 Util.delete(rc); 135 Util.delete(ro); 136 } 137 } 138 } 139 140 class Cmd { 141 private final StringBuffer _sb = new StringBuffer (); 142 private final File _basedir; 143 private final File _bindir; 144 private final boolean _quote; 145 146 public Cmd(File basedir) { 147 _basedir = basedir; 148 _quote = basedir.getPath().indexOf(' ') != -1; 149 String path = System.getProperty("launch4j.bindir"); 150 if (path == null) { 151 _bindir = new File (basedir, "bin"); 152 } else { 153 File bindir = new File (path); 154 _bindir = bindir.isAbsolute() ? bindir : new File (basedir, path); 155 } 156 } 157 158 public Cmd add(String s) { 159 space(); 160 _sb.append(s); 161 return this; 162 } 163 164 public Cmd addAbsFile(File file) { 165 space(); 166 boolean quote = file.getPath().indexOf(' ') != -1; 167 if (quote) { 168 _sb.append('"'); 169 } 170 _sb.append(file.getPath()); 171 if (quote) { 172 _sb.append('"'); 173 } 174 return this; 175 } 176 177 public Cmd addFile(String pathname) { 178 space(); 179 if (_quote) { 180 _sb.append('"'); 181 } 182 _sb.append(new File (_basedir, pathname).getPath()); 183 if (_quote) { 184 _sb.append('"'); 185 } 186 return this; 187 } 188 189 public Cmd addExe(String pathname) { 190 space(); 191 if (_quote) { 192 _sb.append('"'); 193 } 194 if (Util.WINDOWS_OS) { 195 pathname += ".exe"; 196 } 197 _sb.append(new File (_bindir, pathname).getPath()); 198 if (_quote) { 199 _sb.append('"'); 200 } 201 return this; 202 } 203 204 public Cmd addFiles(List files) { 205 for (Iterator iter = files.iterator(); iter.hasNext();) { 206 addFile((String ) iter.next()); 207 } 208 return this; 209 } 210 211 private void space() { 212 if (_sb.length() > 0) { 213 _sb.append(' '); 214 } 215 } 216 217 public String toString() { 218 return _sb.toString(); 219 } 220 221 public void exec(Log log) throws ExecException { 222 Util.exec(_sb.toString(), log); 223 } 224 } 225 | Popular Tags |