Skip to content

Commit 5fff033

Browse files
committed
builder-options: Set RUSTFLAGS in builder environment
This inherits the SDK provided rustflags by default Fixes: #693
1 parent bbd5487 commit 5fff033

File tree

8 files changed

+183
-2
lines changed

8 files changed

+183
-2
lines changed

NEWS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ TBD
1111
* Support setting state-dir for run
1212
* Default to builddir true for cmake and cmake-ninja buildsystems
1313
* Handle case-insensitive YAML boolean and null scalars
14-
* Support setting CGO buildflags in builder environment
14+
* Support setting CGO buildflags and RUSTFLAGS in builder environment
1515

1616
Changes in 1.4.6
1717
================

data/flatpak-manifest.schema.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@
7474
"description": "If this is true, clear cgo-ldflags from previous build options before adding it from these options. If set without cgo-ldflags, CGO_LDFLAGS will not be set.",
7575
"type": "boolean"
7676
},
77+
"rustflags": {
78+
"description": "This is set in the environment variable RUSTFLAGS during the build. Multiple specifications of this (in e.g. per-arch area) are concatenated, separated by spaces.",
79+
"type": "string"
80+
},
81+
"rustflags-override": {
82+
"description": "If this is true, clear rustflags from previous build options before adding it from these options.",
83+
"type": "boolean"
84+
},
7785
"prefix": {
7886
"description": "The build prefix for the modules (defaults to /app for applications and /usr for runtimes).",
7987
"type": "string"

doc/flatpak-manifest.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,14 @@
364364
<term><option>cgo-ldflags-override</option> (boolean)</term>
365365
<listitem><para>If this is true, clear cgo-ldflags from previous build options before adding it from these options. If set without cgo-ldflags, CGO_LDFLAGS will not be set.</para></listitem>
366366
</varlistentry>
367+
<varlistentry>
368+
<term><option>rustflags</option> (string)</term>
369+
<listitem><para>This is set in the environment variable RUSTFLAGS during the build. Multiple specifications of this (in e.g. per-arch area) are concatenated, separated by spaces.</para></listitem>
370+
</varlistentry>
371+
<varlistentry>
372+
<term><option>rustflags-override</option> (boolean)</term>
373+
<listitem><para>If this is true, clear rustflags from previous build options before adding it from these options.</para></listitem>
374+
</varlistentry>
367375
<varlistentry>
368376
<term><option>prefix</option> (string)</term>
369377
<listitem><para>The build prefix for the modules (defaults to <filename>/app</filename> for

src/builder-options.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ struct BuilderOptions
5353
gboolean ldflags_override;
5454
char *cgo_ldflags;
5555
gboolean cgo_ldflags_override;
56+
char *rustflags;
57+
gboolean rustflags_override;
5658
char *append_path;
5759
char *prepend_path;
5860
char *append_ld_library_path;
@@ -98,6 +100,8 @@ enum {
98100
PROP_LDFLAGS_OVERRIDE,
99101
PROP_CGO_LDFLAGS,
100102
PROP_CGO_LDFLAGS_OVERRIDE,
103+
PROP_RUSTFLAGS,
104+
PROP_RUSTFLAGS_OVERRIDE,
101105
PROP_PREFIX,
102106
PROP_LIBDIR,
103107
PROP_ENV,
@@ -134,6 +138,7 @@ builder_options_finalize (GObject *object)
134138
g_free (self->cppflags);
135139
g_free (self->ldflags);
136140
g_free (self->cgo_ldflags);
141+
g_free (self->rustflags);
137142
g_free (self->append_path);
138143
g_free (self->prepend_path);
139144
g_free (self->append_ld_library_path);
@@ -221,6 +226,14 @@ builder_options_get_property (GObject *object,
221226
g_value_set_boolean (value, self->cgo_ldflags_override);
222227
break;
223228

229+
case PROP_RUSTFLAGS:
230+
g_value_set_string (value, self->rustflags);
231+
break;
232+
233+
case PROP_RUSTFLAGS_OVERRIDE:
234+
g_value_set_boolean (value, self->rustflags_override);
235+
break;
236+
224237
case PROP_APPEND_PATH:
225238
g_value_set_string (value, self->append_path);
226239
break;
@@ -380,6 +393,15 @@ builder_options_set_property (GObject *object,
380393
self->cgo_ldflags_override = g_value_get_boolean (value);
381394
break;
382395

396+
case PROP_RUSTFLAGS:
397+
g_clear_pointer (&self->rustflags, g_free);
398+
self->rustflags = g_value_dup_string (value);
399+
break;
400+
401+
case PROP_RUSTFLAGS_OVERRIDE:
402+
self->rustflags_override = g_value_get_boolean (value);
403+
break;
404+
383405
case PROP_APPEND_PATH:
384406
g_clear_pointer (&self->append_path, g_free);
385407
self->append_path = g_value_dup_string (value);
@@ -598,6 +620,20 @@ builder_options_class_init (BuilderOptionsClass *klass)
598620
"",
599621
FALSE,
600622
G_PARAM_READWRITE));
623+
g_object_class_install_property (object_class,
624+
PROP_RUSTFLAGS,
625+
g_param_spec_string ("rustflags",
626+
"",
627+
"",
628+
NULL,
629+
G_PARAM_READWRITE));
630+
g_object_class_install_property (object_class,
631+
PROP_RUSTFLAGS_OVERRIDE,
632+
g_param_spec_boolean ("rustflags-override",
633+
"",
634+
"",
635+
FALSE,
636+
G_PARAM_READWRITE));
601637
g_object_class_install_property (object_class,
602638
PROP_APPEND_PATH,
603639
g_param_spec_string ("append-path",
@@ -1087,6 +1123,15 @@ builder_options_get_cgo_ldflags (BuilderOptions *self, BuilderContext *context)
10871123
ldflags);
10881124
}
10891125

1126+
char *
1127+
builder_options_get_rustflags (BuilderOptions *self, BuilderContext *context)
1128+
{
1129+
return builder_options_get_flags (self, context,
1130+
G_STRUCT_OFFSET (BuilderOptions, rustflags),
1131+
G_STRUCT_OFFSET (BuilderOptions, rustflags_override),
1132+
get_sdk_flags (self, context, builder_sdk_config_get_rustflags));
1133+
}
1134+
10901135
static char *
10911136
builder_options_get_appended_path (BuilderOptions *self, BuilderContext *context, const char *initial_value, size_t append_field_offset, size_t prepend_field_offset)
10921137
{
@@ -1283,6 +1328,7 @@ builder_options_get_env (BuilderOptions *self, BuilderContext *context)
12831328
g_autofree char *cgo_cxxflags = NULL;
12841329
g_autofree char *ldflags = NULL;
12851330
g_autofree char *cgo_ldflags = NULL;
1331+
g_autofree char *rustflags = NULL;
12861332

12871333
envp = builder_context_extend_env_pre (context, envp);
12881334

@@ -1314,6 +1360,10 @@ builder_options_get_env (BuilderOptions *self, BuilderContext *context)
13141360
if (cgo_ldflags)
13151361
envp = g_environ_setenv (envp, "CGO_LDFLAGS", cgo_ldflags, FALSE);
13161362

1363+
rustflags = builder_options_get_rustflags (self, context);
1364+
if (rustflags)
1365+
envp = g_environ_setenv (envp, "RUSTFLAGS", rustflags, FALSE);
1366+
13171367
/* We traverse in reverse order because the list is "last first" */
13181368
for (l = g_list_last (options); l != NULL; l = l->prev)
13191369
{
@@ -1561,6 +1611,8 @@ builder_options_checksum (BuilderOptions *self,
15611611
builder_cache_checksum_compat_boolean (cache, self->ldflags_override);
15621612
builder_cache_checksum_str (cache, self->cgo_ldflags);
15631613
builder_cache_checksum_compat_boolean (cache, self->cgo_ldflags_override);
1614+
builder_cache_checksum_str (cache, self->rustflags);
1615+
builder_cache_checksum_compat_boolean (cache, self->rustflags_override);
15641616
builder_cache_checksum_str (cache, self->prefix);
15651617
builder_cache_checksum_compat_str (cache, self->libdir);
15661618
builder_cache_checksum_strv (cache, self->env);

src/builder-options.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ char *builder_options_get_ldflags (BuilderOptions *self,
5151
BuilderContext *context);
5252
char *builder_options_get_cgo_ldflags (BuilderOptions *self,
5353
BuilderContext *context);
54+
char *builder_options_get_rustflags (BuilderOptions *self,
55+
BuilderContext *context);
5456
const char *builder_options_get_prefix (BuilderOptions *self,
5557
BuilderContext *context);
5658
const char *builder_options_get_libdir (BuilderOptions *self,

src/builder-sdk-config.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ struct BuilderSdkConfig {
3030
char *cflags;
3131
char *cxxflags;
3232
char *ldflags;
33+
char *rustflags;
3334
};
3435

3536
typedef struct
@@ -49,6 +50,7 @@ builder_sdk_config_finalize (GObject *object)
4950
g_free (self->cflags);
5051
g_free (self->cxxflags);
5152
g_free (self->ldflags);
53+
g_free (self->rustflags);
5254

5355
G_OBJECT_CLASS (builder_sdk_config_parent_class)->finalize (object);
5456
}
@@ -60,6 +62,7 @@ enum {
6062
PROP_CFLAGS,
6163
PROP_CXXFLAGS,
6264
PROP_LDFLAGS,
65+
PROP_RUSTFLAGS,
6366
LAST_PROP
6467
};
6568

@@ -93,6 +96,10 @@ builder_sdk_config_get_property (GObject *object,
9396
g_value_set_string (value, self->ldflags);
9497
break;
9598

99+
case PROP_RUSTFLAGS:
100+
g_value_set_string (value, self->rustflags);
101+
break;
102+
96103
default:
97104
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
98105
}
@@ -132,6 +139,11 @@ builder_sdk_config_set_property (GObject *object,
132139
g_free (self->ldflags);
133140
self->ldflags = g_value_dup_string(value);
134141
break ;
142+
143+
case PROP_RUSTFLAGS:
144+
g_free (self->rustflags);
145+
self->rustflags = g_value_dup_string(value);
146+
break ;
135147
default:
136148
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
137149
}
@@ -181,6 +193,13 @@ builder_sdk_config_class_init (BuilderSdkConfigClass *klass)
181193
"",
182194
NULL,
183195
G_PARAM_READWRITE));
196+
g_object_class_install_property (object_class,
197+
PROP_RUSTFLAGS,
198+
g_param_spec_string ("rustflags",
199+
"",
200+
"",
201+
NULL,
202+
G_PARAM_READWRITE));
184203
}
185204

186205
static void
@@ -219,6 +238,12 @@ builder_sdk_config_get_ldflags (BuilderSdkConfig *self)
219238
return self->ldflags;
220239
}
221240

241+
const char *
242+
builder_sdk_config_get_rustflags (BuilderSdkConfig *self)
243+
{
244+
return self->rustflags;
245+
}
246+
222247
BuilderSdkConfig *
223248
builder_sdk_config_from_file (GFile *file,
224249
GError **error)

src/builder-sdk-config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const char * builder_sdk_config_get_cppflags (BuilderSdkConfig *self);
3838
const char * builder_sdk_config_get_cflags (BuilderSdkConfig *self);
3939
const char * builder_sdk_config_get_cxxflags (BuilderSdkConfig *self);
4040
const char * builder_sdk_config_get_ldflags (BuilderSdkConfig *self);
41+
const char * builder_sdk_config_get_rustflags (BuilderSdkConfig *self);
4142

4243
BuilderSdkConfig *builder_sdk_config_from_file (GFile *file,
4344
GError **error);

tests/test-builder-flags.sh

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ set -euo pipefail
2323

2424
skip_without_fuse
2525

26-
echo "1..4"
26+
echo "1..7"
2727

2828
setup_repo
2929
install_repo
@@ -145,3 +145,88 @@ run_build test-cgo-only-override.json
145145
assert_file_has_content appdir/files/cgo_cflags_out '^unset$'
146146

147147
echo "ok only cgo-cflags-override clears CGO_CFLAGS"
148+
149+
# Default: RUSTFLAGS should be passed. The test SDK does not ship with
150+
# defaults so it is set here via rustflags explicitly.
151+
cat > test-rustflags-set.json <<'EOF'
152+
{
153+
"app-id": "org.test.RustflagsSet",
154+
"runtime": "org.test.Platform",
155+
"sdk": "org.test.Sdk",
156+
"build-options": {
157+
"rustflags": "-C opt-level=2"
158+
},
159+
"modules": [{
160+
"name": "test",
161+
"buildsystem": "simple",
162+
"build-commands": [
163+
"echo ${RUSTFLAGS} > /app/rustflags_out"
164+
]
165+
}]
166+
}
167+
EOF
168+
169+
run_build test-rustflags-set.json
170+
171+
assert_file_has_content appdir/files/rustflags_out '\-C opt\-level=2'
172+
173+
echo "ok rustflags is passed by default"
174+
175+
# rustflags-override at module level clears manifest-level rustflags
176+
# or equivalently clears SDK rustflags
177+
cat > test-rustflags-override.json <<'EOF'
178+
{
179+
"app-id": "org.test.RustflagsOverride",
180+
"runtime": "org.test.Platform",
181+
"sdk": "org.test.Sdk",
182+
"build-options": {
183+
"rustflags": "-C debuginfo=0"
184+
},
185+
"modules": [{
186+
"name": "test",
187+
"buildsystem": "simple",
188+
"build-options": {
189+
"rustflags": "-C opt-level=3",
190+
"rustflags-override": true
191+
},
192+
"build-commands": [
193+
"echo ${RUSTFLAGS} > /app/rustflags_out"
194+
]
195+
}]
196+
}
197+
EOF
198+
199+
run_build test-rustflags-override.json
200+
201+
assert_file_has_content appdir/files/rustflags_out '\-C opt\-level=3'
202+
assert_not_file_has_content appdir/files/rustflags_out 'debuginfo'
203+
204+
echo "ok rustflags-override at module level clears manifest-level rustflags"
205+
206+
# Only rustflags-override is set, rustflags should be cleared
207+
cat > test-rustflags-only-override.json <<'EOF'
208+
{
209+
"app-id": "org.test.RustflagsOnlyOverride",
210+
"runtime": "org.test.Platform",
211+
"sdk": "org.test.Sdk",
212+
"build-options": {
213+
"rustflags": "-C debuginfo=0"
214+
},
215+
"modules": [{
216+
"name": "test",
217+
"buildsystem": "simple",
218+
"build-options": {
219+
"rustflags-override": true
220+
},
221+
"build-commands": [
222+
"echo ${RUSTFLAGS:-unset} > /app/rustflags_out"
223+
]
224+
}]
225+
}
226+
EOF
227+
228+
run_build test-rustflags-only-override.json
229+
230+
assert_file_has_content appdir/files/rustflags_out '^unset$'
231+
232+
echo "ok only rustflags-override clears rustflags"

0 commit comments

Comments
 (0)