2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 2005--2015 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"
49 #define FRAMEWORKDIR ".."
52 sane_putenv (char const *key, const string &value, bool overwrite)
54 if (overwrite || !getenv (key))
56 string combine = string (key) + "=" + value;
57 char *s = strdup (combine.c_str ());
59 debug_output (_f ("Setting %s to %s", key, value.c_str ())
62 int retval = putenv (s);
64 unfortunately, we can't portably free S here,
65 due to various bugs in glibc prior to 2.1.1
74 set_env_file (char const *key, const string &value, bool overwrite = false)
77 return sane_putenv (key, value, overwrite);
78 else if (is_loglevel (LOG_DEBUG))
79 // this warning should only be printed in debug mode!
80 warning (_f ("no such file: %s for %s", value, key));
85 set_env_dir (char const *key, const string &value)
88 return sane_putenv (key, value, false);
89 else if (is_loglevel (LOG_DEBUG))
90 // this warning should only be printed in debug mode!
91 warning (_f ("no such directory: %s for %s", value, key));
96 prepend_env_path (char const *key, string value)
100 debug_output (_f ("%s=%s (prepend)\n", key, value.c_str ()), false);
102 if (char const *cur = getenv (key))
103 value += to_string (PATHSEP) + cur;
105 return sane_putenv (key, value.c_str (), true);
107 else if (is_loglevel (LOG_DEBUG))
108 // this warning should only be printed in debug mode
109 warning (_f ("no such directory: %s for %s", value, key));
114 prefix_relocation (const string &prefix)
116 string bindir = prefix + "/bin";
117 string datadir = prefix + "/share";
118 string localedir = datadir + "/locale";
119 string package_datadir = datadir + "/lilypond/";
120 string old_lilypond_datadir = lilypond_datadir;
122 if (is_dir (package_datadir + "/" + TOPLEVEL_VERSION))
123 lilypond_datadir = package_datadir + "/" + TOPLEVEL_VERSION;
124 else if (is_dir (package_datadir + "/current"))
125 lilypond_datadir = package_datadir + "/current";
127 warning (_f ("not relocating, no %s/ or current/ found under %s",
128 TOPLEVEL_VERSION, package_datadir.c_str ()));
131 if (is_dir (localedir))
132 bindtextdomain ("lilypond", localedir.c_str ());
135 prepend_env_path ("PATH", bindir);
137 debug_output (_f ("Relocation: compile datadir=%s, new datadir=%s",
138 old_lilypond_datadir.c_str (),
139 lilypond_datadir.c_str ()));
143 UGH : this is a complete mess.
147 framework_relocation (const string &prefix)
149 debug_output (_f ("Relocation: framework_prefix=%s", prefix));
151 sane_putenv ("INSTALLER_PREFIX", prefix, true);
153 read_relocation_dir (prefix + "/etc/relocate/");
155 string bindir = prefix + "/bin";
157 prepend_env_path ("PATH", bindir);
161 UGH : this is a complete mess.
164 setup_paths (char const *argv0_ptr)
166 File_name argv0_filename (argv0_ptr);
170 string prefix_directory;
172 if (argv0_filename.is_absolute ())
174 argv0_abs = argv0_filename.to_string ();
175 debug_output (_f ("Relocation: is absolute: argv0=%s\n", argv0_ptr));
177 else if (argv0_filename.dir_.length ())
179 argv0_abs = get_working_directory ()
180 + "/" + string (argv0_filename.to_string ());
181 debug_output (_f ("Relocation : from cwd: argv0=%s\n", argv0_ptr));
185 /* Find absolute ARGV0 name, using PATH. */
187 char *p = getenv ("PATH");
192 argv0_abs = path.find (argv0_filename.to_string ());
193 #else /* __MINGW32__ */
194 path.prepend (get_working_directory ());
195 char const *ext[] = {"exe", "", 0 };
196 argv0_abs = path.find (argv0_filename.to_string (), ext);
197 #endif /* __MINGW32__ */
199 debug_output (_f ("Relocation: from PATH=%s\nargv0=%s\n",
200 path.to_string ().c_str (), argv0_ptr), true);
202 if (argv0_abs.empty ())
203 programming_error ("cannot find absolute argv0");
206 string bindir = dir_name (argv0_abs);
207 string argv0_prefix = dir_name (bindir);
208 string compile_prefix = dir_name (dir_name (dir_name (lilypond_datadir)));
209 if (argv0_prefix != compile_prefix)
211 prefix_relocation (argv0_prefix);
212 prefix_directory = argv0_prefix;
214 if (argv0_prefix != compile_prefix || string (FRAMEWORKDIR) != "..")
216 framework_relocation (bindir + "/" + FRAMEWORKDIR);
217 prefix_directory = bindir + "/" + FRAMEWORKDIR;
220 lilypond_datadir = prefix_directory
221 + "/share/lilypond/" TOPLEVEL_VERSION;
224 if (getenv ("LILYPONDPREFIX"))
225 error (_ ("LILYPONDPREFIX is obsolete, use LILYPOND_DATADIR"));
227 if (char const *env = getenv ("LILYPOND_DATADIR"))
229 /* Normalize file name. */
230 lilypond_datadir = File_name (env).to_string ();
233 /* When running from build dir, a full LILYPOND_DATADIR is set-up at
234 $(OUTBASE)/{share, lib}/lilypond/current. Configure lily using
235 ./configure --prefix=$(pwd)/out */
236 string build_datadir_current = dir_name (lilypond_datadir) + "/current";
237 if (!is_dir (lilypond_datadir.c_str ())
238 && is_dir (build_datadir_current.c_str ()))
239 lilypond_datadir = build_datadir_current;
241 lilypond_datadir = File_name (lilypond_datadir).canonicalized ().to_string ();
243 global_path.append ("");
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 = lilypond_datadir + to_string ('/') + string (*s);
253 dirs.push_back (path);
256 dirs.push_back (lilypond_datadir + "/fonts/otf/");
257 dirs.push_back (lilypond_datadir + "/fonts/type1/");
258 dirs.push_back (lilypond_datadir + "/fonts/svg/");
260 for (vsize i = 0; i < dirs.size (); i++)
261 global_path.prepend (dirs[i]);
265 expand_environment_variables (const string &orig)
267 char const *start_ptr = orig.c_str ();
268 char const *ptr = orig.c_str ();
269 size_t len = orig.length ();
272 while (ptr < start_ptr + len)
274 char const *dollar = strchr (ptr, '$');
278 char const *start_var = dollar + 1;
279 char const *end_var = start_var;
280 char const *start_next = end_var;
282 out += string (ptr, dollar - ptr);
285 if (*start_var == '{')
289 end_var = strchr (start_var, '}');
293 end_var = start_var + len;
294 start_next = end_var;
298 start_next = end_var + 1;
304 Hmm. what to do for $1 , $~ etc.?
310 while (isalnum (*end_var) || *end_var == '_');
311 start_next = end_var;
314 if (start_var < end_var)
316 string var_name (start_var, end_var - start_var);
317 char const *value = getenv (var_name.c_str ());
319 out += string (value);
334 // Ugh - very inefficient, but safer than fgets.
341 while ((c = fgetc (f)) != EOF && c != '\n')
348 read_relocation_file (const string &filename)
350 debug_output (_f ("Relocation file: %s", filename.c_str ()) + "\n");
351 char const *cname = filename.c_str ();
352 FILE *f = fopen (cname, "r");
354 error (_f ("cannot open file: `%s'", cname));
358 string line = read_line (f);
359 size_t idx = line.find (' ');
363 string command = line.substr (0, idx);
364 line = line.substr (idx + 1);
368 idx = line.find ('=');
370 string variable = line.substr (0, idx);
371 string value = line.substr (idx + 1);
373 value = expand_environment_variables (value);
375 if (command == "set")
376 sane_putenv (variable.c_str (), value, true);
377 else if (command == "setdir")
378 set_env_dir (variable.c_str (), value);
379 else if (command == "setfile")
380 set_env_file (variable.c_str (), value);
381 else if (command == "prependdir")
382 prepend_env_path (variable.c_str (), value);
384 error (_f ("Unknown relocation command %s", command));
391 read_relocation_dir (const string &dirname)
393 if (DIR *dir = opendir (dirname.c_str ()))
394 while (struct dirent *ent = readdir (dir))
396 File_name name (ent->d_name);
397 if (name.ext_ == "reloc")
398 read_relocation_file (dirname + "/" + name.to_string ());