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, std::string value, bool overwrite)
34 if (overwrite || !getenv (key))
36 std::string combine = std::string (key) + "=" + value;
37 char *s = strdup (combine.c_str ());
45 set_env_file (char const *key, std::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, std::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, std::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 (std::string const file_name)
85 std::string s = 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 (std::string prefix)
101 if (be_verbose_global)
102 warning (_f ("Relocation: compile prefix=%s, new prefix=%s",
106 std::string bindir = prefix + "/bin";
107 std::string datadir = prefix + "/share";
108 std::string localedir = datadir + "/locale";
109 std::string lilypond_datadir = datadir + "/lilypond/" TOPLEVEL_VERSION;
111 if (is_dir (lilypond_datadir))
112 prefix_directory = lilypond_datadir;
115 if (is_dir (localedir))
116 bindtextdomain ("lilypond", localedir.c_str ());
119 prepend_env_path ("PATH", bindir);
123 framework_relocation (std::string prefix)
125 if (be_verbose_global)
126 warning (_f ("Relocation: framework_prefix=%s", prefix));
128 std::string bindir = prefix + "/bin";
129 std::string datadir = prefix + "/share";
130 std::string libdir = prefix + "/lib";
131 std::string sysconfdir = prefix + "/etc";
133 /* need otherwise dynamic .so's aren't found. */
134 prepend_env_path ("DYLD_LIBRARY_PATH", libdir);
136 set_env_file ("FONTCONFIG_FILE", sysconfdir + "/fonts/fonts.conf", true);
137 set_env_dir ("FONTCONFIG_PATH", sysconfdir + "/fonts");
140 char font_dir[PATH_MAX];
141 ExpandEnvironmentStrings ("%windir%/fonts", font_dir, sizeof (font_dir));
142 prepend_env_path ("GS_FONTPATH", font_dir);
145 /* FIXME: *cough* 8.15 *cough* */
146 prepend_env_path ("GS_FONTPATH", datadir + "/ghostscript/8.15/fonts");
147 prepend_env_path ("GS_LIB", datadir + "/ghostscript/8.15/Resource");
148 prepend_env_path ("GS_LIB", datadir + "/ghostscript/8.15/lib");
150 prepend_env_path ("GS_FONTPATH", datadir + "/gs/fonts");
151 prepend_env_path ("GS_LIB", datadir + "/gs/Resource");
152 prepend_env_path ("GS_LIB", datadir + "/gs/lib");
154 prepend_env_path ("GUILE_LOAD_PATH", datadir
155 + to_string ("/guile/%d.%d",
156 SCM_MAJOR_VERSION, SCM_MINOR_VERSION));
158 set_env_file ("PANGO_RC_FILE", sysconfdir + "/pango/pangorc");
159 set_env_dir ("PANGO_PREFIX", prefix);
161 prepend_env_path ("PATH", bindir);
165 get_working_directory ()
168 getcwd (cwd, PATH_MAX);
170 return std::string (cwd);
174 setup_paths (char const *argv0_ptr)
176 File_name argv0_filename (argv0_ptr);
178 prefix_directory = LILYPOND_DATADIR;
180 && getenv ("LILYPOND_RELOCATE_PREFIX"))
182 std::string prefix = getenv ("LILYPOND_RELOCATE_PREFIX");
184 /* Normalize file name. */
185 prefix = File_name (prefix).to_string ().get_copy_str0 ();
186 #endif /* __MINGW32__ */
187 prefix_relocation (prefix);
188 std::string bindir = prefix + "/bin";
189 framework_relocation (bindir + "/" FRAMEWORKDIR);
191 else if (relocate_binary)
193 std::string argv0_abs;
194 if (argv0_filename.is_absolute ())
196 argv0_abs = argv0_filename.to_string ();
197 if (be_verbose_global)
198 warning (_f ("Relocation: is absolute: argv0=%s", argv0_ptr));
200 else if (argv0_filename.dir_.length ())
202 argv0_abs = get_working_directory ()
203 + "/" + std::string (argv0_filename.to_string ());
204 if (be_verbose_global)
205 warning (_f ("Relocation: from cwd: argv0=%s", argv0_ptr));
209 /* Find absolute ARGV0 name, using PATH. */
211 path.parse_path (getenv ("PATH"));
213 if (be_verbose_global)
214 warning (_f ("Relocation: from PATH=%s\nargv0=%s",
215 path.to_string ().c_str (), argv0_ptr));
218 argv0_abs = path.find (argv0_filename.to_string ());
219 #else /* __MINGW32__ */
220 char const *ext[] = {"exe", "", 0 };
221 argv0_abs = path.find (argv0_filename.to_string (), ext);
222 #endif /* __MINGW32__ */
224 if (argv0_abs.empty ())
225 programming_error ("can't find absolute argv0.");
228 std::string bindir = dir_name (argv0_abs);
229 std::string argv0_prefix = dir_name (bindir);
230 std::string compile_prefix = dir_name (dir_name (dir_name (prefix_directory)));
231 if (argv0_prefix != compile_prefix)
232 prefix_relocation (argv0_prefix);
233 if (argv0_prefix != compile_prefix || std::string (FRAMEWORKDIR) != "..")
234 framework_relocation (bindir + "/" FRAMEWORKDIR);
237 /* FIXME: use LILYPOND_DATADIR. */
238 if (char const *env = getenv ("LILYPONDPREFIX"))
241 /* Normalize file name. */
242 env = File_name (env).to_string ().get_copy_str0 ();
244 prefix_directory = env;
247 global_path.append ("");
251 When running from build dir, a full LILYPOND_PREFIX is set-up at
253 $(OUTBASE)/share/lilypond/TOPLEVEL_VERSION
255 This historical hack will allow the shorthand
257 LILYPONDPREFIX=out lily/out/lilypond ...
262 std::string build_prefix = prefix_directory + "/share/lilypond/" TOPLEVEL_VERSION;
263 if (stat (build_prefix.c_str (), &statbuf) == 0)
264 prefix_directory = build_prefix;
267 /* Adding mf/out make lilypond unchanged source directory, when setting
268 LILYPONDPREFIX to lilypond-x.y.z */
269 char *suffixes[] = {"ly", "ps", "scm", 0 };
272 Array<std::string> dirs;
273 for (char **s = suffixes; *s; s++)
275 std::string path = prefix_directory + to_string ('/') + std::string (*s);
280 dirs.push (prefix_directory + "/fonts/otf/");
281 dirs.push (prefix_directory + "/fonts/type1/");
282 dirs.push (prefix_directory + "/fonts/svg/");
284 for (int i = 0; i < dirs.size (); i++)
285 global_path.prepend (dirs[i]);