]> git.donarmstrong.com Git - lilypond.git/blob - lily/relocate.cc
rename from laissez-vibrer-tie.cc. Rename
[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 #define FRAMEWORKDIR ".."
31
32
33 int
34 sane_putenv (char const *key, string value, bool overwrite)
35 {
36   if (overwrite || !getenv (key))
37     {
38       string combine = string (key) + "=" + value;
39       char *s = strdup (combine.c_str ());
40       return putenv (s);
41     }
42   
43   return -1;
44 }
45
46 static int
47 set_env_file (char const *key, string value, bool overwrite = false)
48 {
49   if (is_file (value))
50     return sane_putenv (key, value, overwrite);
51   else if (be_verbose_global)
52     warning (_f ("no such file: %s for %s", value, key));
53   return -1;
54 }
55
56 static int
57 set_env_dir (char const *key, string value)
58 {
59   if (is_dir (value))
60     return sane_putenv (key, value, false);
61   else if (be_verbose_global)
62     warning (_f ("no such directory: %s for %s", value, key));
63   return -1;
64 }
65
66 static int
67 prepend_env_path (char const *key, string value)
68 {
69   if (is_dir (value))
70     {
71       if (be_verbose_global)
72         progress_indication (_f ("%s=%s (prepend)\n", key, value.c_str ())); 
73
74       if (char const *cur = getenv (key))
75         value += to_string (PATHSEP) + cur;
76
77       return sane_putenv (key, value.c_str (), true);
78     }
79   else if (be_verbose_global)
80     warning (_f ("no such directory: %s for %s", value, key));
81   return -1;
82 }
83
84 string
85 dir_name (string const file_name)
86 {
87   string s = file_name;
88   replace_all (s, '\\', '/');
89   ssize n = s.length ();
90   if (n && s[n - 1] == '/')
91     s[n - 1] = 0;
92   s = s.substr (0, s.rfind ('/'));
93   return s;
94 }
95
96 #ifdef __MINGW32__
97 #include <winbase.h>
98 #endif
99
100 void
101 prefix_relocation (string prefix)
102 {
103   if (be_verbose_global)
104     warning (_f ("Relocation: compile prefix=%s, new prefix=%s",
105                  prefix_directory,
106                  prefix.c_str ()));
107   
108   string bindir = prefix + "/bin";
109   string datadir = prefix + "/share";
110   string localedir = datadir + "/locale";
111   string lilypond_datadir = datadir + "/lilypond/";
112
113   if (is_dir (lilypond_datadir + "/" + TOPLEVEL_VERSION))
114     prefix_directory = lilypond_datadir + "/" + TOPLEVEL_VERSION;
115   else if (is_dir (lilypond_datadir + "/current"))
116     prefix_directory = lilypond_datadir + "/current";
117
118 #if HAVE_GETTEXT
119   if (is_dir (localedir))
120     bindtextdomain ("lilypond", localedir.c_str ());
121 #endif
122
123   prepend_env_path ("PATH", bindir);
124 }
125
126 void
127 framework_relocation (string prefix)
128 {
129   if (be_verbose_global)
130     warning (_f ("Relocation: framework_prefix=%s", prefix));
131
132   string bindir = prefix + "/bin";
133   string datadir = prefix + "/share";
134   string libdir = prefix + "/lib";
135   string sysconfdir = prefix + "/etc";
136
137   /* need otherwise dynamic .so's aren't found.   */
138   prepend_env_path ("DYLD_LIBRARY_PATH", libdir);
139   
140   set_env_file ("FONTCONFIG_FILE", sysconfdir + "/fonts/fonts.conf", true);
141   set_env_dir ("FONTCONFIG_PATH", sysconfdir + "/fonts");
142
143 #ifdef __MINGW32__
144   char font_dir[PATH_MAX];
145   ExpandEnvironmentStrings ("%windir%/fonts", font_dir, sizeof (font_dir));
146   prepend_env_path ("GS_FONTPATH", font_dir);
147 #endif
148
149   string gs_version =
150 #ifdef GHOSTSCRIPT_VERSION
151     GHOSTSCRIPT_VERSION
152 #else
153     "ghostscript-version-undefined"
154 #endif
155     ;
156   
157   if (char const *cur = getenv ("LILYPOND_GS_VERSION"))
158     gs_version = cur;
159   
160   prepend_env_path ("GS_FONTPATH", datadir + "/ghostscript/" + gs_version + "/fonts");
161   prepend_env_path ("GS_LIB", datadir + "/ghostscript/" + gs_version + "/Resource");
162   prepend_env_path ("GS_LIB", datadir + "/ghostscript/" + gs_version + "/lib");
163
164   prepend_env_path ("GS_FONTPATH", datadir + "/gs/fonts");
165   prepend_env_path ("GS_LIB", datadir + "/gs/Resource");
166   prepend_env_path ("GS_LIB", datadir + "/gs/lib");
167
168   prepend_env_path ("GUILE_LOAD_PATH", datadir
169                     + to_string ("/guile/%d.%d",
170                                  SCM_MAJOR_VERSION, SCM_MINOR_VERSION));
171
172   set_env_file ("PANGO_RC_FILE", sysconfdir + "/pango/pangorc");
173   set_env_dir ("PANGO_PREFIX", prefix);
174   
175   prepend_env_path ("PATH", bindir);
176 }
177
178 string
179 get_working_directory ()
180 {
181   char cwd[PATH_MAX];
182   getcwd (cwd, PATH_MAX);
183
184   return string (cwd);
185 }
186
187 void
188 setup_paths (char const *argv0_ptr)
189 {
190   File_name argv0_filename (argv0_ptr);
191   
192   prefix_directory = LILYPOND_DATADIR;
193   if (relocate_binary
194       && getenv ("LILYPOND_RELOCATE_PREFIX"))
195     {
196       string prefix = getenv ("LILYPOND_RELOCATE_PREFIX");
197 #ifdef __MINGW32__
198       /* Normalize file name.  */
199       prefix = File_name (prefix).to_string ();
200 #endif /* __MINGW32__ */
201       prefix_relocation (prefix);
202       string bindir = prefix + "/bin";
203       framework_relocation (bindir);
204     }
205   else if (relocate_binary)
206     {
207       string argv0_abs;
208       if (argv0_filename.is_absolute ())
209         {
210           argv0_abs = argv0_filename.to_string ();
211           if (be_verbose_global)
212             warning (_f ("Relocation: is absolute: argv0=%s", argv0_ptr));
213         }
214       else if (argv0_filename.dir_.length ())
215         {
216           argv0_abs = get_working_directory ()
217             + "/" + string (argv0_filename.to_string ());
218           if (be_verbose_global)
219             warning (_f ("Relocation: from cwd: argv0=%s", argv0_ptr));
220         }
221       else
222         {
223           /* Find absolute ARGV0 name, using PATH.  */
224           File_path path;
225           path.parse_path (getenv ("PATH"));
226
227           if (be_verbose_global)
228             warning (_f ("Relocation: from PATH=%s\nargv0=%s",
229                          path.to_string ().c_str (), argv0_ptr));
230
231 #ifndef __MINGW32__
232           argv0_abs = path.find (argv0_filename.to_string ());
233 #else /* __MINGW32__ */
234           char const *ext[] = {"exe", "", 0 };
235           argv0_abs = path.find (argv0_filename.to_string (), ext);
236 #endif /* __MINGW32__ */
237
238           if (argv0_abs.empty ())
239             programming_error ("can't find absolute argv0.");
240         }
241
242       string bindir = dir_name (argv0_abs);
243       string argv0_prefix = dir_name (bindir);
244       string compile_prefix = dir_name (dir_name (dir_name (prefix_directory)));
245       if (argv0_prefix != compile_prefix)
246         prefix_relocation (argv0_prefix);
247       if (argv0_prefix != compile_prefix || string (FRAMEWORKDIR) != "..")
248         framework_relocation (bindir + "/" + FRAMEWORKDIR);
249     }
250
251   /* FIXME: use LILYPOND_DATADIR.  */
252   if (char const *env = getenv ("LILYPONDPREFIX"))
253     {
254
255 #ifdef __MINGW32__
256       /* Normalize file name.  */
257       prefix_directory = File_name (env).to_string ();
258 #else
259       prefix_directory = env;
260 #endif
261     }
262
263   global_path.append ("");
264
265
266   /*
267     When running from build dir, a full LILYPOND_PREFIX is set-up at
268
269         $(OUTBASE)/share/lilypond/TOPLEVEL_VERSION
270
271      This historical hack will allow the shorthand
272
273         LILYPONDPREFIX=out lily/out/lilypond ...
274
275   */
276   
277   struct stat statbuf;
278   string build_prefix_current = prefix_directory + "/share/lilypond/" "current";
279   string build_prefix_version = prefix_directory + "/share/lilypond/" TOPLEVEL_VERSION;
280   if (stat (build_prefix_version.c_str (), &statbuf) == 0)
281     prefix_directory = build_prefix_version;
282   else if (stat (build_prefix_current.c_str (), &statbuf) == 0)
283     prefix_directory = build_prefix_current;
284
285   
286   /* Adding mf/out make lilypond unchanged source directory, when setting
287      LILYPONDPREFIX to lilypond-x.y.z */
288   char const *suffixes[] = {"ly", "ps", "scm", 0 };
289
290   
291   vector<string> dirs;
292   for (char const **s = suffixes; *s; s++)
293     {
294       string path = prefix_directory + to_string ('/') + string (*s);
295       dirs.push_back (path);
296     }
297
298
299   dirs.push_back (prefix_directory + "/fonts/otf/");
300   dirs.push_back (prefix_directory + "/fonts/type1/");
301   dirs.push_back (prefix_directory + "/fonts/svg/");
302   
303   for (vsize i = 0; i < dirs.size (); i++)
304     global_path.prepend (dirs[i]);
305 }