pkg-config file now has script pathes
[unitool.git] / db / LoadLibtoolArchive.java
blobcadfefdc006009036c3457747f1a6810add21627
2 package org.de.metux.unitool.db;
4 import org.de.metux.util.ShellVariableDef;
5 import org.de.metux.util.StrSplit;
6 import org.de.metux.util.StrReplace;
7 import org.de.metux.util.PathNormalizer;
8 import org.de.metux.util.StrUtil;
10 import java.io.BufferedReader;
11 import java.io.IOException;
12 import java.io.FileNotFoundException;
13 import java.io.File;
14 import java.io.FileReader;
16 import org.de.metux.unitool.base.LibraryInfo;
18 public class LoadLibtoolArchive
20 public final static String tmp_libdir = ".libs/";
22 // for -l direct imports
23 static public LibraryInfo directImport(String name, String sysroot, String prefix)
25 LibraryInfo inf = new LibraryInfo();
26 inf.prefix = prefix;
27 if (name.startsWith("lib"))
29 inf.library_name = name;
30 inf.module_name = name.substring(3);
32 else
34 inf.library_name = "lib"+name;
35 inf.module_name = name;
38 inf.sysroot = sysroot;
39 return inf;
42 static public LibraryInfo load_archive(
43 String fn,
44 String sysroot,
45 String prefix,
46 boolean link_uninstalled_static
48 throws FileNotFoundException, IOException
50 PathNormalizer norm = new PathNormalizer();
52 norm.setSysroot(sysroot);
54 sysroot = PathNormalizer.normalize_dir(sysroot);
56 if (prefix==null)
57 prefix=PathNormalizer.dirname(fn);
59 prefix = PathNormalizer.normalize_dir(prefix);
61 System.out.println("Loading libtool library: "+fn+" (sysroot="+sysroot+" prefix="+prefix+")");
63 String search_path = "";
64 String line = "";
66 BufferedReader in = new BufferedReader(new FileReader(fn));
67 LibraryInfo inf = new LibraryInfo();
69 inf.sysroot = sysroot;
70 inf.uninstalled_libdir = tmp_libdir;
71 inf.prefix = prefix;
72 inf.cf = fn;
74 while ((line=in.readLine())!=null)
76 ShellVariableDef shvar;
78 try
80 shvar = new ShellVariableDef(line);
82 catch (ShellVariableDef.XEmpty e)
84 continue;
86 catch (ShellVariableDef.XParseFailed e)
88 throw new RuntimeException("parse error in "+fn+": "+e);
91 if (shvar.value.length()==0)
92 continue;
94 if (shvar.name.equals("dlname"))
95 inf.dlname = shvar.value;
96 else if (shvar.name.equals("library_names"))
97 inf.dynamic_libnames = StrSplit.split(shvar.value);
98 else if (shvar.name.equals("old_library"))
99 inf.arname = shvar.value;
100 else if (shvar.name.equals("release"))
101 inf.release = shvar.value;
102 else if (shvar.name.equals("age"))
103 inf.version_age = shvar.getInt(0);
104 else if (shvar.name.equals("revision"))
105 inf.version_revision = shvar.getInt(0);
106 else if (shvar.name.equals("current"))
107 inf.version_current = shvar.getInt(0);
108 else if (shvar.name.equals("installed"))
109 inf.installed = shvar.getBoolean();
110 else if (shvar.name.equals("shouldnotlink"))
111 inf.should_not_link = shvar.getBoolean();
112 else if (shvar.name.equals("dlopen"))
113 inf.param_dlopen = shvar.value;
114 else if (shvar.name.equals("dlpreopen"))
115 inf.param_dlpreopen = shvar.value;
116 else if (shvar.name.equals("libdir"))
117 inf.libdir = norm.strip_sysroot(shvar.value);
118 else if (shvar.name.equals("dependency_libs"))
120 inf.dependency_names = StrSplit.split(shvar.value);
121 if (inf.dependency_names.length>0)
123 inf.dependencies = new LibraryInfo[inf.dependency_names.length];
124 for (int y=0; y<inf.dependency_names.length; y++)
126 String dep = inf.dependency_names[y];
127 if (dep.startsWith("-l"))
129 System.out.println("LoadLibtool ["+fn+"]: direct import: "+dep);
130 inf.dependencies[y] = directImport(dep.substring(2),sysroot,prefix);
132 else if (dep.startsWith("-L"))
134 String dirname = PathNormalizer.normalize_dir(dep.substring(2));
135 if (dirname.startsWith(sysroot))
136 throw new RuntimeException("-L: path starts with sysroot - is this correct ?!");
138 dirname = norm.enc_sysroot(dirname);
139 search_path += " "+dirname;
141 else if (dep.endsWith(".la"))
143 System.out.println("LoadLibtool ["+fn+"]: la file "+dep);
144 if (dep.startsWith("/")) /* absolute path */
145 inf.dependencies[y] = load_archive(norm.dec_sysroot(dep), sysroot, prefix, link_uninstalled_static);
146 else
148 String dep_dirname = PathNormalizer.dirname(dep);
150 inf.dependencies[y] =
151 load_archive(
152 prefix+dep,
153 sysroot,
154 PathNormalizer.normalize_dir(prefix+dep_dirname),
155 link_uninstalled_static
159 else
160 throw new RuntimeException("["+inf.dlname+"] --> unhandled depedency type: "+dep);
164 else
165 throw new RuntimeException
167 "Unhandled variable: \""+
168 shvar.name+"\" => \""+
169 shvar.value+"\""
173 inf.search_pathes = StrSplit.split(search_path);
175 // get the library name
176 // FIXME!
178 if (StrUtil.isEmpty(inf.module_name))
180 String basename = new File(fn).getName();
182 if (!basename.endsWith(".la"))
183 throw new RuntimeException("filename does not end with .la - cannot guess libname:" +basename);
185 inf.library_name = basename.substring(0,basename.length()-3);
186 if (inf.library_name.startsWith("lib"))
187 inf.module_name = inf.library_name.substring(3);
188 else
189 inf.module_name = inf.library_name;
192 if (StrUtil.isEmpty(inf.libdir))
194 System.out.println("[INFO] "+fn+" has no (dynamic) libdir. linking statically");
195 link_uninstalled_static = true;
196 inf.link_static = true;
199 // we have to do some fixups ...
200 if (inf.installed)
202 throw new RuntimeException("loading of installed libs not handled yet: "+fn);
204 else
206 // we can pass via parameter how to link uninstalled libs
207 // the libtool frontend gets this from $LT_UNITOOL_LINK_UNINSTALLED_STATIC
208 // at least problematic packages like SDL (which links several
209 // .a's together to one .so) should work this way.
211 // FIXME: is this really correct ?
212 inf.link_static = link_uninstalled_static;
214 if (inf.dynamic_libnames!=null)
215 for (int x=0; x<inf.dynamic_libnames.length; x++)
216 if (!StrUtil.isEmpty(inf.dynamic_libnames[x]))
217 inf.dynamic_libnames[x] =
218 inf.prefix+inf.uninstalled_libdir+inf.dynamic_libnames[x];
221 return inf;