minor fixes for clang++
[senf.git] / site_scons / SENFSCons.py
1 import os.path, glob, yaptu
2 import SCons.Options, SCons.Environment, SCons.Script.SConscript, SCons.Node.FS
3 import SCons.Defaults, SCons.Action
4 from SCons.Script import *
5
6 def Glob(env, exclude=[], subdirs=[]):
7     testSources = env.Glob("*.test.cc",strings=True)
8     sources = [ x
9                 for x in env.Glob("*.cc",strings=True) \
10                     + env.Glob("*.c",strings=True)
11                 if x not in testSources and x not in exclude ]
12     for subdir in subdirs:
13         testSources += env.Glob(os.path.join(subdir,"*.test.cc"),strings=True)
14         sources += [ x
15                      for x in env.Glob(os.path.join(subdir,"*.cc"),strings=True) \
16                          + env.Glob(os.path.join(subdir,"*.c"),strings=True)
17                      if x not in testSources and x not in exclude ]
18     includes = []
19     for d in [ '' ] + [ x+'/' for x in subdirs ]:
20         for p in env.Glob("%s*" % d, strings=True) + env.Glob("%s*" % d, strings=True, ondisk=False):
21             ext = '.' + p.split('.',1)[-1]
22             if ext in env['CPP_INCLUDE_EXTENSIONS'] \
23                and ext not in env['CPP_EXCLUDE_EXTENSIONS'] \
24                and p not in exclude:
25                 includes.append(p)
26     includes = list(set(includes))
27     sources.sort()
28     testSources.sort()
29     includes.sort()
30     return ( sources, testSources, includes )
31
32 def Doxygen(env, doxyfile = "Doxyfile", extra_sources = [], output_directory = "doc"):
33     # There is one small problem we need to solve with this builder: The Doxygen builder reads
34     # the Doxyfile and thus depends on the environment variables set by site_scons/lib/doxygen.sh.
35     # We thus have to provide all necessary definitions here manually via DOXYENV !
36
37     if type(doxyfile) is type(""):
38         doxyfile = env.File(doxyfile)
39
40     # Module name is derived from the doxyfile path
41     # Utils/Console/Doxyfile -> Utils_Console
42     module = doxyfile.dir.get_path(env.Dir('#')).replace('/','_')
43     if module == '.' : module = "Main"
44
45     # Standard doc build vars and opts
46     def vars(env=env, **kw):
47         denv = { 'TOPDIR'          : env.Dir('#').abspath,
48                  'LIBDIR'          : env.Dir('#/site_scons/lib').abspath,
49                  'output_dir'      : '$OUTPUT_DIRECTORY',
50                  'html_dir'        : 'html',
51                  'html'            : 'NO',
52                  'DOXYGEN'         : '$DOXYGEN' }
53         denv.update(kw)
54         return { 'DOXYENV'         : denv,
55                  'MODULE'          : module,
56                  'OUTPUT_DIRECTORY': output_directory,
57                  'DOXYGENCOM'      : "site_scons/lib/doxygen.sh $DOXYOPTS $SOURCE",
58                  };
59     opts = [ '--tagfile-name', '"${MODULE}.tag"',
60              '--output-dir', '$OUTPUT_DIRECTORY' ]
61     
62     # Rule to generate tagfile
63     # (need to exclude the 'clean' case, otherwise we'll have duplicate nodes)
64     if not env.GetOption('clean'):
65         tagfile = env.Doxygen(doxyfile, DOXYOPTS = opts + [ '--tagfile' ],
66                               **vars(generate_tagfile='${OUTPUT_DIRECTORY}/${MODULE}.tag'))
67         env.Append(ALL_TAGFILES = [ tagfile[0].abspath ])
68         env.Depends(tagfile, [ env.File('#/site_scons/lib/doxygen.sh'),
69                                env.File('#/site_scons/lib/tag-munge.xsl') ])
70
71         env.Install(env.Dir('$DOCINSTALLDIR').Dir(tagfile[0].dir.get_path(env.Dir('#'))),
72                     tagfile[0])
73
74     # Rule to generate HTML documentation
75     doc = env.Doxygen(doxyfile, DOXYOPTS = opts + [ '--tagfiles', '"$ALL_TAGFILES"', '--html' ],
76                       **vars(html='YES', tagfiles='$ALL_TAGFILES'))
77     env.Depends(doc, [ env.File('#/site_scons/lib/doxygen.sh'),
78                        env.File('#/site_scons/lib/html-munge.xsl') ])
79
80     # Copy the extra_sources (the images) into the documentation directory
81     # (need to exclude the 'clean' case otherwise there are multiple ways to clean the copies)
82     if extra_sources:
83         if env.GetOption('clean'):
84             env.Depends(doc, extra_sources)
85         else:
86             env.Depends(tagfile, env.CopyToDir(doc[0].dir, extra_sources))
87
88     # Install documentation into DOCINSTALLDIR
89     env.InstallDir(env.Dir('$DOCINSTALLDIR').Dir(doc[0].dir.dir.get_path(env.Dir('#'))), doc[0].dir,
90                    FILTER_SUFFIXES=['.html','.css','.png','.php','.idx'])
91
92     # Useful aliases
93     env.Alias('all_docs', doc)
94     env.Clean(env.Alias('all_docs'), doc)
95     env.Clean(env.Alias('all'), doc)
96
97     return doc
98
99 def AllIncludesHH(env, exclude=[]):
100     exclude = exclude + ['all_includes.hh']
101     headers = [ f for f in env.Glob("*.hh", source=True)
102                 if f.name not in exclude and not f.name.endswith('.test.hh') ]
103     headers.sort(key=lambda x:x.name)
104     target = env.File("all_includes.hh")
105     allinch = env.CreateFile(target,
106                              env.Value("".join([ '#include <%s>\n' % f.srcnode().get_path(env.Dir('#'))
107                                                  for f in headers ])))
108     env.Default(allinch)
109     env.Depends(allinch, headers)
110
111 INDEXPAGE="""
112 /** \mainpage ${TITLE}
113
114     ${TEXT}
115
116     \htmlonly
117     <dl>
118
119 {{  for name, title in SUBPAGES:
120       <dt><a href="../../${name}/doc/html/index.html">${name}</a></dt><dd>${title}</a></dd>
121 }}
122
123     </dl>
124     \endhtmlonly
125  */
126 """
127
128 def IndexPage(env, name, title, text=""):
129     SUBPAGES = []
130     for dox in sorted(env.Glob("*/Mainpage.dox",strings=True)):
131         subtitle = ([None] + [ line.split('\\mainpage',1)[-1].strip() for line in file(dox)
132                                if '\\mainpage' in line ])[-1]
133         if subtitle:
134             SUBPAGES.append( (dox.split('/',1)[0], subtitle) )
135     file(name,"w").write(yaptu.process(
136             INDEXPAGE, globals(), { 'TITLE': title, 'TEXT': text, 'SUBPAGES': SUBPAGES }))
137     env.Clean('all',name)
138     env.Clean('all_docs',name)
139
140 ###########################################################################
141 # The following functions serve as simple macros for most SConscript files
142 #
143 # If you need to customize these rules, copy-and-paste the code into the
144 # SConscript file and adjust at will (don't forget to replace the
145 # parameters with their actual value. Parameters are marked with ((name)) )
146
147 def AutoRules(env, exclude=[], subdirs=[], doc_extra_sources = []):
148     import SENFSCons
149
150     sources, tests, includes = SENFSCons.Glob(env, exclude=((exclude)), subdirs=((subdirs)) )
151     subscripts               = sorted(env.Glob("*/SConscript", strings=True))
152     doxyfile                 = env.Glob("Doxyfile")
153
154     if sources               : env.Append(ALLOBJECTS = env.Object(sources))
155     if tests                 : env.BoostUnitTest('test', tests)
156     if includes              : env.InstallSubdir('$INCLUDEINSTALLDIR', includes)
157     if doxyfile              : SENFSCons.Doxygen(env, extra_sources=((doc_extra_sources)) )
158     if subscripts            : SConscript(subscripts)
159
160
161 def AutoPacketBundle(env, name, exclude=[], subdirs=[], doc_extra_sources=[]):
162     import SENFSCons
163
164     sources, tests, includes = SENFSCons.Glob(env, exclude=((exclude)), subdirs=((subdirs)) )
165     subscripts               = sorted(env.Glob("*/SConscript", strings=True))
166     doxyfile                 = env.Glob("Doxyfile")
167
168     objects  = env.Object         (sources)
169     cobject  = env.CombinedObject ('${LOCALLIBDIR}/${NAME}${OBJADDSUFFIX}', objects, NAME=((name)))
170     sobundle = env.SharedLibrary  ('${LOCALLIBDIR}/${NAME}${OBJADDSUFFIX}', sources, NAME=((name)),
171                                    LIBS=[], SHLIBPREFIX='')
172
173     env.Default(cobject)
174     env.Default(sobundle)
175     env.Append(ALLOBJECTS = objects, PACKET_BUNDLES = cobject)
176     env.Install('$OBJINSTALLDIR', cobject)
177     env.Install('$OBJINSTALLDIR', sobundle)
178
179     if tests                 : env.BoostUnitTest('test', tests + cobject)
180     if includes              : env.InstallSubdir('$INCLUDEINSTALLDIR', includes)
181     if doxyfile              : SENFSCons.Doxygen(env, extra_sources=((doc_extra_sources)) )
182     if subscripts            : SConscript(subscripts)
183
184
185 def BuildExample(env, sconstruct):
186     dir     = env.File( ((sconstruct)) ).dir
187     example = env.Command( dir.File('.example.phony'), env.Alias('default'),
188                            [ '$SCONS -C $EXAMPLEDIR' ],
189                            CONCURRENCY_LEVEL=1, EXAMPLEDIR=dir )
190     env.Alias('examples', example)
191
192     if env.GetOption('clean') and ('all' in BUILD_TARGETS or 'examples' in BUILD_TARGETS):
193         env.Clone(CONCURRENCY_LEVEL=1, EXAMPLEDIR=dir).Execute([ '$SCONS -C $EXAMPLEDIR -c' ])