~maelkum/viuact

04fb08490dc1755b12ae1e406f95462c91e48ead — Marek Marecki 7 months ago 5c0817b
A little bit of work on switch an environment

I should probably define what I *want* from the `viuact switch' command
and only then start implementing it. Back to the whiteboard, I guess.

Let's take some inspiration from OCaml's opam, Rust's cargo, and OCaml's
drom (cargo for OCaml, something like that).
5 files changed, 71 insertions(+), 52 deletions(-)

M tools/cc.py
M tools/front.py
M tools/switch.py
M viuact/__init__.py
M viuact/env.py
M tools/cc.py => tools/cc.py +1 -1
@@ 289,7 289,7 @@ def main(executable_name, args):
            source_file.rsplit('.', maxsplit = 1)[0].replace('/', '::')
        )

        output_directory = 'build/_default'
        output_directory = viuact.env.output_directory()

        viuact.core.cc(
            source_root,

M tools/front.py => tools/front.py +27 -45
@@ 7,6 7,7 @@ import shutil
import sys

import viuact.util.help
import viuact.env


HELP = '''{NAME}


@@ 153,13 154,18 @@ KNOWN_TOOLS = (
)

DEFAULT_CORE_DIR = '.'
CORE_DIR = os.environ.get('VIUACT_CORE_DIR', DEFAULT_CORE_DIR)
CORE_DIR = viuact.env.core_directory(DEFAULT_CORE_DIR)
def get_core_exec_path(executable):
    is_development = (CORE_DIR == '.')
    return os.path.join(os.path.expanduser(CORE_DIR), {
        'cc': ('tools/cc.py' if is_development else 'viuact-cc'),
        'fmt': ('tools/format.py' if is_development else 'viuact-format'),
        'opt': ('tools/opt.py' if is_development else 'viuact-opt'),
        # Note that `switch' tool should be somewhat independent of the compiler
        # version. It is a tool for switching compiler versions, similar to
        # OCaml's opam.
        # FIXME Maybe detect switch tool as special and exempt it from
        # VIUACT_CORE_DIR set by the user or itself.
        'switch': ('tools/switch.py' if is_development else 'viuact-switch'),
    }.get(executable))



