Skip to content

cgen: Don't emit mman.h and pthreads related code for freestanding bu…#24118

Merged
spytheman merged 1 commit into
vlang:masterfrom
raw-bin:fix_freestanding_closure_codegen
Apr 3, 2025
Merged

cgen: Don't emit mman.h and pthreads related code for freestanding bu…#24118
spytheman merged 1 commit into
vlang:masterfrom
raw-bin:fix_freestanding_closure_codegen

Conversation

@raw-bin

@raw-bin raw-bin commented Apr 2, 2025

Copy link
Copy Markdown
Contributor
  • This PR addresses an issue encountered with freestanding builds used for bare metal embedded targets that use anonymous functions.
  • The issue is to do with the code generated for closure management - which are inherently associated with anonymous functions. The closure management code assumes that the run-time environment has support for mmap and posix locking primitives.
  • Mmap and posix locking primitives are typically not available on bare metal embedded systems.
  • This PR ensures that the generated code for closure managment doesn't contain mmap and posix locking for freestanding builds.
  • Although this code solves the immediate problem a proper solution needs to parameterise things like page size assumptions etc.
  • Unfortunately I could not figure out how to write a test for freestanding builds just yet.

@huly-for-github

Copy link
Copy Markdown

Connected to Huly®: V_0.6-22509

Comment thread vlib/v/gen/c/cheaders.v
fn c_closure_helpers(pref_ &pref.Preferences) string {
mut builder := strings.new_builder(2048)
if pref_.os != .windows {
if pref_.os != .windows && pref_.is_bare == false {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if pref_.os != .windows && pref_.is_bare == false {
if pref_.os != .windows && !pref_.is_bare {

@spytheman

Copy link
Copy Markdown
Contributor

Unfortunately I could not figure out how to write a test for freestanding builds just yet.

You can put a pair of .vv/.out files in vlib/v/gen/c/testdata/ , then run:
./v vlib/v/gen/c/coutput_test.v .

Note: the .vv file has to start with:
// vtest vflags: -cc gcc -freestanding .
The corresponding .out file should contain the output of the .vv program, which will be compiled with the flags in that comment.

See for example https://github.com/vlang/v/blob/master/vlib/v/gen/c/testdata/freestanding_module_import_2.vv .

@spytheman spytheman merged commit 87f92b1 into vlang:master Apr 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants