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