2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 2005--2014 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 */
26 #include <sys/types.h>
33 #include "file-name.hh"
34 #include "file-path.hh"
35 #include "international.hh"
36 #include "lily-guile.hh"
37 #include "lily-version.hh"
42 #define FRAMEWORKDIR ".."
45 sane_putenv (char const *key, const string &value, bool overwrite)
47 if (overwrite || !getenv (key))
49 string combine = string (key) + "=" + value;
50 char *s = strdup (combine.c_str ());
52 debug_output (_f ("Setting %s to %s", key, value.c_str ())
55 int retval = putenv (s);
57 unfortunately, we can't portably free S here,
58 due to various bugs in glibc prior to 2.1.1
67 set_env_file (char const *key, const string &value, bool overwrite = false)
70 return sane_putenv (key, value, overwrite);
71 else if (is_loglevel (LOG_DEBUG))
72 // this warning should only be printed in debug mode!
73 warning (_f ("no such file: %s for %s", value, key));
78 set_env_dir (char const *key, const string &value)
81 return sane_putenv (key, value, false);
82 else if (is_loglevel (LOG_DEBUG))
83 // this warning should only be printed in debug mode!
84 warning (_f ("no such directory: %s for %s", value, key));
89 prepend_env_path (char const *key, string value)
93 debug_output (_f ("%s=%s (prepend)\n", key, value.c_str ()), false);
95 if (char const *cur = getenv (key))
96 value += to_string (PATHSEP) + cur;
98 return sane_putenv (key, value.c_str (), true);
100 else if (is_loglevel (LOG_DEBUG))
101 // this warning should only be printed in debug mode
102 warning (_f ("no such directory: %s for %s", value, key));
111 prefix_relocation (const string &prefix)
113 string bindir = prefix + "/bin";
114 string datadir = prefix + "/share";
115 string localedir = datadir + "/locale";
116 string package_datadir = datadir + "/lilypond/";
117 string old_lilypond_datadir = lilypond_datadir;
119 if (is_dir (package_datadir + "/" + TOPLEVEL_VERSION))
120 lilypond_datadir = package_datadir + "/" + TOPLEVEL_VERSION;
121 else if (is_dir (package_datadir + "/current"))
122 lilypond_datadir = package_datadir + "/current";
124 warning (_f ("not relocating, no %s/ or current/ found under %s",
125 TOPLEVEL_VERSION, package_datadir.c_str ()));
128 if (is_dir (localedir))
129 bindtextdomain ("lilypond", localedir.c_str ());
132 prepend_env_path ("PATH", bindir);
134 debug_output (_f ("Relocation: compile datadir=%s, new datadir=%s",
135 old_lilypond_datadir.c_str (),
136 lilypond_datadir.c_str ()));
140 UGH : this is a complete mess.
144 framework_relocation (const string &prefix)
146 debug_output (_f ("Relocation: framework_prefix=%s", prefix));
148 sane_putenv ("INSTALLER_PREFIX", prefix, true);
150 read_relocation_dir (prefix + "/etc/relocate/");
152 string bindir = prefix + "/bin";
154 prepend_env_path ("PATH", bindir);
158 UGH : this is a complete mess.
161 setup_paths (char const *argv0_ptr)
163 File_name argv0_filename (argv0_ptr);
167 string prefix_directory;
169 if (argv0_filename.is_absolute ())
171 argv0_abs = argv0_filename.to_string ();
172 debug_output (_f ("Relocation: is absolute: argv0=%s\n", argv0_ptr));
174 else if (argv0_filename.dir_.length ())
176 argv0_abs = get_working_directory ()
177 + "/" + string (argv0_filename.to_string ());
178 debug_output (_f ("Relocation : from cwd: argv0=%s\n", argv0_ptr));
182 /* Find absolute ARGV0 name, using PATH. */
184 path.parse_path (getenv ("PATH"));
187 argv0_abs = path.find (argv0_filename.to_string ());
188 #else /* __MINGW32__ */
189 path.prepend (get_working_directory ());
190 char const *ext[] = {"exe", "", 0 };
191 argv0_abs = path.find (argv0_filename.to_string (), ext);
192 #endif /* __MINGW32__ */
194 debug_output (_f ("Relocation: from PATH=%s\nargv0=%s\n",
195 path.to_string ().c_str (), argv0_ptr), true);
197 if (argv0_abs.empty ())
198 programming_error ("cannot find absolute argv0");
201 string bindir = dir_name (argv0_abs);
202 string argv0_prefix = dir_name (bindir);
203 string compile_prefix = dir_name (dir_name (dir_name (lilypond_datadir)));
204 if (argv0_prefix != compile_prefix)
206 prefix_relocation (argv0_prefix);
207 prefix_directory = argv0_prefix;
209 if (argv0_prefix != compile_prefix || string (FRAMEWORKDIR) != "..")
211 framework_relocation (bindir + "/" + FRAMEWORKDIR);
212 prefix_directory = bindir + "/" + FRAMEWORKDIR;
215 lilypond_datadir = prefix_directory
216 + "/share/lilypond/" TOPLEVEL_VERSION;
219 if (getenv ("LILYPONDPREFIX"))
220 error (_ ("LILYPONDPREFIX is obsolete, use LILYPOND_DATADIR"));
222 if (char const *env = getenv ("LILYPOND_DATADIR"))
224 /* Normalize file name. */
225 lilypond_datadir = File_name (env).to_string ();
228 /* When running from build dir, a full LILYPOND_DATADIR is set-up at
229 $(OUTBASE)/{share, lib}/lilypond/current. Configure lily using
230 ./configure --prefix=$(pwd)/out */
231 string build_datadir_current = dir_name (lilypond_datadir) + "/current";
232 if (!is_dir (lilypond_datadir.c_str ())
233 && is_dir (build_datadir_current.c_str ()))
234 lilypond_datadir = build_datadir_current;
236 lilypond_datadir = File_name (lilypond_datadir).canonicalized ().to_string ();
238 global_path.append ("");
240 /* Adding mf/out make lilypond unchanged source directory, when setting
241 LILYPONDPREFIX to lilypond-x.y.z */
242 char const *suffixes[] = {"ly", "ps", "scm", 0 };
245 for (char const **s = suffixes; *s; s++)
247 string path = lilypond_datadir + to_string ('/') + string (*s);
248 dirs.push_back (path);
251 dirs.push_back (lilypond_datadir + "/fonts/otf/");
252 dirs.push_back (lilypond_datadir + "/fonts/type1/");
253 dirs.push_back (lilypond_datadir + "/fonts/svg/");
255 for (vsize i = 0; i < dirs.size (); i++)
256 global_path.prepend (dirs[i]);
260 expand_environment_variables (const string &orig)
262 char const *start_ptr = orig.c_str ();
263 char const *ptr = orig.c_str ();
264 size_t len = orig.length ();
267 while (ptr < start_ptr + len)
269 char const *dollar = strchr (ptr, '$');
273 char const *start_var = dollar + 1;
274 char const *end_var = start_var;
275 char const *start_next = end_var;
277 out += string (ptr, dollar - ptr);
280 if (*start_var == '{')
284 end_var = strchr (start_var, '}');
288 end_var = start_var + len;
289 start_next = end_var;
293 start_next = end_var + 1;
299 Hmm. what to do for $1 , $~ etc.?
305 while (isalnum (*end_var) || *end_var == '_');
306 start_next = end_var;
309 if (start_var < end_var)
311 string var_name (start_var, end_var - start_var);
312 char const *value = getenv (var_name.c_str ());
314 out += string (value);
329 // Ugh - very inefficient, but safer than fgets.
336 while ((c = fgetc (f)) != EOF && c != '\n')
343 read_relocation_file (const string &filename)
345 debug_output (_f ("Relocation file: %s", filename.c_str ()) + "\n");
346 char const *cname = filename.c_str ();
347 FILE *f = fopen (cname, "r");
349 error (_f ("cannot open file: `%s'", cname));
353 string line = read_line (f);
354 size_t idx = line.find (' ');
358 string command = line.substr (0, idx);
359 line = line.substr (idx + 1);
363 idx = line.find ('=');
365 string variable = line.substr (0, idx);
366 string value = line.substr (idx + 1);
368 value = expand_environment_variables (value);
370 if (command == "set")
371 sane_putenv (variable.c_str (), value, true);
372 else if (command == "setdir")
373 set_env_dir (variable.c_str (), value);
374 else if (command == "setfile")
375 set_env_file (variable.c_str (), value);
376 else if (command == "prependdir")
377 prepend_env_path (variable.c_str (), value);
379 error (_f ("Unknown relocation command %s", command));
386 read_relocation_dir (const string &dirname)
388 if (DIR *dir = opendir (dirname.c_str ()))
389 while (struct dirent *ent = readdir (dir))
391 File_name name (ent->d_name);
392 if (name.ext_ == "reloc")
393 read_relocation_file (dirname + "/" + name.to_string ());