pkg-config file now has script pathes
[unitool.git] / db / StorePkgConfig.java
blob28630283202370342b6f1d7bc108990c27867752
2 package org.de.metux.unitool.db;
4 import java.util.Properties;
5 import java.util.Enumeration;
7 import org.de.metux.util.PathNormalizer;
8 import org.de.metux.util.StoreFile;
9 import org.de.metux.util.StrUtil;
10 import org.de.metux.util.UniqueNameList;
12 import org.de.metux.unitool.base.PackageInfo;
13 import org.de.metux.unitool.base.EPkgConfigMissingProperty;
14 import org.de.metux.unitool.base.EPkgConfigBrokenVariableReference;
16 public class StorePkgConfig
18 static public boolean store_file(PackageInfo inf, String filename)
19 throws EPkgConfigMissingProperty, EPkgConfigBrokenVariableReference
21 return StoreFile.store(filename,render(inf));
24 static String render_property(String name, String value)
26 if (name.equals("prefix") ||
27 name.equals("exec_prefix") ||
28 name.equals("includedir") ||
29 name.equals("libdir"))
30 value = PathNormalizer.normalize(value);
32 return name + "=" + value + "\n";
35 static String render_property_rec(
36 Properties properties,
37 Properties done,
38 String name)
39 throws
40 EPkgConfigMissingProperty,
41 EPkgConfigBrokenVariableReference
43 if (done.get(name)!=null)
44 return "";
46 String value = properties.getProperty(name);
47 if (value==null)
48 return "<"+name+">\n";
50 // check if we have some variable
51 String res = "";
52 int startpos = 0;
53 int endpos = -1;
54 while ((startpos=value.indexOf("${",endpos))>=0)
56 if ((endpos = value.indexOf("}",startpos))<startpos)
57 throw new EPkgConfigBrokenVariableReference(value);
59 String refvar = value.substring(startpos+2,endpos);
60 res += render_property_rec(properties, done, refvar);
61 endpos++;
64 done.setProperty(name,value);
65 res += render_property(name,value);
66 return res;
69 static private String _field(String name, String value)
71 if (StrUtil.isEmpty(name))
72 return "";
74 return name+": "+value+"\n";
77 static private String _fold(UniqueNameList l, String elem_prefix)
79 if (l==null)
80 return "";
82 String[] list = l.getNames();
84 if ((list==null)||(list.length==0))
85 return "";
87 String s = "";
88 for (int x=0; x<list.length; x++)
89 s += elem_prefix+list[x];
91 return s;
94 static private String _render_cflags_public(PackageInfo inf)
96 return inf.cflags + _fold(inf.include_pathes," -I");
99 static private String _render_cflags_private(PackageInfo inf)
101 return inf.cflags_private+_fold(inf.include_pathes_private," -I");
104 static private String _render_requires_public(PackageInfo inf)
106 return
107 inf.requires+
108 _fold(inf.requires_pkgconfig, " ");
111 static private String _render_requires_private(PackageInfo inf)
113 return
114 inf.requires_private+
115 _fold(inf.requires_pkgconfig_private, " ");
118 static private String _render_ldflags_public(PackageInfo inf)
120 return
121 inf.ldflags+
122 _fold(inf.library_pathes," -L")+
123 _fold(inf.libraries," -l");
126 static private String _render_ldflags_private(PackageInfo inf)
128 return inf.ldflags_private+_fold(inf.library_pathes_private," -L");
131 static public String render(PackageInfo inf)
132 throws EPkgConfigMissingProperty, EPkgConfigBrokenVariableReference
134 if (inf.name == null)
135 throw new EPkgConfigMissingProperty("Name");
136 if (inf.description == null)
137 throw new EPkgConfigMissingProperty("Description");
138 if (inf.version == null)
139 throw new EPkgConfigMissingProperty("Version");
140 if (inf.cflags == null)
141 throw new EPkgConfigMissingProperty("Cflags");
143 Properties got = new Properties();
145 String str = "\n";
146 Enumeration e = inf.properties.keys();
147 while (e.hasMoreElements())
148 str += render_property_rec(
149 inf.properties,
150 got,
151 (String)e.nextElement()
153 str += "\n";
155 // master fields
156 str += _field("Name", inf.name)+
157 _field("Description", inf.description)+
158 _field("Version", inf.version)+
159 _field("Conflicts", inf.conflicts)+
160 _field("Requires", _render_requires_public(inf))+
161 _field("Requires.private", _render_requires_private(inf))+
162 _field("Cflags", _render_cflags_public(inf))+
163 _field("Cflags.private", _render_cflags_private(inf))+
164 _field("Libs", _render_ldflags_public(inf))+
165 _field("Libs.private", _render_ldflags_private(inf))
168 return str;