]> git.donarmstrong.com Git - lilypond.git/blob - lily/relocate.cc
* flower/include/std-string.hh:
[lilypond.git] / lily / relocate.cc
1 /*
2   relocate.cc -- implement relocation based on argv0
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2005--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
7
8 */
9
10 #include "relocate.hh"
11
12 #include "config.hh"
13
14 #include <sys/stat.h>
15 #include <unistd.h>
16
17 #if HAVE_GETTEXT
18 #include <libintl.h>
19 #endif
20
21 #include "file-name.hh"
22 #include "file-path.hh"
23 #include "international.hh"
24 #include "lily-guile.hh"
25 #include "lily-version.hh"
26 #include "main.hh"
27 #include "version.hh"
28 #include "warn.hh"
29
30
31 int
32 sane_putenv (char const *key, string value, bool overwrite)
33 {
34   if (overwrite || !getenv (key))
35     {
36       string combine = string (key) + "=" + value;
37       char *s = strdup (combine.c_str ());
38       return putenv (s);
39     }
40   
41   return -1;
42 }
43
44 static int
45 set_env_file (char const *key, string value, bool overwrite = false)
46 {
47   if (is_file (value))
48     return sane_putenv (key, value, overwrite);
49   else if (be_verbose_global)
50     warning (_f ("no such file: %s for %s", value, key));
51   return -1;
52 }
53
54 static int
55 set_env_dir (char const *key, string value)
56 {
57   if (is_dir (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));
61   return -1;
62 }
63
64 static int
65 prepend_env_path (char const *key, string value)
66 {
67   if (is_dir (value))
68     {
69       if (be_verbose_global)
70         progress_indication (_f ("%s=%s\n", key, value.c_str ())); 
71
72       if (char const *cur = getenv (key))
73         value += to_string (PATHSEP) + cur;
74
75       return sane_putenv (key, value.c_str (), true);
76     }
77   else if (be_verbose_global)
78     warning (_f ("no such directory: %s for %s", value, key));
79   return -1;
80 }
81
82 string
83 dir_name (string const file_name)
84 {
85   string s = file_name;
86   replace_all (s, '\\', '/');
87   ssize n = s.length ();
88   if (n && s[n - 1] == '/')
89     s[n - 1] = 0;
90   s = s.substr (0, s.rfind ('/'));
91   return s;
92 }
93
94 #ifdef __MINGW32__
95 #include <winbase.h>
96 #endif
97
98 void
99 prefix_relocation (string prefix)
100 {
101   if (be_verbose_global)
102     warning (_f ("Relocation: compile prefix=%s, new prefix=%s",
103                  prefix_directory,
104                  prefix.c_str ()));
105   
106   string bindir = prefix + "/bin";
107   string datadir = prefix + "/share";
108   string localedir = datadir + "/locale";
109   string lilypond_datadir = datadir + "/lilypond/";
110
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";
115
116 #if HAVE_GETTEXT
117   if (is_dir (localedir))
118     bindtextdomain ("lilypond", localedir.c_str ());
119 #endif
120
121   prepend_env_path ("PATH", bindir);
122 }
123
124 void
125 framework_relocation (string prefix)
126 {
127   if (be_verbose_global)
128     warning (_f ("Relocation: framework_prefix=%s", prefix));
129
130   string bindir = prefix + "/bin";
131   string datadir = prefix + "/share";
132   string libdir = prefix + "/lib";
133   string sysconfdir = prefix + "/etc";
134
135   /* need otherwise dynamic .so's aren't found.   */
136   prepend_env_path ("DYLD_LIBRARY_PATH", libdir);
137   
138   set_env_file ("FONTCONFIG_FILE", sysconfdir + "/fonts/fonts.conf", true);
139   set_env_dir ("FONTCONFIG_PATH", sysconfdir + "/fonts");
140
141 #ifdef __MINGW32__
142   char font_dir[PATH_MAX];
143   ExpandEnvironmentStrings ("%windir%/fonts", font_dir, sizeof (font_dir));
144   prepend_env_path ("GS_FONTPATH", font_dir);
145 #endif
146
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");
151
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");
155
156   prepend_env_path ("GUILE_LOAD_PATH", datadir
157                     + to_string ("/guile/%d.%d",
158                                  SCM_MAJOR_VERSION, SCM_MINOR_VERSION));
159
160   set_env_file ("PANGO_RC_FILE", sysconfdir + "/pango/pangorc");
161   set_env_dir ("PANGO_PREFIX", prefix);
162   
163   prepend_env_path ("PATH", bindir);
164 }
165
166 string
167 get_working_directory ()
168 {
169   char cwd[PATH_MAX];
170   getcwd (cwd, PATH_MAX);
171
172   return string (cwd);
173 }
174
175 void
176 setup_paths (char const *argv0_ptr)
177 {
178   File_name argv0_filename (argv0_ptr);
179   
180   prefix_directory = LILYPOND_DATADIR;
181   if (relocate_binary
182       && getenv ("LILYPOND_RELOCATE_PREFIX"))
183     {
184       string prefix = getenv ("LILYPOND_RELOCATE_PREFIX");
185 #ifdef __MINGW32__
186       /* Normalize file name.  */
187       prefix = File_name (prefix).to_string ();
188 #endif /* __MINGW32__ */
189       prefix_relocation (prefix);
190       string bindir = prefix + "/bin";
191       framework_relocation (bindir + "/" FRAMEWORKDIR);
192     }
193   else if (relocate_binary)
194     {
195       string argv0_abs;
196       if (argv0_filename.is_absolute ())
197         {
198           argv0_abs = argv0_filename.to_string ();
199           if (be_verbose_global)
200             warning (_f ("Relocation: is absolute: argv0=%s", argv0_ptr));
201         }
202       else if (argv0_filename.dir_.length ())
203         {
204           argv0_abs = get_working_directory ()
205             + "/" + string (argv0_filename.to_string ());
206           if (be_verbose_global)
207             warning (_f ("Relocation: from cwd: argv0=%s", argv0_ptr));
208         }
209       else
210         {
211           /* Find absolute ARGV0 name, using PATH.  */
212           File_path path;
213           path.parse_path (getenv ("PATH"));
214
215           if (be_verbose_global)
216             warning (_f ("Relocation: from PATH=%s\nargv0=%s",
217                          path.to_string ().c_str (), argv0_ptr));
218
219 #ifndef __MINGW32__
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__ */
225
226           if (argv0_abs.empty ())
227             programming_error ("can't find absolute argv0.");
228         }
229
230       string bindir = dir_name (argv0_abs);
231       string argv0_prefix = dir_name (bindir);
232       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 || string (FRAMEWORKDIR) != "..")
236         framework_relocation (bindir + "/" FRAMEWORKDIR);
237     }
238
239   /* FIXME: use LILYPOND_DATADIR.  */
240   if (char const *env = getenv ("LILYPONDPREFIX"))
241     {
242
243 #ifdef __MINGW32__
244       /* Normalize file name.  */
245       prefix_directory = File_name (env).to_string ();
246 #else
247       prefix_directory = env;
248 #endif
249     }
250
251   global_path.append ("");
252
253
254   /*
255     When running from build dir, a full LILYPOND_PREFIX is set-up at
256
257         $(OUTBASE)/share/lilypond/TOPLEVEL_VERSION
258
259      This historical hack will allow the shorthand
260
261         LILYPONDPREFIX=out lily/out/lilypond ...
262
263   */
264   
265   struct stat statbuf;
266   string build_prefix_current = prefix_directory + "/share/lilypond/" "current";
267   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;
272
273   
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 };
277
278   
279   vector<string> dirs;
280   for (char **s = suffixes; *s; s++)
281     {
282       string path = prefix_directory + to_string ('/') + string (*s);
283       dirs.push_back (path);
284     }
285
286
287   dirs.push_back (prefix_directory + "/fonts/otf/");
288   dirs.push_back (prefix_directory + "/fonts/type1/");
289   dirs.push_back (prefix_directory + "/fonts/svg/");
290   
291   for (vsize i = 0; i < dirs.size (); i++)
292     global_path.prepend (dirs[i]);
293 }