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