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