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)
47 return sane_putenv (key, value, false);
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 ('\\', '/');
86 s = s.left_string (s.index_last ('/'));
95 set_relocation (String bindir, String prefix)
97 if (be_verbose_global)
98 warning (_f ("Relocation: compile prefix=%s, new prefix=%s",
102 String datadir = prefix + "/share";
103 String libdir = prefix + "/lib";
104 String localedir = datadir + "/locale";
105 String sysconfdir = prefix + "/etc";
106 String lilypond_datadir = datadir + "/lilypond/" TOPLEVEL_VERSION;
108 if (is_dir (lilypond_datadir))
109 prefix_directory = lilypond_datadir;
112 if (is_dir (localedir))
113 bindtextdomain ("lilypond", localedir.to_str0 ());
117 set_env_file ("FONTCONFIG_FILE", sysconfdir + "/fonts/fonts.conf");
119 char font_dir[PATH_MAX];
120 ExpandEnvironmentStrings ("%windir%/fonts", font_dir, sizeof (font_dir));
121 prepend_env_path ("GS_FONTPATH", font_dir);
124 /* FIXME: *cough* 8.15 *cough* */
125 prepend_env_path ("GS_FONTPATH", datadir + "/ghostscript/8.15/fonts");
126 prepend_env_path ("GS_LIB", datadir + "/ghostscript/8.15/Resource");
127 prepend_env_path ("GS_LIB", datadir + "/ghostscript/8.15/lib");
129 prepend_env_path ("GS_FONTPATH", datadir + "/gs/fonts");
130 prepend_env_path ("GS_LIB", datadir + "/gs/Resource");
131 prepend_env_path ("GS_LIB", datadir + "/gs/lib");
133 /* need otherwise dynamic .so's aren't found. */
134 prepend_env_path ("DYLD_LIBRARY_PATH", libdir);
136 prepend_env_path ("GUILE_LOAD_PATH", datadir
137 + to_string ("/guile/%d.%d",
138 SCM_MAJOR_VERSION, SCM_MINOR_VERSION));
139 set_env_file ("PANGO_RC_FILE", sysconfdir + "/pango/pangorc");
141 set_env_dir ("PANGO_PREFIX", prefix);
142 set_env_dir ("FONTCONFIG_PATH", sysconfdir + "/fonts");
144 prepend_env_path ("PATH", bindir);
148 get_working_directory ()
151 getcwd (cwd, PATH_MAX);
157 setup_paths (char const *argv0_ptr)
159 File_name argv0_filename (argv0_ptr);
161 prefix_directory = LILYPOND_DATADIR;
163 && getenv ("LILYPOND_RELOCATE_PREFIX"))
165 String prefix = getenv ("LILYPOND_RELOCATE_PREFIX");
167 fixme: need different sep for mingw?
169 set_relocation (prefix + "/" + "bin", prefix);
171 else if (relocate_binary)
174 if (argv0_filename.is_absolute ())
175 argv0_abs = argv0_filename.to_string ();
176 else if (argv0_filename.dir_.length ())
177 argv0_abs = get_working_directory ()
178 + "/" + String (argv0_filename.to_string ());
181 /* Find absolute ARGV0 name, using PATH. */
183 path.parse_path (getenv ("PATH"));
186 String argv0_abs = path.find (argv0_filename.to_string ());
187 #else /* __MINGW32__ */
188 char const *ext[] = {"exe", "", 0 };
189 String argv0_abs = path.find (argv0_filename.to_string (), ext);
190 #endif /* __MINGW32__ */
192 if (argv0_abs.is_empty ())
193 programming_error ("can't find absolute argv0.");
196 String bindir = dir_name (argv0_abs);
197 String argv0_prefix = dir_name (bindir);
198 if (argv0_prefix != dir_name (dir_name (dir_name (prefix_directory))))
199 set_relocation (bindir, argv0_prefix);
202 /* FIXME: use LILYPOND_DATADIR. */
203 if (char const *env = getenv ("LILYPONDPREFIX"))
206 /* Normalize file name. */
207 env = File_name (env).to_string ().get_copy_str0 ();
209 prefix_directory = env;
212 global_path.append ("");
216 When running from build dir, a full LILYPOND_PREFIX is set-up at
218 $(OUTBASE)/share/lilypond/TOPLEVEL_VERSION
220 This historical hack will allow the shorthand
222 LILYPONDPREFIX=out lily/out/lilypond ...
227 String build_prefix = prefix_directory + "/share/lilypond/" TOPLEVEL_VERSION;
228 if (stat (build_prefix.to_str0 (), &statbuf) == 0)
229 prefix_directory = build_prefix;
232 /* Adding mf/out make lilypond unchanged source directory, when setting
233 LILYPONDPREFIX to lilypond-x.y.z */
234 char *suffixes[] = {"ly", "ps", "scm", 0 };
238 for (char **s = suffixes; *s; s++)
240 String path = prefix_directory + to_string ('/') + String (*s);
245 dirs.push (prefix_directory + "/fonts/otf/");
246 dirs.push (prefix_directory + "/fonts/type1/");
247 dirs.push (prefix_directory + "/fonts/svg/");
249 for (int i = 0; i < dirs.size (); i++)
250 global_path.prepend (dirs[i]);