1 # vi:filetype=python:expandtab:tabstop=2:shiftwidth=2
5 # This file is part of LyX, the document processor.
6 # Licence details can be found in the file COPYING.
9 # Full author contact details are available in file CREDITS.
11 # This file defines all the utility functions for the
12 # scons-based build system of lyx
15 import os, sys, re, shutil, glob
16 from SCons.Util import WhereIs
18 config_h = os.path.join('src', 'config.h')
21 def writeToFile(filename, lines, append = False):
22 " utility function: write or append lines to filename "
24 file = open(filename, 'a')
26 file = open(filename, 'w')
31 def printEnvironment(env, keys=[]):
32 ''' used to check profile settings '''
33 dict = env.Dictionary()
39 # try to expand, but this is not always possible
40 print key, '=', env.subst('$'+key)
42 print '<<UNEXPANDED>>:', key, '=', dict[key]
45 def env_subst(target, source, env):
46 ''' subst variables in source by those in env, and output to target
47 source and target are scons File() objects
49 %key% (not key itself) is an indication of substitution
51 assert len(target) == 1
52 assert len(source) == 1
53 target_file = file(str(target[0]), "w")
54 source_file = file(str(source[0]), "r")
56 contents = source_file.read()
57 for k, v in env.items():
59 contents = re.sub('@'+k+'@', env.subst('$'+k).replace('\n',r'\\n\\\n'), contents)
60 contents = re.sub('%'+k+'%', env.subst('$'+k).replace('\n',r'\\n\\\n'), contents)
63 target_file.write(contents + "\n")
65 #st = os.stat(str(source[0]))
66 #os.chmod(str(target[0]), stat.S_IMODE(st[stat.ST_MODE]) | stat.S_IWRITE)
73 def checkPkgConfig(conf, version):
74 ''' Return false if pkg_config does not exist, or is too old '''
75 conf.Message('Checking for pkg-config...')
76 ret = conf.TryAction('pkg-config --atleast-pkgconfig-version=%s' % version)[0]
81 def checkPackage(conf, pkg):
82 ''' check if pkg is under the control of conf '''
83 conf.Message('Checking for package %s...' % pkg)
84 ret = conf.TryAction("pkg-config --print-errors --exists %s" % pkg)[0]
89 def startConfigH(top_src_dir):
90 ''' Write the first part of config.h '''
92 config_content = '''/* src/config.h. Generated by scon. */
97 * This file is part of LyX, the document processor.
98 * Licence details can be found in the file COPYING.
100 * This is the compilation configuration file for LyX.
101 * It was generated by scon.
102 * You might want to change some of the defaults if something goes wrong
103 * during the compilation.
111 def addToConfig(lines, top_src_dir):
112 ''' utility function: shortcut for appending lines to outfile
113 add newline at the end of lines.
115 global config_content
116 if lines.strip() != '':
117 config_content += lines + '\n\n'
120 def endConfigH(top_src_dir):
121 ''' Write the last part of config.h '''
122 global config_content
123 writeToFile(os.path.join(top_src_dir, config_h), config_content +
124 '''/************************************************************
125 ** You should not need to change anything beyond this point */
127 #ifndef HAVE_STRERROR
128 #if defined(__cplusplus)
131 char * strerror(int n);
135 #ifndef HAVE_DECL_MKSTEMP
136 #if defined(__cplusplus)
143 #if defined(HAVE_OSTREAM) && defined(HAVE_LOCALE) && defined(HAVE_SSTREAM)
144 # define USE_BOOST_FORMAT 1
146 # define USE_BOOST_FORMAT 0
149 #define BOOST_USER_CONFIG <config.h>
151 #if !defined(ENABLE_ASSERTIONS)
152 # define BOOST_DISABLE_ASSERTS 1
154 #define BOOST_ENABLE_ASSERT_HANDLER 1
156 #define BOOST_DISABLE_THREADS 1
157 #define BOOST_NO_WREGEX 1
158 #define BOOST_NO_WSTRING 1
161 # define BOOST_POSIX 1
164 #if defined(HAVE_NEWAPIS_H)
165 # define WANT_GETFILEATTRIBUTESEX_WRAPPER 1
173 def checkMkdirOneArg(conf):
174 check_mkdir_one_arg_source = """
175 #include <sys/stat.h>
181 conf.Message('Checking for the number of args for mkdir... ')
182 ret = conf.TryLink(check_mkdir_one_arg_source, '.c') or \
183 conf.TryLink('#include <unistd.h>' + check_mkdir_one_arg_source, '.c')
194 def checkSelectArgType(conf):
195 ''' Adapted from autoconf '''
196 conf.Message('Checking for arg types for select... ')
197 for arg234 in ['fd_set *', 'int *', 'void *']:
198 for arg1 in ['int', 'size_t', 'unsigned long', 'unsigned']:
199 for arg5 in ['struct timeval *', 'const struct timeval *']:
200 check_select_source = '''
201 #if HAVE_SYS_SELECT_H
202 # include <sys/select.h>
204 #if HAVE_SYS_SOCKET_H
205 # include <sys/socket.h>
207 extern int select (%s, %s, %s, %s, %s);
212 ''' % (arg1, arg234, arg234, arg234, arg5)
213 ret = conf.TryLink(check_select_source, '.c')
216 return (arg1, arg234, arg5)
217 conf.Result('no (use default)')
218 return ('int', 'int *', 'struct timeval *')
221 def checkBoostLibraries(conf, lib, pathes):
222 ''' look for boost libraries '''
223 conf.Message('Checking for boost library %s... ' % lib)
225 # direct form: e.g. libboost_iostreams.a
226 if os.path.isfile(os.path.join(path, 'lib%s.a' % lib)):
229 # check things like libboost_iostreams-gcc.a
230 files = glob.glob(os.path.join(path, 'lib%s-*.a' % lib))
231 # if there are more than one, choose the first one
232 # FIXME: choose the best one.
234 # get xxx-gcc from /usr/local/lib/libboost_xxx-gcc.a
236 return (path, files[0].split(os.sep)[-1][3:-2])
241 def checkCommand(conf, cmd):
242 ''' check the existence of a command
243 return full path to the command, or none
245 conf.Message('Checking for command %s...' % cmd)
247 conf.Result(res is not None)
251 def installCygwinLDScript(path):
252 ''' Install i386pe.x-no-rdata '''
253 ld_script = os.path.join(path, 'i386pe.x-no-rdata')
254 script = open(ld_script, 'w')
255 script.write('''/* specific linker script avoiding .rdata sections, for normal executables
257 http://www.cygwin.com/ml/cygwin/2004-09/msg01101.html
258 http://www.cygwin.com/ml/cygwin-apps/2004-09/msg00309.html
260 OUTPUT_FORMAT(pei-i386)
261 SEARCH_DIR("/usr/i686-pc-cygwin/lib"); SEARCH_DIR("/usr/lib"); SEARCH_DIR("/usr/lib/w32api");
262 ENTRY(_mainCRTStartup)
265 .text __image_base__ + __section_alignment__ :
272 ___CTOR_LIST__ = .; __CTOR_LIST__ = . ;
273 LONG (-1);*(.ctors); *(.ctor); *(SORT(.ctors.*)); LONG (0);
274 ___DTOR_LIST__ = .; __DTOR_LIST__ = . ;
275 LONG (-1); *(.dtors); *(.dtor); *(SORT(.dtors.*)); LONG (0);
277 /* ??? Why is .gcc_exc here? */
282 /* The Cygwin32 library uses a section to avoid copying certain data
283 on fork. This used to be named ".data". The linker used
284 to include this between __data_start__ and __data_end__, but that
285 breaks building the cygwin32 dll. Instead, we name the section
286 ".data_cygwin_nocopy" and explictly include it after __data_end__. */
287 .data BLOCK(__section_alignment__) :
296 ___RUNTIME_PSEUDO_RELOC_LIST__ = .;
297 __RUNTIME_PSEUDO_RELOC_LIST__ = .;
298 *(.rdata_runtime_pseudo_reloc)
299 ___RUNTIME_PSEUDO_RELOC_LIST_END__ = .;
300 __RUNTIME_PSEUDO_RELOC_LIST_END__ = .;
302 *(.data_cygwin_nocopy)
304 .rdata BLOCK(__section_alignment__) :
307 .pdata BLOCK(__section_alignment__) :
311 .bss BLOCK(__section_alignment__) :
318 .edata BLOCK(__section_alignment__) :
329 .idata BLOCK(__section_alignment__) :
331 /* This cannot currently be handled with grouped sections.
332 See pe.em:sort_sections. */
335 /* These zeroes mark the end of the import list. */
336 LONG (0); LONG (0); LONG (0); LONG (0); LONG (0);
342 .CRT BLOCK(__section_alignment__) :
344 ___crt_xc_start__ = . ;
345 *(SORT(.CRT$XC*)) /* C initialization */
346 ___crt_xc_end__ = . ;
347 ___crt_xi_start__ = . ;
348 *(SORT(.CRT$XI*)) /* C++ initialization */
349 ___crt_xi_end__ = . ;
350 ___crt_xl_start__ = . ;
351 *(SORT(.CRT$XL*)) /* TLS callbacks */
352 /* ___crt_xl_end__ is defined in the TLS Directory support code */
353 ___crt_xp_start__ = . ;
354 *(SORT(.CRT$XP*)) /* Pre-termination */
355 ___crt_xp_end__ = . ;
356 ___crt_xt_start__ = . ;
357 *(SORT(.CRT$XT*)) /* Termination */
358 ___crt_xt_end__ = . ;
360 .tls BLOCK(__section_alignment__) :
368 .endjunk BLOCK(__section_alignment__) :
370 /* end is deprecated, don't use it */
375 .rsrc BLOCK(__section_alignment__) :
380 .reloc BLOCK(__section_alignment__) :
384 .stab BLOCK(__section_alignment__) (NOLOAD) :
388 .stabstr BLOCK(__section_alignment__) (NOLOAD) :
392 /* DWARF debug sections.
393 Symbols in the DWARF debugging sections are relative to the beginning
394 of the section. Unlike other targets that fake this by putting the
395 section VMA at 0, the PE format will not allow it. */
396 /* DWARF 1.1 and DWARF 2. */
397 .debug_aranges BLOCK(__section_alignment__) (NOLOAD) :
401 .debug_pubnames BLOCK(__section_alignment__) (NOLOAD) :
406 .debug_info BLOCK(__section_alignment__) (NOLOAD) :
408 *(.debug_info) *(.gnu.linkonce.wi.*)
410 .debug_abbrev BLOCK(__section_alignment__) (NOLOAD) :
414 .debug_line BLOCK(__section_alignment__) (NOLOAD) :
418 .debug_frame BLOCK(__section_alignment__) (NOLOAD) :
422 .debug_str BLOCK(__section_alignment__) (NOLOAD) :
426 .debug_loc BLOCK(__section_alignment__) (NOLOAD) :
430 .debug_macinfo BLOCK(__section_alignment__) (NOLOAD) :
434 /* SGI/MIPS DWARF 2 extensions. */
435 .debug_weaknames BLOCK(__section_alignment__) (NOLOAD) :
439 .debug_funcnames BLOCK(__section_alignment__) (NOLOAD) :
443 .debug_typenames BLOCK(__section_alignment__) (NOLOAD) :
447 .debug_varnames BLOCK(__section_alignment__) (NOLOAD) :
452 .debug_ranges BLOCK(__section_alignment__) (NOLOAD) :
463 # these will be used under win32
469 # does not matter if it fails on other systems
474 def __init__(self, env, logfile, longarg, info):
475 # save the spawn system
477 self.logfile = logfile
478 # clear the logfile (it may not exist)
480 # this will overwrite existing content.
481 writeToFile(logfile, info, append=False)
483 self.longarg = longarg
484 # get hold of the old spawn? (necessary?)
485 self._spawn = env['SPAWN']
488 def spawn(self, sh, escape, cmd, args, spawnenv):
490 newargs = ' '.join(map(escape, args[1:]))
491 cmdline = cmd + " " + newargs
493 # if log is not empty, write to it
494 if self.logfile != '':
495 # this tend to be slow (?) but ensure correct output
496 # Note that cmdline may be long so I do not escape it
498 # since this is not an essential operation, proceed if things go wrong here.
499 writeToFile(self.logfile, cmd + " " + ' '.join(args[1:]) + '\n', append=True)
501 print "Warning: can not write to log file ", self.logfile
503 # if the command is not too long, use the old
504 if not self.longarg or len(cmdline) < 8000:
505 exit_code = self._spawn(sh, escape, cmd, args, spawnenv)
507 sAttrs = win32security.SECURITY_ATTRIBUTES()
508 StartupInfo = win32process.STARTUPINFO()
510 spawnenv[var] = spawnenv[var].encode('ascii', 'replace')
511 # check for any special operating system commands
514 win32file.DeleteFile(arg)
517 # otherwise execute the command.
518 hProcess, hThread, dwPid, dwTid = win32process.CreateProcess(None, cmdline, None, None, 1, 0, spawnenv, None, StartupInfo)
519 win32event.WaitForSingleObject(hProcess, win32event.INFINITE)
520 exit_code = win32process.GetExitCodeProcess(hProcess)
521 win32file.CloseHandle(hProcess);
522 win32file.CloseHandle(hThread);
526 def setLoggedSpawn(env, logfile = '', longarg=False, info=''):
527 ''' This function modify env and allow logging of
528 commands to a logfile. If the argument is too long
529 a win32 spawn will be used instead of the system one
532 # create a new spwn object
533 ls = loggedSpawn(env, logfile, longarg, info)
534 # replace the old SPAWN by the new function
535 env['SPAWN'] = ls.spawn
538 ## def DistSources(env, node):
539 ## env.DistFiles(_get_sources(env, node))
541 ## def DistFiles(env, files):
542 ## assert isinstance(files, (list, tuple))
543 ## DISTFILES = [env.File(fname) for fname in files]
544 ## env.AppendUnique(DISTFILES=DISTFILES)
547 ## def make_distdir(target=None, source=None, env=None):
548 ## distdir = env.subst('$DISTDIR')
549 ## Execute(Delete(distdir))
550 ## Execute(Mkdir(distdir))
551 ## for fnode in env["DISTFILES"]:
552 ## dirname, fname = os.path.split(str(fnode))
554 ## distdirname = os.path.join(distdir, dirname)
555 ## if not os.path.exists(distdirname):
556 ## Execute(Mkdir(distdirname))
557 ## Execute(Copy(os.path.join(distdir, dirname, fname), str(fnode)))
559 ## def make_dist(target=None, source=None, env=None):
560 ## return Popen([env['TAR'], "-zcf",
561 ## env.subst("${PACKAGE}-${VERSION}.tar.gz"),
562 ## env.subst('$DISTDIR')]).wait()
564 ## def make_distcheck(target=None, source=None, env=None):
565 ## distdir = env.subst('$DISTDIR')
566 ## distcheckinstdir = tempfile.mkdtemp('', env.subst('${PACKAGE}-${VERSION}-instdir-'))
567 ## distcheckdestdir = tempfile.mkdtemp('', env.subst('${PACKAGE}-${VERSION}-destdir-'))
568 ## instdirs = [os.path.join(distcheckinstdir, d) for d in
569 ## 'lib', 'share', 'bin', 'include']
570 ## for dir_ in instdirs:
571 ## Execute(Mkdir(dir_))
573 ## cmd = env.subst("cd $DISTDIR && scons DESTDIR=%s prefix=%s"
574 ## " && scons check && scons install") %\
575 ## (os.path.join(distcheckdestdir, ''), distcheckinstdir)
576 ## status = Popen(cmd, shell=True).wait()
579 ## ## Check that inst dirs are empty (to catch cases of $DESTDIR not being honored
580 ## for dir_ in instdirs:
581 ## if os.listdir(dir_):
582 ## raise SCons.Errors.BuildError(target, "%s not empy" % dir_)
583 ## ## Check that something inside $DESTDIR was installed
584 ## dir_ = os.path.join(distcheckdestdir, distcheckinstdir)
585 ## if not os.path.exists(dir_):
586 ## raise SCons.Errors.BuildError(target, "%s does not exist" % dir_)
587 ## Execute(Delete(distcheckinstdir))
588 ## Execute(Delete(distcheckdestdir))
589 ## Execute(Delete(distdir))
591 ## def InstallWithDestDir(self, dir_, source):
592 ## dir_ = '${DESTDIR}' + str(dir_)
593 ## return SConsEnvironment.Install(self, dir_, source)
596 ## def InstallAsWithDestDir(self, target, source):
597 ## target = '${DESTDIR}' + str(target)
598 ## return SConsEnvironment.InstallAs(self, target, source)
600 ## def generate(env):
601 ## env.EnsureSConsVersion(0, 96, 91)
603 ## opts = Options(['options.cache'], ARGUMENTS)
604 ## opts.Add(PathOption('prefix', 'Installation prefix', '/usr/local'))
605 ## opts.Add(PathOption('exec_prefix', 'Installation prefix blah blah',
607 ## opts.Add(PathOption('libdir',
608 ## 'Installation prefix for architecture dependent files', '$prefix/lib'))
609 ## opts.Add(PathOption('includedir',
610 ## 'Installation prefix for C header files', '$prefix/include'))
611 ## opts.Add(PathOption('datadir',
612 ## 'Installation prefix for architecture independent files', '$prefix/share'))
613 ## opts.Add(PathOption('bindir', 'Installation prefix for programs', '$prefix/bin'))
614 ## opts.Add(PathOption('DESTDIR', 'blah blah', None))
616 ## opts.Save('options.cache', env)
617 ## SConsEnvironment.Help(env, opts.GenerateHelpText(env))
619 ## env.Append(CPPFLAGS=r' -DVERSION=\"$VERSION\"')
620 ## env.Append(CCFLAGS=ARGUMENTS.get('CCFLAGS', '-g -O2'))
622 ## env['GNOME_TESTS'] = dict(CheckPython=CheckPython,
623 ## CheckPythonHeaders=CheckPythonHeaders,
624 ## PkgCheckModules=PkgCheckModules)
626 ## SConsEnvironment.DistSources = DistSources
627 ## SConsEnvironment.DistFiles = DistFiles
628 ## env['DISTDIR'] = "${PACKAGE}-${VERSION}"
630 ## #env.Command(env.Dir("$DISTDIR"), None, make_distdir)
632 ## distdir_alias = env.Alias("distdir", None, make_distdir)
633 ## dist_alias = env.Alias("dist", None, make_dist)
634 ## env.Depends(dist_alias, distdir_alias)
635 ## distcheck_alias = env.Alias("distcheck", None, make_distcheck)
636 ## env.Depends(distcheck_alias, distdir_alias)
637 ## env.AlwaysBuild(env.Alias('check'))
639 ## #env['TARFLAGS'] ='-c -z'
640 ## #env['TARSUFFIX'] = '.tar.gz'
641 ## #tar = env.Tar('${PACKAGE}-${VERSION}.tar.gz', "${DISTDIR}")
642 ## #env.Depends(tar, distdir_alias)
643 ## #print env['DEFAULT_TARGETS']
645 ## #env.Depends(distdir_alias, "${DISTFILES}")
646 ## #env.Alias('dist', tar)
647 ## env.AlwaysBuild('dist')
648 ## env.AlwaysBuild('distdir')
649 ## env.AlwaysBuild('distcheck')
650 ## env.DistFiles(['SConstruct', 'scons/gnome.py'])
652 ## env['BUILDERS']['EnvSubstFile'] = SCons.Builder.Builder(action=env_subst)
654 ## SConsEnvironment.PythonByteCompile = env.Action(byte_compile_python)
656 ## env.Install = new.instancemethod(InstallWithDestDir, env, env.__class__)
657 ## env.InstallAs = new.instancemethod(InstallAsWithDestDir, env, env.__class__)