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 ".."
36 sane_putenv (char const *key, string value, bool overwrite)
38 if (overwrite || !getenv (key))
40 string combine = string (key) + "=" + value;
41 char *s = strdup (combine.c_str ());
43 if (be_verbose_global)
44 progress_indication (_f ("Setting %s to %s\n" , key, value.c_str ()));
53 set_env_file (char const *key, string value, bool overwrite = false)
56 return sane_putenv (key, value, overwrite);
57 else if (be_verbose_global)
58 warning (_f ("no such file: %s for %s", value, key));
63 set_env_dir (char const *key, string value)
66 return sane_putenv (key, value, false);
67 else if (be_verbose_global)
68 warning (_f ("no such directory: %s for %s", value, key));
73 prepend_env_path (char const *key, string value)
77 if (be_verbose_global)
78 progress_indication (_f ("%s=%s (prepend)\n", key, value.c_str ()));
80 if (char const *cur = getenv (key))
81 value += to_string (PATHSEP) + cur;
83 return sane_putenv (key, value.c_str (), true);
85 else if (be_verbose_global)
86 warning (_f ("no such directory: %s for %s", value, key));
95 prefix_relocation (string prefix)
97 if (be_verbose_global)
98 warning (_f ("Relocation: compile prefix=%s, new prefix=%s",
102 string bindir = prefix + "/bin";
103 string datadir = prefix + "/share";
104 string localedir = datadir + "/locale";
105 string lilypond_datadir = datadir + "/lilypond/";
107 if (is_dir (lilypond_datadir + "/" + TOPLEVEL_VERSION))
108 prefix_directory = lilypond_datadir + "/" + TOPLEVEL_VERSION;
109 else if (is_dir (lilypond_datadir + "/current"))
110 prefix_directory = lilypond_datadir + "/current";
113 if (is_dir (localedir))
114 bindtextdomain ("lilypond", localedir.c_str ());
117 prepend_env_path ("PATH", bindir);
121 UGH : this is a complete mess.
125 framework_relocation (string prefix)
127 if (be_verbose_global)
128 warning (_f ("Relocation: framework_prefix=%s", prefix));
130 sane_putenv ("INSTALLER_PREFIX", prefix, true);
132 read_relocation_dir (prefix + "/etc/relocate/");
134 string bindir = prefix + "/bin";
136 prepend_env_path ("PATH", bindir);
140 UGH : this is a complete mess.
143 setup_paths (char const *argv0_ptr)
145 File_name argv0_filename (argv0_ptr);
147 prefix_directory = LILYPOND_DATADIR;
149 && getenv ("LILYPOND_RELOCATE_PREFIX"))
151 prefix_directory = getenv ("LILYPOND_RELOCATE_PREFIX");
153 /* Normalize file name. */
154 prefix_directory = File_name (prefix_directory).to_string ();
155 #endif /* __MINGW32__ */
157 prefix_relocation (prefix_directory);
158 string bindir = prefix_directory + "/bin";
159 framework_relocation (bindir);
161 else if (relocate_binary)
164 if (argv0_filename.is_absolute ())
166 argv0_abs = argv0_filename.to_string ();
167 if (be_verbose_global)
168 warning (_f ("Relocation: is absolute: argv0=%s", argv0_ptr));
170 else if (argv0_filename.dir_.length ())
172 argv0_abs = get_working_directory ()
173 + "/" + string (argv0_filename.to_string ());
174 if (be_verbose_global)
175 warning (_f ("Relocation: from cwd: argv0=%s", argv0_ptr));
179 /* Find absolute ARGV0 name, using PATH. */
181 path.parse_path (getenv ("PATH"));
183 if (be_verbose_global)
184 warning (_f ("Relocation: from PATH=%s\nargv0=%s",
185 path.to_string ().c_str (), argv0_ptr));
188 argv0_abs = path.find (argv0_filename.to_string ());
189 #else /* __MINGW32__ */
190 char const *ext[] = {"exe", "", 0 };
191 argv0_abs = path.find (argv0_filename.to_string (), ext);
192 #endif /* __MINGW32__ */
194 if (argv0_abs.empty ())
195 programming_error ("can't find absolute argv0.");
198 string bindir = dir_name (argv0_abs);
199 string argv0_prefix = dir_name (bindir);
200 string compile_prefix = dir_name (dir_name (dir_name (prefix_directory)));
201 if (argv0_prefix != compile_prefix)
203 prefix_relocation (argv0_prefix);
204 prefix_directory = argv0_prefix;
206 if (argv0_prefix != compile_prefix || string (FRAMEWORKDIR) != "..")
208 framework_relocation (bindir + "/" + FRAMEWORKDIR);
209 prefix_directory = bindir + "/" + FRAMEWORKDIR;
213 /* FIXME: use LILYPOND_DATADIR. */
214 if (char const *env = getenv ("LILYPONDPREFIX"))
217 /* Normalize file name. */
218 prefix_directory = File_name (env).to_string ();
220 prefix_directory = env;
224 global_path.append ("");
228 When running from build dir, a full LILYPOND_PREFIX is set-up at
230 $(OUTBASE)/share/lilypond/TOPLEVEL_VERSION
232 This historical hack will allow the shorthand
234 LILYPONDPREFIX=out lily/out/lilypond ...
238 string build_prefix_current = prefix_directory + "/share/lilypond/" "current";
239 string build_prefix_version = prefix_directory + "/share/lilypond/" TOPLEVEL_VERSION;
240 if (is_dir (build_prefix_version.c_str ()))
241 prefix_directory = build_prefix_version;
242 else if (is_dir (build_prefix_current.c_str ()))
243 prefix_directory = build_prefix_current;
245 /* Adding mf/out make lilypond unchanged source directory, when setting
246 LILYPONDPREFIX to lilypond-x.y.z */
247 char const *suffixes[] = {"ly", "ps", "scm", 0 };
250 for (char const **s = suffixes; *s; s++)
252 string path = prefix_directory + to_string ('/') + string (*s);
253 dirs.push_back (path);
256 dirs.push_back (prefix_directory + "/fonts/otf/");
257 dirs.push_back (prefix_directory + "/fonts/type1/");
258 dirs.push_back (prefix_directory + "/fonts/svg/");
260 for (vsize i = 0; i < dirs.size (); i++)
261 global_path.prepend (dirs[i]);
267 expand_environment_variables (string orig)
269 const char *start_ptr = orig.c_str();
270 const char *ptr = orig.c_str();
271 size_t len = orig.length();
274 while (ptr < start_ptr + len)
276 char *dollar = strchr (ptr, '$');
280 char *start_var = dollar + 1;
281 char *end_var = start_var;
282 char *start_next = end_var;
284 out += string (ptr, dollar - ptr);
287 if (*start_var == '{')
291 end_var = strchr (start_var, '}');
295 end_var = start_var + len;
296 start_next = end_var;
300 start_next = end_var + 1;
306 Hmm. what to do for $1 , $~ etc.?
312 while (isalnum (*end_var) || *end_var == '_');
313 start_next = end_var;
316 if (start_var < end_var)
318 string var_name (start_var, end_var - start_var);
319 const char *value = getenv (var_name.c_str());
321 out += string (value);
343 while ((c = fgetc (f)) != EOF && c != '\n')
350 read_relocation_file (string filename)
352 if (be_verbose_global)
353 progress_indication (_f ("Relocation file %s\n", filename.c_str ()));
355 char const *cname = filename.c_str ();
356 FILE *f = fopen (cname, "r");
358 error (_f ("can't open file %s", cname));
362 string line = read_line (f);
363 size_t idx = line.find (' ');
367 string command = line.substr (0, idx);
368 line = line.substr (idx + 1);
372 idx = line.find ('=');
374 string variable = line.substr (0, idx);
375 string value = line.substr (idx + 1);
377 value = expand_environment_variables (value);
379 if (command == "set")
380 sane_putenv (variable.c_str(), value, true);
381 else if (command == "setdir")
382 set_env_dir (variable.c_str(), value);
383 else if (command == "setfile")
384 set_env_file (variable.c_str(), value);
385 else if (command == "prependdir")
386 prepend_env_path (variable.c_str (), value);
388 error (_f ("Unknown relocation command %s", command));
395 read_relocation_dir (string dirname)
397 if (DIR *dir = opendir (dirname.c_str ()))
398 while (struct dirent *ent = readdir (dir))
400 File_name name (ent->d_name);
401 if (name.ext_ == "reloc")
402 read_relocation_file (dirname + "/" + name.to_string ());