4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * \author Angus Leeming
9 * Full author contact details are available in file CREDITS.
11 * Warning! This file is autogenerated from path_defines.C.in.
12 * All changes to this file will be lost.
17 #include "path_defines.h"
23 #include "filetools.h"
34 // Store for the path to the lyx support files we're actually going to use.
35 string system_lyxdir_;
37 // Store for the path to the autogenerated files when running in place
40 // Store for the path to the user-level support files.
43 // Store for the path to the locale directory.
46 /* The absolute path to the system-level lyx support files.
49 string const & lyx_dir()
51 static string const ld = "%LYX_DIR%";
62 /* The absolute path to the top of the lyx build tree.
65 string const & top_srcdir()
67 static string const lts = "%TOP_SRCDIR%";
72 /* The absolute path to the system-level lyx locale directory.
75 string const & lyx_localedir()
77 static string const ll = "%LOCALEDIR%";
78 if (localedir_.empty())
85 string const & build_dir()
87 static string const bl = "%BUILDDIR%";
92 string const & build_lyxdir()
98 void build_lyxdir(string const & sld)
104 string const & system_lyxdir()
106 return system_lyxdir_;
110 void system_lyxdir(string const & sld)
112 system_lyxdir_ = sld;
116 string const & user_lyxdir()
122 void user_lyxdir(string const & uld)
131 // Determine path of binary
134 string binpath = os::binpath();
135 string binname = os::binname();
136 string fullbinname = MakeAbsPath(binname, binpath);
138 if (binpath.empty()) {
139 lyxerr << _("Warning: could not determine path of binary.")
141 << _("If you have problems, try starting LyX with an absolute path.")
144 lyxerr[Debug::INIT] << "Name of binary: " << binname << endl;
145 lyxerr[Debug::INIT] << "Path of binary: " << binpath << endl;
151 // Determine whether we are running in place and set
152 // build_lyxdir accordingly
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;
163 << "Checking whether LyX is run in place... no"
168 // Determine system directory.
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.
186 // If we had a command line switch, system_lyxdir_ is already set
187 if (!system_lyxdir_.empty())
188 searchpath = MakeAbsPath(system_lyxdir_) + ';';
190 string const lyxdir = GetEnvPath("LYX_DIR_14x");
192 if (!lyxdir.empty()) {
193 lyxerr[Debug::INIT] << "LYX_DIR_14x: " << lyxdir << endl;
194 searchpath += lyxdir + ';';
197 // Path of binary/../Resources/
198 searchpath += NormalizePath(AddPath(binpath, "../Resources/") +
199 OnlyFilename(binname)) + ';';
201 string fullbinpath = binpath;
202 FileInfo file(fullbinname, true);
204 lyxerr[Debug::INIT] << "binary is a link" << endl;
206 if (LyXReadLink(fullbinname, link, true)) {
207 // Path of binary/../share/name of binary/
208 searchpath += NormalizePath(AddPath(binpath,
210 + OnlyFilename(binname));
213 binpath = MakeAbsPath(OnlyPath(fullbinpath));
219 // Path of binary/../share/name of binary/
220 searchpath += NormalizePath(AddPath(binpath, "../share/") +
221 OnlyFilename(binname)) + ';';
224 FileInfo file(fullbinpath, true);
225 followlink = file.isLink();
227 lyxerr[Debug::INIT] << " directory " << fullbinpath
228 << " is a link" << endl;
230 if (LyXReadLink(fullbinpath, link, true)) {
232 binpath = MakeAbsPath(OnlyPath(fullbinpath));
238 } while (followlink);
240 // <absolute top srcdir>/lib
241 searchpath += AddPath(top_srcdir(), "lib") + ';';
243 searchpath += lyx_dir();
245 lyxerr[Debug::INIT] << "System directory search path: "
246 << searchpath << endl;
248 string const filename = "chkconfig.ltx";
249 string const temp = FileOpenSearch(searchpath, filename, string());
250 system_lyxdir_ = OnlyPath(temp);
252 // Reduce "path/../path" stuff out of system directory
253 system_lyxdir_ = NormalizePath(system_lyxdir_);
255 bool path_shown = false;
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.")
262 << _("System directory set to: ")
263 << system_lyxdir_ << endl;
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;
276 FileInfo fi(lyx_dir());
278 lyxerr << "Couldn't even find the default LYX_DIR." << endl
279 << "Giving up." << endl;
282 lyxerr << bformat(_("Using built-in default %1$s but expect problems."),
285 lyxerr << _("Expect problems.") << endl;
287 system_lyxdir_ = lyx_dir();
292 lyxerr[Debug::INIT] << "System directory: '"
293 << system_lyxdir_ << '\'' << endl;
295 // This is true when we are running from a Mac OS X bundle
297 bool const inOSXBundle =
298 system_lyxdir_ == NormalizePath(AddPath(binpath, "../Resources/")
299 + OnlyFilename(binname));
301 lyxerr[Debug::INIT] << "Running from LyX/Mac bundle."
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.
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;
323 // Determine locale directory
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()) {
334 << "LyX/Mac: setting locale directory to "
335 << maybe_localedir << endl;
336 localedir_ = maybe_localedir;
341 // Determine user lyx-dir
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>
349 // If we had a command line switch, user_lyxdir_ is already set
350 bool explicit_userdir = true;
351 if (user_lyxdir_.empty()) {
353 // LYX_USERDIR_14x environment variable
354 user_lyxdir_ = GetEnvPath("LYX_USERDIR_14x");
357 if (user_lyxdir_.empty())
359 user_lyxdir_ = AddPath(GetEnvPath("HOME"),
360 "Library/Preferences/LyX");
362 user_lyxdir_ = AddPath(GetEnvPath("HOME"),
363 string(".") + PACKAGE);
364 explicit_userdir = false;
367 lyxerr[Debug::INIT] << "User LyX directory: '"
368 << user_lyxdir_ << '\'' << endl;
369 return explicit_userdir;
372 } // namespace support