2 relocate.cc -- implement relocation based on argv0
4 source file of the GNU LilyPond music typesetter
6 (c) 2005--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
10 #include "relocate.hh"
21 #include "file-name.hh"
22 #include "file-path.hh"
23 #include "international.hh"
24 #include "lily-guile.hh"
25 #include "lily-version.hh"
30 const char * FRAMEWORKDIR = "..";
34 sane_putenv (char const *key, string value, bool overwrite)
36 if (overwrite || !getenv (key))
38 string combine = string (key) + "=" + value;
39 char *s = strdup (combine.c_str ());
47 set_env_file (char const *key, string value, bool overwrite = false)
50 return sane_putenv (key, value, overwrite);
51 else if (be_verbose_global)
52 warning (_f ("no such file: %s for %s", value, key));
57 set_env_dir (char const *key, string value)
60 return sane_putenv (key, value, false);
61 else if (be_verbose_global)
62 warning (_f ("no such directory: %s for %s", value, key));
67 prepend_env_path (char const *key, string value)
71 if (be_verbose_global)
72 progress_indication (_f ("%s=%s\n", key, value.c_str ()));
74 if (char const *cur = getenv (key))
75 value += to_string (PATHSEP) + cur;
77 return sane_putenv (key, value.c_str (), true);
79 else if (be_verbose_global)
80 warning (_f ("no such directory: %s for %s", value, key));
85 dir_name (string const file_name)
88 replace_all (s, '\\', '/');
89 ssize n = s.length ();
90 if (n && s[n - 1] == '/')
92 s = s.substr (0, s.rfind ('/'));
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 framework_relocation (string prefix)
129 if (be_verbose_global)
130 warning (_f ("Relocation: framework_prefix=%s", prefix));
132 string bindir = prefix + "/bin";
133 string datadir = prefix + "/share";
134 string libdir = prefix + "/lib";
135 string sysconfdir = prefix + "/etc";
137 /* need otherwise dynamic .so's aren't found. */
138 prepend_env_path ("DYLD_LIBRARY_PATH", libdir);
140 set_env_file ("FONTCONFIG_FILE", sysconfdir + "/fonts/fonts.conf", true);
141 set_env_dir ("FONTCONFIG_PATH", sysconfdir + "/fonts");
144 char font_dir[PATH_MAX];
145 ExpandEnvironmentStrings ("%windir%/fonts", font_dir, sizeof (font_dir));
146 prepend_env_path ("GS_FONTPATH", font_dir);
150 #ifdef GHOSTSCRIPT_VERSION
157 if (char const *cur = getenv ("LILYPOND_GS_VERSION"))
160 prepend_env_path ("GS_FONTPATH", datadir + "/ghostscript/" + gs_version + "/fonts");
161 prepend_env_path ("GS_LIB", datadir + "/ghostscript/" + gs_version + "/Resource");
162 prepend_env_path ("GS_LIB", datadir + "/ghostscript/" + gs_version + "/lib");
164 prepend_env_path ("GS_FONTPATH", datadir + "/gs/fonts");
165 prepend_env_path ("GS_LIB", datadir + "/gs/Resource");
166 prepend_env_path ("GS_LIB", datadir + "/gs/lib");
168 prepend_env_path ("GUILE_LOAD_PATH", datadir
169 + to_string ("/guile/%d.%d",
170 SCM_MAJOR_VERSION, SCM_MINOR_VERSION));
172 set_env_file ("PANGO_RC_FILE", sysconfdir + "/pango/pangorc");
173 set_env_dir ("PANGO_PREFIX", prefix);
175 prepend_env_path ("PATH", bindir);
179 get_working_directory ()
182 getcwd (cwd, PATH_MAX);
188 setup_paths (char const *argv0_ptr)
190 File_name argv0_filename (argv0_ptr);
192 prefix_directory = LILYPOND_DATADIR;
194 && getenv ("LILYPOND_RELOCATE_PREFIX"))
196 string prefix = getenv ("LILYPOND_RELOCATE_PREFIX");
198 /* Normalize file name. */
199 prefix = File_name (prefix).to_string ();
200 #endif /* __MINGW32__ */
201 prefix_relocation (prefix);
202 string bindir = prefix + "/bin";
203 framework_relocation (bindir);
205 else if (relocate_binary)
208 if (argv0_filename.is_absolute ())
210 argv0_abs = argv0_filename.to_string ();
211 if (be_verbose_global)
212 warning (_f ("Relocation: is absolute: argv0=%s", argv0_ptr));
214 else if (argv0_filename.dir_.length ())
216 argv0_abs = get_working_directory ()
217 + "/" + string (argv0_filename.to_string ());
218 if (be_verbose_global)
219 warning (_f ("Relocation: from cwd: argv0=%s", argv0_ptr));
223 /* Find absolute ARGV0 name, using PATH. */
225 path.parse_path (getenv ("PATH"));
227 if (be_verbose_global)
228 warning (_f ("Relocation: from PATH=%s\nargv0=%s",
229 path.to_string ().c_str (), argv0_ptr));
232 argv0_abs = path.find (argv0_filename.to_string ());
233 #else /* __MINGW32__ */
234 char const *ext[] = {"exe", "", 0 };
235 argv0_abs = path.find (argv0_filename.to_string (), ext);
236 #endif /* __MINGW32__ */
238 if (argv0_abs.empty ())
239 programming_error ("can't find absolute argv0.");
242 string bindir = dir_name (argv0_abs);
243 string argv0_prefix = dir_name (bindir);
244 string compile_prefix = dir_name (dir_name (dir_name (prefix_directory)));
245 if (argv0_prefix != compile_prefix)
246 prefix_relocation (argv0_prefix);
247 if (argv0_prefix != compile_prefix || string (FRAMEWORKDIR) != "..")
248 framework_relocation (bindir + "/" + FRAMEWORKDIR);
251 /* FIXME: use LILYPOND_DATADIR. */
252 if (char const *env = getenv ("LILYPONDPREFIX"))
256 /* Normalize file name. */
257 prefix_directory = File_name (env).to_string ();
259 prefix_directory = env;
263 global_path.append ("");
267 When running from build dir, a full LILYPOND_PREFIX is set-up at
269 $(OUTBASE)/share/lilypond/TOPLEVEL_VERSION
271 This historical hack will allow the shorthand
273 LILYPONDPREFIX=out lily/out/lilypond ...
278 string build_prefix_current = prefix_directory + "/share/lilypond/" "current";
279 string build_prefix_version = prefix_directory + "/share/lilypond/" TOPLEVEL_VERSION;
280 if (stat (build_prefix_version.c_str (), &statbuf) == 0)
281 prefix_directory = build_prefix_version;
282 else if (stat (build_prefix_current.c_str (), &statbuf) == 0)
283 prefix_directory = build_prefix_current;
286 /* Adding mf/out make lilypond unchanged source directory, when setting
287 LILYPONDPREFIX to lilypond-x.y.z */
288 char *suffixes[] = {"ly", "ps", "scm", 0 };
292 for (char **s = suffixes; *s; s++)
294 string path = prefix_directory + to_string ('/') + string (*s);
295 dirs.push_back (path);
299 dirs.push_back (prefix_directory + "/fonts/otf/");
300 dirs.push_back (prefix_directory + "/fonts/type1/");
301 dirs.push_back (prefix_directory + "/fonts/svg/");
303 for (vsize i = 0; i < dirs.size (); i++)
304 global_path.prepend (dirs[i]);