@@ 185,60 191,36 @@ def main(executable_name, args):
            text = HELP,
        )
    elif arg == '--env':
        print('VIUACT_CORE_DIR: {}'.format(
            os.environ.get('VIUACT_CORE_DIR', '(none)'),
        print('VIUACT_CORE_DIR:   {}'.format(
            CORE_DIR
        ))
        print('DEFAULT_OUTPUT_DIRECTORY: {}'.format(
            viuact.env.DEFAULT_OUTPUT_DIRECTORY,
        print('VIUACT_OUTPUT_DIR: {}'.format(
            viuact.env.output_directory()
        ))
        print('STDLIB_HEADERS_DIRECTORY: {}'.format(
            viuact.env.STDLIB_HEADERS_DIRECTORY,
        ))
        if viuact.env.VIUACT_LIBRARY_PATH:
        if True:
            path = viuact.env.library_path().split(':')
            prefix = 'VIUACT_LIBRARY_PATH:'
            print('{} {}'.format(
                prefix,
                viuact.env.VIUACT_LIBRARY_PATH[0],
            ))
            for each in viuact.env.VIUACT_LIBRARY_PATH[1:]:
                print('{} {}'.format(
                    (len(prefix) * ' '),
                    each,
                ))
        print('VIUA_LIBRARY_PATH: {}'.format(
            '\n'.join(map(lambda each: (
                '{}{}'.format(
                    (' ' * (len('VIUA_LIBRARY_PATH') + 2)),
                    each,
                )),
                os.environ.get('VIUA_LIBRARY_PATH').split(':'))).strip(),
        ))
        print('VIUA_ASM_PATH: {}'.format(
            viuact.env.VIUA_ASM_PATH,
        ))
        if viuact.env.VIUACT_ASM_FLAGS:
            prefix = 'VIUACT_ASM_FLAGS:'
            print('{} {}'.format(
                prefix,
                viuact.env.VIUACT_ASM_FLAGS[0],
            ))
            for each in viuact.env.VIUACT_ASM_FLAGS[1:]:
                print('{} {}'.format(
                    (len(prefix) * ' '),
                    each,
                ))
        if viuact.env.VIUACT_DUMP_INTERMEDIATE:
            prefix = 'VIUACT_DUMP_INTERMEDIATE:'
            print('{} {}'.format(
                prefix,
                viuact.env.VIUACT_DUMP_INTERMEDIATE[0],
                path[0],
            ))
            for each in viuact.env.VIUACT_DUMP_INTERMEDIATE[1:]:
            for each in path[1:]:
                print('{} {}'.format(
                    (len(prefix) * ' '),
                    each,
                ))
    elif arg.startswith('--'):
        # if viuact.env.VIUACT_DUMP_INTERMEDIATE:
        #     prefix = 'VIUACT_DUMP_INTERMEDIATE:'
        #     print('{} {}'.format(
        #         prefix,
        #         viuact.env.VIUACT_DUMP_INTERMEDIATE[0],
        #     ))
        #     for each in viuact.env.VIUACT_DUMP_INTERMEDIATE[1:]:
        #         print('{} {}'.format(
        #             (len(prefix) * ' '),
        #             each,
        #         ))
    elif arg.startswith('--') or arg.startswith('-'):
        sys.stderr.write('error: unknown option: {}\n'.format(arg))
        exit(1)
    elif arg not in KNOWN_TOOLS:

M tools/switch.py => tools/switch.py +11 -5
@@ 16,7 16,7 @@ HELP = '''{NAME}
    {exec_tool} <%fg(man_const)tool%r> [%arg(option)...] [%arg(arg)]
    {exec_blank} --help
    {exec_blank} create [%arg(option)] %arg(name)
    {exec_blank} if     [%arg(option)] $arg(name)
    {exec_blank} if     [%arg(option)] %arg(name)
    {exec_blank} init
    {exec_blank} ls     [%arg(option)]
    {exec_blank} rm     %arg(name)


@@ 210,7 210,7 @@ def main(executable_name, args):
        )
        exit(0)

    switch_tool = item_or(args, 1, 'show')
    switch_tool = item_or(args, 0, 'show')

    switch_root_exists = os.path.isdir(Env_switch.root_path())
    if (switch_tool != 'init') and not switch_root_exists:


@@ 229,7 229,7 @@ def main(executable_name, args):
    )

    if switch_tool == 'create':
        switch_name = item_or(args, 2)
        switch_name = item_or(args, 1)
        if switch_name is None:
            sys.stderr.write(
                'error: name for a new switch is required\n')


@@ 372,13 372,19 @@ def main(executable_name, args):
            print('true' if is_active else 'false')
        exit(0)
    elif switch_tool == 'init':
        if switch_root_exists:
        force = (item_or(args, 1, None) in ('-f', '--force',))
        if switch_root_exists and not force:
            sys.stderr.write('warning: switch root already exists\n')
            # exit(0)
            exit(1)

        root_path = Env_switch.root_path()
        init_path = os.path.join(root_path, 'init')

        if switch_root_exists:
            sys.stderr.write('warning: reinitialising switch root: {}\n'.format(
                root_path,
            ))

        share_path = os.path.expanduser('~/.local/share/viuact/switch/init')

        os.makedirs(root_path, exist_ok = True)

M viuact/__init__.py => viuact/__init__.py +1 -1
@@ 1,4 1,4 @@
__version__ = '0.2.6'
__version__ = '0.2.7'
__commit__ = 'HEAD'
__code__ = 'CODE'


M viuact/env.py => viuact/env.py +31 -0
@@ 1,6 1,13 @@
import os


class Environment_error(Exception):
    pass

class Undefined_environment_variable(Environment_error):
    pass


LIBRARY_PATH = '/opt/lib/viuact:/usr/local/lib/viuact:/usr/lib/viuact'




@@ 10,3 17,27 @@ def library_path():
        return '{}:{}'.format(v, LIBRARY_PATH)
    else:
        return LIBRARY_PATH

def core_directory(default = None):
    v = os.environ.get('VIUACT_CORE_DIR', default)
    if v is None:
        raise Undefined_environment_variable('VIUACT_CORE_DIR')
    return v

def output_directory(default = 'build/_default'):
    return os.environ.get('VIUACT_OUTPUT_DIR', default)

# Variables to consider:
#
#   VIUACT_STDLIB_HEADERS_DIR
#       Interface files for Viuact standard library (this includes interface
#       files to Viua standard library). Single directory.
#
#   VIUA_LIBRARY_PATH
#       Where to find Viua VM bytecode modules. Multiple directories.
#
#   VIUA_ASM_EXEC, VIUA_VM_EXEC
#       Path to viua-asm or viua-vm executable to use. Useful for switches.
#
#   VIUA_ASM_FLAGS
#       A list of additional flags to include when invoking viua-asm.