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"
32 sane_putenv (char const *key, string value, bool overwrite)
34 if (overwrite || !getenv (key))
36 string combine = string (key) + "=" + value;
37 char *s = strdup (combine.c_str ());
45 set_env_file (char const *key, string value, bool overwrite = false)
48 return sane_putenv (key, value, overwrite);
49 else if (be_verbose_global)
50 warning (_f ("no such file: %s for %s", value, key));
55 set_env_dir (char const *key, string value)
58 return sane_putenv (key, value, false);
59 else if (be_verbose_global)
60 warning (_f ("no such directory: %s for %s", value, key));
65 prepend_env_path (char const *key, string value)
69 if (be_verbose_global)
70 progress_indication (_f ("%s=%s\n", key, value.c_str ()));
72 if (char const *cur = getenv (key))
73 value += to_string (PATHSEP) + cur;
75 return sane_putenv (key, value.c_str (), true);
77 else if (be_verbose_global)
78 warning (_f ("no such directory: %s for %s", value, key));
83 dir_name (string const file_name)
86 replace_all (s, '\\', '/');
87 ssize n = s.length ();
88 if (n && s[n - 1] == '/')
90 s = s.substr (0, s.rfind ('/'));
99 prefix_relocation (string prefix)
101 if (be_verbose_global)
102 warning (_f ("Relocation: compile prefix=%s, new prefix=%s",
106 string bindir = prefix + "/bin";
107 string datadir = prefix + "/share";
108 string localedir = datadir + "/locale";
109 string lilypond_datadir = datadir + "/lilypond/";
111 if (is_dir (lilypond_datadir + "/" + TOPLEVEL_VERSION))
112 prefix_directory = lilypond_datadir + "/" + TOPLEVEL_VERSION;
113 else if (is_dir (lilypond_datadir + "/current"))
114 prefix_directory = lilypond_datadir + "/current";
117 if (is_dir (localedir))
118 bindtextdomain ("lilypond", localedir.c_str ());
121 prepend_env_path ("PATH", bindir);
125 framework_relocation (string prefix)
127 if (be_verbose_global)
128 warning (_f ("Relocation: framework_prefix=%s", prefix));
130 string bindir = prefix + "/bin";
131 string datadir = prefix + "/share";
132 string libdir = prefix + "/lib";
133 string sysconfdir = prefix + "/etc";
135 /* need otherwise dynamic .so's aren't found. */
136 prepend_env_path ("DYLD_LIBRARY_PATH", libdir);
138 set_env_file ("FONTCONFIG_FILE", sysconfdir + "/fonts/fonts.conf", true);
139 set_env_dir ("FONTCONFIG_PATH", sysconfdir + "/fonts");
142 char font_dir[PATH_MAX];
143 ExpandEnvironmentStrings ("%windir%/fonts", font_dir, sizeof (font_dir));
144 prepend_env_path ("GS_FONTPATH", font_dir);
148 #ifdef GHOSTSCRIPT_VERSION
155 if (char const *cur = getenv ("LILYPOND_GS_VERSION"))
158 prepend_env_path ("GS_FONTPATH", datadir + "/ghostscript/" + gs_version + "/fonts");
159 prepend_env_path ("GS_LIB", datadir + "/ghostscript/" + gs_version + "/Resource");
160 prepend_env_path ("GS_LIB", datadir + "/ghostscript/" + gs_version + "/lib");
162 prepend_env_path ("GS_FONTPATH", datadir + "/gs/fonts");
163 prepend_env_path ("GS_LIB", datadir + "/gs/Resource");
164 prepend_env_path ("GS_LIB", datadir + "/gs/lib");
166 prepend_env_path ("GUILE_LOAD_PATH", datadir
167 + to_string ("/guile/%d.%d",
168 SCM_MAJOR_VERSION, SCM_MINOR_VERSION));
170 set_env_file ("PANGO_RC_FILE", sysconfdir + "/pango/pangorc");
171 set_env_dir ("PANGO_PREFIX", prefix);
173 prepend_env_path ("PATH", bindir);
177 get_working_directory ()
180 getcwd (cwd, PATH_MAX);
186 setup_paths (char const *argv0_ptr)
188 File_name argv0_filename (argv0_ptr);
190 prefix_directory = LILYPOND_DATADIR;
192 && getenv ("LILYPOND_RELOCATE_PREFIX"))
194 string prefix = getenv ("LILYPOND_RELOCATE_PREFIX");
196 /* Normalize file name. */
197 prefix = File_name (prefix).to_string ();
198 #endif /* __MINGW32__ */
199 prefix_relocation (prefix);
200 string bindir = prefix + "/bin";
201 framework_relocation (bindir + "/" FRAMEWORKDIR);
203 else if (relocate_binary)
206 if (argv0_filename.is_absolute ())
208 argv0_abs = argv0_filename.to_string ();
209 if (be_verbose_global)
210 warning (_f ("Relocation: is absolute: argv0=%s", argv0_ptr));
212 else if (argv0_filename.dir_.length ())
214 argv0_abs = get_working_directory ()
215 + "/" + string (argv0_filename.to_string ());
216 if (be_verbose_global)
217 warning (_f ("Relocation: from cwd: argv0=%s", argv0_ptr));
221 /* Find absolute ARGV0 name, using PATH. */
223 path.parse_path (getenv ("PATH"));
225 if (be_verbose_global)
226 warning (_f ("Relocation: from PATH=%s\nargv0=%s",
227 path.to_string ().c_str (), argv0_ptr));
230 argv0_abs = path.find (argv0_filename.to_string ());
231 #else /* __MINGW32__ */
232 char const *ext[] = {"exe", "", 0 };
233 argv0_abs = path.find (argv0_filename.to_string (), ext);
234 #endif /* __MINGW32__ */
236 if (argv0_abs.empty ())
237 programming_error ("can't find absolute argv0.");
240 string bindir = dir_name (argv0_abs);
241 string argv0_prefix = dir_name (bindir);
242 string compile_prefix = dir_name (dir_name (dir_name (prefix_directory)));
243 if (argv0_prefix != compile_prefix)
244 prefix_relocation (argv0_prefix);
245 if (argv0_prefix != compile_prefix || string (FRAMEWORKDIR) != "..")
246 framework_relocation (bindir + "/" FRAMEWORKDIR);
249 /* FIXME: use LILYPOND_DATADIR. */
250 if (char const *env = getenv ("LILYPONDPREFIX"))
254 /* Normalize file name. */
255 prefix_directory = File_name (env).to_string ();
257 prefix_directory = env;
261 global_path.append ("");
265 When running from build dir, a full LILYPOND_PREFIX is set-up at
267 $(OUTBASE)/share/lilypond/TOPLEVEL_VERSION
269 This historical hack will allow the shorthand
271 LILYPONDPREFIX=out lily/out/lilypond ...
276 string build_prefix_current = prefix_directory + "/share/lilypond/" "current";
277 string build_prefix_version = prefix_directory + "/share/lilypond/" TOPLEVEL_VERSION;
278 if (stat (build_prefix_version.c_str (), &statbuf) == 0)
279 prefix_directory = build_prefix_version;
280 else if (stat (build_prefix_current.c_str (), &statbuf) == 0)
281 prefix_directory = build_prefix_current;
284 /* Adding mf/out make lilypond unchanged source directory, when setting
285 LILYPONDPREFIX to lilypond-x.y.z */
286 char *suffixes[] = {"ly", "ps", "scm", 0 };
290 for (char **s = suffixes; *s; s++)
292 string path = prefix_directory + to_string ('/') + string (*s);
293 dirs.push_back (path);
297 dirs.push_back (prefix_directory + "/fonts/otf/");
298 dirs.push_back (prefix_directory + "/fonts/type1/");
299 dirs.push_back (prefix_directory + "/fonts/svg/");
301 for (vsize i = 0; i < dirs.size (); i++)
302 global_path.prepend (dirs[i]);