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 string const exe_name = OnlyFilename(binname);
222 string const lyx_system_dir_name =
223 suffixIs(exe_name, ".exe") ?
224 ChangeExtension(exe_name, "") :
227 string const lyx_system_dir_name = exe_name;
229 string const shared_dir_name =
230 NormalizePath(AddPath(binpath, "../share/"));
231 searchpath += shared_dir_name + lyx_system_dir_name + ';';
234 FileInfo file(fullbinpath, true);
235 followlink = file.isLink();
237 lyxerr[Debug::INIT] << " directory " << fullbinpath
238 << " is a link" << endl;
240 if (LyXReadLink(fullbinpath, link, true)) {
242 binpath = MakeAbsPath(OnlyPath(fullbinpath));
248 } while (followlink);
250 // <absolute top srcdir>/lib
251 searchpath += AddPath(top_srcdir(), "lib") + ';';
253 searchpath += lyx_dir();
255 lyxerr[Debug::INIT] << "System directory search path: "
256 << searchpath << endl;
258 string const filename = "chkconfig.ltx";
259 string const temp = FileOpenSearch(searchpath, filename, string());
260 system_lyxdir_ = OnlyPath(temp);
262 // Reduce "path/../path" stuff out of system directory
263 system_lyxdir_ = NormalizePath(system_lyxdir_);
265 bool path_shown = false;
267 // Warn if environment variable is set, but unusable
268 if (!lyxdir.empty()) {
269 if (system_lyxdir_ != NormalizePath(lyxdir)) {
270 lyxerr <<_("LYX_DIR_14x environment variable no good.")
272 << _("System directory set to: ")
273 << system_lyxdir_ << endl;
278 // Warn the user if we couldn't find "chkconfig.ltx"
279 if (system_lyxdir_ == "./") {
280 lyxerr <<_("LyX Warning! Couldn't determine system directory. ")
281 <<_("Try the '-sysdir' command line parameter or ")
282 <<_("set the environment variable LYX_DIR_14x to the "
283 "LyX system directory ")
284 << _("containing the file `chkconfig.ltx'.") << endl;
286 FileInfo fi(lyx_dir());
288 lyxerr << "Couldn't even find the default LYX_DIR." << endl
289 << "Giving up." << endl;
292 lyxerr << bformat(_("Using built-in default %1$s but expect problems."),
295 lyxerr << _("Expect problems.") << endl;
297 system_lyxdir_ = lyx_dir();
302 lyxerr[Debug::INIT] << "System directory: '"
303 << system_lyxdir_ << '\'' << endl;
305 // This is true when we are running from a Mac OS X bundle
307 bool const inOSXBundle =
308 system_lyxdir_ == NormalizePath(AddPath(binpath, "../Resources/")
309 + OnlyFilename(binname));
311 lyxerr[Debug::INIT] << "Running from LyX/Mac bundle."
315 // Set PATH for LyX/Mac
316 // LyX/Mac is a relocatable application bundle; here we add to
317 // the PATH so it can find binaries like reLyX inside its own
318 // application bundle, and also append PATH elements that it
319 // needs to run latex, previewers, etc.
323 const string newpath = GetEnv("PATH") + ":" + binpath
324 + ":/sw/bin:/usr/local/bin"
325 + ":/usr/local/teTeX/bin/powerpc-apple-darwin-current";
326 lyx::support::putenv("PATH", newpath);
327 lyxerr[Debug::INIT] << "LyX/Mac: setting PATH to: "
328 << GetEnv("PATH") << endl;
333 // Determine locale directory
335 if (!GetEnv("LYX_LOCALEDIR").empty()) {
336 localedir_ = GetEnv("LYX_LOCALEDIR");
337 lyxerr[Debug::INIT] << "LYX_LOCALEDIR=" << localedir_ << endl;
338 } else if (inOSXBundle) {
339 string const maybe_localedir =
340 NormalizePath(AddPath(system_lyxdir_, "../locale"));
341 FileInfo fi(maybe_localedir);
342 if (fi.isOK() && fi.isDir()) {
344 << "LyX/Mac: setting locale directory to "
345 << maybe_localedir << endl;
346 localedir_ = maybe_localedir;
351 // Determine user lyx-dir
354 // Directories are searched in this order:
355 // 1) -userdir command line parameter
356 // 2) LYX_USERDIR_14x environment variable
357 // 3) $HOME/.<name of binary>
359 // If we had a command line switch, user_lyxdir_ is already set
360 bool explicit_userdir = true;
361 if (user_lyxdir_.empty()) {
363 // LYX_USERDIR_14x environment variable
364 user_lyxdir_ = GetEnvPath("LYX_USERDIR_14x");
367 if (user_lyxdir_.empty())
369 user_lyxdir_ = AddPath(os::homepath(),
370 "Library/Preferences/LyX");
372 user_lyxdir_ = AddPath(os::homepath(),
373 string(".") + PACKAGE);
374 explicit_userdir = false;
377 lyxerr[Debug::INIT] << "User LyX directory: '"
378 << user_lyxdir_ << '\'' << endl;
379 return explicit_userdir;
382 } // namespace support