The Meson Build System
http://mesonbuild.com/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
385 lines
16 KiB
385 lines
16 KiB
12 years ago
|
#!/usr/bin/python3 -tt
|
||
|
|
||
|
# Copyright 2012 Jussi Pakkanen
|
||
|
|
||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
# you may not use this file except in compliance with the License.
|
||
|
# You may obtain a copy of the License at
|
||
|
|
||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||
|
|
||
|
# Unless required by applicable law or agreed to in writing, software
|
||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
# See the License for the specific language governing permissions and
|
||
|
# limitations under the License.
|
||
|
|
||
12 years ago
|
import os, stat, re
|
||
|
import interpreter, nodes
|
||
12 years ago
|
|
||
12 years ago
|
def shell_quote(cmdlist):
|
||
|
return ["'" + x + "'" for x in cmdlist]
|
||
|
|
||
12 years ago
|
def do_conf_file(src, dst, variables):
|
||
|
data = open(src).readlines()
|
||
|
regex = re.compile('@(.*?)@')
|
||
|
result = []
|
||
|
for line in data:
|
||
|
match = re.search(regex, line)
|
||
|
while match:
|
||
|
varname = match.group(1)
|
||
|
if varname in variables:
|
||
|
var = variables[varname]
|
||
|
if isinstance(var, str):
|
||
|
pass
|
||
|
elif isinstance(var, nodes.StringStatement):
|
||
12 years ago
|
var = var.get_value()
|
||
12 years ago
|
else:
|
||
|
raise RuntimeError('Tried to replace a variable with something other than a string.')
|
||
|
else:
|
||
|
var = ''
|
||
|
line = line.replace('@' + varname + '@', var)
|
||
|
match = re.search(regex, line)
|
||
|
result.append(line)
|
||
|
open(dst, 'w').writelines(result)
|
||
|
|
||
12 years ago
|
class ShellGenerator():
|
||
|
|
||
12 years ago
|
def __init__(self, build, interp):
|
||
12 years ago
|
self.build = build
|
||
|
self.environment = build.environment
|
||
12 years ago
|
self.interpreter = interp
|
||
12 years ago
|
self.build_filename = 'compile.sh'
|
||
12 years ago
|
self.test_filename = 'run_tests.sh'
|
||
12 years ago
|
self.install_filename = 'install.sh'
|
||
12 years ago
|
self.processed_targets = {}
|
||
|
|
||
12 years ago
|
def generate(self):
|
||
12 years ago
|
self.generate_compile_script()
|
||
|
self.generate_test_script()
|
||
12 years ago
|
self.generate_install_script()
|
||
12 years ago
|
|
||
12 years ago
|
def create_shfile(self, outfilename, message):
|
||
12 years ago
|
outfile = open(outfilename, 'w')
|
||
12 years ago
|
outfile.write('#!/bin/sh\n\n')
|
||
12 years ago
|
outfile.write(message)
|
||
12 years ago
|
cdcmd = ['cd', self.environment.get_build_dir()]
|
||
|
outfile.write(' '.join(shell_quote(cdcmd)) + '\n')
|
||
12 years ago
|
os.chmod(outfilename, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC |\
|
||
|
stat.S_IRGRP | stat.S_IXGRP | stat.S_IROTH | stat.S_IXOTH)
|
||
12 years ago
|
return outfile
|
||
|
|
||
|
def generate_compile_script(self):
|
||
|
outfilename = os.path.join(self.environment.get_build_dir(), self.build_filename)
|
||
|
message = """echo This is an autogenerated shell script build file for project \\"%s\\"
|
||
|
echo This is experimental and most likely will not work!
|
||
|
""" % self.build.get_project()
|
||
|
outfile = self.create_shfile(outfilename, message)
|
||
|
self.generate_commands(outfile)
|
||
|
outfile.close()
|
||
12 years ago
|
|
||
12 years ago
|
def generate_test_script(self):
|
||
|
outfilename = os.path.join(self.environment.get_build_dir(), self.test_filename)
|
||
12 years ago
|
message = """echo This is an autogenerated test script for project \\"%s\\"
|
||
|
echo This is experimental and most likely will not work!
|
||
|
echo Run compile.sh before this or bad things will happen.
|
||
|
""" % self.build.get_project()
|
||
|
outfile = self.create_shfile(outfilename, message)
|
||
12 years ago
|
self.generate_tests(outfile)
|
||
|
outfile.close()
|
||
12 years ago
|
|
||
|
def generate_install_script(self):
|
||
|
outfilename = os.path.join(self.environment.get_build_dir(), self.install_filename)
|
||
|
message = """echo This is an autogenerated install script for project \\"%s\\"
|
||
|
echo This is experimental and most likely will not work!
|
||
|
echo Run compile.sh before this or bad things will happen.
|
||
|
""" % self.build.get_project()
|
||
|
outfile = self.create_shfile(outfilename, message)
|
||
12 years ago
|
self.generate_configure_files()
|
||
12 years ago
|
self.generate_target_install(outfile)
|
||
|
self.generate_header_install(outfile)
|
||
12 years ago
|
self.generate_man_install(outfile)
|
||
12 years ago
|
self.generate_data_install(outfile)
|
||
12 years ago
|
outfile.close()
|
||
12 years ago
|
|
||
|
def make_subdir(self, outfile, dir):
|
||
|
cmdlist = ['mkdir', '-p', dir]
|
||
12 years ago
|
outfile.write(' '.join(shell_quote(cmdlist)) + ' || exit\n')
|
||
12 years ago
|
|
||
|
def copy_file(self, outfile, filename, outdir):
|
||
|
cpcommand = ['cp', filename, outdir]
|
||
12 years ago
|
cpcommand = ' '.join(shell_quote(cpcommand)) + ' || exit\n'
|
||
12 years ago
|
outfile.write(cpcommand)
|
||
12 years ago
|
|
||
|
def generate_configure_files(self):
|
||
|
for cf in self.build.get_configure_files():
|
||
|
infile = os.path.join(self.environment.get_source_dir(),
|
||
|
cf.get_subdir(),
|
||
|
cf.get_source_name())
|
||
12 years ago
|
outdir = os.path.join(self.environment.get_build_dir(),
|
||
|
cf.get_subdir())
|
||
|
os.makedirs(outdir, exist_ok=True)
|
||
|
outfile = os.path.join(outdir, cf.get_target_name())
|
||
12 years ago
|
do_conf_file(infile, outfile, self.interpreter.get_variables())
|
||
12 years ago
|
|
||
|
def generate_data_install(self, outfile):
|
||
|
prefix = self.environment.get_prefix()
|
||
|
dataroot = os.path.join(prefix, self.environment.get_datadir())
|
||
|
data = self.build.get_data()
|
||
|
if len(data) != 0:
|
||
|
outfile.write('\necho Installing data files.\n')
|
||
|
else:
|
||
|
outfile.write('\necho This project has no data files to install.\n')
|
||
|
for d in data:
|
||
12 years ago
|
subdir = os.path.join(dataroot, d.get_subdir())
|
||
12 years ago
|
absdir = os.path.join(self.environment.get_prefix(), subdir)
|
||
|
for f in d.get_sources():
|
||
|
self.make_subdir(outfile, absdir)
|
||
|
srcabs = os.path.join(self.environment.get_source_dir(), f)
|
||
|
dstabs = os.path.join(absdir, f)
|
||
|
self.copy_file(outfile, srcabs, dstabs)
|
||
|
|
||
12 years ago
|
def generate_man_install(self, outfile):
|
||
|
prefix = self.environment.get_prefix()
|
||
|
manroot = os.path.join(prefix, self.environment.get_mandir())
|
||
|
man = self.build.get_man()
|
||
|
if len(man) != 0:
|
||
|
outfile.write('\necho Installing man pages.\n')
|
||
|
else:
|
||
|
outfile.write('\necho This project has no man pages to install.\n')
|
||
|
for m in man:
|
||
|
for f in m.get_sources():
|
||
|
num = f.split('.')[-1]
|
||
|
subdir = 'man' + num
|
||
|
absdir = os.path.join(manroot, subdir)
|
||
|
self.make_subdir(outfile, absdir)
|
||
|
srcabs = os.path.join(self.environment.get_source_dir(), f)
|
||
|
dstabs = os.path.join(manroot,
|
||
|
os.path.join(subdir, f + '.gz'))
|
||
|
cmd = "gzip < '%s' > '%s' || exit\n" % (srcabs, dstabs)
|
||
|
outfile.write(cmd)
|
||
12 years ago
|
|
||
|
def generate_header_install(self, outfile):
|
||
|
prefix = self.environment.get_prefix()
|
||
|
incroot = os.path.join(prefix, self.environment.get_includedir())
|
||
|
headers = self.build.get_headers()
|
||
|
if len(headers) != 0:
|
||
|
outfile.write('\necho Installing headers.\n')
|
||
|
else:
|
||
12 years ago
|
outfile.write('\necho This project has no headers to install.\n')
|
||
12 years ago
|
for h in headers:
|
||
|
outdir = os.path.join(incroot, h.get_subdir())
|
||
|
self.make_subdir(outfile, outdir)
|
||
|
for f in h.get_sources():
|
||
12 years ago
|
abspath = os.path.join(self.environment.get_source_dir(), f) # FIXME
|
||
12 years ago
|
self.copy_file(outfile, abspath, outdir)
|
||
12 years ago
|
|
||
12 years ago
|
def generate_target_install(self, outfile):
|
||
12 years ago
|
prefix = self.environment.get_prefix()
|
||
|
libdir = os.path.join(prefix, self.environment.get_libdir())
|
||
|
bindir = os.path.join(prefix, self.environment.get_bindir())
|
||
12 years ago
|
self.make_subdir(outfile, libdir)
|
||
|
self.make_subdir(outfile, bindir)
|
||
12 years ago
|
if len(self.build.get_targets()) != 0:
|
||
|
outfile.write('\necho Installing targets.\n')
|
||
|
else:
|
||
|
outfile.write('\necho This project has no targets to install.\n')
|
||
12 years ago
|
for tmp in self.build.get_targets().items():
|
||
|
(name, t) = tmp
|
||
|
if t.should_install():
|
||
|
if isinstance(t, interpreter.Executable):
|
||
|
outdir = bindir
|
||
|
else:
|
||
|
outdir = libdir
|
||
|
outfile.write('echo Installing "%s".\n' % name)
|
||
12 years ago
|
self.copy_file(outfile, self.get_target_filename(t), outdir)
|
||
12 years ago
|
self.generate_shlib_aliases(t, outdir, outfile)
|
||
12 years ago
|
self.fix_deps(outfile, t, outdir)
|
||
|
|
||
|
def fix_deps(self, outfile, target, outdir):
|
||
|
if isinstance(target, interpreter.StaticLibrary):
|
||
|
return
|
||
|
depfixer = self.environment.get_depfixer()
|
||
|
fname = os.path.join(outdir, target.get_filename())
|
||
|
cmds = [depfixer, fname, self.environment.get_build_dir()]
|
||
|
outfile.write(' '.join(shell_quote(cmds)) + ' || exit\n')
|
||
12 years ago
|
|
||
|
def generate_tests(self, outfile):
|
||
12 years ago
|
for t in self.build.get_tests():
|
||
12 years ago
|
cmds = []
|
||
|
cmds.append(self.get_target_filename(t.get_exe()))
|
||
|
outfile.write('echo Running test \\"%s\\".\n' % t.get_name())
|
||
|
outfile.write(' '.join(shell_quote(cmds)) + ' || exit\n')
|
||
|
|
||
12 years ago
|
def get_compiler_for_source(self, src):
|
||
12 years ago
|
for i in self.build.compilers:
|
||
12 years ago
|
if i.can_compile(src):
|
||
12 years ago
|
return i
|
||
|
raise RuntimeError('No specified compiler can handle file ' + src)
|
||
|
|
||
|
def generate_basic_compiler_arguments(self, target, compiler):
|
||
12 years ago
|
commands = []
|
||
|
commands += compiler.get_exelist()
|
||
12 years ago
|
commands += self.build.get_global_flags(compiler)
|
||
12 years ago
|
commands += target.get_extra_args(compiler.get_language())
|
||
12 years ago
|
commands += compiler.get_debug_flags()
|
||
12 years ago
|
commands += compiler.get_std_warn_flags()
|
||
|
commands += compiler.get_compile_only_flags()
|
||
12 years ago
|
if isinstance(target, interpreter.SharedLibrary):
|
||
|
commands += compiler.get_pic_flags()
|
||
12 years ago
|
for dep in target.get_external_deps():
|
||
|
commands += dep.get_compile_flags()
|
||
12 years ago
|
return commands
|
||
|
|
||
|
def get_pch_include_args(self, compiler, target):
|
||
|
args = []
|
||
12 years ago
|
pchpath = self.get_target_private_dir(target)
|
||
12 years ago
|
includearg = compiler.get_include_arg(pchpath)
|
||
|
for p in target.get_pch():
|
||
|
if compiler.can_compile(p):
|
||
|
args.append('-include')
|
||
|
args.append(os.path.split(p)[-1])
|
||
|
if len(args) > 0:
|
||
|
args = [includearg] + args
|
||
|
return args
|
||
|
|
||
|
def generate_single_compile(self, target, outfile, src):
|
||
|
compiler = self.get_compiler_for_source(src)
|
||
|
commands = self.generate_basic_compiler_arguments(target, compiler)
|
||
|
abs_src = os.path.join(self.environment.get_source_dir(), target.get_source_subdir(), src)
|
||
12 years ago
|
abs_obj = os.path.join(self.get_target_private_dir(target), src)
|
||
12 years ago
|
abs_obj += '.' + self.environment.get_object_suffix()
|
||
12 years ago
|
for i in target.get_include_dirs():
|
||
|
basedir = i.get_curdir()
|
||
|
for d in i.get_incdirs():
|
||
12 years ago
|
expdir = os.path.join(basedir, d)
|
||
|
fulldir = os.path.join(self.environment.get_source_dir(), expdir)
|
||
|
barg = compiler.get_include_arg(expdir)
|
||
|
sarg = compiler.get_include_arg(fulldir)
|
||
|
commands.append(barg)
|
||
|
commands.append(sarg)
|
||
12 years ago
|
commands += self.get_pch_include_args(compiler, target)
|
||
12 years ago
|
commands.append(abs_src)
|
||
|
commands += compiler.get_output_flags()
|
||
|
commands.append(abs_obj)
|
||
12 years ago
|
quoted = shell_quote(commands)
|
||
12 years ago
|
outfile.write('\necho Compiling \\"%s\\"\n' % src)
|
||
12 years ago
|
outfile.write(' '.join(quoted) + ' || exit\n')
|
||
12 years ago
|
return abs_obj
|
||
12 years ago
|
|
||
12 years ago
|
def build_target_link_arguments(self, deps):
|
||
|
args = []
|
||
|
for d in deps:
|
||
12 years ago
|
if not isinstance(d, interpreter.StaticLibrary) and\
|
||
|
not isinstance(d, interpreter.SharedLibrary):
|
||
|
raise RuntimeError('Tried to link with a non-library target "%s".' % d.get_basename())
|
||
12 years ago
|
args.append(self.get_target_filename(d))
|
||
|
return args
|
||
12 years ago
|
|
||
12 years ago
|
def generate_link(self, target, outfile, outname, obj_list):
|
||
12 years ago
|
if isinstance(target, interpreter.StaticLibrary):
|
||
12 years ago
|
linker = self.build.static_linker
|
||
12 years ago
|
else:
|
||
12 years ago
|
linker = self.build.compilers[0] # Fixme.
|
||
12 years ago
|
commands = []
|
||
|
commands += linker.get_exelist()
|
||
12 years ago
|
if isinstance(target, interpreter.Executable):
|
||
|
commands += linker.get_std_exe_link_flags()
|
||
|
elif isinstance(target, interpreter.SharedLibrary):
|
||
|
commands += linker.get_std_shared_lib_link_flags()
|
||
12 years ago
|
commands += linker.get_pic_flags()
|
||
12 years ago
|
elif isinstance(target, interpreter.StaticLibrary):
|
||
|
commands += linker.get_std_link_flags()
|
||
|
else:
|
||
|
raise RuntimeError('Unknown build target type.')
|
||
12 years ago
|
for dep in target.get_external_deps():
|
||
|
commands += dep.get_link_flags()
|
||
12 years ago
|
commands += linker.get_output_flags()
|
||
|
commands.append(outname)
|
||
12 years ago
|
commands += obj_list
|
||
12 years ago
|
commands += self.build_target_link_arguments(target.get_dependencies())
|
||
12 years ago
|
quoted = shell_quote(commands)
|
||
12 years ago
|
outfile.write('\necho Linking \\"%s\\".\n' % target.get_basename())
|
||
12 years ago
|
outfile.write(' '.join(quoted) + ' || exit\n')
|
||
12 years ago
|
|
||
12 years ago
|
def get_target_dir(self, target):
|
||
12 years ago
|
dirname = os.path.join(self.environment.get_build_dir(), target.get_subdir())
|
||
|
os.makedirs(dirname, exist_ok=True)
|
||
|
return dirname
|
||
|
|
||
|
def get_target_private_dir(self, target):
|
||
|
dirname = os.path.join(self.get_target_dir(target), target.get_basename() + '.dir')
|
||
12 years ago
|
os.makedirs(dirname, exist_ok=True)
|
||
|
return dirname
|
||
|
|
||
12 years ago
|
def generate_commands(self, outfile):
|
||
12 years ago
|
for i in self.build.get_targets().items():
|
||
12 years ago
|
target = i[1]
|
||
12 years ago
|
self.generate_target(target, outfile)
|
||
|
|
||
|
def process_target_dependencies(self, target, outfile):
|
||
|
for t in target.get_dependencies():
|
||
|
tname = t.get_basename()
|
||
|
if not tname in self.processed_targets:
|
||
|
self.generate_target(t, outfile)
|
||
|
|
||
|
def get_target_filename(self, target):
|
||
|
targetdir = self.get_target_dir(target)
|
||
|
filename = os.path.join(targetdir, target.get_filename())
|
||
|
return filename
|
||
|
|
||
12 years ago
|
def generate_pch(self, target, outfile):
|
||
|
print('Generating pch for "%s"' % target.get_basename())
|
||
|
for pch in target.get_pch():
|
||
|
if '/' not in pch:
|
||
|
raise interpreter.InvalidArguments('Precompiled header of "%s" must not be in the same direcotory as source, please put it in a subdirectory.' % target.get_basename())
|
||
|
compiler = self.get_compiler_for_source(pch)
|
||
|
commands = self.generate_basic_compiler_arguments(target, compiler)
|
||
|
srcabs = os.path.join(self.environment.get_source_dir(), target.get_source_subdir(), pch)
|
||
|
dstabs = os.path.join(self.environment.get_build_dir(),
|
||
12 years ago
|
self.get_target_private_dir(target),
|
||
12 years ago
|
os.path.split(pch)[-1] + '.' + compiler.get_pch_suffix())
|
||
|
commands.append(srcabs)
|
||
|
commands += compiler.get_output_flags()
|
||
|
commands.append(dstabs)
|
||
|
quoted = shell_quote(commands)
|
||
|
outfile.write('\necho Generating pch \\"%s\\".\n' % pch)
|
||
|
outfile.write(' '.join(quoted) + ' || exit\n')
|
||
12 years ago
|
|
||
|
def generate_shlib_aliases(self, target, outdir, outfile):
|
||
|
basename = target.get_filename()
|
||
|
aliases = target.get_aliaslist()
|
||
|
for alias in aliases:
|
||
|
aliasfile = os.path.join(outdir, alias)
|
||
|
cmd = ['ln', '-s', '-f', basename, aliasfile]
|
||
|
outfile.write(' '.join(shell_quote(cmd)) + '|| exit\n')
|
||
12 years ago
|
|
||
12 years ago
|
def generate_target(self, target, outfile):
|
||
|
name = target.get_basename()
|
||
|
if name in self.processed_targets:
|
||
|
return
|
||
|
self.process_target_dependencies(target, outfile)
|
||
|
print('Generating target', name)
|
||
|
outname = self.get_target_filename(target)
|
||
|
obj_list = []
|
||
12 years ago
|
if target.has_pch():
|
||
|
self.generate_pch(target, outfile)
|
||
12 years ago
|
for src in target.get_sources():
|
||
|
obj_list.append(self.generate_single_compile(target, outfile, src))
|
||
|
self.generate_link(target, outfile, outname, obj_list)
|
||
12 years ago
|
self.generate_shlib_aliases(target, self.get_target_dir(target), outfile)
|
||
12 years ago
|
self.processed_targets[name] = True
|
||
12 years ago
|
|
||
12 years ago
|
if __name__ == '__main__':
|
||
|
code = """
|
||
|
project('simple generator')
|
||
|
language('c')
|
||
12 years ago
|
executable('prog', 'prog.c', 'dep.c')
|
||
12 years ago
|
"""
|
||
12 years ago
|
import environment
|
||
12 years ago
|
os.chdir(os.path.split(__file__)[0])
|
||
12 years ago
|
envir = environment.Environment('.', 'work area')
|
||
12 years ago
|
intpr = interpreter.Interpreter(code, envir)
|
||
12 years ago
|
g = ShellGenerator(intpr, envir)
|
||
12 years ago
|
g.generate()
|