]> git.lyx.org Git - lyx.git/blob - src/support/path_defines.C.in
* lyxfunctional.h: delete compare_memfun and helper classes
[lyx.git] / src / support / path_defines.C.in
1 // -*- C++ -*-
2 /**
3  * \file path_defines.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 path_defines.C.in.
12  * All changes to this file will be lost.
13  */
14
15 #include <config.h>
16
17 #include "path_defines.h"
18
19 #include "debug.h"
20 #include "gettext.h"
21
22 #include "FileInfo.h"
23 #include "filetools.h"
24 #include "lstrings.h"
25 #include "os.h"
26 #include "lyxlib.h"
27
28 using std::string;
29 using std::endl;
30
31
32 namespace {
33
34 // Store for the path to the lyx support files we're actually going to use.
35 string system_lyxdir_;
36
37 // Store for the path to the autogenerated files when running in place
38 string build_lyxdir_;
39
40 // Store for the path to the user-level support files.
41 string user_lyxdir_;
42
43 // Store for the path to the locale directory.
44 string localedir_;
45
46 /* The absolute path to the system-level lyx support files.
47  * (Make-time value.)
48  */
49 string const & lyx_dir()
50 {
51         static string const ld = "%LYX_DIR%";
52         return ld;
53 }
54
55 } // namespace anon
56
57
58 namespace lyx {
59 namespace support {
60
61
62 /* The absolute path to the top of the lyx build tree.
63  * (Make-time value.)
64  */
65 string const & top_srcdir()
66 {
67         static string const lts = "%TOP_SRCDIR%";
68         return lts;
69 }
70
71
72 /* The absolute path to the system-level lyx locale directory.
73  * (Make-time value.)
74  */
75 string const & lyx_localedir()
76 {
77         static string const ll = "%LOCALEDIR%";
78         if (localedir_.empty())
79                 return ll;
80         else
81                 return localedir_;
82 }
83
84
85 string const & build_dir()
86 {
87         static string const bl = "%BUILDDIR%";
88         return bl;
89 }
90
91
92 string const & build_lyxdir()
93 {
94         return build_lyxdir_;
95 }
96
97
98 void build_lyxdir(string const & sld)
99 {
100         build_lyxdir_ = sld;
101 }
102
103
104 string const & system_lyxdir()
105 {
106         return system_lyxdir_;
107 }
108
109
110 void system_lyxdir(string const & sld)
111 {
112         system_lyxdir_ = sld;
113 }
114
115
116 string const & user_lyxdir()
117 {
118         return user_lyxdir_;
119 }
120
121
122 void user_lyxdir(string const & uld)
123 {
124         user_lyxdir_ = uld;
125 }
126
127
128 bool setLyxPaths()
129 {
130         //
131         // Determine path of binary
132         //
133
134         string binpath = os::binpath();
135         string binname = os::binname();
136         string fullbinname = MakeAbsPath(binname, binpath);
137
138         if (binpath.empty()) {
139                 lyxerr << _("Warning: could not determine path of binary.")
140                        << "\n"
141                        << _("If you have problems, try starting LyX with an absolute path.")
142                        << endl;
143         }
144         lyxerr[Debug::INIT] << "Name of binary: " << binname << endl;
145         lyxerr[Debug::INIT] << "Path of binary: " << binpath << endl;
146
147
148         string searchpath;
149
150         //
151         // Determine whether we are running in place and set
152         // build_lyxdir accordingly
153         //
154
155         string const buildlyxdir = MakeAbsPath("../lib", binpath);
156         if (!FileSearch(buildlyxdir, "lyxrc.defaults").empty()) {
157                 searchpath += AddPath(top_srcdir(), "lib") + ';';
158                 build_lyxdir(buildlyxdir);
159                 lyxerr[Debug::INIT] << "Checking whether LyX is run in "
160                         "place... yes" << endl;
161         } else {
162                 lyxerr[Debug::INIT]
163                         << "Checking whether LyX is run in place... no"
164                         << endl;
165         }
166
167         //
168         // Determine system directory.
169         //
170
171         // Directories are searched in this order:
172         // 1) -sysdir command line parameter
173         // 2) LYX_DIR_14x environment variable
174         // 3) Maybe <path of binary>/TOP_SRCDIR/lib
175         // 4) <path of binary>/../Resources/<name of binary>/  [for LyX/Mac]
176         // 5) <path of binary>/../share/<name of binary>/
177         // 5a) repeat 4 after following the Symlink if <path of
178         //     binary> is a symbolic link.
179         // 6) hardcoded lyx_dir
180         // The directory is checked for the presence of the file
181         // "chkconfig.ltx", and if that is present, the directory
182         // is accepted as the system directory.
183         // If no chkconfig.ltx file is found, a warning is given,
184         // and the hardcoded lyx_dir is used.
185
186         // If we had a command line switch, system_lyxdir_ is already set
187         if (!system_lyxdir_.empty())
188                 searchpath = MakeAbsPath(system_lyxdir_) + ';';
189
190         string const lyxdir = GetEnvPath("LYX_DIR_14x");
191
192         if (!lyxdir.empty()) {
193                 lyxerr[Debug::INIT] << "LYX_DIR_14x: " << lyxdir << endl;
194                 searchpath += lyxdir + ';';
195         }
196
197         // Path of binary/../Resources/
198         searchpath += NormalizePath(AddPath(binpath, "../Resources/") +
199                                     OnlyFilename(binname)) + ';';
200
201         string fullbinpath = binpath;
202         FileInfo file(fullbinname, true);
203         if (file.isLink()) {
204                 lyxerr[Debug::INIT] << "binary is a link" << endl;
205                 string link;
206                 if (LyXReadLink(fullbinname, link, true)) {
207                         // Path of binary/../share/name of binary/
208                         searchpath += NormalizePath(AddPath(binpath,
209                                                             "../share/")
210                                                     + OnlyFilename(binname));
211                         searchpath += ';';
212                         fullbinpath = link;
213                         binpath = MakeAbsPath(OnlyPath(fullbinpath));
214                 }
215         }
216
217         bool followlink;
218         do {
219                 // Path of binary/../share/name of binary/
220                 searchpath += NormalizePath(AddPath(binpath, "../share/") +
221                       OnlyFilename(binname)) + ';';
222
223                 // Follow Symlinks
224                 FileInfo file(fullbinpath, true);
225                 followlink = file.isLink();
226                 if (followlink) {
227                         lyxerr[Debug::INIT] << " directory " << fullbinpath
228                                             << " is a link" << endl;
229                         string link;
230                         if (LyXReadLink(fullbinpath, link, true)) {
231                                 fullbinpath = link;
232                                 binpath = MakeAbsPath(OnlyPath(fullbinpath));
233                         }
234                         else {
235                                 followlink = false;
236                         }
237                 }
238         } while (followlink);
239
240         // <absolute top srcdir>/lib
241         searchpath += AddPath(top_srcdir(), "lib") + ';';
242         // Hardcoded dir
243         searchpath += lyx_dir();
244
245         lyxerr[Debug::INIT] << "System directory search path: "
246                             << searchpath << endl;
247
248         string const filename = "chkconfig.ltx";
249         string const temp = FileOpenSearch(searchpath, filename, string());
250         system_lyxdir_ = OnlyPath(temp);
251
252         // Reduce "path/../path" stuff out of system directory
253         system_lyxdir_ = NormalizePath(system_lyxdir_);
254
255         bool path_shown = false;
256
257         // Warn if environment variable is set, but unusable
258         if (!lyxdir.empty()) {
259                 if (system_lyxdir_ != NormalizePath(lyxdir)) {
260                         lyxerr <<_("LYX_DIR_14x environment variable no good.")
261                                << '\n'
262                                << _("System directory set to: ")
263                                << system_lyxdir_ << endl;
264                         path_shown = true;
265                 }
266         }
267
268         // Warn the user if we couldn't find "chkconfig.ltx"
269         if (system_lyxdir_ == "./") {
270                 lyxerr <<_("LyX Warning! Couldn't determine system directory. ")
271                        <<_("Try the '-sysdir' command line parameter or ")
272                        <<_("set the environment variable LYX_DIR_14x to the "
273                            "LyX system directory ")
274                        << _("containing the file `chkconfig.ltx'.") << endl;
275                 if (!path_shown) {
276                         FileInfo fi(lyx_dir());
277                         if (!fi.exist()) {
278                                 lyxerr << "Couldn't even find the default LYX_DIR." << endl
279                                         << "Giving up." << endl;
280                                 exit(1);
281                         }
282                         lyxerr << bformat(_("Using built-in default %1$s but expect problems."),
283                                 lyx_dir()) << endl;
284                 } else {
285                         lyxerr << _("Expect problems.") << endl;
286                 }
287                 system_lyxdir_ = lyx_dir();
288                 path_shown = true;
289         }
290
291         if (!path_shown)
292                 lyxerr[Debug::INIT] << "System directory: '"
293                                     << system_lyxdir_ << '\'' << endl;
294
295         // This is true when we are running from a Mac OS X bundle
296         // (for LyX/Mac)
297         bool const inOSXBundle =
298                 system_lyxdir_ == NormalizePath(AddPath(binpath, "../Resources/")
299                                                 + OnlyFilename(binname));
300         if (inOSXBundle)
301                 lyxerr[Debug::INIT] << "Running from LyX/Mac bundle."
302                                     << endl;
303
304         //
305         // Set PATH for LyX/Mac
306         // LyX/Mac is a relocatable application bundle; here we add to
307         // the PATH so it can find binaries like reLyX inside its own
308         // application bundle, and also append PATH elements that it
309         // needs to run latex, previewers, etc.
310         //
311
312         if (inOSXBundle) {
313                 const string newpath = GetEnv("PATH") + ":" + binpath
314                         + ":/sw/bin:/usr/local/bin"
315                         + ":/usr/local/teTeX/bin/powerpc-apple-darwin-current";
316                 lyx::support::putenv("PATH", newpath);
317                 lyxerr[Debug::INIT] << "LyX/Mac: setting PATH to: "
318                                     << GetEnv("PATH") << endl;
319         }
320
321
322         //
323         // Determine locale directory
324         //
325         if (!GetEnv("LYX_LOCALEDIR").empty()) {
326                 localedir_ = GetEnv("LYX_LOCALEDIR");
327                 lyxerr[Debug::INIT] << "LYX_LOCALEDIR=" << localedir_ << endl;
328         } else if (inOSXBundle) {
329                 string const maybe_localedir =
330                         NormalizePath(AddPath(system_lyxdir_, "../locale"));
331                 FileInfo fi(maybe_localedir);
332                 if (fi.isOK() && fi.isDir()) {
333                         lyxerr[Debug::INIT]
334                                 << "LyX/Mac: setting locale directory to "
335                                 << maybe_localedir << endl;
336                         localedir_ = maybe_localedir;
337                 }
338         }
339
340         //
341         // Determine user lyx-dir
342         //
343
344         // Directories are searched in this order:
345         // 1) -userdir command line parameter
346         // 2) LYX_USERDIR_14x environment variable
347         // 3) $HOME/.<name of binary>
348
349         // If we had a command line switch, user_lyxdir_ is already set
350         bool explicit_userdir = true;
351         if (user_lyxdir_.empty()) {
352
353                 // LYX_USERDIR_14x environment variable
354                 user_lyxdir_ = GetEnvPath("LYX_USERDIR_14x");
355
356                 // default behaviour
357                 if (user_lyxdir_.empty())
358                         if (inOSXBundle)
359                                 user_lyxdir_ = AddPath(GetEnvPath("HOME"),
360                                                        "Library/Preferences/LyX");
361                         else
362                                 user_lyxdir_ = AddPath(GetEnvPath("HOME"),
363                                                        string(".") + PACKAGE);
364                 explicit_userdir = false;
365         }
366
367         lyxerr[Debug::INIT] << "User LyX directory: '"
368                             <<  user_lyxdir_ << '\'' << endl;
369         return explicit_userdir;
370 }
371
372 } // namespace support
373 } // namespace lyx