]> git.donarmstrong.com Git - lilypond.git/blob - lily/relocate.cc
*** empty log message ***
[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 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 "lily-guile.hh"
24 #include "lily-version.hh"
25 #include "main.hh"
26 #include "version.hh"
27 #include "warn.hh"
28
29
30 int
31 sane_putenv (char const *key, String value, bool overwrite)
32 {
33   if (overwrite || !getenv (key))
34     {
35       String combine = String (key) + "=" + value;
36       char *s = strdup (combine.to_str0 ());
37       return putenv (s);
38     }
39   
40   return -1;
41 }
42
43 static int
44 set_env_file (char const *key, String value)
45 {
46   if (is_file (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));
50   return -1;
51 }
52
53 static int
54 set_env_dir (char const *key, String value)
55 {
56   if (is_dir (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));
60   return -1;
61 }
62
63 static int
64 prepend_env_path (char const *key, String value)
65 {
66   if (is_dir (value))
67     {
68       if (be_verbose_global)
69         progress_indication (_f ("%s=%s\n", key, value.to_str0 ())); 
70
71       if (char const *cur = getenv (key))
72         value += to_string (PATHSEP) + cur;
73
74       return sane_putenv (key, value.to_str0 (), true);
75     }
76   else if (be_verbose_global)
77     warning (_f ("no such directory: %s for %s", value, key));
78   return -1;
79 }
80
81 String
82 dir_name (String const file_name)
83 {
84   String s = file_name;
85   s.substitute ('\\', '/');
86   s = s.left_string (s.index_last ('/'));
87   return s;
88 }
89
90 #ifdef __MINGW32__
91 #include <winbase.h>
92 #endif
93
94 void
95 set_relocation (String bindir, String prefix)
96 {
97   if (be_verbose_global)
98     warning (_f ("Relocation: compile prefix=%s, new prefix=%s",
99                  prefix_directory,
100                  prefix.to_str0 ()));
101   
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;
107
108   if (is_dir (lilypond_datadir))
109     prefix_directory = lilypond_datadir;
110
111 #if HAVE_GETTEXT
112   if (is_dir (localedir))
113     bindtextdomain ("lilypond", localedir.to_str0 ());
114 #endif
115
116   set_env_file ("FONTCONFIG_FILE", sysconfdir + "/fonts/fonts.conf");
117 #ifdef __MINGW32__
118   char font_dir[PATH_MAX];
119   ExpandEnvironmentStrings ("%windir%/fonts", font_dir, sizeof (font_dir));
120   prepend_env_path ("GS_FONTPATH", font_dir);
121 #endif
122
123   /* FIXME: *cough* 8.15 *cough* */
124   prepend_env_path ("GS_FONTPATH", datadir + "/ghostscript/8.15/fonts");
125   prepend_env_path ("GS_LIB", datadir + "/ghostscript/8.15/Resource");
126   prepend_env_path ("GS_LIB", datadir + "/ghostscript/8.15/lib");
127
128   prepend_env_path ("GS_FONTPATH", datadir + "/gs/fonts");
129   prepend_env_path ("GS_LIB", datadir + "/gs/Resource");
130   prepend_env_path ("GS_LIB", datadir + "/gs/lib");
131
132   /* need otherwise dynamic .so's aren't found.   */
133   prepend_env_path ("DYLD_LIBRARY_PATH", libdir);
134   
135   prepend_env_path ("GUILE_LOAD_PATH", datadir
136                     + to_string ("/guile/%d.%d",
137                                  SCM_MAJOR_VERSION, SCM_MINOR_VERSION));
138   set_env_file ("PANGO_RC_FILE", sysconfdir + "/pango/pangorc");
139
140   set_env_dir ("PANGO_PREFIX", prefix);
141                 
142   prepend_env_path ("PATH", bindir);
143 }
144
145 String
146 get_working_directory ()
147 {
148   char cwd[PATH_MAX];
149   getcwd (cwd, PATH_MAX);
150
151   return String (cwd);
152 }
153
154 void
155 setup_paths (char const *argv0_ptr)
156 {
157   File_name argv0_filename (argv0_ptr);
158   
159   prefix_directory = LILYPOND_DATADIR;
160   if (relocate_binary
161       && getenv ("LILYPOND_RELOCATE_PREFIX"))
162     {
163       String prefix = getenv ("LILYPOND_RELOCATE_PREFIX");
164       /*
165         fixme: need different sep for mingw? 
166       */
167       set_relocation (prefix + "/" + "bin", prefix);
168     }
169   else if (relocate_binary)
170     {
171       String argv0_abs;
172       if (argv0_filename.is_absolute ())
173         argv0_abs = argv0_filename.to_string ();
174       else if (argv0_filename.dir_.length ())
175         argv0_abs = get_working_directory ()
176           + "/" + String (argv0_filename.to_string ());
177       else
178         {
179           /* Find absolute ARGV0 name, using PATH.  */
180           File_path path;
181           path.parse_path (getenv ("PATH"));
182       
183 #ifndef __MINGW32__
184           String argv0_abs = path.find (argv0_filename.to_string ());
185 #else /* __MINGW32__ */
186           char const *ext[] = {"exe", "", 0 };
187           String argv0_abs = path.find (argv0_filename.to_string (), ext);
188 #endif /* __MINGW32__ */
189
190           if (argv0_abs.is_empty ())
191             programming_error ("can't find absolute argv0.");
192         }
193       
194       String bindir = dir_name (argv0_abs);
195       String argv0_prefix = dir_name (bindir);
196       if (argv0_prefix != dir_name (dir_name (dir_name (prefix_directory))))
197         set_relocation (bindir, argv0_prefix);
198     }
199
200   /* FIXME: use LILYPOND_DATADIR.  */
201   if (char const *env = getenv ("LILYPONDPREFIX"))
202     {
203 #ifdef __MINGW32__
204       /* Normalize file name.  */
205       env = File_name (env).to_string ().get_copy_str0 ();
206 #endif
207       prefix_directory = env;
208     }
209
210   global_path.append ("");
211
212
213   /*
214     When running from build dir, a full LILYPOND_PREFIX is set-up at
215
216         $(OUTBASE)/share/lilypond/TOPLEVEL_VERSION
217
218      This historical hack will allow the shorthand
219
220         LILYPONDPREFIX=out lily/out/lilypond ...
221
222   */
223   
224   struct stat statbuf;
225   String build_prefix = prefix_directory + "/share/lilypond/" TOPLEVEL_VERSION;
226   if (stat (build_prefix.to_str0 (), &statbuf) == 0)
227     prefix_directory = build_prefix;
228
229   
230   /* Adding mf/out make lilypond unchanged source directory, when setting
231      LILYPONDPREFIX to lilypond-x.y.z */
232   char *suffixes[] = {"ly", "ps", "scm", 0 };
233
234   
235   Array<String> dirs;
236   for (char **s = suffixes; *s; s++)
237     {
238       String path = prefix_directory + to_string ('/') + String (*s);
239       dirs.push (path);
240     }
241
242
243   dirs.push (prefix_directory + "/fonts/otf/");
244   dirs.push (prefix_directory + "/fonts/type1/");
245   dirs.push (prefix_directory + "/fonts/svg/");
246   
247   for (int i = 0; i < dirs.size (); i++)
248     global_path.prepend (dirs[i]);
249 }