]> git.lyx.org Git - lyx.git/blob - src/support/package.C.in
Fix bug 2285 (tex2lyx is not found by lib/configure when compiling with
[lyx.git] / src / support / package.C.in
1 // -*- C++ -*-
2 /**
3  * \file package.C
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Angus Leeming
8  *
9  * Full author contact details are available in file CREDITS.
10  *
11  * Warning! This file is autogenerated from package.C.in.
12  * All changes to this file will be lost.
13  */
14
15 #include <config.h>
16
17 #include "support/package.h"
18
19 #include "debug.h"
20 #include "gettext.h"
21
22 #include "support/environment.h"
23 #include "support/filetools.h"
24 #include "support/lstrings.h"
25 #include "support/os.h"
26
27 #if defined (USE_WINDOWS_PACKAGING)
28 # include "support/os_win32.h"
29 #endif
30
31 #include <boost/filesystem/operations.hpp>
32 #include <boost/tuple/tuple.hpp>
33
34 #include <list>
35 #include <utility>
36
37 #if !defined (USE_WINDOWS_PACKAGING) && \
38     !defined (USE_MACOSX_PACKAGING) && \
39     !defined (USE_POSIX_PACKAGING)
40 #error USE_FOO_PACKAGING must be defined for FOO = WINDOWS, MACOSX or POSIX.
41 #endif
42
43 #if defined (USE_MACOSX_PACKAGING)
44 # include <CoreServices/CoreServices.h> // FSFindFolder, FSRefMakePath
45 #endif
46
47 using std::string;
48
49 namespace fs = boost::filesystem;
50
51 namespace lyx {
52 namespace support {
53
54 namespace {
55
56 Package package_;
57 bool initialised_ = false;
58
59 } // namespace anon
60
61
62 void init_package(string const & command_line_arg0,
63                   string const & command_line_system_support_dir,
64                   string const & command_line_user_support_dir,
65                   exe_build_dir_to_top_build_dir top_build_dir_location)
66 {
67         // Can do so only once.
68         if (initialised_)
69                 return;
70
71         package_ = Package(command_line_arg0,
72                            command_line_system_support_dir,
73                            command_line_user_support_dir,
74                            top_build_dir_location);
75         initialised_ = true;
76 }
77
78
79 Package const & package()
80 {
81         // Commented out because package().locale_dir() can be called
82         // from the message translation code in messages.C before
83         // init_package() is called. Lars is on the case...
84         // BOOST_ASSERT(initialised_);
85         return package_;
86 }
87
88
89 namespace {
90
91 string const abs_path_from_binary_name(string const & exe);
92
93 std::pair<string, string> const
94 get_build_dirs(string const & abs_binary,
95                exe_build_dir_to_top_build_dir top_build_dir_location);
96
97 string const get_document_dir(string const & home_dir);
98
99 string const get_home_dir();
100
101 string const get_locale_dir(string const & system_support_dir);
102
103 string const get_system_support_dir(string const & abs_binary,
104                                     string const & command_line_system_support_dir);
105
106 string const get_temp_dir();
107
108 string const get_default_user_support_dir(string const & home_dir);
109
110 std::pair<string, bool> const
111 get_user_support_dir(string const & default_user_support_dir,
112                      string const & command_line_user_support_dir);
113
114
115 string const & with_version_suffix();
116
117 } // namespace anon
118
119
120 Package::Package(string const & command_line_arg0,
121                  string const & command_line_system_support_dir,
122                  string const & command_line_user_support_dir,
123                  exe_build_dir_to_top_build_dir top_build_dir_location)
124         : explicit_user_support_dir_(false)
125 {
126         home_dir_ = get_home_dir();
127         temp_dir_ = get_temp_dir();
128         document_dir_ = get_document_dir(home_dir_);
129
130         string const abs_binary = abs_path_from_binary_name(command_line_arg0);
131         binary_dir_ = onlyPath(abs_binary);
132
133         // Is LyX being run in-place from the build tree?
134         boost::tie(build_support_dir_, system_support_dir_) =
135                 get_build_dirs(abs_binary, top_build_dir_location);
136
137         if (build_support_dir_.empty())
138                 system_support_dir_ =
139                         get_system_support_dir(abs_binary,
140                                                command_line_system_support_dir);
141
142         locale_dir_ = get_locale_dir(system_support_dir_);
143
144         string const default_user_support_dir =
145                 get_default_user_support_dir(home_dir_);
146         boost::tie(user_support_dir_, explicit_user_support_dir_) =
147                 get_user_support_dir(default_user_support_dir,
148                                      command_line_user_support_dir);
149
150         string const configure_script = addName(system_support(), "configure.py");
151         configure_command_ = "python " + quoteName(configure_script)
152                                        + with_version_suffix();
153
154         lyxerr[Debug::INIT]
155                 << "<package>\n"
156                 << "\tbinary_dir " << binary_dir() << '\n'
157                 << "\tsystem_support " << system_support() << '\n'
158                 << "\tbuild_support " << build_support() << '\n'
159                 << "\tuser_support " << user_support() << '\n'
160                 << "\tlocale_dir " << locale_dir() << '\n'
161                 << "\tdocument_dir " << document_dir() << '\n'
162                 << "\ttemp_dir " << temp_dir() << '\n'
163                 << "\thome_dir " << home_dir() << '\n'
164                 << "</package>\n" << std::endl;
165 }
166
167
168 namespace {
169
170 // These next three functions contain the stuff that is substituted at
171 // configuration-time.
172 string const top_srcdir()
173 {
174         static string const dir("%TOP_SRCDIR%");
175         return dir;
176 }
177
178
179 string const hardcoded_localedir()
180 {
181         return string("%LOCALEDIR%");
182 }
183
184
185 string const hardcoded_system_support_dir()
186 {
187         return string("%LYX_DIR%");
188 }
189
190
191 string const & with_version_suffix()
192 {
193         static string const program_suffix("%PROGRAM_SUFFIX%");
194         static string const
195                 with_version_suffix(" --with-version-suffix=%PROGRAM_SUFFIX%");
196         return program_suffix.empty() ? program_suffix : with_version_suffix;
197 }
198
199 } // namespace anon
200
201
202 string const & Package::top_srcdir() const
203 {
204         static string const dir("%TOP_SRCDIR%");
205         return dir;
206 }
207
208
209 namespace {
210
211 bool check_command_line_dir(string const & dir,
212                             string const & file,
213                             string const & command_line_switch);
214
215 string const extract_env_var_dir(string const & env_var);
216
217 bool check_env_var_dir(string const & dir,
218                        string const & env_var);
219
220 bool check_env_var_dir(string const & dir,
221                        string const & file,
222                        string const & env_var);
223
224 string const relative_locale_dir();
225
226 string const relative_system_support_dir();
227
228
229 std::string const
230 get_build_support_dir(std::string const & binary_dir,
231                       exe_build_dir_to_top_build_dir top_build_dir_location)
232 {
233         string indirection;
234         switch (top_build_dir_location) {
235         case top_build_dir_is_one_level_up:
236                 indirection = "../lib";
237                 break;
238         case top_build_dir_is_two_levels_up:
239                 indirection = "../../lib";
240                 break;
241         }
242
243         return normalizePath(addPath(binary_dir, indirection));
244 }
245
246
247 std::pair<string, string> const
248 get_build_dirs(string const & abs_binary,
249                exe_build_dir_to_top_build_dir top_build_dir_location)
250 {
251         string const check_text = "Checking whether LyX is run in place...";
252
253         // We're looking for "lyxrc.defaults" in a directory
254         //   binary_dir/../lib
255         // We're also looking for "chkconfig.ltx" in a directory
256         //   binary_dir/top_srcdir()/lib
257         // If both are found, then we're running LyX in-place.
258
259         // Note that the name of the lyx binary may be a symbolic link.
260         // If that is the case, then we follow the links too.
261         string binary = abs_binary;
262         while (true) {
263                 // Try and find "lyxrc.defaults".
264                 string const binary_dir = onlyPath(binary);
265                 string const build_support_dir =
266                         get_build_support_dir(binary_dir, top_build_dir_location);
267
268                 if (!fileSearch(build_support_dir, "lyxrc.defaults").empty()) {
269                         // Try and find "chkconfig.ltx".
270                         string const system_support_dir =
271                                 makeAbsPath(addPath(top_srcdir(), "lib"),
272                                             addPath(binary_dir, "support"));
273
274                         if (!fileSearch(system_support_dir, "chkconfig.ltx").empty()) {
275                                 lyxerr[Debug::INIT] << check_text << " yes"
276                                                     << std::endl;
277                                 return std::make_pair(build_support_dir, system_support_dir);
278                         }
279                 }
280
281                 // Check whether binary is a symbolic link.
282                 // If so, resolve it and repeat the exercise.
283                 if (!fs::symbolic_link_exists(binary))
284                         break;
285
286                 string link;
287                 if (readLink(binary, link, true)) {
288                         binary = link;
289                 } else {
290                         // Unable to resolve the link.
291                         break;
292                 }
293         }
294
295         lyxerr[Debug::INIT] << check_text << " no" << std::endl;
296         return std::make_pair(string(), string());
297 }
298
299
300 // Specification of document_dir_ may be reset by LyXRC,
301 // but the default is fixed for a given OS.
302 string const get_document_dir(string const & home_dir)
303 {
304 #if defined (USE_WINDOWS_PACKAGING)
305         (void)home_dir; // Silence warning about unused variable.
306         os::GetFolderPath win32_folder_path;
307         return win32_folder_path(os::GetFolderPath::PERSONAL);
308 #else // Posix-like.
309         return home_dir;
310 #endif
311 }
312
313
314 // The specification of home_dir_ is fixed for a given OS.
315 // A typical example on Windows: "C:/Documents and Settings/USERNAME"
316 // and on a Posix-like machine: "/home/USERNAME".
317 string const get_home_dir()
318 {
319 #if defined (USE_WINDOWS_PACKAGING)
320         string const home_dir = getEnv("USERPROFILE");
321 #else // Posix-like.
322         string const home_dir = getEnv("HOME");
323 #endif
324
325         return os::internal_path(home_dir);
326 }
327
328
329 // Several sources are probed to ascertain the locale directory.
330 // The only requirement is that the result is indeed a directory.
331 string const get_locale_dir(string const & system_support_dir)
332 {
333         // 1. Use the "LYX_LOCALEDIR" environment variable.
334         string path = extract_env_var_dir("LYX_LOCALEDIR");
335         if (!path.empty() && check_env_var_dir(path, "LYX_LOCALEDIR"))
336                 return path;
337
338         // 2. Search for system_support_dir / <relative locale dir>
339         // The <relative locale dir> is OS-dependent. (On Unix, it will
340         // be "../locale/".)
341         path = normalizePath(addPath(system_support_dir, relative_locale_dir()));
342
343         if (fs::exists(path) && fs::is_directory(path))
344                 return path;
345
346         // 3. Fall back to the hard-coded LOCALEDIR.
347         path = hardcoded_localedir();
348         if (fs::exists(path) && fs::is_directory(path))
349                 return path;
350
351         return string();
352 }
353
354
355 // Specification of temp_dir_ may be reset by LyXRC,
356 // but the default is fixed for a given OS.
357 string const get_temp_dir()
358 {
359 #if defined (USE_WINDOWS_PACKAGING)
360         // Typical example: C:/TEMP/.
361         char path[PATH_MAX];
362         GetTempPath(PATH_MAX, path);
363         return os::internal_path(path);
364 #else // Posix-like.
365         return "/tmp";
366 #endif
367 }
368
369
370 void bail_out()
371 {
372 #ifndef CXX_GLOBAL_CSTD
373         using std::exit;
374 #endif
375         exit(1);
376 }
377
378
379 // Extracts the absolute path from the foo of "-sysdir foo" or "-userdir foo"
380 string const abs_path_from_command_line(string const & command_line)
381 {
382         if (command_line.empty())
383                 return string();
384
385         string const path = os::internal_path(command_line);
386         return os::is_absolute_path(path) ? path : makeAbsPath(path);
387 }
388
389
390 // Does the grunt work for abs_path_from_binary_name()
391 string const get_binary_path(string const & exe)
392 {
393 #if defined (USE_WINDOWS_PACKAGING)
394         // The executable may have been invoked either with or
395         // without the .exe extension.
396         // Ensure that it is present.
397         string const as_internal_path = os::internal_path(exe);
398         string const exe_path = suffixIs(as_internal_path, ".exe") ?
399                 as_internal_path : as_internal_path + ".exe";
400 #else
401         string const exe_path = os::internal_path(exe);
402 #endif
403         if (os::is_absolute_path(exe_path))
404                 return exe_path;
405
406         // Two possibilities present themselves.
407         // 1. The binary is relative to the CWD.
408         string const abs_exe_path = makeAbsPath(exe_path);
409         if (fs::exists(abs_exe_path))
410                 return abs_exe_path;
411
412         // 2. exe must be the name of the binary only and it
413         // can be found on the PATH.
414         string const exe_name = onlyFilename(exe_path);
415         if (exe_name != exe_path)
416                 return string();
417
418         std::vector<string> const path = getEnvPath("PATH");
419         std::vector<string>::const_iterator it = path.begin();
420         std::vector<string>::const_iterator const end = path.end();
421         for (; it != end; ++it) {
422                 // This will do nothing if *it is already absolute.
423                 string const exe_dir = makeAbsPath(*it);
424
425                 string const exe_path = addName(exe_dir, exe_name);
426                 if (fs::exists(exe_path))
427                         return exe_path;
428         }
429
430         // Didn't find anything.
431         return string();
432 }
433
434
435 // Extracts the absolute path to the binary name received as argv[0].
436 string const abs_path_from_binary_name(string const & exe)
437 {
438         string const abs_binary = get_binary_path(exe);
439         if (abs_binary.empty()) {
440                 lyxerr << bformat(_("Unable to determine the path to the "
441                                     "LyX binary from the command line %1$s"),
442                                   exe)
443                        << std::endl;
444                 bail_out();
445         }
446         return abs_binary;
447 }
448
449
450 // A plethora of directories is searched to ascertain the system
451 // lyxdir which is defined as the first directory to contain
452 // "chkconfig.ltx".
453 string const
454 get_system_support_dir(string const & abs_binary,
455                   string const & command_line_system_support_dir)
456 {
457         string const chkconfig_ltx = "chkconfig.ltx";
458
459         // searched_dirs is used for diagnostic purposes only in the case
460         // that "chkconfig.ltx" is not found.
461         std::list<string> searched_dirs;
462
463         // 1. Use the -sysdir command line parameter.
464         string path = abs_path_from_command_line(command_line_system_support_dir);
465         if (!path.empty()) {
466                 searched_dirs.push_back(path);
467                 if (check_command_line_dir(path, chkconfig_ltx, "-sysdir"))
468                         return path;
469         }
470
471         // 2. Use the "LYX_DIR_14x" environment variable.
472         path = extract_env_var_dir("LYX_DIR_14x");
473         if (!path.empty()) {
474                 searched_dirs.push_back(path);
475                 if (check_env_var_dir(path, chkconfig_ltx, "LYX_DIR_14x"))
476                         return path;
477         }
478
479         // 3. Search relative to the lyx binary.
480         // We're looking for "chkconfig.ltx" in a directory
481         //   OnlyPath(abs_binary) / <relative dir> / PACKAGE /
482         // PACKAGE is hardcoded in config.h. Eg "lyx" or "lyx-1.3.6cvs".
483         // <relative dir> is OS-dependent; on Unix, it will be "../share/".
484         string const relative_lyxdir = relative_system_support_dir();
485
486         // One subtlety to be aware of. The name of the lyx binary may be
487         // a symbolic link. If that is the case, then we follow the links too.
488         string binary = abs_binary;
489         while (true) {
490                 // Try and find "chkconfig.ltx".
491                 string const binary_dir = onlyPath(binary);
492
493                 string const lyxdir =
494                         normalizePath(addPath(binary_dir, relative_lyxdir));
495                 searched_dirs.push_back(lyxdir);
496
497                 if (!fileSearch(lyxdir, chkconfig_ltx).empty()) {
498                         // Success! "chkconfig.ltx" has been found.
499                         return lyxdir;
500                 }
501
502                 // Check whether binary is a symbolic link.
503                 // If so, resolve it and repeat the exercise.
504                 if (!fs::symbolic_link_exists(binary))
505                         break;
506
507                 string link;
508                 if (readLink(binary, link, true)) {
509                         binary = link;
510                 } else {
511                         // Unable to resolve the link.
512                         break;
513                 }
514         }
515
516         // 4. Repeat the exercise on the directory itself.
517         string binary_dir = onlyPath(abs_binary);
518         while (true) {
519                 // This time test whether the directory is a symbolic link
520                 // *before* looking for "chkconfig.ltx".
521                 // (We've looked relative to the original already.)
522                 if (!fs::symbolic_link_exists(binary))
523                         break;
524
525                 string link;
526                 if (readLink(binary_dir, link, true)) {
527                         binary_dir = link;
528                 } else {
529                         // Unable to resolve the link.
530                         break;
531                 }
532
533                 // Try and find "chkconfig.ltx".
534                 string const lyxdir =
535                         normalizePath(addPath(binary_dir, relative_lyxdir));
536                 searched_dirs.push_back(lyxdir);
537
538                 if (!fileSearch(lyxdir, chkconfig_ltx).empty()) {
539                         // Success! "chkconfig.ltx" has been found.
540                         return lyxdir;
541                 }
542         }
543
544         // 5. In desparation, try the hard-coded system support dir.
545         path = hardcoded_system_support_dir();
546         if (!fileSearch(path, chkconfig_ltx).empty())
547                 return path;
548
549         // Everything has failed :-(
550         // So inform the user and exit.
551         string searched_dirs_str;
552         typedef std::list<string>::const_iterator iterator;
553         iterator const begin = searched_dirs.begin();
554         iterator const end = searched_dirs.end();
555         for (iterator it = begin; it != end; ++it) {
556                 if (it != begin)
557                         searched_dirs_str += "\n\t";
558                 searched_dirs_str += *it;
559         }
560
561         lyxerr << bformat(_("Unable to determine the system directory "
562                             "having searched\n"
563                             "\t%1$s\n"
564                             "Use the '-sysdir' command line parameter or "
565                             "set the environment variable LYX_DIR_14x to "
566                             "the LyX system directory containing the file "
567                             "`chkconfig.ltx'."),
568                           searched_dirs_str)
569                << std::endl;
570
571         bail_out();
572         // Keep the compiler happy.
573         return string();
574 }
575
576
577 // Returns the absolute path to the user lyxdir, together with a flag
578 // indicating whether this directory was specified explicitly (as -userdir
579 // or through an environment variable) or whether it was deduced.
580 std::pair<string, bool> const
581 get_user_support_dir(string const & default_user_support_dir,
582                      string const & command_line_user_support_dir)
583 {
584         bool explicit_userdir = true;
585
586         // 1. Use the -userdir command line parameter.
587         string path =
588                 abs_path_from_command_line(command_line_user_support_dir);
589         if (!path.empty())
590                 return std::make_pair(path, explicit_userdir);
591
592         // 2. Use the LYX_USERDIR_14x environment variable.
593         path = extract_env_var_dir("LYX_USERDIR_14x");
594         if (!path.empty())
595                 return std::make_pair(path, explicit_userdir);
596
597         // 3. Use the OS-dependent default_user_support_dir
598         explicit_userdir = false;
599         return std::make_pair(default_user_support_dir, explicit_userdir);
600 }
601
602
603 // $HOME/.lyx on POSIX but on Win32 it will be something like
604 // "C:/Documents and Settings/USERNAME/Application Data/LyX"
605 string const get_default_user_support_dir(string const & home_dir)
606 {
607 #if defined (USE_WINDOWS_PACKAGING)
608         (void)home_dir; // Silence warning about unused variable.
609
610         os::GetFolderPath win32_folder_path;
611         return addPath(win32_folder_path(os::GetFolderPath::APPDATA), PACKAGE);
612
613 #elif defined (USE_MACOSX_PACKAGING)
614         (void)home_dir; // Silence warning about unused variable.
615
616         FSRef fsref;
617         OSErr const error_code =
618                 FSFindFolder(kUserDomain, kApplicationSupportFolderType,
619                              kDontCreateFolder, &fsref);
620         if (error_code != 0)
621                 return string();
622
623         char store[PATH_MAX + 1];
624         OSStatus const status_code =
625                 FSRefMakePath(&fsref,
626                               reinterpret_cast<UInt8*>(store), PATH_MAX);
627         if (status_code != 0)
628                 return string();
629
630         return addPath(reinterpret_cast<char const *>(store), PACKAGE);
631
632 #else // USE_POSIX_PACKAGING
633         return addPath(home_dir, string(".") + PACKAGE);
634 #endif
635 }
636
637
638 // Check that directory @c dir contains @c file.
639 // Else emit a warning about an invalid @c command_line_switch.
640 bool check_command_line_dir(string const & dir,
641                             string const & file,
642                             string const & command_line_switch)
643 {
644         string const abs_path = fileSearch(dir, file);
645         if (abs_path.empty()) {
646                 lyxerr << bformat(_("Invalid %1$s switch.\n"
647                                     "Directory %2$s does not contain %3$s."),
648                                   command_line_switch, dir, file)
649                        << std::endl;
650         }
651
652         return !abs_path.empty();
653 }
654
655
656 // The environment variable @c env_var expands to a (single) file path.
657 string const extract_env_var_dir(string const & env_var)
658 {
659         string const dir = os::internal_path(getEnv(env_var));
660         return dir.empty() ? dir : makeAbsPath(dir);
661 }
662
663
664 // Check that directory @c dir contains @c file.
665 // Else emit a warning about an invalid @c env_var.
666 bool check_env_var_dir(string const & dir,
667                        string const & file,
668                        string const & env_var)
669 {
670         string const abs_path = fileSearch(dir, file);
671         if (abs_path.empty()) {
672                 lyxerr << bformat(_("Invalid %1$s environment variable.\n"
673                                     "Directory %2$s does not contain %3$s."),
674                                   env_var, dir, file)
675                        << std::endl;
676         }
677
678         return !abs_path.empty();
679 }
680
681
682 // Check that directory @c dir is indeed a directory.
683 // Else emit a warning about an invalid @c env_var.
684 bool check_env_var_dir(string const & dir,
685                        string const & env_var)
686 {
687         bool const success = (fs::exists(dir) && fs::is_directory(dir));
688
689         if (!success) {
690                 // Put this string on a single line so that the gettext
691                 // search mechanism in po/Makefile.in.in will register
692                 // package.C.in as a file containing strings that need
693                 // translation.
694                 string const fmt =
695                 _("Invalid %1$s environment variable.\n%2$s is not a directory.");
696
697                 lyxerr << bformat(fmt, env_var, dir)
698                        << std::endl;
699         }
700
701         return success;
702 }
703
704
705 // The locale directory relative to the LyX system directory.
706 string const relative_locale_dir()
707 {
708         return "../locale/";
709 }
710
711
712 // The system lyxdir is relative to the directory containing the LyX binary.
713 string const relative_system_support_dir()
714 {
715         string result;
716
717 #if defined (USE_WINDOWS_PACKAGING) || defined (USE_MACOSX_PACKAGING)
718         result = "../Resources/";
719 #else // Posix-like.
720         result = addPath("../share/", PACKAGE);
721 #endif
722
723         return result;
724 }
725
726 } // namespace anon
727
728 } // namespace support
729 } // namespace lyx