]> git.donarmstrong.com Git - lilypond.git/blob - lily/relocate.cc
* configure.in (--enable-std-vector): New option.
[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, std::string value, bool overwrite)
33 {
34   if (overwrite || !getenv (key))
35     {
36       std::string combine = std::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, std::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, std::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, std::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 std::string
83 dir_name (std::string const file_name)
84 {
85   std::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 (std::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   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;
110
111   if (is_dir (lilypond_datadir))
112     prefix_directory = lilypond_datadir;
113
114 #if HAVE_GETTEXT
115   if (is_dir (localedir))
116     bindtextdomain ("lilypond", localedir.c_str ());
117 #endif
118
119   prepend_env_path ("PATH", bindir);
120 }
121
122 void
123 framework_relocation (std::string prefix)
124 {
125   if (be_verbose_global)
126     warning (_f ("Relocation: framework_prefix=%s", prefix));
127
128   std::string bindir = prefix + "/bin";
129   std::string datadir = prefix + "/share";
130   std::string libdir = prefix + "/lib";
131   std::string sysconfdir = prefix + "/etc";
132
133   /* need otherwise dynamic .so's aren't found.   */
134   prepend_env_path ("DYLD_LIBRARY_PATH", libdir);
135   
136   set_env_file ("FONTCONFIG_FILE", sysconfdir + "/fonts/fonts.conf", true);
137   set_env_dir ("FONTCONFIG_PATH", sysconfdir + "/fonts");
138
139 #ifdef __MINGW32__
140   char font_dir[PATH_MAX];
141   ExpandEnvironmentStrings ("%windir%/fonts", font_dir, sizeof (font_dir));
142   prepend_env_path ("GS_FONTPATH", font_dir);
143 #endif
144
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");
149
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");
153
154   prepend_env_path ("GUILE_LOAD_PATH", datadir
155                     + to_string ("/guile/%d.%d",
156                                  SCM_MAJOR_VERSION, SCM_MINOR_VERSION));
157
158   set_env_file ("PANGO_RC_FILE", sysconfdir + "/pango/pangorc");
159   set_env_dir ("PANGO_PREFIX", prefix);
160   
161   prepend_env_path ("PATH", bindir);
162 }
163
164 std::string
165 get_working_directory ()
166 {
167   char cwd[PATH_MAX];
168   getcwd (cwd, PATH_MAX);
169
170   return std::string (cwd);
171 }
172
173 void
174 setup_paths (char const *argv0_ptr)
175 {
176   File_name argv0_filename (argv0_ptr);
177   
178   prefix_directory = LILYPOND_DATADIR;
179   if (relocate_binary
180       && getenv ("LILYPOND_RELOCATE_PREFIX"))
181     {
182       std::string prefix = getenv ("LILYPOND_RELOCATE_PREFIX");
183 #ifdef __MINGW32__
184       /* Normalize file name.  */
185       prefix = File_name (prefix).to_string ();
186 #endif /* __MINGW32__ */
187       prefix_relocation (prefix);
188       std::string bindir = prefix + "/bin";
189       framework_relocation (bindir + "/" FRAMEWORKDIR);
190     }
191   else if (relocate_binary)
192     {
193       std::string argv0_abs;
194       if (argv0_filename.is_absolute ())
195         {
196           argv0_abs = argv0_filename.to_string ();
197           if (be_verbose_global)
198             warning (_f ("Relocation: is absolute: argv0=%s", argv0_ptr));
199         }
200       else if (argv0_filename.dir_.length ())
201         {
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));
206         }
207       else
208         {
209           /* Find absolute ARGV0 name, using PATH.  */
210           File_path path;
211           path.parse_path (getenv ("PATH"));
212
213           if (be_verbose_global)
214             warning (_f ("Relocation: from PATH=%s\nargv0=%s",
215                          path.to_string ().c_str (), argv0_ptr));
216
217 #ifndef __MINGW32__
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__ */
223
224           if (argv0_abs.empty ())
225             programming_error ("can't find absolute argv0.");
226         }
227
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);
235     }
236
237   /* FIXME: use LILYPOND_DATADIR.  */
238   if (char const *env = getenv ("LILYPONDPREFIX"))
239     {
240
241 #ifdef __MINGW32__
242       /* Normalize file name.  */
243       prefix_directory = File_name (env).to_string ();
244 #else
245       prefix_directory = env;
246 #endif
247     }
248
249   global_path.append ("");
250
251
252   /*
253     When running from build dir, a full LILYPOND_PREFIX is set-up at
254
255         $(OUTBASE)/share/lilypond/TOPLEVEL_VERSION
256
257      This historical hack will allow the shorthand
258
259         LILYPONDPREFIX=out lily/out/lilypond ...
260
261   */
262   
263   struct stat statbuf;
264   std::string build_prefix = prefix_directory + "/share/lilypond/" TOPLEVEL_VERSION;
265   if (stat (build_prefix.c_str (), &statbuf) == 0)
266     prefix_directory = build_prefix;
267
268   
269   /* Adding mf/out make lilypond unchanged source directory, when setting
270      LILYPONDPREFIX to lilypond-x.y.z */
271   char *suffixes[] = {"ly", "ps", "scm", 0 };
272
273   
274   std::vector<std::string> dirs;
275   for (char **s = suffixes; *s; s++)
276     {
277       std::string path = prefix_directory + to_string ('/') + std::string (*s);
278       dirs.push_back (path);
279     }
280
281
282   dirs.push_back (prefix_directory + "/fonts/otf/");
283   dirs.push_back (prefix_directory + "/fonts/type1/");
284   dirs.push_back (prefix_directory + "/fonts/svg/");
285   
286   for (vsize i = 0; i < dirs.size (); i++)
287     global_path.prepend (dirs[i]);
288 }