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 + "/" + 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 (std::string prefix)
127 if (be_verbose_global)
128 warning (_f ("Relocation: framework_prefix=%s", prefix));
130 std::string bindir = prefix + "/bin";
131 std::string datadir = prefix + "/share";
132 std::string libdir = prefix + "/lib";
133 std::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);
147 /* FIXME: *cough* 8.15 *cough* */
148 prepend_env_path ("GS_FONTPATH", datadir + "/ghostscript/8.15/fonts");
149 prepend_env_path ("GS_LIB", datadir + "/ghostscript/8.15/Resource");
150 prepend_env_path ("GS_LIB", datadir + "/ghostscript/8.15/lib");
152 prepend_env_path ("GS_FONTPATH", datadir + "/gs/fonts");
153 prepend_env_path ("GS_LIB", datadir + "/gs/Resource");
154 prepend_env_path ("GS_LIB", datadir + "/gs/lib");
156 prepend_env_path ("GUILE_LOAD_PATH", datadir
157 + to_string ("/guile/%d.%d",
158 SCM_MAJOR_VERSION, SCM_MINOR_VERSION));
160 set_env_file ("PANGO_RC_FILE", sysconfdir + "/pango/pangorc");
161 set_env_dir ("PANGO_PREFIX", prefix);
163 prepend_env_path ("PATH", bindir);
167 get_working_directory ()
170 getcwd (cwd, PATH_MAX);
172 return std::string (cwd);
176 setup_paths (char const *argv0_ptr)
178 File_name argv0_filename (argv0_ptr);
180 prefix_directory = LILYPOND_DATADIR;
182 && getenv ("LILYPOND_RELOCATE_PREFIX"))
184 std::string prefix = getenv ("LILYPOND_RELOCATE_PREFIX");
186 /* Normalize file name. */
187 prefix = File_name (prefix).to_string ();
188 #endif /* __MINGW32__ */
189 prefix_relocation (prefix);
190 std::string bindir = prefix + "/bin";
191 framework_relocation (bindir + "/" FRAMEWORKDIR);
193 else if (relocate_binary)
195 std::string argv0_abs;
196 if (argv0_filename.is_absolute ())
198 argv0_abs = argv0_filename.to_string ();
199 if (be_verbose_global)
200 warning (_f ("Relocation: is absolute: argv0=%s", argv0_ptr));
202 else if (argv0_filename.dir_.length ())
204 argv0_abs = get_working_directory ()
205 + "/" + std::string (argv0_filename.to_string ());
206 if (be_verbose_global)
207 warning (_f ("Relocation: from cwd: argv0=%s", argv0_ptr));
211 /* Find absolute ARGV0 name, using PATH. */
213 path.parse_path (getenv ("PATH"));
215 if (be_verbose_global)
216 warning (_f ("Relocation: from PATH=%s\nargv0=%s",
217 path.to_string ().c_str (), argv0_ptr));
220 argv0_abs = path.find (argv0_filename.to_string ());
221 #else /* __MINGW32__ */
222 char const *ext[] = {"exe", "", 0 };
223 argv0_abs = path.find (argv0_filename.to_string (), ext);
224 #endif /* __MINGW32__ */
226 if (argv0_abs.empty ())
227 programming_error ("can't find absolute argv0.");
230 std::string bindir = dir_name (argv0_abs);
231 std::string argv0_prefix = dir_name (bindir);
232 std::string compile_prefix = dir_name (dir_name (dir_name (prefix_directory)));
233 if (argv0_prefix != compile_prefix)
234 prefix_relocation (argv0_prefix);
235 if (argv0_prefix != compile_prefix || std::string (FRAMEWORKDIR) != "..")
236 framework_relocation (bindir + "/" FRAMEWORKDIR);
239 /* FIXME: use LILYPOND_DATADIR. */
240 if (char const *env = getenv ("LILYPONDPREFIX"))
244 /* Normalize file name. */
245 prefix_directory = File_name (env).to_string ();
247 prefix_directory = env;
251 global_path.append ("");
255 When running from build dir, a full LILYPOND_PREFIX is set-up at
257 $(OUTBASE)/share/lilypond/TOPLEVEL_VERSION
259 This historical hack will allow the shorthand
261 LILYPONDPREFIX=out lily/out/lilypond ...
266 std::string build_prefix_current = prefix_directory + "/share/lilypond/" "current";
267 std::string build_prefix_version = prefix_directory + "/share/lilypond/" TOPLEVEL_VERSION;
268 if (stat (build_prefix_version.c_str (), &statbuf) == 0)
269 prefix_directory = build_prefix_version;
270 else if (stat (build_prefix_current.c_str (), &statbuf) == 0)
271 prefix_directory = build_prefix_current;
274 /* Adding mf/out make lilypond unchanged source directory, when setting
275 LILYPONDPREFIX to lilypond-x.y.z */
276 char *suffixes[] = {"ly", "ps", "scm", 0 };
279 std::vector<std::string> dirs;
280 for (char **s = suffixes; *s; s++)
282 std::string path = prefix_directory + to_string ('/') + std::string (*s);
283 dirs.push_back (path);
287 dirs.push_back (prefix_directory + "/fonts/otf/");
288 dirs.push_back (prefix_directory + "/fonts/type1/");
289 dirs.push_back (prefix_directory + "/fonts/svg/");
291 for (vsize i = 0; i < dirs.size (); i++)
292 global_path.prepend (dirs[i]);