meson/shellgenerator.py

211 lines
8.5 KiB
Python
Raw Normal View History

#!/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.
import os, stat
2013-01-06 00:13:38 +08:00
import interpreter
2013-01-02 05:43:25 +08:00
def shell_quote(cmdlist):
return ["'" + x + "'" for x in cmdlist]
class ShellGenerator():
2013-01-12 04:59:49 +08:00
def __init__(self, build):
self.build = build
self.environment = build.environment
self.build_filename = 'compile.sh'
self.test_filename = 'run_tests.sh'
2013-01-12 08:25:06 +08:00
self.install_filename = 'install.sh'
2013-01-06 08:59:54 +08:00
self.processed_targets = {}
def generate(self):
self.generate_compile_script()
self.generate_test_script()
2013-01-12 08:25:06 +08:00
self.generate_install_script()
2013-01-12 08:25:06 +08:00
def create_shfile(self, outfilename, message):
outfile = open(outfilename, 'w')
2012-12-30 04:13:14 +08:00
outfile.write('#!/bin/sh\n\n')
2013-01-12 08:25:06 +08:00
outfile.write(message)
cdcmd = ['cd', self.environment.get_build_dir()]
outfile.write(' '.join(shell_quote(cdcmd)) + '\n')
os.chmod(outfilename, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC |\
stat.S_IRGRP | stat.S_IXGRP | stat.S_IROTH | stat.S_IXOTH)
2013-01-12 08:25:06 +08:00
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()
def generate_test_script(self):
outfilename = os.path.join(self.environment.get_build_dir(), self.test_filename)
2013-01-12 08:25:06 +08:00
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)
self.generate_tests(outfile)
outfile.close()
2013-01-12 08:25:06 +08:00
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)
self.generate_install(outfile)
outfile.close()
def generate_install(self, outfile):
prefix = self.environment.get_prefix()
libdir = os.path.join(prefix, self.environment.get_libdir())
bindir = os.path.join(prefix, self.environment.get_bindir())
outfile.write("mkdir -p '%s'\n" % libdir)
outfile.write("mkdir -p '%s'\n" % bindir)
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)
cpcommand = ['cp', self.get_target_filename(t), outdir]
cpcommand = ' '.join(shell_quote(cpcommand)) + '\n'
outfile.write(cpcommand)
def generate_tests(self, outfile):
2013-01-12 04:59:49 +08:00
for t in self.build.get_tests():
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')
2012-12-30 06:55:35 +08:00
def generate_single_compile(self, target, outfile, src):
2012-12-30 01:51:32 +08:00
compiler = None
2013-01-12 04:59:49 +08:00
for i in self.build.compilers:
2012-12-30 01:51:32 +08:00
if i.can_compile(src):
compiler = i
break
if compiler is None:
raise RuntimeError('No specified compiler can handle file ' + src)
abs_src = os.path.join(self.environment.get_source_dir(), src)
2013-01-02 06:00:24 +08:00
abs_obj = os.path.join(self.get_target_dir(target), src)
2012-12-30 01:51:32 +08:00
abs_obj += '.' + self.environment.get_object_suffix()
2012-12-30 04:04:24 +08:00
commands = []
commands += compiler.get_exelist()
2012-12-30 02:02:37 +08:00
commands += compiler.get_debug_flags()
2012-12-30 01:51:32 +08:00
commands += compiler.get_std_warn_flags()
commands += compiler.get_compile_only_flags()
2013-01-06 03:08:08 +08:00
if isinstance(target, interpreter.SharedLibrary):
commands += compiler.get_pic_flags()
2012-12-30 06:55:35 +08:00
for dep in target.get_external_deps():
commands += dep.get_compile_flags()
2012-12-30 01:51:32 +08:00
commands.append(abs_src)
commands += compiler.get_output_flags()
commands.append(abs_obj)
2013-01-02 05:43:25 +08:00
quoted = shell_quote(commands)
2012-12-30 04:13:14 +08:00
outfile.write('\necho Compiling \\"%s\\"\n' % src)
2012-12-30 07:08:50 +08:00
outfile.write(' '.join(quoted) + ' || exit\n')
2012-12-30 01:51:32 +08:00
return abs_obj
2013-01-07 00:40:32 +08:00
2013-01-06 08:59:54 +08:00
def build_target_link_arguments(self, deps):
args = []
for d in deps:
2013-01-07 00:40:32 +08:00
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())
2013-01-06 08:59:54 +08:00
args.append(self.get_target_filename(d))
return args
2012-12-30 01:51:32 +08:00
2012-12-30 06:55:35 +08:00
def generate_link(self, target, outfile, outname, obj_list):
2013-01-06 00:13:38 +08:00
if isinstance(target, interpreter.StaticLibrary):
2013-01-12 04:59:49 +08:00
linker = self.build.static_linker
2013-01-06 00:13:38 +08:00
else:
2013-01-12 04:59:49 +08:00
linker = self.build.compilers[0] # Fixme.
2012-12-30 04:04:24 +08:00
commands = []
commands += linker.get_exelist()
2013-01-06 03:08:08 +08:00
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()
commands += linker.get_pic_flags()
2013-01-06 03:08:08 +08:00
elif isinstance(target, interpreter.StaticLibrary):
commands += linker.get_std_link_flags()
else:
raise RuntimeError('Unknown build target type.')
2012-12-30 06:55:35 +08:00
for dep in target.get_external_deps():
commands += dep.get_link_flags()
2012-12-30 04:04:24 +08:00
commands += linker.get_output_flags()
commands.append(outname)
2013-01-06 00:13:38 +08:00
commands += obj_list
2013-01-06 08:59:54 +08:00
commands += self.build_target_link_arguments(target.get_dependencies())
2013-01-02 05:43:25 +08:00
quoted = shell_quote(commands)
2013-01-02 06:56:46 +08:00
outfile.write('\necho Linking \\"%s\\".\n' % target.get_basename())
2012-12-30 07:08:50 +08:00
outfile.write(' '.join(quoted) + ' || exit\n')
2012-12-30 01:51:32 +08:00
2013-01-02 06:00:24 +08:00
def get_target_dir(self, target):
dirname = os.path.join(self.environment.get_build_dir(), target.get_basename())
os.makedirs(dirname, exist_ok=True)
return dirname
2012-12-30 01:51:32 +08:00
def generate_commands(self, outfile):
2013-01-12 04:59:49 +08:00
for i in self.build.get_targets().items():
2013-01-02 06:00:24 +08:00
target = i[1]
2013-01-06 08:59:54 +08:00
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
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 = []
for src in target.get_sources():
obj_list.append(self.generate_single_compile(target, outfile, src))
self.generate_link(target, outfile, outname, obj_list)
self.processed_targets[name] = True
2012-12-30 01:51:32 +08:00
if __name__ == '__main__':
code = """
project('simple generator')
language('c')
executable('prog', 'prog.c', 'dep.c')
"""
2013-01-06 03:08:08 +08:00
import environment
os.chdir(os.path.split(__file__)[0])
envir = environment.Environment('.', 'work area')
intpr = interpreter.Interpreter(code, envir)
g = ShellGenerator(intpr, envir)
g.generate()