<commit_before>#!/usr/bin/python3 -tt

# Copyright 2012 <NAME>

# 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

def shell_quote(cmdlist):
    return ["'" + x + "'" for x in cmdlist]

class ShellGenerator():
    
    def __init__(self, interpreter, environment):
        self.environment = environment
        self.interpreter = interpreter
        self.build_filename = 'compile.sh'
    
    def generate(self):
        self.interpreter.run()
        outfilename = os.path.join(self.environment.get_build_dir(), self.build_filename)
        outfile = open(outfilename, 'w')
        outfile.write('#!/bin/sh\n\n')
        outfile.write('echo This is an autogenerated shell script build file for project \\"%s\\".\n'
                      % self.interpreter.get_project())
        outfile.write('echo This is experimental and most likely will not work!\n')
        cdcmd = ['cd', self.environment.get_build_dir()]
        outfile.write(' '.join(shell_quote(cdcmd)) + '\n')
        self.generate_commands(outfile)
        outfile.close()
        os.chmod(outfilename, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC |\
                 stat.S_IRGRP | stat.S_IXGRP | stat.S_IROTH | stat.S_IXOTH)

    def generate_single_compile(self, target, outfile, src):
        compiler = None
        for i in self.interpreter.compilers:
            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)
        abs_obj = os.path.join(self.get_target_dir(target), src)
        abs_obj += '.' + self.environment.get_object_suffix()
        commands = []
        commands += compiler.get_exelist()
        commands += compiler.get_debug_flags()
        commands += compiler.get_std_warn_flags()
        commands += compiler.get_compile_only_flags()
        for dep in target.get_external_deps():
            commands += dep.get_compile_flags()
        commands.append(abs_src)
        commands += compiler.get_output_flags()
        commands.append(abs_obj)
        quoted = shell_quote(commands)
        outfile.write('\necho Compiling \\"%s\\"\n' % src)
        outfile.write(' '.join(quoted) + ' || exit\n')
        return abs_obj

    def generate_link(self, target, outfile, outname, obj_list):
        linker = self.interpreter.compilers[0] # Fixme.
        commands = []
        commands += linker.get_exelist()
        commands += obj_list
        for dep in target.get_external_deps():
            commands += dep.get_link_flags()
        commands += linker.get_output_flags()
        commands.append(outname)
        quoted = shell_quote(commands)
        outfile.write('\necho Linking \\"%s\\".\n' % outname)
        outfile.write(' '.join(quoted) + ' || exit\n')

    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

    def generate_commands(self, outfile):
        for i in self.interpreter.get_targets().items():
            name = i[0]
            target = i[1]
            print('Generating target', name)
            targetdir = self.get_target_dir(target)
            outname = os.path.join(targetdir, target.get_basename())
            suffix = self.environment.get_exe_suffix()
            if suffix != '':
                outname = outname + '.' + suffix
            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)

if __name__ == '__main__':
    code = """
    project('simple generator')
    language('c')
    executable('prog', 'prog.c', 'dep.c')
    """
    import interpreter, environment
    os.chdir(os.path.split(__file__)[0])
    envir = environment.Environment('.', 'work area')
    intpr = interpreter.Interpreter(code, envir)
    g = ShellGenerator(intpr, envir)
    g.generate()
<commit_msg>Better message.<commit_after>#!/usr/bin/python3 -tt

# Copyright 2012 <NAME>

# 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

def shell_quote(cmdlist):
    return ["'" + x + "'" for x in cmdlist]

class ShellGenerator():
    
    def __init__(self, interpreter, environment):
        self.environment = environment
        self.interpreter = interpreter
        self.build_filename = 'compile.sh'
    
    def generate(self):
        self.interpreter.run()
        outfilename = os.path.join(self.environment.get_build_dir(), self.build_filename)
        outfile = open(outfilename, 'w')
        outfile.write('#!/bin/sh\n\n')
        outfile.write('echo This is an autogenerated shell script build file for project \\"%s\\".\n'
                      % self.interpreter.get_project())
        outfile.write('echo This is experimental and most likely will not work!\n')
        cdcmd = ['cd', self.environment.get_build_dir()]
        outfile.write(' '.join(shell_quote(cdcmd)) + '\n')
        self.generate_commands(outfile)
        outfile.close()
        os.chmod(outfilename, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC |\
                 stat.S_IRGRP | stat.S_IXGRP | stat.S_IROTH | stat.S_IXOTH)

    def generate_single_compile(self, target, outfile, src):
        compiler = None
        for i in self.interpreter.compilers:
            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)
        abs_obj = os.path.join(self.get_target_dir(target), src)
        abs_obj += '.' + self.environment.get_object_suffix()
        commands = []
        commands += compiler.get_exelist()
        commands += compiler.get_debug_flags()
        commands += compiler.get_std_warn_flags()
        commands += compiler.get_compile_only_flags()
        for dep in target.get_external_deps():
            commands += dep.get_compile_flags()
        commands.append(abs_src)
        commands += compiler.get_output_flags()
        commands.append(abs_obj)
        quoted = shell_quote(commands)
        outfile.write('\necho Compiling \\"%s\\"\n' % src)
        outfile.write(' '.join(quoted) + ' || exit\n')
        return abs_obj

    def generate_link(self, target, outfile, outname, obj_list):
        linker = self.interpreter.compilers[0] # Fixme.
        commands = []
        commands += linker.get_exelist()
        commands += obj_list
        for dep in target.get_external_deps():
            commands += dep.get_link_flags()
        commands += linker.get_output_flags()
        commands.append(outname)
        quoted = shell_quote(commands)
        outfile.write('\necho Linking \\"%s\\".\n' % target.get_basename())
        outfile.write(' '.join(quoted) + ' || exit\n')

    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

    def generate_commands(self, outfile):
        for i in self.interpreter.get_targets().items():
            name = i[0]
            target = i[1]
            print('Generating target', name)
            targetdir = self.get_target_dir(target)
            outname = os.path.join(targetdir, target.get_basename())
            suffix = self.environment.get_exe_suffix()
            if suffix != '':
                outname = outname + '.' + suffix
            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)

if __name__ == '__main__':
    code = """
    project('simple generator')
    language('c')
    executable('prog', 'prog.c', 'dep.c')
    """
    import interpreter, environment
    os.chdir(os.path.split(__file__)[0])
    envir = environment.Environment('.', 'work area')
    intpr = interpreter.Interpreter(code, envir)
    g = ShellGenerator(intpr, envir)
    g.generate()

{'fraction_non_alphanumeric': 0.11778690296088178, 'fraction_numerical': 0.0028095958504430517, 'mean_word_length': 2.5706018518518516, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '575609', 'n_tokens_mistral': 2745, 'n_tokens_neox': 2701, 'n_words': 653}