]> git.donarmstrong.com Git - lilypond.git/blob - lily/relocate.cc
Merge branch 'lilypond/translation' of ssh://git.sv.gnu.org/srv/git/lilypond
[lilypond.git] / lily / relocate.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2005--2010 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "relocate.hh"
21
22 #include "config.hh"
23
24
25 /* TODO: autoconf support */
26
27 #include <sys/types.h>
28 #include <dirent.h>
29
30 #if HAVE_GETTEXT
31 #include <libintl.h>
32 #endif
33
34 #include "file-name.hh"
35 #include "file-path.hh"
36 #include "international.hh"
37 #include "lily-guile.hh"
38 #include "lily-version.hh"
39 #include "main.hh"
40 #include "version.hh"
41 #include "warn.hh"
42
43 #define FRAMEWORKDIR ".."
44
45 int
46 sane_putenv (char const *key, string value, bool overwrite)
47 {
48   if (overwrite || !getenv (key))
49     {
50       string combine = string (key) + "=" + value;
51       char *s = strdup (combine.c_str ());
52
53       if (be_verbose_global)
54         progress_indication (_f ("Setting %s to %s" , key, value.c_str ())
55                              + "\n");
56                              
57       int retval =  putenv (s);
58       /*
59         unfortunately, we can't portably free S here,
60         due to various bugs in glibc prior to 2.1.1
61       */ 
62       return retval;
63     }
64   
65   return -1;
66 }
67
68 static int
69 set_env_file (char const *key, string value, bool overwrite = false)
70 {
71   if (is_file (value))
72     return sane_putenv (key, value, overwrite);
73   else if (be_verbose_global)
74     warning (_f ("no such file: %s for %s", value, key));
75   return -1;
76 }
77
78 static int
79 set_env_dir (char const *key, string value)
80 {
81   if (is_dir (value))
82     return sane_putenv (key, value, false);
83   else if (be_verbose_global)
84     warning (_f ("no such directory: %s for %s", value, key));
85   return -1;
86 }
87
88 static int
89 prepend_env_path (char const *key, string value)
90 {
91   if (is_dir (value))
92     {
93       if (be_verbose_global)
94         progress_indication (_f ("%s=%s (prepend)\n", key, value.c_str ())); 
95
96       if (char const *cur = getenv (key))
97         value += to_string (PATHSEP) + cur;
98
99       return sane_putenv (key, value.c_str (), true);
100     }
101   else if (be_verbose_global)
102     warning (_f ("no such directory: %s for %s", value, key));
103   return -1;
104 }
105
106 #ifdef __MINGW32__
107 #include <winbase.h>
108 #endif
109
110 static void
111 prefix_relocation (string prefix)
112 {
113   string bindir = prefix + "/bin";
114   string datadir = prefix + "/share";
115   string localedir = datadir + "/locale";
116   string package_datadir = datadir + "/lilypond/";
117   string old_lilypond_datadir = lilypond_datadir;
118
119   if (is_dir (package_datadir + "/" + TOPLEVEL_VERSION))
120     lilypond_datadir = package_datadir + "/" + TOPLEVEL_VERSION;
121   else if (is_dir (package_datadir + "/current"))
122     lilypond_datadir = package_datadir + "/current";
123   else
124     warning (_f ("not relocating, no %s/ or current/ found under %s",
125                  TOPLEVEL_VERSION, package_datadir.c_str ()));
126   
127 #if HAVE_GETTEXT
128   if (is_dir (localedir))
129     bindtextdomain ("lilypond", localedir.c_str ());
130 #endif
131
132   prepend_env_path ("PATH", bindir);
133
134   if (be_verbose_global)
135     warning (_f ("Relocation: compile datadir=%s, new datadir=%s",
136                  old_lilypond_datadir.c_str (),
137                  lilypond_datadir.c_str ()));
138 }
139
140 /*
141   UGH : this is a complete mess.
142 */
143
144 static void
145 framework_relocation (string prefix)
146 {
147   if (be_verbose_global)
148     warning (_f ("Relocation: framework_prefix=%s", prefix));
149
150   sane_putenv ("INSTALLER_PREFIX", prefix, true);
151                
152   read_relocation_dir (prefix + "/etc/relocate/");
153
154   string bindir = prefix + "/bin";
155   
156   prepend_env_path ("PATH", bindir);
157 }
158
159 /*
160   UGH : this is a complete mess.
161 */
162 void
163 setup_paths (char const *argv0_ptr)
164 {
165   File_name argv0_filename (argv0_ptr);
166   
167   if (relocate_binary)
168     {
169       string prefix_directory;
170       if (getenv ("LILYPOND_RELOCATE_PREFIX"))
171         {
172           prefix_directory = getenv ("LILYPOND_RELOCATE_PREFIX");
173 #ifdef __MINGW32__
174           /* Normalize file name.  */
175           prefix_directory = File_name (prefix_directory).to_string ();
176 #endif /* __MINGW32__ */
177       
178           prefix_relocation (prefix_directory);
179           string bindir = prefix_directory + "/bin";
180           framework_relocation (bindir);
181         }
182       else if (relocate_binary)
183         {
184           string argv0_abs;
185           if (argv0_filename.is_absolute ())
186             {
187               argv0_abs = argv0_filename.to_string ();
188               if (be_verbose_global)
189                 warning (_f ("Relocation: is absolute: argv0=%s", argv0_ptr));
190             }
191           else if (argv0_filename.dir_.length ())
192             {
193               argv0_abs = get_working_directory ()
194                 + "/" + string (argv0_filename.to_string ());
195               if (be_verbose_global)
196                 warning (_f ("Relocation: from cwd: argv0=%s", argv0_ptr));
197             }
198           else
199             {
200               /* Find absolute ARGV0 name, using PATH.  */
201               File_path path;
202               path.parse_path (getenv ("PATH"));
203
204               if (be_verbose_global)
205                 warning (_f ("Relocation: from PATH=%s\nargv0=%s",
206                              path.to_string ().c_str (), argv0_ptr));
207
208 #ifndef __MINGW32__
209               argv0_abs = path.find (argv0_filename.to_string ());
210 #else /* __MINGW32__ */
211               char const *ext[] = {"exe", "", 0 };
212               argv0_abs = path.find (argv0_filename.to_string (), ext);
213 #endif /* __MINGW32__ */
214
215               if (argv0_abs.empty ())
216                 programming_error ("cannot find absolute argv0");
217             }
218
219           string bindir = dir_name (argv0_abs);
220           string argv0_prefix = dir_name (bindir);
221           string compile_prefix = dir_name (dir_name (dir_name (lilypond_datadir)));
222           if (argv0_prefix != compile_prefix)
223             {
224               prefix_relocation (argv0_prefix);
225               prefix_directory = argv0_prefix;
226             }
227           if (argv0_prefix != compile_prefix || string (FRAMEWORKDIR) != "..")
228             {
229               framework_relocation (bindir + "/" + FRAMEWORKDIR);
230               prefix_directory = bindir + "/" + FRAMEWORKDIR;
231             }
232         }
233
234       lilypond_datadir = prefix_directory
235         + "/share/lilypond/" TOPLEVEL_VERSION;
236     }
237
238   if (getenv ("LILYPONDPREFIX"))
239     error (_ ("LILYPONDPREFIX is obsolete, use LILYPOND_DATADIR"));
240
241   if (char const *env = getenv ("LILYPOND_DATADIR"))
242     {
243       /* Normalize file name.  */
244       lilypond_datadir = File_name (env).to_string ();
245     }
246
247   /* When running from build dir, a full LILYPOND_DATADIR is set-up at
248      $(OUTBASE)/{share, lib}/lilypond/current.  Configure lily using
249      ./configure --prefix=$(pwd)/out */
250   string build_datadir_current = dir_name (lilypond_datadir) + "/current";
251   if (!is_dir (lilypond_datadir.c_str ())
252       && is_dir (build_datadir_current.c_str ()))
253     lilypond_datadir = build_datadir_current;
254
255
256   lilypond_datadir = File_name (lilypond_datadir).canonicalized().to_string();
257   
258   global_path.append ("");
259
260   /* Adding mf/out make lilypond unchanged source directory, when setting
261      LILYPONDPREFIX to lilypond-x.y.z */
262   char const *suffixes[] = {"ly", "ps", "scm", 0 };
263   
264   vector<string> dirs;
265   for (char const **s = suffixes; *s; s++)
266     {
267       string path = lilypond_datadir + to_string ('/') + string (*s);
268       dirs.push_back (path);
269     }
270   
271   dirs.push_back (lilypond_datadir + "/fonts/otf/");
272   dirs.push_back (lilypond_datadir + "/fonts/type1/");
273   dirs.push_back (lilypond_datadir + "/fonts/svg/");
274   
275   for (vsize i = 0; i < dirs.size (); i++)
276     global_path.prepend (dirs[i]);
277 }
278
279 string
280 expand_environment_variables (string orig)
281 {
282   char const *start_ptr = orig.c_str ();
283   char const *ptr = orig.c_str ();
284   size_t len = orig.length ();
285
286   string out;
287   while (ptr < start_ptr + len)
288     {
289       char const *dollar = strchr (ptr, '$');
290       
291       if (dollar != NULL)
292         {
293           char const *start_var = dollar + 1;
294           char const *end_var = start_var;
295           char const *start_next = end_var;
296           
297           out += string (ptr, dollar - ptr);
298           ptr = dollar;
299
300           if (*start_var == '{')
301             {
302               start_var ++;
303               
304               end_var = strchr (start_var, '}');
305               
306               if (end_var == NULL)
307                 {
308                   end_var = start_var + len;
309                   start_next = end_var;
310                 }
311               else
312                 {
313                   start_next = end_var + 1; 
314                 }
315             }
316           else 
317             {
318               /*
319                 Hmm. what to do for $1 , $~ etc.?
320               */
321               do
322                 {
323                   end_var ++;
324                 }
325               while (isalnum (*end_var) || *end_var == '_');
326               start_next = end_var;
327             }
328
329           if (start_var < end_var)
330             {
331               string var_name (start_var, end_var - start_var);
332               char const *value = getenv (var_name.c_str ());
333               if (value != NULL)
334                 out += string (value);
335
336               ptr = start_next;
337             }
338         }
339       else
340         break;
341
342     }
343
344   out += ptr;
345
346   return out;
347 }
348
349 // Ugh - very inefficient, but safer than fgets.
350 static string
351 read_line (FILE *f)
352 {
353   string out;
354   
355   int c = 0;
356   while ((c = fgetc (f)) != EOF && c != '\n')
357     out += char(c);
358
359   return out;
360 }
361
362 void
363 read_relocation_file (string filename)
364 {
365   if (be_verbose_global)
366     progress_indication (_f ("Relocation file: %s", filename.c_str ())
367                          + "\n");
368       
369   char const *cname = filename.c_str ();
370   FILE *f = fopen (cname, "r");
371   if (!f)
372     error (_f ("cannot open file: `%s'", cname));
373
374   while (!feof (f))
375     {
376       string line = read_line (f);
377       size_t idx = line.find (' ');
378       if (idx == NPOS)
379         continue;
380       
381       string command = line.substr (0, idx);
382       line = line.substr (idx + 1);
383       
384       if (idx == NPOS)
385         continue;
386       idx = line.find ('=');
387
388       string variable = line.substr (0, idx);
389       string value = line.substr (idx + 1);
390
391       value = expand_environment_variables (value);
392
393       if (command == "set")
394         sane_putenv (variable.c_str (), value, true);
395       else if (command == "setdir")
396         set_env_dir (variable.c_str (), value);
397       else if (command == "setfile")
398         set_env_file (variable.c_str (), value);
399       else if (command == "prependdir")
400         prepend_env_path (variable.c_str (), value);
401       else
402         error (_f ("Unknown relocation command %s", command));
403     }
404
405   fclose (f);
406 }
407
408 void
409 read_relocation_dir (string dirname)
410 {
411   if (DIR *dir = opendir (dirname.c_str ()))
412     while (struct dirent *ent = readdir (dir))
413       {
414         File_name name (ent->d_name);
415         if (name.ext_ == "reloc")
416           read_relocation_file (dirname + "/" + name.to_string ());
417       }
418 }