Debian Codegraph @

mesonbuild/modules/icestorm.py

raw
1 # SPDX-License-Identifier: Apache-2.0
2 # Copyright 2017 The Meson development team
3
4 from __future__ import annotations
5 import itertools
6 import typing as T
7
8 from . import ExtensionModule, ModuleReturnValue, ModuleInfo
9 from .. import build
10 from .. import mesonlib
11 from ..interpreter.type_checking import CT_INPUT_KW
12 from ..interpreterbase.decorators import KwargInfo, typed_kwargs, typed_pos_args
13
14 if T.TYPE_CHECKING:
15 from typing_extensions import TypedDict
16
17 from . import ModuleState
18 from ..interpreter import Interpreter
19 from ..programs import Program
20
21 class ProjectKwargs(TypedDict):
22
23 sources: T.List[str | build.TargetSources]
24 constraint_file: str | build.TargetSources
25
26 class IceStormModule(ExtensionModule):
27
28 INFO = ModuleInfo('FPGA/Icestorm', '0.45.0', unstable=True)
29
30 def __init__(self, interpreter: Interpreter) -> None:
31 super().__init__(interpreter)
32 self.tools: T.Dict[str, Program] = {}
33 self.methods.update({
34 'project': self.project,
35 })
36
37 def detect_tools(self, state: ModuleState) -> None:
38 self.tools['yosys'] = state.find_program('yosys')
39 self.tools['arachne'] = state.find_program('arachne-pnr')
40 self.tools['icepack'] = state.find_program('icepack')
41 self.tools['iceprog'] = state.find_program('iceprog')
42 self.tools['icetime'] = state.find_program('icetime')
43
44 @typed_pos_args('icestorm.project', str,
45 varargs=(str, mesonlib.File, build.CustomTarget, build.CustomTargetIndex,
46 build.GeneratedList))
47 @typed_kwargs(
48 'icestorm.project',
49 CT_INPUT_KW.evolve(name='sources'),
50 KwargInfo(
51 'constraint_file',
52 (str, mesonlib.File, build.CustomTarget, build.CustomTargetIndex, build.GeneratedList),
53 required=True,
54 )
55 )
56 def project(self, state: ModuleState,
57 args: T.Tuple[str, T.List[str | build.TargetSources]],
58 kwargs: ProjectKwargs) -> ModuleReturnValue:
59 if not self.tools:
60 self.detect_tools(state)
61 proj_name, arg_sources = args
62 all_sources = self.interpreter.source_strings_to_files(
63 list(itertools.chain(arg_sources, kwargs['sources'])))
64 constraint_file = self.interpreter.source_strings_to_files([kwargs['constraint_file']])[0]
65
66 blif_target = build.CustomTarget(
67 f'{proj_name}_blif',
68 state.subdir,
69 state.environment,
70 [self.tools['yosys'], '-q', '-p', 'synth_ice40 -blif @OUTPUT@', '@INPUT@'],
71 all_sources,
72 [f'{proj_name}.blif'],
73 state.current_build_project,
74 )
75
76 asc_target = build.CustomTarget(
77 f'{proj_name}_asc',
78 state.subdir,
79 state.environment,
80 [self.tools['arachne'], '-q', '-d', '1k', '-p', '@INPUT@', '-o', '@OUTPUT@'],
81 [constraint_file, blif_target],
82 [f'{proj_name}.asc'],
83 state.current_build_project,
84 )
85
86 bin_target = build.CustomTarget(
87 f'{proj_name}_bin',
88 state.subdir,
89 state.environment,
90 [self.tools['icepack'], '@INPUT@', '@OUTPUT@'],
91 [asc_target],
92 [f'{proj_name}.bin'],
93 state.current_build_project,
94 build_by_default=True,
95 )
96
97 upload_target = build.RunTarget(
98 f'{proj_name}-upload',
99 [self.tools['iceprog'], bin_target],
100 [],
101 state.subdir,
102 state.environment,
103 state.current_build_project,
104 )
105
106 time_target = build.RunTarget(
107 f'{proj_name}-time',
108 [self.tools['icetime'], bin_target],
109 [],
110 state.subdir,
111 state.environment,
112 state.current_build_project,
113 )
114
115 return ModuleReturnValue(
116 None,
117 [blif_target, asc_target, bin_target, upload_target, time_target])
118
119
120 def initialize(interp: Interpreter) -> IceStormModule:
121 return IceStormModule(interp)