]> git.donarmstrong.com Git - lilypond.git/blob - lily/relocate.cc
* configure.in (LINK_GXX_STATICALLY): strip -DSTRING_UTILS_INLINED options.
[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   string gs_version =
148 #ifdef GHOSTSCRIPT_VERSION
149     GHOSTSCRIPT_VERSION
150 #else
151     "8.15"
152 #endif
153     ;
154   
155   if (char const *cur = getenv ("LILYPOND_GS_VERSION"))
156     gs_version = cur;
157   
158   prepend_env_path ("GS_FONTPATH", datadir + "/ghostscript/" + gs_version + "/fonts");
159   prepend_env_path ("GS_LIB", datadir + "/ghostscript/" + gs_version + "/Resource");
160   prepend_env_path ("GS_LIB", datadir + "/ghostscript/" + gs_version + "/lib");
161
162   prepend_env_path ("GS_FONTPATH", datadir + "/gs/fonts");
163   prepend_env_path ("GS_LIB", datadir + "/gs/Resource");
164   prepend_env_path ("GS_LIB", datadir + "/gs/lib");
165
166   prepend_env_path ("GUILE_LOAD_PATH", datadir
167                     + to_string ("/guile/%d.%d",
168                                  SCM_MAJOR_VERSION, SCM_MINOR_VERSION));
169
170   set_env_file ("PANGO_RC_FILE", sysconfdir + "/pango/pangorc");
171   set_env_dir ("PANGO_PREFIX", prefix);
172   
173   prepend_env_path ("PATH", bindir);
174 }
175
176 string
177 get_working_directory ()
178 {
179   char cwd[PATH_MAX];
180   getcwd (cwd, PATH_MAX);
181
182   return string (cwd);
183 }
184
185 void
186 setup_paths (char const *argv0_ptr)
187 {
188   File_name argv0_filename (argv0_ptr);
189   
190   prefix_directory = LILYPOND_DATADIR;
191   if (relocate_binary
192       && getenv ("LILYPOND_RELOCATE_PREFIX"))
193     {
194       string prefix = getenv ("LILYPOND_RELOCATE_PREFIX");
195 #ifdef __MINGW32__
196       /* Normalize file name.  */
197       prefix = File_name (prefix).to_string ();
198 #endif /* __MINGW32__ */
199       prefix_relocation (prefix);
200       string bindir = prefix + "/bin";
201       framework_relocation (bindir + "/" FRAMEWORKDIR);
202     }
203   else if (relocate_binary)
204     {
205       string argv0_abs;
206       if (argv0_filename.is_absolute ())
207         {
208           argv0_abs = argv0_filename.to_string ();
209           if (be_verbose_global)
210             warning (_f ("Relocation: is absolute: argv0=%s", argv0_ptr));
211         }
212       else if (argv0_filename.dir_.length ())
213         {
214           argv0_abs = get_working_directory ()
215             + "/" + string (argv0_filename.to_string ());
216           if (be_verbose_global)
217             warning (_f ("Relocation: from cwd: argv0=%s", argv0_ptr));
218         }
219       else
220         {
221           /* Find absolute ARGV0 name, using PATH.  */
222           File_path path;
223           path.parse_path (getenv ("PATH"));
224
225           if (be_verbose_global)
226             warning (_f ("Relocation: from PATH=%s\nargv0=%s",
227                          path.to_string ().c_str (), argv0_ptr));
228
229 #ifndef __MINGW32__
230           argv0_abs = path.find (argv0_filename.to_string ());
231 #else /* __MINGW32__ */
232           char const *ext[] = {"exe", "", 0 };
233           argv0_abs = path.find (argv0_filename.to_string (), ext);
234 #endif /* __MINGW32__ */
235
236           if (argv0_abs.empty ())
237             programming_error ("can't find absolute argv0.");
238         }
239
240       string bindir = dir_name (argv0_abs);
241       string argv0_prefix = dir_name (bindir);
242       string compile_prefix = dir_name (dir_name (dir_name (prefix_directory)));
243       if (argv0_prefix != compile_prefix)
244         prefix_relocation (argv0_prefix);
245       if (argv0_prefix != compile_prefix || string (FRAMEWORKDIR) != "..")
246         framework_relocation (bindir + "/" FRAMEWORKDIR);
247     }
248
249   /* FIXME: use LILYPOND_DATADIR.  */
250   if (char const *env = getenv ("LILYPONDPREFIX"))
251     {
252
253 #ifdef __MINGW32__
254       /* Normalize file name.  */
255       prefix_directory = File_name (env).to_string ();
256 #else
257       prefix_directory = env;
258 #endif
259     }
260
261   global_path.append ("");
262
263
264   /*
265     When running from build dir, a full LILYPOND_PREFIX is set-up at
266
267         $(OUTBASE)/share/lilypond/TOPLEVEL_VERSION
268
269      This historical hack will allow the shorthand
270
271         LILYPONDPREFIX=out lily/out/lilypond ...
272
273   */
274   
275   struct stat statbuf;
276   string build_prefix_current = prefix_directory + "/share/lilypond/" "current";
277   string build_prefix_version = prefix_directory + "/share/lilypond/" TOPLEVEL_VERSION;
278   if (stat (build_prefix_version.c_str (), &statbuf) == 0)
279     prefix_directory = build_prefix_version;
280   else if (stat (build_prefix_current.c_str (), &statbuf) == 0)
281     prefix_directory = build_prefix_current;
282
283   
284   /* Adding mf/out make lilypond unchanged source directory, when setting
285      LILYPONDPREFIX to lilypond-x.y.z */
286   char *suffixes[] = {"ly", "ps", "scm", 0 };
287
288   
289   vector<string> dirs;
290   for (char **s = suffixes; *s; s++)
291     {
292       string path = prefix_directory + to_string ('/') + string (*s);
293       dirs.push_back (path);
294     }
295
296
297   dirs.push_back (prefix_directory + "/fonts/otf/");
298   dirs.push_back (prefix_directory + "/fonts/type1/");
299   dirs.push_back (prefix_directory + "/fonts/svg/");
300   
301   for (vsize i = 0; i < dirs.size (); i++)
302     global_path.prepend (dirs[i]);
303 }