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