2 relocate.cc -- implement relocation based on argv0
4 source file of the GNU LilyPond music typesetter
6 (c) 2005 Han-Wen Nienhuys <hanwen@xs4all.nl>
10 #include "relocate.hh"
21 #include "file-name.hh"
22 #include "file-path.hh"
23 #include "lily-guile.hh"
24 #include "lily-version.hh"
31 sane_putenv (char const *key, String value, bool overwrite)
33 if (overwrite || !getenv (key))
35 String combine = String (key) + "=" + value;
36 char *s = strdup (combine.to_str0 ());
44 set_env_file (char const *key, String value, bool overwrite = false)
47 return sane_putenv (key, value, overwrite);
48 else if (be_verbose_global)
49 warning (_f ("no such file: %s for %s", value, key));
54 set_env_dir (char const *key, String value)
57 return sane_putenv (key, value, false);
58 else if (be_verbose_global)
59 warning (_f ("no such directory: %s for %s", value, key));
64 prepend_env_path (char const *key, String value)
68 if (be_verbose_global)
69 progress_indication (_f ("%s=%s\n", key, value.to_str0 ()));
71 if (char const *cur = getenv (key))
72 value += to_string (PATHSEP) + cur;
74 return sane_putenv (key, value.to_str0 (), true);
76 else if (be_verbose_global)
77 warning (_f ("no such directory: %s for %s", value, key));
82 dir_name (String const file_name)
85 s.substitute ('\\', '/');
87 if (n && s[n - 1] == '/')
89 s = s.left_string (s.index_last ('/'));
98 prefix_relocation (String prefix)
100 if (be_verbose_global)
101 warning (_f ("Relocation: compile prefix=%s, new prefix=%s",
105 String bindir = prefix + "/bin";
106 String datadir = prefix + "/share";
107 String localedir = datadir + "/locale";
108 String lilypond_datadir = datadir + "/lilypond/" TOPLEVEL_VERSION;
110 if (is_dir (lilypond_datadir))
111 prefix_directory = lilypond_datadir;
114 if (is_dir (localedir))
115 bindtextdomain ("lilypond", localedir.to_str0 ());
118 prepend_env_path ("PATH", bindir);
122 framework_relocation (String prefix)
124 if (be_verbose_global)
125 warning (_f ("Relocation: framework_prefix=%s", prefix));
127 String bindir = prefix + "/bin";
128 String datadir = prefix + "/share";
129 String libdir = prefix + "/lib";
130 String sysconfdir = prefix + "/etc";
132 /* need otherwise dynamic .so's aren't found. */
133 prepend_env_path ("DYLD_LIBRARY_PATH", libdir);
135 set_env_file ("FONTCONFIG_FILE", sysconfdir + "/fonts/fonts.conf", true);
136 set_env_dir ("FONTCONFIG_PATH", sysconfdir + "/fonts");
139 char font_dir[PATH_MAX];
140 ExpandEnvironmentStrings ("%windir%/fonts", font_dir, sizeof (font_dir));
141 prepend_env_path ("GS_FONTPATH", font_dir);
144 /* FIXME: *cough* 8.15 *cough* */
145 prepend_env_path ("GS_FONTPATH", datadir + "/ghostscript/8.15/fonts");
146 prepend_env_path ("GS_LIB", datadir + "/ghostscript/8.15/Resource");
147 prepend_env_path ("GS_LIB", datadir + "/ghostscript/8.15/lib");
149 prepend_env_path ("GS_FONTPATH", datadir + "/gs/fonts");
150 prepend_env_path ("GS_LIB", datadir + "/gs/Resource");
151 prepend_env_path ("GS_LIB", datadir + "/gs/lib");
153 prepend_env_path ("GUILE_LOAD_PATH", datadir
154 + to_string ("/guile/%d.%d",
155 SCM_MAJOR_VERSION, SCM_MINOR_VERSION));
157 set_env_file ("PANGO_RC_FILE", sysconfdir + "/pango/pangorc");
158 set_env_dir ("PANGO_PREFIX", prefix);
160 prepend_env_path ("PATH", bindir);
164 get_working_directory ()
167 getcwd (cwd, PATH_MAX);
173 setup_paths (char const *argv0_ptr)
175 File_name argv0_filename (argv0_ptr);
177 prefix_directory = LILYPOND_DATADIR;
179 && getenv ("LILYPOND_RELOCATE_PREFIX"))
181 String prefix = getenv ("LILYPOND_RELOCATE_PREFIX");
183 /* Normalize file name. */
184 prefix = File_name (prefix).to_string ().get_copy_str0 ();
185 #endif /* __MINGW32__ */
186 prefix_relocation (prefix);
187 String bindir = prefix + "/bin";
188 framework_relocation (bindir + "/" FRAMEWORKDIR);
190 else if (relocate_binary)
193 if (argv0_filename.is_absolute ())
195 argv0_abs = argv0_filename.to_string ();
196 if (be_verbose_global)
197 warning (_f ("Relocation: is absolute: argv0=%s", argv0_ptr));
199 else if (argv0_filename.dir_.length ())
201 argv0_abs = get_working_directory ()
202 + "/" + String (argv0_filename.to_string ());
203 if (be_verbose_global)
204 warning (_f ("Relocation: from cwd: argv0=%s", argv0_ptr));
208 /* Find absolute ARGV0 name, using PATH. */
210 path.parse_path (getenv ("PATH"));
212 if (be_verbose_global)
213 warning (_f ("Relocation: from PATH=%s\nargv0=%s",
214 path.to_string ().get_str0 (), argv0_ptr));
217 String argv0_abs = path.find (argv0_filename.to_string ());
218 #else /* __MINGW32__ */
219 char const *ext[] = {"exe", "", 0 };
220 String argv0_abs = path.find (argv0_filename.to_string (), ext);
221 #endif /* __MINGW32__ */
223 if (argv0_abs.is_empty ())
224 programming_error ("can't find absolute argv0.");
227 String bindir = dir_name (argv0_abs);
228 String argv0_prefix = dir_name (bindir);
229 String compile_prefix = dir_name (dir_name (dir_name (prefix_directory)));
230 if (argv0_prefix != compile_prefix)
231 prefix_relocation (argv0_prefix);
232 if (argv0_prefix != compile_prefix || String (FRAMEWORKDIR) != "..")
233 framework_relocation (bindir + "/" FRAMEWORKDIR);
236 /* FIXME: use LILYPOND_DATADIR. */
237 if (char const *env = getenv ("LILYPONDPREFIX"))
240 /* Normalize file name. */
241 env = File_name (env).to_string ().get_copy_str0 ();
243 prefix_directory = env;
246 global_path.append ("");
250 When running from build dir, a full LILYPOND_PREFIX is set-up at
252 $(OUTBASE)/share/lilypond/TOPLEVEL_VERSION
254 This historical hack will allow the shorthand
256 LILYPONDPREFIX=out lily/out/lilypond ...
261 String build_prefix = prefix_directory + "/share/lilypond/" TOPLEVEL_VERSION;
262 if (stat (build_prefix.to_str0 (), &statbuf) == 0)
263 prefix_directory = build_prefix;
266 /* Adding mf/out make lilypond unchanged source directory, when setting
267 LILYPONDPREFIX to lilypond-x.y.z */
268 char *suffixes[] = {"ly", "ps", "scm", 0 };
272 for (char **s = suffixes; *s; s++)
274 String path = prefix_directory + to_string ('/') + String (*s);
279 dirs.push (prefix_directory + "/fonts/otf/");
280 dirs.push (prefix_directory + "/fonts/type1/");
281 dirs.push (prefix_directory + "/fonts/svg/");
283 for (int i = 0; i < dirs.size (); i++)
284 global_path.prepend (dirs[i]);