2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 2005--2011 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, string value, bool overwrite)
47 if (overwrite || !getenv (key))
49 string combine = string (key) + "=" + value;
50 char *s = strdup (combine.c_str ());
52 if (be_verbose_global)
53 progress_indication (_f ("Setting %s to %s", key, value.c_str ())
56 int retval = putenv (s);
58 unfortunately, we can't portably free S here,
59 due to various bugs in glibc prior to 2.1.1
68 set_env_file (char const *key, string value, bool overwrite = false)
71 return sane_putenv (key, value, overwrite);
72 else if (be_verbose_global)
73 warning (_f ("no such file: %s for %s", value, key));
78 set_env_dir (char const *key, string value)
81 return sane_putenv (key, value, false);
82 else if (be_verbose_global)
83 warning (_f ("no such directory: %s for %s", value, key));
88 prepend_env_path (char const *key, string value)
92 if (be_verbose_global)
93 progress_indication (_f ("%s=%s (prepend)\n", key, value.c_str ()));
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 (be_verbose_global)
101 warning (_f ("no such directory: %s for %s", value, key));
110 prefix_relocation (string prefix)
112 string bindir = prefix + "/bin";
113 string datadir = prefix + "/share";
114 string localedir = datadir + "/locale";
115 string package_datadir = datadir + "/lilypond/";
116 string old_lilypond_datadir = lilypond_datadir;
118 if (is_dir (package_datadir + "/" + TOPLEVEL_VERSION))
119 lilypond_datadir = package_datadir + "/" + TOPLEVEL_VERSION;
120 else if (is_dir (package_datadir + "/current"))
121 lilypond_datadir = package_datadir + "/current";
123 warning (_f ("not relocating, no %s/ or current/ found under %s",
124 TOPLEVEL_VERSION, package_datadir.c_str ()));
127 if (is_dir (localedir))
128 bindtextdomain ("lilypond", localedir.c_str ());
131 prepend_env_path ("PATH", bindir);
133 if (be_verbose_global)
134 warning (_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 (string prefix)
146 if (be_verbose_global)
147 warning (_f ("Relocation: framework_prefix=%s", prefix));
149 sane_putenv ("INSTALLER_PREFIX", prefix, true);
151 read_relocation_dir (prefix + "/etc/relocate/");
153 string bindir = prefix + "/bin";
155 prepend_env_path ("PATH", bindir);
159 UGH : this is a complete mess.
162 setup_paths (char const *argv0_ptr)
164 File_name argv0_filename (argv0_ptr);
168 string prefix_directory;
169 if (getenv ("LILYPOND_RELOCATE_PREFIX"))
171 prefix_directory = getenv ("LILYPOND_RELOCATE_PREFIX");
173 /* Normalize file name. */
174 prefix_directory = File_name (prefix_directory).to_string ();
175 #endif /* __MINGW32__ */
177 prefix_relocation (prefix_directory);
178 string bindir = prefix_directory + "/bin";
179 framework_relocation (bindir);
181 else if (relocate_binary)
184 if (argv0_filename.is_absolute ())
186 argv0_abs = argv0_filename.to_string ();
187 if (be_verbose_global)
188 warning (_f ("Relocation: is absolute: argv0=%s", argv0_ptr));
190 else if (argv0_filename.dir_.length ())
192 argv0_abs = get_working_directory ()
193 + "/" + string (argv0_filename.to_string ());
194 if (be_verbose_global)
195 warning (_f ("Relocation: from cwd: argv0=%s", argv0_ptr));
199 /* Find absolute ARGV0 name, using PATH. */
201 path.parse_path (getenv ("PATH"));
204 argv0_abs = path.find (argv0_filename.to_string ());
205 #else /* __MINGW32__ */
206 path.prepend (get_working_directory ());
207 char const *ext[] = {"exe", "", 0 };
208 argv0_abs = path.find (argv0_filename.to_string (), ext);
209 #endif /* __MINGW32__ */
211 if (be_verbose_global)
212 warning (_f ("Relocation: from PATH=%s\nargv0=%s",
213 path.to_string ().c_str (), argv0_ptr));
215 if (argv0_abs.empty ())
216 programming_error ("cannot find absolute argv0");
219 string bindir = dir_name (argv0_abs);
220 string argv0_prefix = dir_name (bindir);
221 string compile_prefix = dir_name (dir_name (dir_name (lilypond_datadir)));
222 if (argv0_prefix != compile_prefix)
224 prefix_relocation (argv0_prefix);
225 prefix_directory = argv0_prefix;
227 if (argv0_prefix != compile_prefix || string (FRAMEWORKDIR) != "..")
229 framework_relocation (bindir + "/" + FRAMEWORKDIR);
230 prefix_directory = bindir + "/" + FRAMEWORKDIR;
234 lilypond_datadir = prefix_directory
235 + "/share/lilypond/" TOPLEVEL_VERSION;
238 if (getenv ("LILYPONDPREFIX"))
239 error (_ ("LILYPONDPREFIX is obsolete, use LILYPOND_DATADIR"));
241 if (char const *env = getenv ("LILYPOND_DATADIR"))
243 /* Normalize file name. */
244 lilypond_datadir = File_name (env).to_string ();
247 /* When running from build dir, a full LILYPOND_DATADIR is set-up at
248 $(OUTBASE)/{share, lib}/lilypond/current. Configure lily using
249 ./configure --prefix=$(pwd)/out */
250 string build_datadir_current = dir_name (lilypond_datadir) + "/current";
251 if (!is_dir (lilypond_datadir.c_str ())
252 && is_dir (build_datadir_current.c_str ()))
253 lilypond_datadir = build_datadir_current;
255 lilypond_datadir = File_name (lilypond_datadir).canonicalized ().to_string ();
257 global_path.append ("");
259 /* Adding mf/out make lilypond unchanged source directory, when setting
260 LILYPONDPREFIX to lilypond-x.y.z */
261 char const *suffixes[] = {"ly", "ps", "scm", 0 };
264 for (char const **s = suffixes; *s; s++)
266 string path = lilypond_datadir + to_string ('/') + string (*s);
267 dirs.push_back (path);
270 dirs.push_back (lilypond_datadir + "/fonts/otf/");
271 dirs.push_back (lilypond_datadir + "/fonts/type1/");
272 dirs.push_back (lilypond_datadir + "/fonts/svg/");
274 for (vsize i = 0; i < dirs.size (); i++)
275 global_path.prepend (dirs[i]);
279 expand_environment_variables (string orig)
281 char const *start_ptr = orig.c_str ();
282 char const *ptr = orig.c_str ();
283 size_t len = orig.length ();
286 while (ptr < start_ptr + len)
288 char const *dollar = strchr (ptr, '$');
292 char const *start_var = dollar + 1;
293 char const *end_var = start_var;
294 char const *start_next = end_var;
296 out += string (ptr, dollar - ptr);
299 if (*start_var == '{')
303 end_var = strchr (start_var, '}');
307 end_var = start_var + len;
308 start_next = end_var;
312 start_next = end_var + 1;
318 Hmm. what to do for $1 , $~ etc.?
324 while (isalnum (*end_var) || *end_var == '_');
325 start_next = end_var;
328 if (start_var < end_var)
330 string var_name (start_var, end_var - start_var);
331 char const *value = getenv (var_name.c_str ());
333 out += string (value);
348 // Ugh - very inefficient, but safer than fgets.
355 while ((c = fgetc (f)) != EOF && c != '\n')
362 read_relocation_file (string filename)
364 if (be_verbose_global)
365 progress_indication (_f ("Relocation file: %s", filename.c_str ())
368 char const *cname = filename.c_str ();
369 FILE *f = fopen (cname, "r");
371 error (_f ("cannot open file: `%s'", cname));
375 string line = read_line (f);
376 size_t idx = line.find (' ');
380 string command = line.substr (0, idx);
381 line = line.substr (idx + 1);
385 idx = line.find ('=');
387 string variable = line.substr (0, idx);
388 string value = line.substr (idx + 1);
390 value = expand_environment_variables (value);
392 if (command == "set")
393 sane_putenv (variable.c_str (), value, true);
394 else if (command == "setdir")
395 set_env_dir (variable.c_str (), value);
396 else if (command == "setfile")
397 set_env_file (variable.c_str (), value);
398 else if (command == "prependdir")
399 prepend_env_path (variable.c_str (), value);
401 error (_f ("Unknown relocation command %s", command));
408 read_relocation_dir (string dirname)
410 if (DIR *dir = opendir (dirname.c_str ()))
411 while (struct dirent *ent = readdir (dir))
413 File_name name (ent->d_name);
414 if (name.ext_ == "reloc")
415 read_relocation_file (dirname + "/" + name.to_string ());