2 relocate.cc -- implement relocation based on argv0
4 source file of the GNU LilyPond music typesetter
6 (c) 2005--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
10 #include "relocate.hh"
15 /* TODO: autoconf support */
17 #include <sys/types.h>
24 #include "file-name.hh"
25 #include "file-path.hh"
26 #include "international.hh"
27 #include "lily-guile.hh"
28 #include "lily-version.hh"
33 #define FRAMEWORKDIR ".."
37 sane_putenv (char const *key, string value, bool overwrite)
39 if (overwrite || !getenv (key))
41 string combine = string (key) + "=" + value;
42 char *s = strdup (combine.c_str ());
50 set_env_file (char const *key, string value, bool overwrite = false)
53 return sane_putenv (key, value, overwrite);
54 else if (be_verbose_global)
55 warning (_f ("no such file: %s for %s", value, key));
60 set_env_dir (char const *key, string value)
63 return sane_putenv (key, value, false);
64 else if (be_verbose_global)
65 warning (_f ("no such directory: %s for %s", value, key));
70 prepend_env_path (char const *key, string value)
74 if (be_verbose_global)
75 progress_indication (_f ("%s=%s (prepend)\n", key, value.c_str ()));
77 if (char const *cur = getenv (key))
78 value += to_string (PATHSEP) + cur;
80 return sane_putenv (key, value.c_str (), true);
82 else if (be_verbose_global)
83 warning (_f ("no such directory: %s for %s", value, key));
92 prefix_relocation (string prefix)
94 if (be_verbose_global)
95 warning (_f ("Relocation: compile prefix=%s, new prefix=%s",
99 string bindir = prefix + "/bin";
100 string datadir = prefix + "/share";
101 string localedir = datadir + "/locale";
102 string lilypond_datadir = datadir + "/lilypond/";
104 if (is_dir (lilypond_datadir + "/" + TOPLEVEL_VERSION))
105 prefix_directory = lilypond_datadir + "/" + TOPLEVEL_VERSION;
106 else if (is_dir (lilypond_datadir + "/current"))
107 prefix_directory = lilypond_datadir + "/current";
110 if (is_dir (localedir))
111 bindtextdomain ("lilypond", localedir.c_str ());
114 prepend_env_path ("PATH", bindir);
118 framework_relocation (string prefix)
120 if (be_verbose_global)
121 warning (_f ("Relocation: framework_prefix=%s", prefix));
123 sane_putenv ("INSTALLER_ROOT", prefix, true);
125 string bindir = prefix + "/bin";
126 string datadir = prefix + "/share";
127 string libdir = prefix + "/lib";
128 string sysconfdir = prefix + "/etc";
130 /* need otherwise dynamic .so's aren't found. */
131 prepend_env_path ("DYLD_LIBRARY_PATH", libdir);
133 set_env_file ("FONTCONFIG_FILE", sysconfdir + "/fonts/fonts.conf", true);
134 set_env_dir ("FONTCONFIG_PATH", sysconfdir + "/fonts");
137 char font_dir[PATH_MAX];
138 ExpandEnvironmentStrings ("%windir%/fonts", font_dir, sizeof (font_dir));
139 prepend_env_path ("GS_FONTPATH", font_dir);
143 #ifdef GHOSTSCRIPT_VERSION
146 "ghostscript-version-undefined"
150 if (char const *cur = getenv ("LILYPOND_GS_VERSION"))
153 prepend_env_path ("GS_FONTPATH", datadir + "/ghostscript/" + gs_version + "/fonts");
154 prepend_env_path ("GS_LIB", datadir + "/ghostscript/" + gs_version + "/Resource");
155 prepend_env_path ("GS_LIB", datadir + "/ghostscript/" + gs_version + "/lib");
157 prepend_env_path ("GS_FONTPATH", datadir + "/gs/fonts");
158 prepend_env_path ("GS_LIB", datadir + "/gs/Resource");
159 prepend_env_path ("GS_LIB", datadir + "/gs/lib");
161 prepend_env_path ("GUILE_LOAD_PATH", datadir
162 + to_string ("/guile/%d.%d",
163 SCM_MAJOR_VERSION, SCM_MINOR_VERSION));
165 set_env_file ("PANGO_RC_FILE", sysconfdir + "/pango/pangorc");
166 set_env_dir ("PANGO_PREFIX", prefix);
168 prepend_env_path ("PATH", bindir);
172 setup_paths (char const *argv0_ptr)
174 File_name argv0_filename (argv0_ptr);
176 prefix_directory = LILYPOND_DATADIR;
178 && getenv ("LILYPOND_RELOCATE_PREFIX"))
180 string prefix = getenv ("LILYPOND_RELOCATE_PREFIX");
182 /* Normalize file name. */
183 prefix = File_name (prefix).to_string ();
184 #endif /* __MINGW32__ */
185 prefix_relocation (prefix);
186 string bindir = prefix + "/bin";
187 framework_relocation (bindir);
189 else if (relocate_binary)
192 if (argv0_filename.is_absolute ())
194 argv0_abs = argv0_filename.to_string ();
195 if (be_verbose_global)
196 warning (_f ("Relocation: is absolute: argv0=%s", argv0_ptr));
198 else if (argv0_filename.dir_.length ())
200 argv0_abs = get_working_directory ()
201 + "/" + string (argv0_filename.to_string ());
202 if (be_verbose_global)
203 warning (_f ("Relocation: from cwd: argv0=%s", argv0_ptr));
207 /* Find absolute ARGV0 name, using PATH. */
209 path.parse_path (getenv ("PATH"));
211 if (be_verbose_global)
212 warning (_f ("Relocation: from PATH=%s\nargv0=%s",
213 path.to_string ().c_str (), argv0_ptr));
216 argv0_abs = path.find (argv0_filename.to_string ());
217 #else /* __MINGW32__ */
218 char const *ext[] = {"exe", "", 0 };
219 argv0_abs = path.find (argv0_filename.to_string (), ext);
220 #endif /* __MINGW32__ */
222 if (argv0_abs.empty ())
223 programming_error ("can't find absolute argv0.");
226 string bindir = dir_name (argv0_abs);
227 string argv0_prefix = dir_name (bindir);
228 string compile_prefix = dir_name (dir_name (dir_name (prefix_directory)));
229 if (argv0_prefix != compile_prefix)
230 prefix_relocation (argv0_prefix);
231 if (argv0_prefix != compile_prefix || string (FRAMEWORKDIR) != "..")
232 framework_relocation (bindir + "/" + FRAMEWORKDIR);
235 /* FIXME: use LILYPOND_DATADIR. */
236 if (char const *env = getenv ("LILYPONDPREFIX"))
240 /* Normalize file name. */
241 prefix_directory = File_name (env).to_string ();
243 prefix_directory = env;
247 global_path.append ("");
251 When running from build dir, a full LILYPOND_PREFIX is set-up at
253 $(OUTBASE)/share/lilypond/TOPLEVEL_VERSION
255 This historical hack will allow the shorthand
257 LILYPONDPREFIX=out lily/out/lilypond ...
261 string build_prefix_current = prefix_directory + "/share/lilypond/" "current";
262 string build_prefix_version = prefix_directory + "/share/lilypond/" TOPLEVEL_VERSION;
263 if (is_dir (build_prefix_version.c_str ()))
264 prefix_directory = build_prefix_version;
265 else if (is_dir (build_prefix_current.c_str ()))
266 prefix_directory = build_prefix_current;
268 /* Adding mf/out make lilypond unchanged source directory, when setting
269 LILYPONDPREFIX to lilypond-x.y.z */
270 char const *suffixes[] = {"ly", "ps", "scm", 0 };
273 for (char const **s = suffixes; *s; s++)
275 string path = prefix_directory + to_string ('/') + string (*s);
276 dirs.push_back (path);
279 dirs.push_back (prefix_directory + "/fonts/otf/");
280 dirs.push_back (prefix_directory + "/fonts/type1/");
281 dirs.push_back (prefix_directory + "/fonts/svg/");
283 for (vsize i = 0; i < dirs.size (); i++)
284 global_path.prepend (dirs[i]);
290 expand_environment_variables (string orig)
292 const char *start_ptr = orig.c_str();
293 const char *ptr = orig.c_str();
294 size_t len = orig.length();
297 while (ptr < start_ptr + len)
299 char *dollar = strchr (ptr, '$');
303 char *start_var = dollar + 1;
304 char *end_var = start_var;
305 char *start_next = end_var;
307 out += string (ptr, dollar - ptr);
310 if (*start_var == '{')
314 end_var = strchr (start_var, '}');
318 end_var = start_var + len;
319 start_next = end_var;
323 start_next = end_var + 1;
329 Hmm. what to do for $1 , $~ etc.?
335 while (isalnum (*end_var) || *end_var == '_');
336 start_next = end_var;
339 if (start_var < end_var)
341 string var_name (start_var, end_var - start_var);
342 const char *value = getenv (var_name.c_str());
344 out += string (value);
366 while ((c = fgetc (f)) != EOF && c != '\n')
373 read_relocation_file (string filename)
375 char const *cname = filename.c_str ();
376 FILE *f = fopen (cname, "r");
378 error (_f ("can't open file %s", cname));
382 string line = read_line (f);
383 size_t idx = line.find (' ');
387 string command = line.substr (0, idx);
388 line = line.substr (idx + 1);
392 idx = line.find ('=');
394 string variable = line.substr (0, idx);
395 string value = line.substr (idx + 1);
397 value = expand_environment_variables (value);
399 if (command == "set")
400 sane_putenv (variable.c_str(), value, true);
401 else if (command == "setdir")
402 set_env_dir (variable.c_str(), value);
403 else if (command == "setfile")
404 set_env_file (variable.c_str(), value);
405 else if (command == "prependdir")
406 prepend_env_path (variable.c_str (), value);
408 error ( _f("Unknown relocation command %s", command));
415 read_relocation_dir (string dirname)
417 DIR *dir = opendir (dirname.c_str ());
419 while (struct dirent *ent = readdir (dir))
421 File_name name (ent->d_name);
422 if (name.ext_ == "reloc")
424 read_relocation_file (name.to_string ());