Debian Codegraph @

src/briefcase/commands/build.py

raw
1 from __future__ import annotations
2
3 import argparse
4
5 from briefcase.config import AppConfig
6 from briefcase.exceptions import BriefcaseCommandError
7
8 from .base import BaseCommand, full_options
9
10
11 class BuildCommand(BaseCommand):
12 command = "build"
13 description = "Build an app for a target platform."
14
15 def add_options(self, parser):
16 self._add_update_options(parser, context_label=" before building")
17 self._add_test_options(parser, context_label="Build")
18
19 if self.supports_debugger:
20 self._add_debug_options(parser, context_label="Build")
21
22 parser.add_argument(
23 "-a",
24 "--app",
25 dest="app_name",
26 help="Name of the app to build (if multiple apps exist in the project)",
27 default=argparse.SUPPRESS,
28 )
29
30 def build_app(self, app: AppConfig, **options):
31 """Build an application.
32
33 :param app: The application to build
34 """
35 # Default implementation; nothing to build.
36
37 def _build_app(
38 self,
39 app: AppConfig,
40 update: bool,
41 update_requirements: bool,
42 update_resources: bool,
43 update_support: bool,
44 update_stub: bool,
45 no_update: bool,
46 **options,
47 ) -> dict | None:
48 """Internal method to invoke a build on a single app. Ensures the app exists,
49 and has been updated (if requested) before attempting to issue the actual build
50 command.
51
52 :param app: The application to build
53 :param update: Should the application be updated before building?
54 :param update_requirements: Should the application requirements be updated
55 before building?
56 :param update_resources: Should the application resources be updated before
57 building?
58 :param update_support: Should the application support be updated?
59 :param update_stub: Should the stub binary be updated?
60 :param no_update: Should automated updates be disabled?
61 """
62 if app.external_package_path:
63 raise BriefcaseCommandError(
64 f"{app.app_name!r} is declared as an external app. External apps "
65 "(apps defining 'external_package_path') cannot be built."
66 )
67
68 if not self.bundle_path(app).exists():
69 state = self.create_command(app, **options)
70 elif (
71 update # An explicit update has been requested
72 or update_requirements # An explicit requirements update has been requested
73 or update_resources # An explicit resource update has been requested
74 or update_support # An explicit app support update has been requested
75 or update_stub # An explicit stub binary update has been requested
76 or (app.test_mode and not no_update) # Test mode, but updates are enabled
77 or (app.debugger and not no_update) # Debug mode, but updates are enabled
78 ):
79 state = self.update_command(
80 app,
81 update_requirements=update_requirements,
82 update_resources=update_resources,
83 update_support=update_support,
84 update_stub=update_stub,
85 **options,
86 )
87 else:
88 state = None
89
90 self.verify_app(app)
91
92 state = self.build_app(app, **full_options(state, options))
93
94 qualifier = " (test mode)" if app.test_mode else ""
95 qualifier += " (debug mode)" if app.debugger else ""
96 self.console.info(
97 f"Built {self.binary_path(app).relative_to(self.base_path)}{qualifier}",
98 prefix=app.app_name,
99 )
100 return state
101
102 def __call__(
103 self,
104 app: AppConfig | None = None,
105 app_name: str | None = None,
106 update: bool = False,
107 update_requirements: bool = False,
108 update_resources: bool = False,
109 update_support: bool = False,
110 update_stub: bool = False,
111 no_update: bool = False,
112 test_mode: bool = False,
113 debugger: str | None = None,
114 **options,
115 ) -> dict | None:
116 # Has the user requested an invalid set of options?
117 # This can't be done with argparse because it isn't
118 # a simple mutually exclusive group.
119 if no_update:
120 if update:
121 raise BriefcaseCommandError(
122 "Cannot specify both --update and --no-update"
123 )
124 if update_requirements:
125 raise BriefcaseCommandError(
126 "Cannot specify both --update-requirements and --no-update"
127 )
128 if update_resources:
129 raise BriefcaseCommandError(
130 "Cannot specify both --update-resources and --no-update"
131 )
132 if update_support:
133 raise BriefcaseCommandError(
134 "Cannot specify both --update-support and --no-update"
135 )
136 if update_stub:
137 raise BriefcaseCommandError(
138 "Cannot specify both --update-stub and --no-update"
139 )
140
141 apps_to_build = self.resolve_apps(app=app, app_name=app_name)
142
143 # Confirm host compatibility, that all required tools are available,
144 # and that the app configuration is finalized.
145 self.finalize(
146 apps=apps_to_build.values(),
147 test_mode=test_mode,
148 debugger=debugger,
149 )
150
151 state = None
152 for _, app_obj in sorted(apps_to_build.items()):
153 state = self._build_app(
154 app_obj,
155 update=update,
156 update_requirements=update_requirements,
157 update_resources=update_resources,
158 update_support=update_support,
159 update_stub=update_stub,
160 no_update=no_update,
161 **full_options(state, options),
162 )
163
164 return state