2 relocate.cc -- implement relocation based on argv0
4 source file of the GNU LilyPond music typesetter
6 (c) 2005--2007 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" , key, value.c_str ())
47 int retval = putenv (s);
49 unfortunately, we can't portably free S here,
50 due to various bugs in glibc prior to 2.1.1
59 set_env_file (char const *key, string value, bool overwrite = false)
62 return sane_putenv (key, value, overwrite);
63 else if (be_verbose_global)
64 warning (_f ("no such file: %s for %s", value, key));
69 set_env_dir (char const *key, string value)
72 return sane_putenv (key, value, false);
73 else if (be_verbose_global)
74 warning (_f ("no such directory: %s for %s", value, key));
79 prepend_env_path (char const *key, string value)
83 if (be_verbose_global)
84 progress_indication (_f ("%s=%s (prepend)\n", key, value.c_str ()));
86 if (char const *cur = getenv (key))
87 value += to_string (PATHSEP) + cur;
89 return sane_putenv (key, value.c_str (), true);
91 else if (be_verbose_global)
92 warning (_f ("no such directory: %s for %s", value, key));
101 prefix_relocation (string prefix)
103 if (be_verbose_global)
104 warning (_f ("Relocation: compile prefix=%s, new prefix=%s",
108 string bindir = prefix + "/bin";
109 string datadir = prefix + "/share";
110 string localedir = datadir + "/locale";
111 string lilypond_datadir = datadir + "/lilypond/";
113 if (is_dir (lilypond_datadir + "/" + TOPLEVEL_VERSION))
114 prefix_directory = lilypond_datadir + "/" + TOPLEVEL_VERSION;
115 else if (is_dir (lilypond_datadir + "/current"))
116 prefix_directory = lilypond_datadir + "/current";
119 if (is_dir (localedir))
120 bindtextdomain ("lilypond", localedir.c_str ());
123 prepend_env_path ("PATH", bindir);
127 UGH : this is a complete mess.
131 framework_relocation (string prefix)
133 if (be_verbose_global)
134 warning (_f ("Relocation: framework_prefix=%s", prefix));
136 sane_putenv ("INSTALLER_PREFIX", prefix, true);
138 read_relocation_dir (prefix + "/etc/relocate/");
140 string bindir = prefix + "/bin";
142 prepend_env_path ("PATH", bindir);
146 UGH : this is a complete mess.
149 setup_paths (char const *argv0_ptr)
151 File_name argv0_filename (argv0_ptr);
153 prefix_directory = LILYPOND_DATADIR;
155 && getenv ("LILYPOND_RELOCATE_PREFIX"))
157 prefix_directory = getenv ("LILYPOND_RELOCATE_PREFIX");
159 /* Normalize file name. */
160 prefix_directory = File_name (prefix_directory).to_string ();
161 #endif /* __MINGW32__ */
163 prefix_relocation (prefix_directory);
164 string bindir = prefix_directory + "/bin";
165 framework_relocation (bindir);
167 else if (relocate_binary)
170 if (argv0_filename.is_absolute ())
172 argv0_abs = argv0_filename.to_string ();
173 if (be_verbose_global)
174 warning (_f ("Relocation: is absolute: argv0=%s", argv0_ptr));
176 else if (argv0_filename.dir_.length ())
178 argv0_abs = get_working_directory ()
179 + "/" + string (argv0_filename.to_string ());
180 if (be_verbose_global)
181 warning (_f ("Relocation: from cwd: argv0=%s", argv0_ptr));
185 /* Find absolute ARGV0 name, using PATH. */
187 path.parse_path (getenv ("PATH"));
189 if (be_verbose_global)
190 warning (_f ("Relocation: from PATH=%s\nargv0=%s",
191 path.to_string ().c_str (), argv0_ptr));
194 argv0_abs = path.find (argv0_filename.to_string ());
195 #else /* __MINGW32__ */
196 char const *ext[] = {"exe", "", 0 };
197 argv0_abs = path.find (argv0_filename.to_string (), ext);
198 #endif /* __MINGW32__ */
200 if (argv0_abs.empty ())
201 programming_error ("cannot find absolute argv0");
204 string bindir = dir_name (argv0_abs);
205 string argv0_prefix = dir_name (bindir);
206 string compile_prefix = dir_name (dir_name (dir_name (prefix_directory)));
207 if (argv0_prefix != compile_prefix)
209 prefix_relocation (argv0_prefix);
210 prefix_directory = argv0_prefix;
212 if (argv0_prefix != compile_prefix || string (FRAMEWORKDIR) != "..")
214 framework_relocation (bindir + "/" + FRAMEWORKDIR);
215 prefix_directory = bindir + "/" + FRAMEWORKDIR;
219 /* FIXME: use LILYPOND_DATADIR. */
220 if (char const *env = getenv ("LILYPONDPREFIX"))
223 /* Normalize file name. */
224 prefix_directory = File_name (env).to_string ();
226 prefix_directory = env;
230 global_path.append ("");
234 When running from build dir, a full LILYPOND_PREFIX is set-up at
236 $(OUTBASE)/{share,lib}/lilypond/current
240 string build_prefix_current = prefix_directory + "/share/lilypond/" "current";
241 string build_prefix_version = prefix_directory + "/share/lilypond/" TOPLEVEL_VERSION;
242 if (is_dir (build_prefix_version.c_str ()))
243 prefix_directory = build_prefix_version;
244 else if (is_dir (build_prefix_current.c_str ()))
245 prefix_directory = build_prefix_current;
247 /* Adding mf/out make lilypond unchanged source directory, when setting
248 LILYPONDPREFIX to lilypond-x.y.z */
249 char const *suffixes[] = {"ly", "ps", "scm", 0 };
252 for (char const **s = suffixes; *s; s++)
254 string path = prefix_directory + to_string ('/') + string (*s);
255 dirs.push_back (path);
258 dirs.push_back (prefix_directory + "/fonts/otf/");
259 dirs.push_back (prefix_directory + "/fonts/type1/");
260 dirs.push_back (prefix_directory + "/fonts/svg/");
262 for (vsize i = 0; i < dirs.size (); i++)
263 global_path.prepend (dirs[i]);
269 expand_environment_variables (string orig)
271 const char *start_ptr = orig.c_str();
272 const char *ptr = orig.c_str();
273 size_t len = orig.length();
276 while (ptr < start_ptr + len)
278 char *dollar = strchr (ptr, '$');
282 char *start_var = dollar + 1;
283 char *end_var = start_var;
284 char *start_next = end_var;
286 out += string (ptr, dollar - ptr);
289 if (*start_var == '{')
293 end_var = strchr (start_var, '}');
297 end_var = start_var + len;
298 start_next = end_var;
302 start_next = end_var + 1;
308 Hmm. what to do for $1 , $~ etc.?
314 while (isalnum (*end_var) || *end_var == '_');
315 start_next = end_var;
318 if (start_var < end_var)
320 string var_name (start_var, end_var - start_var);
321 const char *value = getenv (var_name.c_str());
323 out += string (value);
345 while ((c = fgetc (f)) != EOF && c != '\n')
352 read_relocation_file (string filename)
354 if (be_verbose_global)
355 progress_indication (_f ("Relocation file: %s", filename.c_str ())
358 char const *cname = filename.c_str ();
359 FILE *f = fopen (cname, "r");
361 error (_f ("cannot open file: `%s'", cname));
365 string line = read_line (f);
366 size_t idx = line.find (' ');
370 string command = line.substr (0, idx);
371 line = line.substr (idx + 1);
375 idx = line.find ('=');
377 string variable = line.substr (0, idx);
378 string value = line.substr (idx + 1);
380 value = expand_environment_variables (value);
382 if (command == "set")
383 sane_putenv (variable.c_str(), value, true);
384 else if (command == "setdir")
385 set_env_dir (variable.c_str(), value);
386 else if (command == "setfile")
387 set_env_file (variable.c_str(), value);
388 else if (command == "prependdir")
389 prepend_env_path (variable.c_str (), value);
391 error (_f ("Unknown relocation command %s", command));
398 read_relocation_dir (string dirname)
400 if (DIR *dir = opendir (dirname.c_str ()))
401 while (struct dirent *ent = readdir (dir))
403 File_name name (ent->d_name);
404 if (name.ext_ == "reloc")
405 read_relocation_file (dirname + "/" + name.to_string ());