2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 2005--2012 Han-Wen Nienhuys <hanwen@xs4all.nl>
6 LilyPond is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 LilyPond is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
20 #include "relocate.hh"
24 /* TODO: autoconf support */
27 #include <sys/types.h>
35 #include "file-name.hh"
36 #include "file-path.hh"
37 #include "international.hh"
38 #include "lily-guile.hh"
39 #include "lily-version.hh"
44 #define FRAMEWORKDIR ".."
47 sane_putenv (char const *key, string value, bool overwrite)
49 if (overwrite || !getenv (key))
51 string combine = string (key) + "=" + value;
52 char *s = strdup (combine.c_str ());
54 debug_output (_f ("Setting %s to %s", key, value.c_str ())
57 int retval = putenv (s);
59 unfortunately, we can't portably free S here,
60 due to various bugs in glibc prior to 2.1.1
69 set_env_file (char const *key, string value, bool overwrite = false)
72 return sane_putenv (key, value, overwrite);
73 else if (is_loglevel (LOG_DEBUG))
74 // this warning should only be printed in debug mode!
75 warning (_f ("no such file: %s for %s", value, key));
80 set_env_dir (char const *key, string value)
83 return sane_putenv (key, value, false);
84 else if (is_loglevel (LOG_DEBUG))
85 // this warning should only be printed in debug mode!
86 warning (_f ("no such directory: %s for %s", value, key));
91 prepend_env_path (char const *key, string value)
95 debug_output (_f ("%s=%s (prepend)\n", key, value.c_str ()), false);
97 if (char const *cur = getenv (key))
98 value += to_string (PATHSEP) + cur;
100 return sane_putenv (key, value.c_str (), true);
102 else if (is_loglevel (LOG_DEBUG))
103 // this warning should only be printed in debug mode
104 warning (_f ("no such directory: %s for %s", value, key));
113 prefix_relocation (string prefix)
115 string bindir = prefix + "/bin";
116 string datadir = prefix + "/share";
117 string localedir = datadir + "/locale";
118 string package_datadir = datadir + "/lilypond/";
119 string old_lilypond_datadir = lilypond_datadir;
121 if (is_dir (package_datadir + "/" + TOPLEVEL_VERSION))
122 lilypond_datadir = package_datadir + "/" + TOPLEVEL_VERSION;
123 else if (is_dir (package_datadir + "/current"))
124 lilypond_datadir = package_datadir + "/current";
126 warning (_f ("not relocating, no %s/ or current/ found under %s",
127 TOPLEVEL_VERSION, package_datadir.c_str ()));
130 if (is_dir (localedir))
131 bindtextdomain ("lilypond", localedir.c_str ());
134 prepend_env_path ("PATH", bindir);
136 debug_output (_f ("Relocation: compile datadir=%s, new datadir=%s",
137 old_lilypond_datadir.c_str (),
138 lilypond_datadir.c_str ()));
142 UGH : this is a complete mess.
146 framework_relocation (string prefix)
148 debug_output (_f ("Relocation: framework_prefix=%s", prefix));
150 sane_putenv ("INSTALLER_PREFIX", prefix, true);
152 read_relocation_dir (prefix + "/etc/relocate/");
154 string bindir = prefix + "/bin";
156 prepend_env_path ("PATH", bindir);
160 UGH : this is a complete mess.
163 setup_paths (char const *argv0_ptr)
165 File_name argv0_filename (argv0_ptr);
169 string prefix_directory;
170 if (getenv ("LILYPOND_RELOCATE_PREFIX"))
172 prefix_directory = getenv ("LILYPOND_RELOCATE_PREFIX");
174 /* Normalize file name. */
175 prefix_directory = File_name (prefix_directory).to_string ();
176 #endif /* __MINGW32__ */
178 prefix_relocation (prefix_directory);
179 string bindir = prefix_directory + "/bin";
180 framework_relocation (bindir);
182 else if (relocate_binary)
185 if (argv0_filename.is_absolute ())
187 argv0_abs = argv0_filename.to_string ();
188 debug_output (_f ("Relocation: is absolute: argv0=%s\n", argv0_ptr));
190 else if (argv0_filename.dir_.length ())
192 argv0_abs = get_working_directory ()
193 + "/" + string (argv0_filename.to_string ());
194 debug_output (_f ("Relocation: from cwd: argv0=%s\n", argv0_ptr));
198 /* Find absolute ARGV0 name, using PATH. */
200 path.parse_path (getenv ("PATH"));
203 argv0_abs = path.find (argv0_filename.to_string ());
204 #else /* __MINGW32__ */
205 path.prepend (get_working_directory ());
206 char const *ext[] = {"exe", "", 0 };
207 argv0_abs = path.find (argv0_filename.to_string (), ext);
208 #endif /* __MINGW32__ */
210 debug_output (_f ("Relocation: from PATH=%s\nargv0=%s",
211 path.to_string ().c_str (), argv0_ptr), true);
213 if (argv0_abs.empty ())
214 programming_error ("cannot find absolute argv0");
217 string bindir = dir_name (argv0_abs);
218 string argv0_prefix = dir_name (bindir);
219 string compile_prefix = dir_name (dir_name (dir_name (lilypond_datadir)));
220 if (argv0_prefix != compile_prefix)
222 prefix_relocation (argv0_prefix);
223 prefix_directory = argv0_prefix;
225 if (argv0_prefix != compile_prefix || string (FRAMEWORKDIR) != "..")
227 framework_relocation (bindir + "/" + FRAMEWORKDIR);
228 prefix_directory = bindir + "/" + FRAMEWORKDIR;
232 lilypond_datadir = prefix_directory
233 + "/share/lilypond/" TOPLEVEL_VERSION;
236 if (getenv ("LILYPONDPREFIX"))
237 error (_ ("LILYPONDPREFIX is obsolete, use LILYPOND_DATADIR"));
239 if (char const *env = getenv ("LILYPOND_DATADIR"))
241 /* Normalize file name. */
242 lilypond_datadir = File_name (env).to_string ();
245 /* When running from build dir, a full LILYPOND_DATADIR is set-up at
246 $(OUTBASE)/{share, lib}/lilypond/current. Configure lily using
247 ./configure --prefix=$(pwd)/out */
248 string build_datadir_current = dir_name (lilypond_datadir) + "/current";
249 if (!is_dir (lilypond_datadir.c_str ())
250 && is_dir (build_datadir_current.c_str ()))
251 lilypond_datadir = build_datadir_current;
253 lilypond_datadir = File_name (lilypond_datadir).canonicalized ().to_string ();
255 global_path.append ("");
257 /* Adding mf/out make lilypond unchanged source directory, when setting
258 LILYPONDPREFIX to lilypond-x.y.z */
259 char const *suffixes[] = {"ly", "ps", "scm", 0 };
262 for (char const **s = suffixes; *s; s++)
264 string path = lilypond_datadir + to_string ('/') + string (*s);
265 dirs.push_back (path);
268 dirs.push_back (lilypond_datadir + "/fonts/otf/");
269 dirs.push_back (lilypond_datadir + "/fonts/type1/");
270 dirs.push_back (lilypond_datadir + "/fonts/svg/");
272 for (vsize i = 0; i < dirs.size (); i++)
273 global_path.prepend (dirs[i]);
277 expand_environment_variables (string orig)
279 char const *start_ptr = orig.c_str ();
280 char const *ptr = orig.c_str ();
281 size_t len = orig.length ();
284 while (ptr < start_ptr + len)
286 char const *dollar = strchr (ptr, '$');
290 char const *start_var = dollar + 1;
291 char const *end_var = start_var;
292 char const *start_next = end_var;
294 out += string (ptr, dollar - ptr);
297 if (*start_var == '{')
301 end_var = strchr (start_var, '}');
305 end_var = start_var + len;
306 start_next = end_var;
310 start_next = end_var + 1;
316 Hmm. what to do for $1 , $~ etc.?
322 while (isalnum (*end_var) || *end_var == '_');
323 start_next = end_var;
326 if (start_var < end_var)
328 string var_name (start_var, end_var - start_var);
329 char const *value = getenv (var_name.c_str ());
331 out += string (value);
346 // Ugh - very inefficient, but safer than fgets.
353 while ((c = fgetc (f)) != EOF && c != '\n')
360 read_relocation_file (string filename)
362 debug_output (_f ("Relocation file: %s", filename.c_str ()) + "\n");
363 char const *cname = filename.c_str ();
364 FILE *f = fopen (cname, "r");
366 error (_f ("cannot open file: `%s'", cname));
370 string line = read_line (f);
371 size_t idx = line.find (' ');
375 string command = line.substr (0, idx);
376 line = line.substr (idx + 1);
380 idx = line.find ('=');
382 string variable = line.substr (0, idx);
383 string value = line.substr (idx + 1);
385 value = expand_environment_variables (value);
387 if (command == "set")
388 sane_putenv (variable.c_str (), value, true);
389 else if (command == "setdir")
390 set_env_dir (variable.c_str (), value);
391 else if (command == "setfile")
392 set_env_file (variable.c_str (), value);
393 else if (command == "prependdir")
394 prepend_env_path (variable.c_str (), value);
396 error (_f ("Unknown relocation command %s", command));
403 read_relocation_dir (string dirname)
405 if (DIR *dir = opendir (dirname.c_str ()))
406 while (struct dirent *ent = readdir (dir))
408 File_name name (ent->d_name);
409 if (name.ext_ == "reloc")
410 read_relocation_file (dirname + "/" + name.to_string ());