1#!/usr/bin/env python3
2# Copyright (c) 2016 Nuxi (https://nuxi.nl/) and contributors.
3#
4# SPDX-License-Identifier: BSD-2-Clause
5
6from contextlib import redirect_stdout
7import io
8import os
9import subprocess
10
11from generator.abi import *
12from generator.asm import *
13from generator.c import *
14from generator.markdown import *
15from generator.parser import *
16from generator.rust import *
17from generator.syscalls_master import *
18
19abi = AbiParser().parse_abi_file(
20    os.path.join(os.path.dirname(__file__), 'cloudabi.txt'))
21
22procs = []
23
24
25# Pipes output through clang-format to format the C code.
26def open_and_format(filename):
27    proc = subprocess.Popen(
28        [
29            'clang-format', '''-style={
30               BasedOnStyle: Google,
31               AllowShortIfStatementsOnASingleLine: false,
32               AllowShortLoopsOnASingleLine: false,
33               AllowShortFunctionsOnASingleLine: None,
34               DerivePointerBinding: false,
35               PointerAlignment: Right,
36         }'''
37        ],
38        stdin=subprocess.PIPE,
39        stdout=open(filename, 'w'))
40    procs.append(proc)
41    return io.TextIOWrapper(proc.stdin, encoding='UTF-8')
42
43
44with open('headers/cloudabi_types_common.h', 'w') as f:
45    with redirect_stdout(f):
46        CSyscalldefsGenerator(
47            naming=CNaming('cloudabi_'),
48            header_guard='CLOUDABI_TYPES_COMMON_H',
49            machine_dep=False,
50            preamble='#if defined(__FreeBSD__) && defined(_KERNEL)\n'
51            '#include <sys/types.h>\n'
52            '#elif defined(__linux__) && defined(__KERNEL__)\n'
53            '#include <linux/types.h>\n'
54            '#else\n'
55            '#include <stddef.h>\n'
56            '#include <stdint.h>\n'
57            '#endif\n').generate_abi(abi)
58
59with open('headers/cloudabi_types.h', 'w') as f:
60    with redirect_stdout(f):
61        CSyscalldefsGenerator(
62            naming=CNaming('cloudabi_'),
63            header_guard='CLOUDABI_TYPES_H',
64            machine_dep=True,
65            preamble='#include "cloudabi_types_common.h"\n').generate_abi(abi)
66
67with open('headers/cloudabi32_types.h', 'w') as f:
68    with redirect_stdout(f):
69        CSyscalldefsGenerator(
70            naming=CNaming('cloudabi_', 'cloudabi32_'),
71            header_guard='CLOUDABI32_TYPES_H',
72            machine_dep=True,
73            md_type=int_types['uint32'],
74            preamble='#include "cloudabi_types_common.h"\n').generate_abi(abi)
75
76with open('headers/cloudabi64_types.h', 'w') as f:
77    with redirect_stdout(f):
78        CSyscalldefsGenerator(
79            naming=CNaming('cloudabi_', 'cloudabi64_'),
80            header_guard='CLOUDABI64_TYPES_H',
81            machine_dep=True,
82            md_type=int_types['uint64'],
83            preamble='#include "cloudabi_types_common.h"\n').generate_abi(abi)
84
85with open_and_format('headers/cloudabi_syscalls.h') as f:
86    with redirect_stdout(f):
87        CSyscallsGenerator(
88            naming=CNaming('cloudabi_'),
89            header_guard='CLOUDABI_SYSCALLS_H',
90            preamble='#include "cloudabi_types.h"\n').generate_abi(abi)
91
92with open('headers/cloudabi_syscalls_info.h', 'w') as f:
93    with redirect_stdout(f):
94        CSyscallsInfoGenerator(
95            naming=CNaming('cloudabi_'),
96            header_guard='CLOUDABI_SYSCALLS_INFO_H',
97        ).generate_abi(abi)
98
99with open('rust/cloudabi.rs', 'w') as f:
100    with redirect_stdout(f):
101        RustGenerator(naming=RustNaming()).generate_abi(abi)
102
103with open('vdsos/cloudabi_vdso_aarch64.S', 'w') as f:
104    with redirect_stdout(f):
105        AsmVdsoAarch64Generator().generate_abi(abi)
106
107with open('vdsos/cloudabi_vdso_armv6.S', 'w') as f:
108    with redirect_stdout(f):
109        AsmVdsoArmv6Generator().generate_abi(abi)
110
111with open('vdsos/cloudabi_vdso_armv6_on_64bit.S', 'w') as f:
112    with redirect_stdout(f):
113        AsmVdsoArmv6On64bitGenerator().generate_abi(abi)
114
115with open('vdsos/cloudabi_vdso_i686.S', 'w') as f:
116    with redirect_stdout(f):
117        AsmVdsoI686Generator().generate_abi(abi)
118
119with open('vdsos/cloudabi_vdso_i686_on_64bit.S', 'w') as f:
120    with redirect_stdout(f):
121        AsmVdsoI686On64bitGenerator().generate_abi(abi)
122
123with open('vdsos/cloudabi_vdso_x86_64.S', 'w') as f:
124    with redirect_stdout(f):
125        AsmVdsoX86_64Generator().generate_abi(abi)
126
127with open('freebsd/syscalls32.master', 'w') as f:
128    with redirect_stdout(f):
129        SyscallsMasterGenerator(
130            naming=CNaming('cloudabi_', 'cloudabi32_', c11=False),
131        ).generate_abi(abi)
132
133with open('freebsd/syscalls64.master', 'w') as f:
134    with redirect_stdout(f):
135        SyscallsMasterGenerator(
136            naming=CNaming('cloudabi_', 'cloudabi64_', c11=False),
137        ).generate_abi(abi)
138
139with open_and_format('linux/cloudabi_syscalls.h') as f:
140    with redirect_stdout(f):
141        CLinuxSyscallsGenerator(
142            naming=CNaming('cloudabi_', c11=False, pointer_prefix='__user '),
143            header_guard='CLOUDABI_SYSCALLS_H',
144            machine_dep=False,
145            preamble='#include "cloudabi_types_common.h"\n').generate_abi(abi)
146
147with open_and_format('linux/cloudabi64_syscalls.h') as f:
148    with redirect_stdout(f):
149        CLinuxSyscallsGenerator(
150            naming=CNaming(
151                'cloudabi_',
152                'cloudabi64_',
153                c11=False,
154                pointer_prefix='__user '),
155            header_guard='CLOUDABI64_SYSCALLS_H',
156            machine_dep=True,
157            preamble='#include "cloudabi64_types.h"\n').generate_abi(abi)
158
159with open_and_format('linux/cloudabi64_syscalls_table.h') as f:
160    with redirect_stdout(f):
161        CLinuxSyscallTableGenerator(
162            naming=CNaming(
163                'cloudabi_',
164                'cloudabi64_',
165                c11=False,
166                pointer_prefix='__user '),
167            md_type=int_types['uint64'],
168            preamble='#include <asm/byteorder.h>\n'
169            '\n'
170            '#include "cloudabi_syscalls.h"\n'
171            '#include "cloudabi64_syscalls.h"\n').generate_abi(abi)
172
173with open('docs/cloudabi.md', 'w') as f:
174    with redirect_stdout(f):
175        MarkdownGenerator(
176            naming=MarkdownCNaming('cloudabi_'), ).generate_abi(abi)
177
178with open('docs/cloudabi-rust.md', 'w') as f:
179    with redirect_stdout(f):
180        MarkdownGenerator(naming=MarkdownRustNaming(), ).generate_abi(abi)
181
182html = subprocess.check_output('markdown docs/cloudabi.md', shell=True)
183with open('docs/cloudabi.html', 'wb') as f:
184    with open('parts/head.html', 'rb') as p:
185        f.write(p.read())
186    f.write(html)
187    with open('parts/foot.html', 'rb') as p:
188        f.write(p.read())
189
190html = subprocess.check_output('markdown docs/cloudabi-rust.md', shell=True)
191with open('docs/cloudabi-rust.html', 'wb') as f:
192    with open('parts/head.html', 'rb') as p:
193        f.write(p.read())
194    f.write(html)
195    with open('parts/foot.html', 'rb') as p:
196        f.write(p.read())
197
198for proc in procs:
199    proc.wait()
200